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