Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libgfortran / generated / exp_c4.c
blob149d7d0d34e00aebf80a891a20b2401e76bfc3a3
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_4
37 cabsf (GFC_COMPLEX_4 z)
39 return hypotf (REALPART (z), IMAGPART (z));
42 /* Complex argument. The angle made with the +ve real axis.
43 Range -pi-pi. */
44 GFC_REAL_4
45 cargf (GFC_COMPLEX_4 z)
47 GFC_REAL_4 arg;
49 return atan2f (IMAGPART (z), REALPART (z));
52 /* exp(z) = exp(a)*(cos(b) + isin(b)) */
53 GFC_COMPLEX_4
54 cexpf (GFC_COMPLEX_4 z)
56 GFC_REAL_4 a;
57 GFC_REAL_4 b;
58 GFC_COMPLEX_4 v;
60 a = REALPART (z);
61 b = IMAGPART (z);
62 COMPLEX_ASSIGN (v, cosf (b), sinf (b));
63 return expf (a) * v;
66 /* log(z) = log (cabs(z)) + i*carg(z) */
67 GFC_COMPLEX_4
68 clogf (GFC_COMPLEX_4 z)
70 GFC_COMPLEX_4 v;
72 COMPLEX_ASSIGN (v, logf (cabsf (z)), cargf (z));
73 return v;
76 /* log10(z) = log10 (cabs(z)) + i*carg(z) */
77 GFC_COMPLEX_4
78 clog10f (GFC_COMPLEX_4 z)
80 GFC_COMPLEX_4 v;
82 COMPLEX_ASSIGN (v, log10f (cabsf (z)), cargf (z));
83 return v;
86 /* pow(base, power) = cexp (power * clog (base)) */
87 GFC_COMPLEX_4
88 cpowf (GFC_COMPLEX_4 base, GFC_COMPLEX_4 power)
90 return cexpf (power * clogf (base));
93 /* sqrt(z). Algorithm pulled from glibc. */
94 GFC_COMPLEX_4
95 csqrtf (GFC_COMPLEX_4 z)
97 GFC_REAL_4 re;
98 GFC_REAL_4 im;
99 GFC_COMPLEX_4 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, copysignf (sqrtf (-re), im));
109 else
111 COMPLEX_ASSIGN (v, fabsf (sqrt (re)),
112 copysignf (0.0, im));
115 else if (re == 0.0)
117 GFC_REAL_4 r;
119 r = sqrtf (0.5 * fabs (im));
121 COMPLEX_ASSIGN (v, copysignf (r, im), r);
123 else
125 GFC_REAL_4 d, r, s;
127 d = hypotf (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 = sqrtf (0.5 * d + 0.5 * re);
133 s = (0.5 * im) / r;
135 else
137 s = sqrtf (0.5 * d - 0.5 * re);
138 r = fabsf ((0.5 * im) / s);
141 COMPLEX_ASSIGN (v, r, copysignf (s, im));
143 return v;