OSDN Git Service

Fix another mips typo.
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / exp_c8.c
1 /* Complex exponential functions
2    Copyright 2002, 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
6
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.
11
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.
16
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"
23
24
25 /* z = a + ib  */
26 /* Absolute value.  */
27 GFC_REAL_8
28 cabs (GFC_COMPLEX_8 z)
29 {
30   return hypot (REALPART (z), IMAGPART (z));
31 }
32
33 /* Complex argument.  The angle made with the +ve real axis.
34    Range -pi-pi.  */
35 GFC_REAL_8
36 carg (GFC_COMPLEX_8 z)
37 {
38   GFC_REAL_8 arg;
39
40   return atan2 (IMAGPART (z), REALPART (z));
41 }
42
43 /* exp(z) = exp(a)*(cos(b) + isin(b))  */
44 GFC_COMPLEX_8
45 cexp (GFC_COMPLEX_8 z)
46 {
47   GFC_REAL_8 a;
48   GFC_REAL_8 b;
49   GFC_COMPLEX_8 v;
50
51   a = REALPART (z);
52   b = IMAGPART (z);
53   COMPLEX_ASSIGN (v, cos (b), sin (b));
54   return exp (a) * v;
55 }
56
57 /* log(z) = log (cabs(z)) + i*carg(z)  */
58 GFC_COMPLEX_8
59 clog (GFC_COMPLEX_8 z)
60 {
61   GFC_COMPLEX_8 v;
62
63   COMPLEX_ASSIGN (v, log (cabs (z)), carg (z));
64   return v;
65 }
66
67 /* log10(z) = log10 (cabs(z)) + i*carg(z)  */
68 GFC_COMPLEX_8
69 clog10 (GFC_COMPLEX_8 z)
70 {
71   GFC_COMPLEX_8 v;
72
73   COMPLEX_ASSIGN (v, log10 (cabs (z)), carg (z));
74   return v;
75 }
76
77 /* pow(base, power) = cexp (power * clog (base))  */
78 GFC_COMPLEX_8
79 cpow (GFC_COMPLEX_8 base, GFC_COMPLEX_8 power)
80 {
81   return cexp (power * clog (base));
82 }
83
84 /* sqrt(z).  Algorithm pulled from glibc.  */
85 GFC_COMPLEX_8
86 csqrt (GFC_COMPLEX_8 z)
87 {
88   GFC_REAL_8 re;
89   GFC_REAL_8 im;
90   GFC_COMPLEX_8 v;
91
92   re = REALPART (z);
93   im = IMAGPART (z);
94   if (im == 0.0)
95     {
96       if (re < 0.0)
97         {
98           COMPLEX_ASSIGN (v, 0.0, copysign (sqrt (-re), im));
99         }
100       else
101         {
102           COMPLEX_ASSIGN (v, fabs (sqrt (re)),
103                           copysign (0.0, im));
104         }
105     }
106   else if (re == 0.0)
107     {
108       GFC_REAL_8 r;
109
110       r = sqrt (0.5 * fabs (im));
111
112       COMPLEX_ASSIGN (v, copysign (r, im), r);
113     }
114   else
115     {
116       GFC_REAL_8 d, r, s;
117
118       d = hypot (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)
122         {
123           r = sqrt (0.5 * d + 0.5 * re);
124           s = (0.5 * im) / r;
125         }
126       else
127         {
128           s = sqrt (0.5 * d - 0.5 * re);
129           r = fabs ((0.5 * im) / s);
130         }
131
132       COMPLEX_ASSIGN (v, r, copysign (s, im));
133     }
134   return v;
135 }
136