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