]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/m4/cexp.m4
re PR libfortran/19280 (Inconsistent licensing of libgfortran)
[thirdparty/gcc.git] / libgfortran / m4 / cexp.m4
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
33include(`mtype.m4')dnl
34
35/* z = a + ib */
36/* Absolute value. */
37real_type
38cabs`'q (complex_type z)
39{
40 return hypot`'q (REALPART (z), IMAGPART (z));
41}
42
1e38f159
PB
43/* Complex argument. The angle made with the +ve real axis.
44 Range -pi-pi. */
6de9cd9a
DN
45real_type
46carg`'q (complex_type z)
47{
48 real_type arg;
49
1e38f159 50 return atan2`'q (IMAGPART (z), REALPART (z));
6de9cd9a
DN
51}
52
53/* exp(z) = exp(a)*(cos(b) + isin(b)) */
54complex_type
55cexp`'q (complex_type z)
56{
57 real_type a;
58 real_type b;
59 complex_type v;
60
61 a = REALPART (z);
62 b = IMAGPART (z);
63 COMPLEX_ASSIGN (v, cos`'q (b), sin`'q (b));
64 return exp`'q (a) * v;
65}
66
67/* log(z) = log (cabs(z)) + i*carg(z) */
68complex_type
69clog`'q (complex_type z)
70{
71 complex_type v;
72
73 COMPLEX_ASSIGN (v, log`'q (cabs`'q (z)), carg`'q (z));
74 return v;
75}
76
77/* log10(z) = log10 (cabs(z)) + i*carg(z) */
78complex_type
79clog10`'q (complex_type z)
80{
81 complex_type v;
82
83 COMPLEX_ASSIGN (v, log10`'q (cabs`'q (z)), carg`'q (z));
84 return v;
85}
86
87/* pow(base, power) = cexp (power * clog (base)) */
88complex_type
89cpow`'q (complex_type base, complex_type power)
90{
91 return cexp`'q (power * clog`'q (base));
92}
93
94/* sqrt(z). Algorithm pulled from glibc. */
95complex_type
96csqrt`'q (complex_type z)
97{
98 real_type re;
99 real_type im;
100 complex_type v;
101
102 re = REALPART (z);
103 im = IMAGPART (z);
104 if (im == 0.0)
105 {
106 if (re < 0.0)
107 {
108 COMPLEX_ASSIGN (v, 0.0, copysign`'q (sqrt`'q (-re), im));
109 }
110 else
111 {
112 COMPLEX_ASSIGN (v, fabs`'q (sqrt (re)),
113 copysign`'q (0.0, im));
114 }
115 }
116 else if (re == 0.0)
117 {
118 real_type r;
119
120 r = sqrt`'q (0.5 * fabs (im));
121
122 COMPLEX_ASSIGN (v, copysign`'q (r, im), r);
123 }
124 else
125 {
126 real_type d, r, s;
127
128 d = hypot`'q (re, im);
129 /* Use the identity 2 Re res Im res = Im x
130 to avoid cancellation error in d +/- Re x. */
131 if (re > 0)
132 {
133 r = sqrt`'q (0.5 * d + 0.5 * re);
134 s = (0.5 * im) / r;
135 }
136 else
137 {
138 s = sqrt`'q (0.5 * d - 0.5 * re);
139 r = fabs`'q ((0.5 * im) / s);
140 }
141
142 COMPLEX_ASSIGN (v, r, copysign`'q (s, im));
143 }
144 return v;
145}
146