2004-07-23 Matthias Klose <doko@debian.org>
[official-gcc.git] / libgfortran / generated / exp_c4.c
blob06e0103e3d8d05cd7378459c4dbe40ebbbdae233
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"
25 /* z = a + ib */
26 /* Absolute value. */
27 GFC_REAL_4
28 cabsf (GFC_COMPLEX_4 z)
30 return hypotf (REALPART (z), IMAGPART (z));
33 /* Complex argument. The angle made with the +ve real axis.
34 Range -pi-pi. */
35 GFC_REAL_4
36 cargf (GFC_COMPLEX_4 z)
38 GFC_REAL_4 arg;
40 return atan2f (IMAGPART (z), REALPART (z));
43 /* exp(z) = exp(a)*(cos(b) + isin(b)) */
44 GFC_COMPLEX_4
45 cexpf (GFC_COMPLEX_4 z)
47 GFC_REAL_4 a;
48 GFC_REAL_4 b;
49 GFC_COMPLEX_4 v;
51 a = REALPART (z);
52 b = IMAGPART (z);
53 COMPLEX_ASSIGN (v, cosf (b), sinf (b));
54 return expf (a) * v;
57 /* log(z) = log (cabs(z)) + i*carg(z) */
58 GFC_COMPLEX_4
59 clogf (GFC_COMPLEX_4 z)
61 GFC_COMPLEX_4 v;
63 COMPLEX_ASSIGN (v, logf (cabsf (z)), cargf (z));
64 return v;
67 /* log10(z) = log10 (cabs(z)) + i*carg(z) */
68 GFC_COMPLEX_4
69 clog10f (GFC_COMPLEX_4 z)
71 GFC_COMPLEX_4 v;
73 COMPLEX_ASSIGN (v, log10f (cabsf (z)), cargf (z));
74 return v;
77 /* pow(base, power) = cexp (power * clog (base)) */
78 GFC_COMPLEX_4
79 cpowf (GFC_COMPLEX_4 base, GFC_COMPLEX_4 power)
81 return cexpf (power * clogf (base));
84 /* sqrt(z). Algorithm pulled from glibc. */
85 GFC_COMPLEX_4
86 csqrtf (GFC_COMPLEX_4 z)
88 GFC_REAL_4 re;
89 GFC_REAL_4 im;
90 GFC_COMPLEX_4 v;
92 re = REALPART (z);
93 im = IMAGPART (z);
94 if (im == 0.0)
96 if (re < 0.0)
98 COMPLEX_ASSIGN (v, 0.0, copysignf (sqrtf (-re), im));
100 else
102 COMPLEX_ASSIGN (v, fabsf (sqrt (re)),
103 copysignf (0.0, im));
106 else if (re == 0.0)
108 GFC_REAL_4 r;
110 r = sqrtf (0.5 * fabs (im));
112 COMPLEX_ASSIGN (v, copysignf (r, im), r);
114 else
116 GFC_REAL_4 d, r, s;
118 d = hypotf (re, im);
119 /* Use the identity 2 Re res Im res = Im x
120 to avoid cancellation error in d +/- Re x. */
121 if (re > 0)
123 r = sqrtf (0.5 * d + 0.5 * re);
124 s = (0.5 * im) / r;
126 else
128 s = sqrtf (0.5 * d - 0.5 * re);
129 r = fabsf ((0.5 * im) / s);
132 COMPLEX_ASSIGN (v, r, copysignf (s, im));
134 return v;