OSDN Git Service

* gcc.dg/const-elim-1.c: Remove xfail for xtensa-*-*.
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / cexp.m4
1 `/* Complex exponential functions
2    Copyright 2002, 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file.  (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING.  If not,
28 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA.  */
30 #include <math.h>
31 #include "libgfortran.h"'
32
33 include(`mtype.m4')dnl
34
35 /* z = a + ib  */
36 /* Absolute value.  */
37 real_type
38 cabs`'q (complex_type z)
39 {
40   return hypot`'q (REALPART (z), IMAGPART (z));
41 }
42
43 /* Complex argument.  The angle made with the +ve real axis.
44    Range -pi-pi.  */
45 real_type
46 carg`'q (complex_type z)
47 {
48   real_type arg;
49
50   return atan2`'q (IMAGPART (z), REALPART (z));
51 }
52
53 /* exp(z) = exp(a)*(cos(b) + isin(b))  */
54 complex_type
55 cexp`'q (complex_type z)
56 {
57   real_type a;
58   real_type b;
59   complex_type v;
60
61   a = REALPART (z);
62   b = IMAGPART (z);
63   COMPLEX_ASSIGN (v, cos`'q (b), sin`'q (b));
64   return exp`'q (a) * v;
65 }
66
67 /* log(z) = log (cabs(z)) + i*carg(z)  */
68 complex_type
69 clog`'q (complex_type z)
70 {
71   complex_type v;
72
73   COMPLEX_ASSIGN (v, log`'q (cabs`'q (z)), carg`'q (z));
74   return v;
75 }
76
77 /* log10(z) = log10 (cabs(z)) + i*carg(z)  */
78 complex_type
79 clog10`'q (complex_type z)
80 {
81   complex_type v;
82
83   COMPLEX_ASSIGN (v, log10`'q (cabs`'q (z)), carg`'q (z));
84   return v;
85 }
86
87 /* pow(base, power) = cexp (power * clog (base))  */
88 complex_type
89 cpow`'q (complex_type base, complex_type power)
90 {
91   return cexp`'q (power * clog`'q (base));
92 }
93
94 /* sqrt(z).  Algorithm pulled from glibc.  */
95 complex_type
96 csqrt`'q (complex_type z)
97 {
98   real_type re;
99   real_type im;
100   complex_type v;
101
102   re = REALPART (z);
103   im = IMAGPART (z);
104   if (im == 0.0)
105     {
106       if (re < 0.0)
107         {
108           COMPLEX_ASSIGN (v, 0.0, copysign`'q (sqrt`'q (-re), im));
109         }
110       else
111         {
112           COMPLEX_ASSIGN (v, fabs`'q (sqrt (re)),
113                           copysign`'q (0.0, im));
114         }
115     }
116   else if (re == 0.0)
117     {
118       real_type r;
119
120       r = sqrt`'q (0.5 * fabs (im));
121
122       COMPLEX_ASSIGN (v, copysign`'q (r, im), r);
123     }
124   else
125     {
126       real_type d, r, s;
127
128       d = hypot`'q (re, im);
129       /* Use the identity   2  Re res  Im res = Im x
130          to avoid cancellation error in  d +/- Re x.  */
131       if (re > 0)
132         {
133           r = sqrt`'q (0.5 * d + 0.5 * re);
134           s = (0.5 * im) / r;
135         }
136       else
137         {
138           s = sqrt`'q (0.5 * d - 0.5 * re);
139           r = fabs`'q ((0.5 * im) / s);
140         }
141
142       COMPLEX_ASSIGN (v, r, copysign`'q (s, im));
143     }
144   return v;
145 }
146