Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Double-precision 2^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 Shift __exp_data.exp2_shift |
| 15 | #define T __exp_data.tab |
| 16 | #define C1 __exp_data.exp2_poly[0] |
| 17 | #define C2 __exp_data.exp2_poly[1] |
| 18 | #define C3 __exp_data.exp2_poly[2] |
| 19 | #define C4 __exp_data.exp2_poly[3] |
| 20 | #define C5 __exp_data.exp2_poly[4] |
| 21 | #define C6 __exp_data.exp2_poly[5] |
| 22 | |
Szabolcs Nagy | bc4b901 | 2018-07-03 18:54:51 +0100 | [diff] [blame] | 23 | /* Handle cases that may overflow or underflow when computing the result that |
| 24 | is scale*(1+TMP) without intermediate rounding. The bit representation of |
| 25 | scale is in SBITS, however it has a computed exponent that may have |
| 26 | overflown into the sign bit so that needs to be adjusted before using it as |
| 27 | a double. (int32_t)KI is the k used in the argument reduction and exponent |
| 28 | adjustment of scale, positive k here means the result may overflow and |
| 29 | negative k means the result may underflow. */ |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 30 | static inline double |
| 31 | specialcase (double_t tmp, uint64_t sbits, uint64_t ki) |
| 32 | { |
| 33 | double_t scale, y; |
| 34 | |
| 35 | if ((ki & 0x80000000) == 0) |
| 36 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 37 | /* k > 0, the exponent of scale might have overflowed by 1. */ |
| 38 | sbits -= 1ull << 52; |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 39 | scale = asdouble (sbits); |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 40 | y = 2 * (scale + scale * tmp); |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 41 | return check_oflow (eval_as_double (y)); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 42 | } |
| 43 | /* k < 0, need special care in the subnormal range. */ |
| 44 | sbits += 1022ull << 52; |
| 45 | scale = asdouble (sbits); |
| 46 | y = scale + scale * tmp; |
| 47 | if (y < 1.0) |
| 48 | { |
| 49 | /* Round y to the right precision before scaling it into the subnormal |
| 50 | range to avoid double rounding that can cause 0.5+E/2 ulp error where |
| 51 | E is the worst-case ulp error outside the subnormal range. So this |
| 52 | is only useful if the goal is better than 1 ulp worst-case error. */ |
| 53 | double_t hi, lo; |
| 54 | lo = scale - y + scale * tmp; |
| 55 | hi = 1.0 + y; |
| 56 | lo = 1.0 - hi + y + lo; |
| 57 | y = eval_as_double (hi + lo) - 1.0; |
| 58 | /* Avoid -0.0 with downward rounding. */ |
| 59 | if (WANT_ROUNDING && y == 0.0) |
| 60 | y = 0.0; |
| 61 | /* The underflow exception needs to be signaled explicitly. */ |
Szabolcs Nagy | 5fa69e1 | 2018-06-12 17:18:24 +0100 | [diff] [blame] | 62 | force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 63 | } |
| 64 | y = 0x1p-1022 * y; |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 65 | return check_uflow (eval_as_double (y)); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Szabolcs Nagy | 58ce45c | 2018-06-29 11:06:13 +0100 | [diff] [blame] | 68 | /* Top 12 bits of a double (sign and exponent bits). */ |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 69 | static inline uint32_t |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 70 | top12 (double x) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 71 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 72 | return asuint64 (x) >> 52; |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | double |
| 76 | exp2 (double x) |
| 77 | { |
| 78 | uint32_t abstop; |
| 79 | uint64_t ki, idx, top, sbits; |
| 80 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
| 81 | double_t kd, r, r2, scale, tail, tmp; |
| 82 | |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 83 | abstop = top12 (x) & 0x7ff; |
| 84 | if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54))) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 85 | { |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 86 | if (abstop - top12 (0x1p-54) >= 0x80000000) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 87 | /* Avoid spurious underflow for tiny x. */ |
| 88 | /* Note: 0 is common input. */ |
| 89 | return WANT_ROUNDING ? 1.0 + x : 1.0; |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 90 | if (abstop >= top12 (1024.0)) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 91 | { |
| 92 | if (asuint64 (x) == asuint64 (-INFINITY)) |
| 93 | return 0.0; |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 94 | if (abstop >= top12 (INFINITY)) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 95 | return 1.0 + x; |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 96 | if (!(asuint64 (x) >> 63)) |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 97 | return __math_oflow (0); |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 98 | else if (asuint64 (x) >= asuint64 (-1075.0)) |
| 99 | return __math_uflow (0); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 100 | } |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 101 | if (2 * asuint64 (x) > 2 * asuint64 (928.0)) |
| 102 | /* Large x is special cased below. */ |
| 103 | abstop = 0; |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)]. */ |
| 107 | /* x = k/N + r, with int k and r in [-1/2N, 1/2N]. */ |
| 108 | kd = eval_as_double (x + Shift); |
| 109 | ki = asuint64 (kd); /* k. */ |
| 110 | kd -= Shift; /* k/N for int k. */ |
| 111 | r = x - kd; |
| 112 | /* 2^(k/N) ~= scale * (1 + tail). */ |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 113 | idx = 2 * (ki % N); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 114 | top = ki << (52 - EXP_TABLE_BITS); |
| 115 | tail = asdouble (T[idx]); |
| 116 | /* This is only a valid scale when -1023*N < k < 1024*N. */ |
| 117 | sbits = T[idx + 1] + top; |
| 118 | /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1). */ |
| 119 | /* Evaluation is optimized assuming superscalar pipelined execution. */ |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 120 | r2 = r * r; |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 121 | /* Without fma the worst case error is 0.5/N ulp larger. */ |
| 122 | /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp. */ |
| 123 | #if EXP2_POLY_ORDER == 4 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 124 | tmp = tail + r * C1 + r2 * C2 + r * r2 * (C3 + r * C4); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 125 | #elif EXP2_POLY_ORDER == 5 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 126 | tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 127 | #elif EXP2_POLY_ORDER == 6 |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 128 | tmp = tail + r * C1 + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 129 | #endif |
| 130 | if (unlikely (abstop == 0)) |
| 131 | return specialcase (tmp, sbits, ki); |
| 132 | scale = asdouble (sbits); |
Szabolcs Nagy | 2117b83 | 2018-06-14 10:54:06 +0100 | [diff] [blame] | 133 | /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there |
| 134 | is no spurious underflow here even without fma. */ |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 135 | return eval_as_double (scale + scale * tmp); |
Szabolcs Nagy | 872aa77 | 2018-04-27 16:17:24 +0100 | [diff] [blame] | 136 | } |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 137 | #if USE_GLIBC_ABI |
| 138 | strong_alias (exp2, __exp2_finite) |
| 139 | hidden_alias (exp2, __ieee754_exp2) |
Szabolcs Nagy | f934c86 | 2019-08-28 15:15:15 +0100 | [diff] [blame] | 140 | # if LDBL_MANT_DIG == 53 |
| 141 | long double exp2l (long double x) { return exp2 (x); } |
| 142 | # endif |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 143 | #endif |