]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/s_cexp_template.c
Fix the inaccuracy of j0f/j1f/y0f/y1f [BZ #14469, #14470, #14471, #14472]
[thirdparty/glibc.git] / math / s_cexp_template.c
CommitLineData
feb62dda 1/* Return value of complex exponential function for a float type.
2b778ceb 2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
1dbc54f6
PM
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
5a82c748 18 <https://www.gnu.org/licenses/>. */
1dbc54f6
PM
19
20#include <complex.h>
21#include <fenv.h>
22#include <math.h>
23#include <math_private.h>
8f5b00d3 24#include <math-underflow.h>
1dbc54f6
PM
25#include <float.h>
26
feb62dda
PM
27CFLOAT
28M_DECL_FUNC (__cexp) (CFLOAT x)
1dbc54f6 29{
feb62dda 30 CFLOAT retval;
1dbc54f6
PM
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. */
feb62dda
PM
40 const int t = (int) ((M_MAX_EXP - 1) * M_MLIT (M_LN2));
41 FLOAT sinix, cosix;
1dbc54f6 42
feb62dda 43 if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
1dbc54f6 44 {
feb62dda 45 M_SINCOS (__imag__ x, &sinix, &cosix);
1dbc54f6
PM
46 }
47 else
48 {
49 sinix = __imag__ x;
feb62dda 50 cosix = 1;
1dbc54f6
PM
51 }
52
53 if (__real__ x > t)
54 {
feb62dda 55 FLOAT exp_t = M_EXP (t);
1dbc54f6
PM
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). */
feb62dda
PM
69 __real__ retval = M_MAX * cosix;
70 __imag__ retval = M_MAX * sinix;
1dbc54f6
PM
71 }
72 else
73 {
feb62dda 74 FLOAT exp_val = M_EXP (__real__ x);
1dbc54f6
PM
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. */
feb62dda
PM
84 __real__ retval = M_NAN;
85 __imag__ retval = M_NAN;
1dbc54f6
PM
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. */
feb62dda 96 FLOAT value = signbit (__real__ x) ? 0 : M_HUGE_VAL;
1dbc54f6
PM
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 {
feb62dda 106 FLOAT sinix, cosix;
1dbc54f6 107
feb62dda 108 if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
1dbc54f6 109 {
feb62dda 110 M_SINCOS (__imag__ x, &sinix, &cosix);
1dbc54f6
PM
111 }
112 else
113 {
114 sinix = __imag__ x;
feb62dda 115 cosix = 1;
1dbc54f6
PM
116 }
117
feb62dda
PM
118 __real__ retval = M_COPYSIGN (value, cosix);
119 __imag__ retval = M_COPYSIGN (value, sinix);
1dbc54f6
PM
120 }
121 }
122 else if (signbit (__real__ x) == 0)
123 {
feb62dda 124 __real__ retval = M_HUGE_VAL;
e886c367 125 __imag__ retval = __imag__ x - __imag__ x;
1dbc54f6
PM
126 }
127 else
128 {
feb62dda
PM
129 __real__ retval = 0;
130 __imag__ retval = M_COPYSIGN (0, __imag__ x);
1dbc54f6
PM
131 }
132 }
133 else
134 {
135 /* If the real part is NaN the result is NaN + iNaN unless the
136 imaginary part is zero. */
feb62dda 137 __real__ retval = M_NAN;
1dbc54f6
PM
138 if (icls == FP_ZERO)
139 __imag__ retval = __imag__ x;
140 else
141 {
feb62dda 142 __imag__ retval = M_NAN;
1dbc54f6
PM
143
144 if (rcls != FP_NAN || icls != FP_NAN)
145 feraiseexcept (FE_INVALID);
146 }
147 }
148
149 return retval;
150}
feb62dda 151declare_mgen_alias (__cexp, cexp)