Fix missed replacements.
[official-gcc.git] / libgfortran / generated / exp_c8.c
blob9c0fb20e24861610cfe229598b804d6e519672b6
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 (libgfortran).
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.
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.)
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.
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"
34 /* z = a + ib */
35 /* Absolute value. */
36 GFC_REAL_8
37 cabs (GFC_COMPLEX_8 z)
39 return hypot (REALPART (z), IMAGPART (z));
42 /* Complex argument. The angle made with the +ve real axis.
43 Range -pi-pi. */
44 GFC_REAL_8
45 carg (GFC_COMPLEX_8 z)
47 GFC_REAL_8 arg;
49 return atan2 (IMAGPART (z), REALPART (z));
52 /* exp(z) = exp(a)*(cos(b) + isin(b)) */
53 GFC_COMPLEX_8
54 cexp (GFC_COMPLEX_8 z)
56 GFC_REAL_8 a;
57 GFC_REAL_8 b;
58 GFC_COMPLEX_8 v;
60 a = REALPART (z);
61 b = IMAGPART (z);
62 COMPLEX_ASSIGN (v, cos (b), sin (b));
63 return exp (a) * v;
66 /* log(z) = log (cabs(z)) + i*carg(z) */
67 GFC_COMPLEX_8
68 clog (GFC_COMPLEX_8 z)
70 GFC_COMPLEX_8 v;
72 COMPLEX_ASSIGN (v, log (cabs (z)), carg (z));
73 return v;
76 /* log10(z) = log10 (cabs(z)) + i*carg(z) */
77 GFC_COMPLEX_8
78 clog10 (GFC_COMPLEX_8 z)
80 GFC_COMPLEX_8 v;
82 COMPLEX_ASSIGN (v, log10 (cabs (z)), carg (z));
83 return v;
86 /* pow(base, power) = cexp (power * clog (base)) */
87 GFC_COMPLEX_8
88 cpow (GFC_COMPLEX_8 base, GFC_COMPLEX_8 power)
90 return cexp (power * clog (base));
93 /* sqrt(z). Algorithm pulled from glibc. */
94 GFC_COMPLEX_8
95 csqrt (GFC_COMPLEX_8 z)
97 GFC_REAL_8 re;
98 GFC_REAL_8 im;
99 GFC_COMPLEX_8 v;
101 re = REALPART (z);
102 im = IMAGPART (z);
103 if (im == 0.0)
105 if (re < 0.0)
107 COMPLEX_ASSIGN (v, 0.0, copysign (sqrt (-re), im));
109 else
111 COMPLEX_ASSIGN (v, fabs (sqrt (re)),
112 copysign (0.0, im));
115 else if (re == 0.0)
117 GFC_REAL_8 r;
119 r = sqrt (0.5 * fabs (im));
121 COMPLEX_ASSIGN (v, copysign (r, im), r);
123 else
125 GFC_REAL_8 d, r, s;
127 d = hypot (re, im);
128 /* Use the identity 2 Re res Im res = Im x
129 to avoid cancellation error in d +/- Re x. */
130 if (re > 0)
132 r = sqrt (0.5 * d + 0.5 * re);
133 s = (0.5 * im) / r;
135 else
137 s = sqrt (0.5 * d - 0.5 * re);
138 r = fabs ((0.5 * im) / s);
141 COMPLEX_ASSIGN (v, r, copysign (s, im));
143 return v;