1 /* Double-precision e^x function.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 #include <math-barriers.h>
22 #include <math-narrow-eval.h>
23 #include <math-svid-compat.h>
24 #include <libm-alias-finite.h>
25 #include <libm-alias-double.h>
26 #include "math_config.h"
28 #define N (1 << EXP_TABLE_BITS)
29 #define InvLn2N __exp_data.invln2N
30 #define NegLn2hiN __exp_data.negln2hiN
31 #define NegLn2loN __exp_data.negln2loN
32 #define Shift __exp_data.shift
33 #define T __exp_data.tab
34 #define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
35 #define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
36 #define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
37 #define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
39 /* Handle cases that may overflow or underflow when computing the result that
40 is scale*(1+TMP) without intermediate rounding. The bit representation of
41 scale is in SBITS, however it has a computed exponent that may have
42 overflown into the sign bit so that needs to be adjusted before using it as
43 a double. (int32_t)KI is the k used in the argument reduction and exponent
44 adjustment of scale, positive k here means the result may overflow and
45 negative k means the result may underflow. */
47 specialcase (double_t tmp
, uint64_t sbits
, uint64_t ki
)
51 if ((ki
& 0x80000000) == 0)
53 /* k > 0, the exponent of scale might have overflowed by <= 460. */
54 sbits
-= 1009ull << 52;
55 scale
= asdouble (sbits
);
56 y
= 0x1p
1009 * (scale
+ scale
* tmp
);
57 return check_oflow (y
);
59 /* k < 0, need special care in the subnormal range. */
60 sbits
+= 1022ull << 52;
61 scale
= asdouble (sbits
);
62 y
= scale
+ scale
* tmp
;
65 /* Round y to the right precision before scaling it into the subnormal
66 range to avoid double rounding that can cause 0.5+E/2 ulp error where
67 E is the worst-case ulp error outside the subnormal range. So this
68 is only useful if the goal is better than 1 ulp worst-case error. */
70 lo
= scale
- y
+ scale
* tmp
;
72 lo
= 1.0 - hi
+ y
+ lo
;
73 y
= math_narrow_eval (hi
+ lo
) - 1.0;
74 /* Avoid -0.0 with downward rounding. */
75 if (WANT_ROUNDING
&& y
== 0.0)
77 /* The underflow exception needs to be signaled explicitly. */
78 math_force_eval (math_opt_barrier (0x1p
-1022) * 0x1p
-1022);
81 return check_uflow (y
);
84 /* Top 12 bits of a double (sign and exponent bits). */
85 static inline uint32_t
88 return asuint64 (x
) >> 52;
100 uint64_t ki
, idx
, top
, sbits
;
101 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
102 double_t kd
, z
, r
, r2
, scale
, tail
, tmp
;
104 abstop
= top12 (x
) & 0x7ff;
105 if (__glibc_unlikely (abstop
- top12 (0x1p
-54)
106 >= top12 (512.0) - top12 (0x1p
-54)))
108 if (abstop
- top12 (0x1p
-54) >= 0x80000000)
109 /* Avoid spurious underflow for tiny x. */
110 /* Note: 0 is common input. */
111 return WANT_ROUNDING
? 1.0 + x
: 1.0;
112 if (abstop
>= top12 (1024.0))
114 if (asuint64 (x
) == asuint64 (-INFINITY
))
116 if (abstop
>= top12 (INFINITY
))
118 if (asuint64 (x
) >> 63)
119 return __math_uflow (0);
121 return __math_oflow (0);
123 /* Large x is special cased below. */
127 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
128 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
132 ki
= converttoint (z
);
134 /* z - kd is in [-1, 1] in non-nearest rounding modes. */
135 kd
= math_narrow_eval (z
+ Shift
);
139 r
= x
+ kd
* NegLn2hiN
+ kd
* NegLn2loN
;
140 /* 2^(k/N) ~= scale * (1 + tail). */
142 top
= ki
<< (52 - EXP_TABLE_BITS
);
143 tail
= asdouble (T
[idx
]);
144 /* This is only a valid scale when -1023*N < k < 1024*N. */
145 sbits
= T
[idx
+ 1] + top
;
146 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
147 /* Evaluation is optimized assuming superscalar pipelined execution. */
149 /* Without fma the worst case error is 0.25/N ulp larger. */
150 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
151 tmp
= tail
+ r
+ r2
* (C2
+ r
* C3
) + r2
* r2
* (C4
+ r
* C5
);
152 if (__glibc_unlikely (abstop
== 0))
153 return specialcase (tmp
, sbits
, ki
);
154 scale
= asdouble (sbits
);
155 /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-739, so there
156 is no spurious underflow here even without fma. */
157 return scale
+ scale
* tmp
;
161 strong_alias (__exp
, __ieee754_exp
)
162 libm_alias_finite (__ieee754_exp
, __exp
)
163 # if LIBM_SVID_COMPAT
164 versioned_symbol (libm
, __exp
, exp
, GLIBC_2_29
);
165 libm_alias_double_other (__exp
, exp
)
167 libm_alias_double (__exp
, exp
)