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