]> git.ipfire.org Git - thirdparty/glibc.git/blob - math/s_cexp_template.c
Improve the accuracy of tgamma (BZ #26983)
[thirdparty/glibc.git] / math / s_cexp_template.c
1 /* Return value of complex exponential function for a float type.
2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20 #include <complex.h>
21 #include <fenv.h>
22 #include <math.h>
23 #include <math_private.h>
24 #include <math-underflow.h>
25 #include <float.h>
26
27 CFLOAT
28 M_DECL_FUNC (__cexp) (CFLOAT x)
29 {
30 CFLOAT retval;
31 int rcls = fpclassify (__real__ x);
32 int icls = fpclassify (__imag__ x);
33
34 if (__glibc_likely (rcls >= FP_ZERO))
35 {
36 /* Real part is finite. */
37 if (__glibc_likely (icls >= FP_ZERO))
38 {
39 /* Imaginary part is finite. */
40 const int t = (int) ((M_MAX_EXP - 1) * M_MLIT (M_LN2));
41 FLOAT sinix, cosix;
42
43 if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
44 {
45 M_SINCOS (__imag__ x, &sinix, &cosix);
46 }
47 else
48 {
49 sinix = __imag__ x;
50 cosix = 1;
51 }
52
53 if (__real__ x > t)
54 {
55 FLOAT exp_t = M_EXP (t);
56 __real__ x -= t;
57 sinix *= exp_t;
58 cosix *= exp_t;
59 if (__real__ x > t)
60 {
61 __real__ x -= t;
62 sinix *= exp_t;
63 cosix *= exp_t;
64 }
65 }
66 if (__real__ x > t)
67 {
68 /* Overflow (original real part of x > 3t). */
69 __real__ retval = M_MAX * cosix;
70 __imag__ retval = M_MAX * sinix;
71 }
72 else
73 {
74 FLOAT exp_val = M_EXP (__real__ x);
75 __real__ retval = exp_val * cosix;
76 __imag__ retval = exp_val * sinix;
77 }
78 math_check_force_underflow_complex (retval);
79 }
80 else
81 {
82 /* If the imaginary part is +-inf or NaN and the real part
83 is not +-inf the result is NaN + iNaN. */
84 __real__ retval = M_NAN;
85 __imag__ retval = M_NAN;
86
87 feraiseexcept (FE_INVALID);
88 }
89 }
90 else if (__glibc_likely (rcls == FP_INFINITE))
91 {
92 /* Real part is infinite. */
93 if (__glibc_likely (icls >= FP_ZERO))
94 {
95 /* Imaginary part is finite. */
96 FLOAT value = signbit (__real__ x) ? 0 : M_HUGE_VAL;
97
98 if (icls == FP_ZERO)
99 {
100 /* Imaginary part is 0.0. */
101 __real__ retval = value;
102 __imag__ retval = __imag__ x;
103 }
104 else
105 {
106 FLOAT sinix, cosix;
107
108 if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
109 {
110 M_SINCOS (__imag__ x, &sinix, &cosix);
111 }
112 else
113 {
114 sinix = __imag__ x;
115 cosix = 1;
116 }
117
118 __real__ retval = M_COPYSIGN (value, cosix);
119 __imag__ retval = M_COPYSIGN (value, sinix);
120 }
121 }
122 else if (signbit (__real__ x) == 0)
123 {
124 __real__ retval = M_HUGE_VAL;
125 __imag__ retval = __imag__ x - __imag__ x;
126 }
127 else
128 {
129 __real__ retval = 0;
130 __imag__ retval = M_COPYSIGN (0, __imag__ x);
131 }
132 }
133 else
134 {
135 /* If the real part is NaN the result is NaN + iNaN unless the
136 imaginary part is zero. */
137 __real__ retval = M_NAN;
138 if (icls == FP_ZERO)
139 __imag__ retval = __imag__ x;
140 else
141 {
142 __imag__ retval = M_NAN;
143
144 if (rcls != FP_NAN || icls != FP_NAN)
145 feraiseexcept (FE_INVALID);
146 }
147 }
148
149 return retval;
150 }
151 declare_mgen_alias (__cexp, cexp)