George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 1 | /* |
Szabolcs Nagy | 1b94597 | 2018-05-14 14:46:40 +0100 | [diff] [blame] | 2 | * Single-precision pow function. |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 3 | * |
Szabolcs Nagy | 11253b0 | 2018-11-12 11:10:57 +0000 | [diff] [blame] | 4 | * Copyright (c) 2017-2018, Arm Limited. |
| 5 | * SPDX-License-Identifier: MIT |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 8 | #include <math.h> |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include "math_config.h" |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 11 | |
| 12 | /* |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 13 | POWF_LOG2_POLY_ORDER = 5 |
| 14 | EXP2F_TABLE_BITS = 5 |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 15 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 16 | ULP error: 0.82 (~ 0.5 + relerr*2^24) |
| 17 | relerr: 1.27 * 2^-26 (Relative error ~= 128*Ln2*relerr_log2 + relerr_exp2) |
| 18 | relerr_log2: 1.83 * 2^-33 (Relative error of logx.) |
| 19 | relerr_exp2: 1.69 * 2^-34 (Relative error of exp2(ylogx).) |
| 20 | */ |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 21 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 22 | #define N (1 << POWF_LOG2_TABLE_BITS) |
| 23 | #define T __powf_log2_data.tab |
| 24 | #define A __powf_log2_data.poly |
| 25 | #define OFF 0x3f330000 |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 26 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 27 | /* Subnormal input is normalized so ix has negative biased exponent. |
| 28 | Output is multiplied by N (POWF_SCALE) if TOINT_INTRINICS is set. */ |
| 29 | static inline double_t |
| 30 | log2_inline (uint32_t ix) |
| 31 | { |
| 32 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
| 33 | double_t z, r, r2, r4, p, q, y, y0, invc, logc; |
| 34 | uint32_t iz, top, tmp; |
| 35 | int k, i; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 36 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 37 | /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. |
| 38 | The range is split into N subintervals. |
| 39 | The ith subinterval contains z and c is near its center. */ |
| 40 | tmp = ix - OFF; |
| 41 | i = (tmp >> (23 - POWF_LOG2_TABLE_BITS)) % N; |
| 42 | top = tmp & 0xff800000; |
| 43 | iz = ix - top; |
| 44 | k = (int32_t) top >> (23 - POWF_SCALE_BITS); /* arithmetic shift */ |
| 45 | invc = T[i].invc; |
| 46 | logc = T[i].logc; |
| 47 | z = (double_t) asfloat (iz); |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 48 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 49 | /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */ |
| 50 | r = z * invc - 1; |
| 51 | y0 = logc + (double_t) k; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 52 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 53 | /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */ |
| 54 | r2 = r * r; |
| 55 | y = A[0] * r + A[1]; |
| 56 | p = A[2] * r + A[3]; |
| 57 | r4 = r2 * r2; |
| 58 | q = A[4] * r + y0; |
| 59 | q = p * r2 + q; |
| 60 | y = y * r4 + q; |
| 61 | return y; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 62 | } |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 63 | |
| 64 | #undef N |
| 65 | #undef T |
| 66 | #define N (1 << EXP2F_TABLE_BITS) |
| 67 | #define T __exp2f_data.tab |
| 68 | #define SIGN_BIAS (1 << (EXP2F_TABLE_BITS + 11)) |
| 69 | |
| 70 | /* The output of log2 and thus the input of exp2 is either scaled by N |
| 71 | (in case of fast toint intrinsics) or not. The unscaled xd must be |
| 72 | in [-1021,1023], sign_bias sets the sign of the result. */ |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 73 | static inline float |
Szabolcs Nagy | c65db17 | 2018-05-10 17:53:31 +0100 | [diff] [blame] | 74 | exp2_inline (double_t xd, uint32_t sign_bias) |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 75 | { |
| 76 | uint64_t ki, ski, t; |
| 77 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
| 78 | double_t kd, z, r, r2, y, s; |
| 79 | |
| 80 | #if TOINT_INTRINSICS |
| 81 | # define C __exp2f_data.poly_scaled |
| 82 | /* N*x = k + r with r in [-1/2, 1/2] */ |
| 83 | kd = roundtoint (xd); /* k */ |
| 84 | ki = converttoint (xd); |
| 85 | #else |
| 86 | # define C __exp2f_data.poly |
| 87 | # define SHIFT __exp2f_data.shift_scaled |
| 88 | /* x = k/N + r with r in [-1/(2N), 1/(2N)] */ |
Szabolcs Nagy | 5eb9d19 | 2018-05-11 13:20:04 +0100 | [diff] [blame] | 89 | kd = eval_as_double (xd + SHIFT); |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 90 | ki = asuint64 (kd); |
| 91 | kd -= SHIFT; /* k/N */ |
| 92 | #endif |
| 93 | r = xd - kd; |
| 94 | |
| 95 | /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ |
| 96 | t = T[ki % N]; |
| 97 | ski = ki + sign_bias; |
| 98 | t += ski << (52 - EXP2F_TABLE_BITS); |
| 99 | s = asdouble (t); |
| 100 | z = C[0] * r + C[1]; |
| 101 | r2 = r * r; |
| 102 | y = C[2] * r + 1; |
| 103 | y = z * r2 + y; |
| 104 | y = y * s; |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 105 | return eval_as_float (y); |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Szabolcs Nagy | e875f40 | 2018-09-18 10:36:04 +0100 | [diff] [blame] | 108 | /* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is |
| 109 | the bit representation of a non-zero finite floating-point value. */ |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 110 | static inline int |
| 111 | checkint (uint32_t iy) |
| 112 | { |
| 113 | int e = iy >> 23 & 0xff; |
| 114 | if (e < 0x7f) |
| 115 | return 0; |
| 116 | if (e > 0x7f + 23) |
| 117 | return 2; |
| 118 | if (iy & ((1 << (0x7f + 23 - e)) - 1)) |
| 119 | return 0; |
| 120 | if (iy & (1 << (0x7f + 23 - e))) |
| 121 | return 1; |
| 122 | return 2; |
| 123 | } |
| 124 | |
| 125 | static inline int |
| 126 | zeroinfnan (uint32_t ix) |
| 127 | { |
| 128 | return 2 * ix - 1 >= 2u * 0x7f800000 - 1; |
| 129 | } |
| 130 | |
| 131 | float |
Szabolcs Nagy | 0d51c04 | 2018-04-25 13:26:22 +0100 | [diff] [blame] | 132 | powf (float x, float y) |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 133 | { |
Szabolcs Nagy | c65db17 | 2018-05-10 17:53:31 +0100 | [diff] [blame] | 134 | uint32_t sign_bias = 0; |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 135 | uint32_t ix, iy; |
| 136 | |
| 137 | ix = asuint (x); |
| 138 | iy = asuint (y); |
Szabolcs Nagy | b21378a | 2018-05-11 13:29:44 +0100 | [diff] [blame] | 139 | if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000 || zeroinfnan (iy))) |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 140 | { |
| 141 | /* Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan). */ |
Szabolcs Nagy | b21378a | 2018-05-11 13:29:44 +0100 | [diff] [blame] | 142 | if (unlikely (zeroinfnan (iy))) |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 143 | { |
| 144 | if (2 * iy == 0) |
Szabolcs Nagy | 6f846e1 | 2017-09-28 15:45:41 +0100 | [diff] [blame] | 145 | return issignalingf_inline (x) ? x + y : 1.0f; |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 146 | if (ix == 0x3f800000) |
Szabolcs Nagy | 6f846e1 | 2017-09-28 15:45:41 +0100 | [diff] [blame] | 147 | return issignalingf_inline (y) ? x + y : 1.0f; |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 148 | if (2 * ix > 2u * 0x7f800000 || 2 * iy > 2u * 0x7f800000) |
| 149 | return x + y; |
| 150 | if (2 * ix == 2 * 0x3f800000) |
| 151 | return 1.0f; |
| 152 | if ((2 * ix < 2 * 0x3f800000) == !(iy & 0x80000000)) |
| 153 | return 0.0f; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ |
| 154 | return y * y; |
| 155 | } |
Szabolcs Nagy | b21378a | 2018-05-11 13:29:44 +0100 | [diff] [blame] | 156 | if (unlikely (zeroinfnan (ix))) |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 157 | { |
| 158 | float_t x2 = x * x; |
| 159 | if (ix & 0x80000000 && checkint (iy) == 1) |
| 160 | { |
| 161 | x2 = -x2; |
| 162 | sign_bias = 1; |
| 163 | } |
| 164 | #if WANT_ERRNO |
| 165 | if (2 * ix == 0 && iy & 0x80000000) |
| 166 | return __math_divzerof (sign_bias); |
| 167 | #endif |
Szabolcs Nagy | 5fa69e1 | 2018-06-12 17:18:24 +0100 | [diff] [blame] | 168 | /* Without the barrier some versions of clang hoist the 1/x2 and |
| 169 | thus division by zero exception can be signaled spuriously. */ |
| 170 | return iy & 0x80000000 ? opt_barrier_float (1 / x2) : x2; |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 171 | } |
| 172 | /* x and y are non-zero finite. */ |
| 173 | if (ix & 0x80000000) |
| 174 | { |
| 175 | /* Finite x < 0. */ |
| 176 | int yint = checkint (iy); |
| 177 | if (yint == 0) |
| 178 | return __math_invalidf (x); |
| 179 | if (yint == 1) |
| 180 | sign_bias = SIGN_BIAS; |
| 181 | ix &= 0x7fffffff; |
| 182 | } |
| 183 | if (ix < 0x00800000) |
| 184 | { |
| 185 | /* Normalize subnormal x so exponent becomes negative. */ |
| 186 | ix = asuint (x * 0x1p23f); |
| 187 | ix &= 0x7fffffff; |
| 188 | ix -= 23 << 23; |
| 189 | } |
| 190 | } |
| 191 | double_t logx = log2_inline (ix); |
| 192 | double_t ylogx = y * logx; /* Note: cannot overflow, y is single prec. */ |
Szabolcs Nagy | b21378a | 2018-05-11 13:29:44 +0100 | [diff] [blame] | 193 | if (unlikely ((asuint64 (ylogx) >> 47 & 0xffff) |
| 194 | >= asuint64 (126.0 * POWF_SCALE) >> 47)) |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 195 | { |
| 196 | /* |y*log(x)| >= 126. */ |
| 197 | if (ylogx > 0x1.fffffffd1d571p+6 * POWF_SCALE) |
Szabolcs Nagy | 75b8d8c | 2018-12-07 13:47:14 +0000 | [diff] [blame] | 198 | /* |x^y| > 0x1.ffffffp127. */ |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 199 | return __math_oflowf (sign_bias); |
Szabolcs Nagy | 75b8d8c | 2018-12-07 13:47:14 +0000 | [diff] [blame] | 200 | if (WANT_ROUNDING && WANT_ERRNO |
| 201 | && ylogx > 0x1.fffffffa3aae2p+6 * POWF_SCALE) |
| 202 | /* |x^y| > 0x1.fffffep127, check if we round away from 0. */ |
Szabolcs Nagy | 6e60567 | 2018-12-10 12:53:01 +0000 | [diff] [blame] | 203 | if ((!sign_bias |
| 204 | && eval_as_float (1.0f + opt_barrier_float (0x1p-25f)) != 1.0f) |
| 205 | || (sign_bias |
| 206 | && eval_as_float (-1.0f - opt_barrier_float (0x1p-25f)) |
| 207 | != -1.0f)) |
Szabolcs Nagy | 75b8d8c | 2018-12-07 13:47:14 +0000 | [diff] [blame] | 208 | return __math_oflowf (sign_bias); |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 209 | if (ylogx <= -150.0 * POWF_SCALE) |
| 210 | return __math_uflowf (sign_bias); |
| 211 | #if WANT_ERRNO_UFLOW |
| 212 | if (ylogx < -149.0 * POWF_SCALE) |
| 213 | return __math_may_uflowf (sign_bias); |
| 214 | #endif |
| 215 | } |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 216 | return exp2_inline (ylogx, sign_bias); |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 217 | } |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 218 | #if USE_GLIBC_ABI |
| 219 | strong_alias (powf, __powf_finite) |
| 220 | hidden_alias (powf, __ieee754_powf) |
| 221 | #endif |