Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Double-precision e^x function. |
| 3 | * |
| 4 | * Copyright (c) 2018, Arm Limited. |
Szabolcs Nagy | 11253b0 | 2018-11-12 11:10:57 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: MIT |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Szabolcs Nagy | f934c86 | 2019-08-28 15:15:15 +0100 | [diff] [blame] | 8 | #include <float.h> |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 9 | #include <math.h> |
| 10 | #include <stdint.h> |
| 11 | #include "math_config.h" |
| 12 | |
| 13 | #define N (1 << EXP_TABLE_BITS) |
| 14 | #define InvLn2N __exp_data.invln2N |
| 15 | #define NegLn2hiN __exp_data.negln2hiN |
| 16 | #define NegLn2loN __exp_data.negln2loN |
| 17 | #define Shift __exp_data.shift |
| 18 | #define T __exp_data.tab |
| 19 | #define C2 __exp_data.poly[5 - EXP_POLY_ORDER] |
| 20 | #define C3 __exp_data.poly[6 - EXP_POLY_ORDER] |
| 21 | #define C4 __exp_data.poly[7 - EXP_POLY_ORDER] |
| 22 | #define C5 __exp_data.poly[8 - EXP_POLY_ORDER] |
| 23 | #define C6 __exp_data.poly[9 - EXP_POLY_ORDER] |
| 24 | |
Szabolcs Nagy | bc4b901 | 2018-07-03 18:54:51 +0100 | [diff] [blame] | 25 | /* Handle cases that may overflow or underflow when computing the result that |
| 26 | is scale*(1+TMP) without intermediate rounding. The bit representation of |
| 27 | scale is in SBITS, however it has a computed exponent that may have |
| 28 | overflown into the sign bit so that needs to be adjusted before using it as |
| 29 | a double. (int32_t)KI is the k used in the argument reduction and exponent |
| 30 | adjustment of scale, positive k here means the result may overflow and |
| 31 | negative k means the result may underflow. */ |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 32 | static inline double |
| 33 | specialcase (double_t tmp, uint64_t sbits, uint64_t ki) |
| 34 | { |
| 35 | double_t scale, y; |
| 36 | |
| 37 | if ((ki & 0x80000000) == 0) |
| 38 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 39 | /* k > 0, the exponent of scale might have overflowed by <= 460. */ |
| 40 | sbits -= 1009ull << 52; |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 41 | scale = asdouble (sbits); |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 42 | y = 0x1p1009 * (scale + scale * tmp); |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 43 | return check_oflow (eval_as_double (y)); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 44 | } |
| 45 | /* k < 0, need special care in the subnormal range. */ |
| 46 | sbits += 1022ull << 52; |
| 47 | scale = asdouble (sbits); |
| 48 | y = scale + scale * tmp; |
| 49 | if (y < 1.0) |
| 50 | { |
| 51 | /* Round y to the right precision before scaling it into the subnormal |
| 52 | range to avoid double rounding that can cause 0.5+E/2 ulp error where |
| 53 | E is the worst-case ulp error outside the subnormal range. So this |
| 54 | is only useful if the goal is better than 1 ulp worst-case error. */ |
| 55 | double_t hi, lo; |
| 56 | lo = scale - y + scale * tmp; |
| 57 | hi = 1.0 + y; |
| 58 | lo = 1.0 - hi + y + lo; |
| 59 | y = eval_as_double (hi + lo) - 1.0; |
| 60 | /* Avoid -0.0 with downward rounding. */ |
| 61 | if (WANT_ROUNDING && y == 0.0) |
| 62 | y = 0.0; |
| 63 | /* The underflow exception needs to be signaled explicitly. */ |
Szabolcs Nagy | 5fa69e1 | 2018-06-12 17:18:24 +0100 | [diff] [blame] | 64 | force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 65 | } |
| 66 | y = 0x1p-1022 * y; |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 67 | return check_uflow (eval_as_double (y)); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Szabolcs Nagy | 58ce45c | 2018-06-29 11:06:13 +0100 | [diff] [blame] | 70 | /* Top 12 bits of a double (sign and exponent bits). */ |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 71 | static inline uint32_t |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 72 | top12 (double x) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 73 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 74 | return asuint64 (x) >> 52; |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Szabolcs Nagy | 58ce45c | 2018-06-29 11:06:13 +0100 | [diff] [blame] | 77 | /* Computes exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|. |
| 78 | If hastail is 0 then xtail is assumed to be 0 too. */ |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 79 | static inline double |
| 80 | exp_inline (double x, double xtail, int hastail) |
| 81 | { |
| 82 | uint32_t abstop; |
| 83 | uint64_t ki, idx, top, sbits; |
| 84 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
| 85 | double_t kd, z, r, r2, scale, tail, tmp; |
| 86 | |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 87 | abstop = top12 (x) & 0x7ff; |
| 88 | if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54))) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 89 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 90 | if (abstop - top12 (0x1p-54) >= 0x80000000) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 91 | /* Avoid spurious underflow for tiny x. */ |
| 92 | /* Note: 0 is common input. */ |
| 93 | return WANT_ROUNDING ? 1.0 + x : 1.0; |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 94 | if (abstop >= top12 (1024.0)) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 95 | { |
| 96 | if (asuint64 (x) == asuint64 (-INFINITY)) |
| 97 | return 0.0; |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 98 | if (abstop >= top12 (INFINITY)) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 99 | return 1.0 + x; |
| 100 | if (asuint64 (x) >> 63) |
| 101 | return __math_uflow (0); |
| 102 | else |
| 103 | return __math_oflow (0); |
| 104 | } |
| 105 | /* Large x is special cased below. */ |
| 106 | abstop = 0; |
| 107 | } |
| 108 | |
| 109 | /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */ |
| 110 | /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */ |
| 111 | z = InvLn2N * x; |
| 112 | #if TOINT_INTRINSICS |
| 113 | kd = roundtoint (z); |
| 114 | ki = converttoint (z); |
| 115 | #elif EXP_USE_TOINT_NARROW |
| 116 | /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */ |
| 117 | kd = eval_as_double (z + Shift); |
| 118 | ki = asuint64 (kd) >> 16; |
| 119 | kd = (double_t) (int32_t) ki; |
| 120 | #else |
| 121 | /* z - kd is in [-1, 1] in non-nearest rounding modes. */ |
| 122 | kd = eval_as_double (z + Shift); |
| 123 | ki = asuint64 (kd); |
| 124 | kd -= Shift; |
| 125 | #endif |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 126 | r = x + kd * NegLn2hiN + kd * NegLn2loN; |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 127 | /* The code assumes 2^-200 < |xtail| < 2^-8/N. */ |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 128 | if (hastail) |
| 129 | r += xtail; |
| 130 | /* 2^(k/N) ~= scale * (1 + tail). */ |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 131 | idx = 2 * (ki % N); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 132 | top = ki << (52 - EXP_TABLE_BITS); |
| 133 | tail = asdouble (T[idx]); |
| 134 | /* This is only a valid scale when -1023*N < k < 1024*N. */ |
| 135 | sbits = T[idx + 1] + top; |
| 136 | /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */ |
| 137 | /* Evaluation is optimized assuming superscalar pipelined execution. */ |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 138 | r2 = r * r; |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 139 | /* Without fma the worst case error is 0.25/N ulp larger. */ |
| 140 | /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */ |
| 141 | #if EXP_POLY_ORDER == 4 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 142 | tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 143 | #elif EXP_POLY_ORDER == 5 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 144 | tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 145 | #elif EXP_POLY_ORDER == 6 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 146 | tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 147 | #endif |
| 148 | if (unlikely (abstop == 0)) |
| 149 | return specialcase (tmp, sbits, ki); |
| 150 | scale = asdouble (sbits); |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 151 | /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there |
| 152 | is no spurious underflow here even without fma. */ |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 153 | return eval_as_double (scale + scale * tmp); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | double |
| 157 | exp (double x) |
| 158 | { |
| 159 | return exp_inline (x, 0, 0); |
| 160 | } |
| 161 | |
| 162 | /* May be useful for implementing pow where more than double |
| 163 | precision input is needed. */ |
| 164 | double |
| 165 | __exp_dd (double x, double xtail) |
| 166 | { |
| 167 | return exp_inline (x, xtail, 1); |
| 168 | } |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 169 | #if USE_GLIBC_ABI |
| 170 | strong_alias (exp, __exp_finite) |
| 171 | hidden_alias (exp, __ieee754_exp) |
| 172 | hidden_alias (__exp_dd, __exp1) |
Szabolcs Nagy | f934c86 | 2019-08-28 15:15:15 +0100 | [diff] [blame] | 173 | # if LDBL_MANT_DIG == 53 |
| 174 | long double expl (long double x) { return exp (x); } |
| 175 | # endif |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 176 | #endif |