| 1 |
/* |
| 2 |
* Copyright (c) 2017-2018, Arm Limited. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <math.h> |
| 7 |
#include <stdint.h> |
| 8 |
#include "libm.h" |
| 9 |
#include "exp2f_data.h" |
| 10 |
#include "powf_data.h" |
| 11 |
|
| 12 |
/* |
| 13 |
POWF_LOG2_POLY_ORDER = 5 |
| 14 |
EXP2F_TABLE_BITS = 5 |
| 15 |
|
| 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 |
*/ |
| 21 |
|
| 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 |
| 26 |
|
| 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 log2_inline(uint32_t ix) |
| 30 |
{ |
| 31 |
double_t z, r, r2, r4, p, q, y, y0, invc, logc; |
| 32 |
uint32_t iz, top, tmp; |
| 33 |
int k, i; |
| 34 |
|
| 35 |
/* x = 2^k z; where z is in range [OFF,2*OFF] and exact. |
| 36 |
The range is split into N subintervals. |
| 37 |
The ith subinterval contains z and c is near its center. */ |
| 38 |
tmp = ix - OFF; |
| 39 |
i = (tmp >> (23 - POWF_LOG2_TABLE_BITS)) % N; |
| 40 |
top = tmp & 0xff800000; |
| 41 |
iz = ix - top; |
| 42 |
k = (int32_t)top >> (23 - POWF_SCALE_BITS); /* arithmetic shift */ |
| 43 |
invc = T[i].invc; |
| 44 |
logc = T[i].logc; |
| 45 |
z = (double_t)asfloat(iz); |
| 46 |
|
| 47 |
/* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */ |
| 48 |
r = z * invc - 1; |
| 49 |
y0 = logc + (double_t)k; |
| 50 |
|
| 51 |
/* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */ |
| 52 |
r2 = r * r; |
| 53 |
y = A[0] * r + A[1]; |
| 54 |
p = A[2] * r + A[3]; |
| 55 |
r4 = r2 * r2; |
| 56 |
q = A[4] * r + y0; |
| 57 |
q = p * r2 + q; |
| 58 |
y = y * r4 + q; |
| 59 |
return y; |
| 60 |
} |
| 61 |
|
| 62 |
#undef N |
| 63 |
#undef T |
| 64 |
#define N (1 << EXP2F_TABLE_BITS) |
| 65 |
#define T __exp2f_data.tab |
| 66 |
#define SIGN_BIAS (1 << (EXP2F_TABLE_BITS + 11)) |
| 67 |
|
| 68 |
/* The output of log2 and thus the input of exp2 is either scaled by N |
| 69 |
(in case of fast toint intrinsics) or not. The unscaled xd must be |
| 70 |
in [-1021,1023], sign_bias sets the sign of the result. */ |
| 71 |
static inline float exp2_inline(double_t xd, uint32_t sign_bias) |
| 72 |
{ |
| 73 |
uint64_t ki, ski, t; |
| 74 |
double_t kd, z, r, r2, y, s; |
| 75 |
|
| 76 |
#if TOINT_INTRINSICS |
| 77 |
#define C __exp2f_data.poly_scaled |
| 78 |
/* N*x = k + r with r in [-1/2, 1/2] */ |
| 79 |
kd = roundtoint(xd); /* k */ |
| 80 |
ki = converttoint(xd); |
| 81 |
#else |
| 82 |
#define C __exp2f_data.poly |
| 83 |
#define SHIFT __exp2f_data.shift_scaled |
| 84 |
/* x = k/N + r with r in [-1/(2N), 1/(2N)] */ |
| 85 |
kd = eval_as_double(xd + SHIFT); |
| 86 |
ki = asuint64(kd); |
| 87 |
kd -= SHIFT; /* k/N */ |
| 88 |
#endif |
| 89 |
r = xd - kd; |
| 90 |
|
| 91 |
/* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ |
| 92 |
t = T[ki % N]; |
| 93 |
ski = ki + sign_bias; |
| 94 |
t += ski << (52 - EXP2F_TABLE_BITS); |
| 95 |
s = asdouble(t); |
| 96 |
z = C[0] * r + C[1]; |
| 97 |
r2 = r * r; |
| 98 |
y = C[2] * r + 1; |
| 99 |
y = z * r2 + y; |
| 100 |
y = y * s; |
| 101 |
return eval_as_float(y); |
| 102 |
} |
| 103 |
|
| 104 |
/* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is |
| 105 |
the bit representation of a non-zero finite floating-point value. */ |
| 106 |
static inline int checkint(uint32_t iy) |
| 107 |
{ |
| 108 |
int e = iy >> 23 & 0xff; |
| 109 |
if (e < 0x7f) |
| 110 |
return 0; |
| 111 |
if (e > 0x7f + 23) |
| 112 |
return 2; |
| 113 |
if (iy & ((1 << (0x7f + 23 - e)) - 1)) |
| 114 |
return 0; |
| 115 |
if (iy & (1 << (0x7f + 23 - e))) |
| 116 |
return 1; |
| 117 |
return 2; |
| 118 |
} |
| 119 |
|
| 120 |
static inline int zeroinfnan(uint32_t ix) |
| 121 |
{ |
| 122 |
return 2 * ix - 1 >= 2u * 0x7f800000 - 1; |
| 123 |
} |
| 124 |
|
| 125 |
float powf(float x, float y) |
| 126 |
{ |
| 127 |
uint32_t sign_bias = 0; |
| 128 |
uint32_t ix, iy; |
| 129 |
|
| 130 |
ix = asuint(x); |
| 131 |
iy = asuint(y); |
| 132 |
if (predict_false(ix - 0x00800000 >= 0x7f800000 - 0x00800000 || |
| 133 |
zeroinfnan(iy))) { |
| 134 |
/* Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan). */ |
| 135 |
if (predict_false(zeroinfnan(iy))) { |
| 136 |
if (2 * iy == 0) |
| 137 |
return issignalingf_inline(x) ? x + y : 1.0f; |
| 138 |
if (ix == 0x3f800000) |
| 139 |
return issignalingf_inline(y) ? x + y : 1.0f; |
| 140 |
if (2 * ix > 2u * 0x7f800000 || |
| 141 |
2 * iy > 2u * 0x7f800000) |
| 142 |
return x + y; |
| 143 |
if (2 * ix == 2 * 0x3f800000) |
| 144 |
return 1.0f; |
| 145 |
if ((2 * ix < 2 * 0x3f800000) == !(iy & 0x80000000)) |
| 146 |
return 0.0f; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ |
| 147 |
return y * y; |
| 148 |
} |
| 149 |
if (predict_false(zeroinfnan(ix))) { |
| 150 |
float_t x2 = x * x; |
| 151 |
if (ix & 0x80000000 && checkint(iy) == 1) |
| 152 |
x2 = -x2; |
| 153 |
/* Without the barrier some versions of clang hoist the 1/x2 and |
| 154 |
thus division by zero exception can be signaled spuriously. */ |
| 155 |
return iy & 0x80000000 ? fp_barrierf(1 / x2) : x2; |
| 156 |
} |
| 157 |
/* x and y are non-zero finite. */ |
| 158 |
if (ix & 0x80000000) { |
| 159 |
/* Finite x < 0. */ |
| 160 |
int yint = checkint(iy); |
| 161 |
if (yint == 0) |
| 162 |
return __math_invalidf(x); |
| 163 |
if (yint == 1) |
| 164 |
sign_bias = SIGN_BIAS; |
| 165 |
ix &= 0x7fffffff; |
| 166 |
} |
| 167 |
if (ix < 0x00800000) { |
| 168 |
/* Normalize subnormal x so exponent becomes negative. */ |
| 169 |
ix = asuint(x * 0x1p23f); |
| 170 |
ix &= 0x7fffffff; |
| 171 |
ix -= 23 << 23; |
| 172 |
} |
| 173 |
} |
| 174 |
double_t logx = log2_inline(ix); |
| 175 |
double_t ylogx = y * logx; /* cannot overflow, y is single prec. */ |
| 176 |
if (predict_false((asuint64(ylogx) >> 47 & 0xffff) >= |
| 177 |
asuint64(126.0 * POWF_SCALE) >> 47)) { |
| 178 |
/* |y*log(x)| >= 126. */ |
| 179 |
if (ylogx > 0x1.fffffffd1d571p+6 * POWF_SCALE) |
| 180 |
return __math_oflowf(sign_bias); |
| 181 |
if (ylogx <= -150.0 * POWF_SCALE) |
| 182 |
return __math_uflowf(sign_bias); |
| 183 |
} |
| 184 |
return exp2_inline(ylogx, sign_bias); |
| 185 |
} |