* config/mips/mips.h (mips_builtins, mips_cmp_choice): Delete.
[official-gcc.git] / libgfortran / m4 / cexp.m4
blobc91945051e3398290ad5ef0f967f90ca11aed9e8
1 `/* Complex exponential functions
2    Copyright 2002, 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21 #include <math.h>
22 #include "libgfortran.h"'
24 include(`mtype.m4')dnl
26 /* z = a + ib  */
27 /* Absolute value.  */
28 real_type
29 cabs`'q (complex_type z)
31   return hypot`'q (REALPART (z), IMAGPART (z));
34 /* Complex argument.  The angle made with the +ve real axis.
35    Range -pi-pi.  */
36 real_type
37 carg`'q (complex_type z)
39   real_type arg;
41   return atan2`'q (IMAGPART (z), REALPART (z));
44 /* exp(z) = exp(a)*(cos(b) + isin(b))  */
45 complex_type
46 cexp`'q (complex_type z)
48   real_type a;
49   real_type b;
50   complex_type v;
52   a = REALPART (z);
53   b = IMAGPART (z);
54   COMPLEX_ASSIGN (v, cos`'q (b), sin`'q (b));
55   return exp`'q (a) * v;
58 /* log(z) = log (cabs(z)) + i*carg(z)  */
59 complex_type
60 clog`'q (complex_type z)
62   complex_type v;
64   COMPLEX_ASSIGN (v, log`'q (cabs`'q (z)), carg`'q (z));
65   return v;
68 /* log10(z) = log10 (cabs(z)) + i*carg(z)  */
69 complex_type
70 clog10`'q (complex_type z)
72   complex_type v;
74   COMPLEX_ASSIGN (v, log10`'q (cabs`'q (z)), carg`'q (z));
75   return v;
78 /* pow(base, power) = cexp (power * clog (base))  */
79 complex_type
80 cpow`'q (complex_type base, complex_type power)
82   return cexp`'q (power * clog`'q (base));
85 /* sqrt(z).  Algorithm pulled from glibc.  */
86 complex_type
87 csqrt`'q (complex_type z)
89   real_type re;
90   real_type im;
91   complex_type v;
93   re = REALPART (z);
94   im = IMAGPART (z);
95   if (im == 0.0)
96     {
97       if (re < 0.0)
98         {
99           COMPLEX_ASSIGN (v, 0.0, copysign`'q (sqrt`'q (-re), im));
100         }
101       else
102         {
103           COMPLEX_ASSIGN (v, fabs`'q (sqrt (re)),
104                           copysign`'q (0.0, im));
105         }
106     }
107   else if (re == 0.0)
108     {
109       real_type r;
111       r = sqrt`'q (0.5 * fabs (im));
113       COMPLEX_ASSIGN (v, copysign`'q (r, im), r);
114     }
115   else
116     {
117       real_type d, r, s;
119       d = hypot`'q (re, im);
120       /* Use the identity   2  Re res  Im res = Im x
121          to avoid cancellation error in  d +/- Re x.  */
122       if (re > 0)
123         {
124           r = sqrt`'q (0.5 * d + 0.5 * re);
125           s = (0.5 * im) / r;
126         }
127       else
128         {
129           s = sqrt`'q (0.5 * d - 0.5 * re);
130           r = fabs`'q ((0.5 * im) / s);
131         }
133       COMPLEX_ASSIGN (v, r, copysign`'q (s, im));
134     }
135   return v;