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