| 1 | /* Double-precision e^x function. |
| 2 | Copyright (C) 2018-2021 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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/>. */ |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <stdint.h> |
| 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" |
| 27 | |
| 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] |
| 38 | |
| 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. */ |
| 46 | static inline double |
| 47 | specialcase (double_t tmp, uint64_t sbits, uint64_t ki) |
| 48 | { |
| 49 | double_t scale, y; |
| 50 | |
| 51 | if ((ki & 0x80000000) == 0) |
| 52 | { |
| 53 | /* k > 0, the exponent of scale might have overflowed by <= 460. */ |
| 54 | sbits -= 1009ull << 52; |
| 55 | scale = asdouble (sbits); |
| 56 | y = 0x1p1009 * (scale + scale * tmp); |
| 57 | return check_oflow (y); |
| 58 | } |
| 59 | /* k < 0, need special care in the subnormal range. */ |
| 60 | sbits += 1022ull << 52; |
| 61 | scale = asdouble (sbits); |
| 62 | y = scale + scale * tmp; |
| 63 | if (y < 1.0) |
| 64 | { |
| 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. */ |
| 69 | double_t hi, lo; |
| 70 | lo = scale - y + scale * tmp; |
| 71 | hi = 1.0 + y; |
| 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) |
| 76 | y = 0.0; |
| 77 | /* The underflow exception needs to be signaled explicitly. */ |
| 78 | math_force_eval (math_opt_barrier (0x1p-1022) * 0x1p-1022); |
| 79 | } |
| 80 | y = 0x1p-1022 * y; |
| 81 | return check_uflow (y); |
| 82 | } |
| 83 | |
| 84 | /* Top 12 bits of a double (sign and exponent bits). */ |
| 85 | static inline uint32_t |
| 86 | top12 (double x) |
| 87 | { |
| 88 | return asuint64 (x) >> 52; |
| 89 | } |
| 90 | |
| 91 | #ifndef SECTION |
| 92 | # define SECTION |
| 93 | #endif |
| 94 | |
| 95 | double |
| 96 | SECTION |
| 97 | __exp (double x) |
| 98 | { |
| 99 | uint32_t abstop; |
| 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; |
| 103 | |
| 104 | abstop = top12 (x) & 0x7ff; |
| 105 | if (__glibc_unlikely (abstop - top12 (0x1p-54) |
| 106 | >= top12 (512.0) - top12 (0x1p-54))) |
| 107 | { |
| 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)) |
| 113 | { |
| 114 | if (asuint64 (x) == asuint64 (-INFINITY)) |
| 115 | return 0.0; |
| 116 | if (abstop >= top12 (INFINITY)) |
| 117 | return 1.0 + x; |
| 118 | if (asuint64 (x) >> 63) |
| 119 | return __math_uflow (0); |
| 120 | else |
| 121 | return __math_oflow (0); |
| 122 | } |
| 123 | /* Large x is special cased below. */ |
| 124 | abstop = 0; |
| 125 | } |
| 126 | |
| 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]. */ |
| 129 | z = InvLn2N * x; |
| 130 | #if TOINT_INTRINSICS |
| 131 | kd = roundtoint (z); |
| 132 | ki = converttoint (z); |
| 133 | #else |
| 134 | /* z - kd is in [-1, 1] in non-nearest rounding modes. */ |
| 135 | kd = math_narrow_eval (z + Shift); |
| 136 | ki = asuint64 (kd); |
| 137 | kd -= Shift; |
| 138 | #endif |
| 139 | r = x + kd * NegLn2hiN + kd * NegLn2loN; |
| 140 | /* 2^(k/N) ~= scale * (1 + tail). */ |
| 141 | idx = 2 * (ki % N); |
| 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. */ |
| 148 | r2 = r * r; |
| 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; |
| 158 | } |
| 159 | #ifndef __exp |
| 160 | hidden_def (__exp) |
| 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) |
| 166 | # else |
| 167 | libm_alias_double (__exp, exp) |
| 168 | # endif |
| 169 | #endif |
| 170 | |