131914882SAlex Richardson /* 231914882SAlex Richardson * Single-precision pow function. 331914882SAlex Richardson * 4*f3087befSAndrew Turner * Copyright (c) 2017-2024, Arm Limited. 5072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 631914882SAlex Richardson */ 731914882SAlex Richardson 831914882SAlex Richardson #include <math.h> 931914882SAlex Richardson #include <stdint.h> 1031914882SAlex Richardson #include "math_config.h" 11*f3087befSAndrew Turner #include "test_defs.h" 1231914882SAlex Richardson 1331914882SAlex Richardson /* 1431914882SAlex Richardson POWF_LOG2_POLY_ORDER = 5 1531914882SAlex Richardson EXP2F_TABLE_BITS = 5 1631914882SAlex Richardson 1731914882SAlex Richardson ULP error: 0.82 (~ 0.5 + relerr*2^24) 1831914882SAlex Richardson relerr: 1.27 * 2^-26 (Relative error ~= 128*Ln2*relerr_log2 + relerr_exp2) 1931914882SAlex Richardson relerr_log2: 1.83 * 2^-33 (Relative error of logx.) 2031914882SAlex Richardson relerr_exp2: 1.69 * 2^-34 (Relative error of exp2(ylogx).) 2131914882SAlex Richardson */ 2231914882SAlex Richardson 2331914882SAlex Richardson #define N (1 << POWF_LOG2_TABLE_BITS) 2431914882SAlex Richardson #define T __powf_log2_data.tab 2531914882SAlex Richardson #define A __powf_log2_data.poly 2631914882SAlex Richardson #define OFF 0x3f330000 2731914882SAlex Richardson 2831914882SAlex Richardson /* Subnormal input is normalized so ix has negative biased exponent. 2931914882SAlex Richardson Output is multiplied by N (POWF_SCALE) if TOINT_INTRINICS is set. */ 3031914882SAlex Richardson static inline double_t 3131914882SAlex Richardson log2_inline (uint32_t ix) 3231914882SAlex Richardson { 3331914882SAlex Richardson /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ 3431914882SAlex Richardson double_t z, r, r2, r4, p, q, y, y0, invc, logc; 3531914882SAlex Richardson uint32_t iz, top, tmp; 3631914882SAlex Richardson int k, i; 3731914882SAlex Richardson 3831914882SAlex Richardson /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. 3931914882SAlex Richardson The range is split into N subintervals. 4031914882SAlex Richardson The ith subinterval contains z and c is near its center. */ 4131914882SAlex Richardson tmp = ix - OFF; 4231914882SAlex Richardson i = (tmp >> (23 - POWF_LOG2_TABLE_BITS)) % N; 4331914882SAlex Richardson top = tmp & 0xff800000; 4431914882SAlex Richardson iz = ix - top; 4531914882SAlex Richardson k = (int32_t) top >> (23 - POWF_SCALE_BITS); /* arithmetic shift */ 4631914882SAlex Richardson invc = T[i].invc; 4731914882SAlex Richardson logc = T[i].logc; 4831914882SAlex Richardson z = (double_t) asfloat (iz); 4931914882SAlex Richardson 5031914882SAlex Richardson /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */ 5131914882SAlex Richardson r = z * invc - 1; 5231914882SAlex Richardson y0 = logc + (double_t) k; 5331914882SAlex Richardson 5431914882SAlex Richardson /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */ 5531914882SAlex Richardson r2 = r * r; 5631914882SAlex Richardson y = A[0] * r + A[1]; 5731914882SAlex Richardson p = A[2] * r + A[3]; 5831914882SAlex Richardson r4 = r2 * r2; 5931914882SAlex Richardson q = A[4] * r + y0; 6031914882SAlex Richardson q = p * r2 + q; 6131914882SAlex Richardson y = y * r4 + q; 6231914882SAlex Richardson return y; 6331914882SAlex Richardson } 6431914882SAlex Richardson 6531914882SAlex Richardson #undef N 6631914882SAlex Richardson #undef T 6731914882SAlex Richardson #define N (1 << EXP2F_TABLE_BITS) 6831914882SAlex Richardson #define T __exp2f_data.tab 6931914882SAlex Richardson #define SIGN_BIAS (1 << (EXP2F_TABLE_BITS + 11)) 7031914882SAlex Richardson 7131914882SAlex Richardson /* The output of log2 and thus the input of exp2 is either scaled by N 7231914882SAlex Richardson (in case of fast toint intrinsics) or not. The unscaled xd must be 7331914882SAlex Richardson in [-1021,1023], sign_bias sets the sign of the result. */ 7431914882SAlex Richardson static inline float 7531914882SAlex Richardson exp2_inline (double_t xd, uint32_t sign_bias) 7631914882SAlex Richardson { 7731914882SAlex Richardson uint64_t ki, ski, t; 7831914882SAlex Richardson /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ 7931914882SAlex Richardson double_t kd, z, r, r2, y, s; 8031914882SAlex Richardson 8131914882SAlex Richardson #if TOINT_INTRINSICS 8231914882SAlex Richardson # define C __exp2f_data.poly_scaled 8331914882SAlex Richardson /* N*x = k + r with r in [-1/2, 1/2] */ 8431914882SAlex Richardson kd = roundtoint (xd); /* k */ 8531914882SAlex Richardson ki = converttoint (xd); 8631914882SAlex Richardson #else 8731914882SAlex Richardson # define C __exp2f_data.poly 8831914882SAlex Richardson # define SHIFT __exp2f_data.shift_scaled 8931914882SAlex Richardson /* x = k/N + r with r in [-1/(2N), 1/(2N)] */ 9031914882SAlex Richardson kd = eval_as_double (xd + SHIFT); 9131914882SAlex Richardson ki = asuint64 (kd); 9231914882SAlex Richardson kd -= SHIFT; /* k/N */ 9331914882SAlex Richardson #endif 9431914882SAlex Richardson r = xd - kd; 9531914882SAlex Richardson 9631914882SAlex Richardson /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ 9731914882SAlex Richardson t = T[ki % N]; 9831914882SAlex Richardson ski = ki + sign_bias; 9931914882SAlex Richardson t += ski << (52 - EXP2F_TABLE_BITS); 10031914882SAlex Richardson s = asdouble (t); 10131914882SAlex Richardson z = C[0] * r + C[1]; 10231914882SAlex Richardson r2 = r * r; 10331914882SAlex Richardson y = C[2] * r + 1; 10431914882SAlex Richardson y = z * r2 + y; 10531914882SAlex Richardson y = y * s; 10631914882SAlex Richardson return eval_as_float (y); 10731914882SAlex Richardson } 10831914882SAlex Richardson 10931914882SAlex Richardson /* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is 11031914882SAlex Richardson the bit representation of a non-zero finite floating-point value. */ 11131914882SAlex Richardson static inline int 11231914882SAlex Richardson checkint (uint32_t iy) 11331914882SAlex Richardson { 11431914882SAlex Richardson int e = iy >> 23 & 0xff; 11531914882SAlex Richardson if (e < 0x7f) 11631914882SAlex Richardson return 0; 11731914882SAlex Richardson if (e > 0x7f + 23) 11831914882SAlex Richardson return 2; 11931914882SAlex Richardson if (iy & ((1 << (0x7f + 23 - e)) - 1)) 12031914882SAlex Richardson return 0; 12131914882SAlex Richardson if (iy & (1 << (0x7f + 23 - e))) 12231914882SAlex Richardson return 1; 12331914882SAlex Richardson return 2; 12431914882SAlex Richardson } 12531914882SAlex Richardson 12631914882SAlex Richardson static inline int 12731914882SAlex Richardson zeroinfnan (uint32_t ix) 12831914882SAlex Richardson { 12931914882SAlex Richardson return 2 * ix - 1 >= 2u * 0x7f800000 - 1; 13031914882SAlex Richardson } 13131914882SAlex Richardson 13231914882SAlex Richardson float 13331914882SAlex Richardson powf (float x, float y) 13431914882SAlex Richardson { 13531914882SAlex Richardson uint32_t sign_bias = 0; 13631914882SAlex Richardson uint32_t ix, iy; 13731914882SAlex Richardson 13831914882SAlex Richardson ix = asuint (x); 13931914882SAlex Richardson iy = asuint (y); 14031914882SAlex Richardson if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000 || zeroinfnan (iy))) 14131914882SAlex Richardson { 14231914882SAlex Richardson /* Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan). */ 14331914882SAlex Richardson if (unlikely (zeroinfnan (iy))) 14431914882SAlex Richardson { 14531914882SAlex Richardson if (2 * iy == 0) 14631914882SAlex Richardson return issignalingf_inline (x) ? x + y : 1.0f; 14731914882SAlex Richardson if (ix == 0x3f800000) 14831914882SAlex Richardson return issignalingf_inline (y) ? x + y : 1.0f; 14931914882SAlex Richardson if (2 * ix > 2u * 0x7f800000 || 2 * iy > 2u * 0x7f800000) 15031914882SAlex Richardson return x + y; 15131914882SAlex Richardson if (2 * ix == 2 * 0x3f800000) 15231914882SAlex Richardson return 1.0f; 15331914882SAlex Richardson if ((2 * ix < 2 * 0x3f800000) == !(iy & 0x80000000)) 15431914882SAlex Richardson return 0.0f; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ 15531914882SAlex Richardson return y * y; 15631914882SAlex Richardson } 15731914882SAlex Richardson if (unlikely (zeroinfnan (ix))) 15831914882SAlex Richardson { 15931914882SAlex Richardson float_t x2 = x * x; 16031914882SAlex Richardson if (ix & 0x80000000 && checkint (iy) == 1) 16131914882SAlex Richardson { 16231914882SAlex Richardson x2 = -x2; 16331914882SAlex Richardson sign_bias = 1; 16431914882SAlex Richardson } 16531914882SAlex Richardson #if WANT_ERRNO 16631914882SAlex Richardson if (2 * ix == 0 && iy & 0x80000000) 16731914882SAlex Richardson return __math_divzerof (sign_bias); 16831914882SAlex Richardson #endif 16931914882SAlex Richardson /* Without the barrier some versions of clang hoist the 1/x2 and 17031914882SAlex Richardson thus division by zero exception can be signaled spuriously. */ 17131914882SAlex Richardson return iy & 0x80000000 ? opt_barrier_float (1 / x2) : x2; 17231914882SAlex Richardson } 17331914882SAlex Richardson /* x and y are non-zero finite. */ 17431914882SAlex Richardson if (ix & 0x80000000) 17531914882SAlex Richardson { 17631914882SAlex Richardson /* Finite x < 0. */ 17731914882SAlex Richardson int yint = checkint (iy); 17831914882SAlex Richardson if (yint == 0) 17931914882SAlex Richardson return __math_invalidf (x); 18031914882SAlex Richardson if (yint == 1) 18131914882SAlex Richardson sign_bias = SIGN_BIAS; 18231914882SAlex Richardson ix &= 0x7fffffff; 18331914882SAlex Richardson } 18431914882SAlex Richardson if (ix < 0x00800000) 18531914882SAlex Richardson { 18631914882SAlex Richardson /* Normalize subnormal x so exponent becomes negative. */ 18731914882SAlex Richardson ix = asuint (x * 0x1p23f); 18831914882SAlex Richardson ix &= 0x7fffffff; 18931914882SAlex Richardson ix -= 23 << 23; 19031914882SAlex Richardson } 19131914882SAlex Richardson } 19231914882SAlex Richardson double_t logx = log2_inline (ix); 19331914882SAlex Richardson double_t ylogx = y * logx; /* Note: cannot overflow, y is single prec. */ 19431914882SAlex Richardson if (unlikely ((asuint64 (ylogx) >> 47 & 0xffff) 19531914882SAlex Richardson >= asuint64 (126.0 * POWF_SCALE) >> 47)) 19631914882SAlex Richardson { 19731914882SAlex Richardson /* |y*log(x)| >= 126. */ 19831914882SAlex Richardson if (ylogx > 0x1.fffffffd1d571p+6 * POWF_SCALE) 19931914882SAlex Richardson /* |x^y| > 0x1.ffffffp127. */ 20031914882SAlex Richardson return __math_oflowf (sign_bias); 20131914882SAlex Richardson if (WANT_ROUNDING && WANT_ERRNO 20231914882SAlex Richardson && ylogx > 0x1.fffffffa3aae2p+6 * POWF_SCALE) 20331914882SAlex Richardson /* |x^y| > 0x1.fffffep127, check if we round away from 0. */ 20431914882SAlex Richardson if ((!sign_bias 20531914882SAlex Richardson && eval_as_float (1.0f + opt_barrier_float (0x1p-25f)) != 1.0f) 20631914882SAlex Richardson || (sign_bias 20731914882SAlex Richardson && eval_as_float (-1.0f - opt_barrier_float (0x1p-25f)) 20831914882SAlex Richardson != -1.0f)) 20931914882SAlex Richardson return __math_oflowf (sign_bias); 21031914882SAlex Richardson if (ylogx <= -150.0 * POWF_SCALE) 21131914882SAlex Richardson return __math_uflowf (sign_bias); 21231914882SAlex Richardson #if WANT_ERRNO_UFLOW 21331914882SAlex Richardson if (ylogx < -149.0 * POWF_SCALE) 21431914882SAlex Richardson return __math_may_uflowf (sign_bias); 21531914882SAlex Richardson #endif 21631914882SAlex Richardson } 21731914882SAlex Richardson return exp2_inline (ylogx, sign_bias); 21831914882SAlex Richardson } 21931914882SAlex Richardson #if USE_GLIBC_ABI 22031914882SAlex Richardson strong_alias (powf, __powf_finite) 22131914882SAlex Richardson hidden_alias (powf, __ieee754_powf) 22231914882SAlex Richardson #endif 223*f3087befSAndrew Turner 224*f3087befSAndrew Turner TEST_ULP (powf, 0.4) 225*f3087befSAndrew Turner TEST_ULP_NONNEAREST (powf, 0.5) 226*f3087befSAndrew Turner TEST_INTERVAL2 (powf, 0x1p-1, 0x1p1, 0x1p-7, 0x1p7, 50000) 227*f3087befSAndrew Turner TEST_INTERVAL2 (powf, 0x1p-1, 0x1p1, -0x1p-7, -0x1p7, 50000) 228*f3087befSAndrew Turner TEST_INTERVAL2 (powf, 0x1p-70, 0x1p70, 0x1p-1, 0x1p1, 50000) 229*f3087befSAndrew Turner TEST_INTERVAL2 (powf, 0x1p-70, 0x1p70, -0x1p-1, -0x1p1, 50000) 230*f3087befSAndrew Turner TEST_INTERVAL2 (powf, 0x1.ep-1, 0x1.1p0, 0x1p8, 0x1p14, 50000) 231*f3087befSAndrew Turner TEST_INTERVAL2 (powf, 0x1.ep-1, 0x1.1p0, -0x1p8, -0x1p14, 50000) 232