2 * Copyright (c) 2017-2018, Arm Limited.
3 * SPDX-License-Identifier: MIT
9 #include "exp2f_data.h"
10 #include "powf_data.h"
13 POWF_LOG2_POLY_ORDER = 5
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).)
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
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
)
31 double_t z
, r
, r2
, r4
, p
, q
, y
, y0
, invc
, logc
;
32 uint32_t iz
, top
, tmp
;
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. */
39 i
= (tmp
>> (23 - POWF_LOG2_TABLE_BITS
)) % N
;
40 top
= tmp
& 0xff800000;
42 k
= (int32_t)top
>> (23 - POWF_SCALE_BITS
); /* arithmetic shift */
45 z
= (double_t
)asfloat(iz
);
47 /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
49 y0
= logc
+ (double_t
)k
;
51 /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */
64 #define N (1 << EXP2F_TABLE_BITS)
65 #define T __exp2f_data.tab
66 #define SIGN_BIAS (1 << (EXP2F_TABLE_BITS + 11))
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
)
74 double_t kd
, z
, r
, r2
, y
, s
;
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
);
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
);
87 kd
-= SHIFT
; /* k/N */
91 /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
94 t
+= ski
<< (52 - EXP2F_TABLE_BITS
);
101 return eval_as_float(y
);
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
)
108 int e
= iy
>> 23 & 0xff;
113 if (iy
& ((1 << (0x7f + 23 - e
)) - 1))
115 if (iy
& (1 << (0x7f + 23 - e
)))
120 static inline int zeroinfnan(uint32_t ix
)
122 return 2 * ix
- 1 >= 2u * 0x7f800000 - 1;
125 float powf(float x
, float y
)
127 uint32_t sign_bias
= 0;
132 if (predict_false(ix
- 0x00800000 >= 0x7f800000 - 0x00800000 ||
134 /* Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan). */
135 if (predict_false(zeroinfnan(iy
))) {
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)
143 if (2 * ix
== 2 * 0x3f800000)
145 if ((2 * ix
< 2 * 0x3f800000) == !(iy
& 0x80000000))
146 return 0.0f
; /* |x|<1 && y==inf or |x|>1 && y==-inf. */
149 if (predict_false(zeroinfnan(ix
))) {
151 if (ix
& 0x80000000 && checkint(iy
) == 1)
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
;
157 /* x and y are non-zero finite. */
158 if (ix
& 0x80000000) {
160 int yint
= checkint(iy
);
162 return __math_invalidf(x
);
164 sign_bias
= SIGN_BIAS
;
167 if (ix
< 0x00800000) {
168 /* Normalize subnormal x so exponent becomes negative. */
169 ix
= asuint(x
* 0x1p
23f
);
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
);
184 return exp2_inline(ylogx
, sign_bias
);