2 * Double-precision x^y function.
4 * Copyright (c) 2018, Arm Limited.
5 * SPDX-License-Identifier: MIT
15 Worst-case error: 0.54 ULP (~= ulperr_exp + 1024*Ln2*relerr_log*2^53)
16 relerr_log: 1.3 * 2^-68 (Relative error of log, 1.5 * 2^-68 without fma)
17 ulperr_exp: 0.509 ULP (ULP error of exp, 0.511 ULP without fma)
20 #define T __pow_log_data.tab
21 #define A __pow_log_data.poly
22 #define Ln2hi __pow_log_data.ln2hi
23 #define Ln2lo __pow_log_data.ln2lo
24 #define N (1 << POW_LOG_TABLE_BITS)
25 #define OFF 0x3fe6955500000000
27 /* Top 12 bits of a double (sign and exponent bits). */
28 static inline uint32_t top12(double x
)
30 return asuint64(x
) >> 52;
33 /* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about
34 additional 15 bits precision. IX is the bit representation of x, but
35 normalized in the subnormal range using the sign bit for the exponent. */
36 static inline double_t
log_inline(uint64_t ix
, double_t
*tail
)
38 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
39 double_t z
, r
, y
, invc
, logc
, logctail
, kd
, hi
, t1
, t2
, lo
, lo1
, lo2
, p
;
43 /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
44 The range is split into N subintervals.
45 The ith subinterval contains z and c is near its center. */
47 i
= (tmp
>> (52 - POW_LOG_TABLE_BITS
)) % N
;
48 k
= (int64_t)tmp
>> 52; /* arithmetic shift */
49 iz
= ix
- (tmp
& 0xfffULL
<< 52);
53 /* log(x) = k*Ln2 + log(c) + log1p(z/c-1). */
56 logctail
= T
[i
].logctail
;
58 /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and
59 |z/c - 1| < 1/N, so r = z/c - 1 is exactly representible. */
61 r
= __builtin_fma(z
, invc
, -1.0);
63 /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|. */
64 double_t zhi
= asdouble((iz
+ (1ULL << 31)) & (-1ULL << 32));
65 double_t zlo
= z
- zhi
;
66 double_t rhi
= zhi
* invc
- 1.0;
67 double_t rlo
= zlo
* invc
;
71 /* k*Ln2 + log(c) + r. */
72 t1
= kd
* Ln2hi
+ logc
;
74 lo1
= kd
* Ln2lo
+ logctail
;
77 /* Evaluation is optimized assuming superscalar pipelined execution. */
78 double_t ar
, ar2
, ar3
, lo3
, lo4
;
79 ar
= A
[0] * r
; /* A[0] = -0.5. */
82 /* k*Ln2 + log(c) + r + A[0]*r*r. */
85 lo3
= __builtin_fma(ar
, r
, -ar2
);
88 double_t arhi
= A
[0] * rhi
;
89 double_t arhi2
= rhi
* arhi
;
91 lo3
= rlo
* (ar
+ arhi
);
92 lo4
= t2
- hi
+ arhi2
;
94 /* p = log1p(r) - r - A[0]*r*r. */
95 p
= (ar3
* (A
[1] + r
* A
[2] +
96 ar2
* (A
[3] + r
* A
[4] + ar2
* (A
[5] + r
* A
[6]))));
97 lo
= lo1
+ lo2
+ lo3
+ lo4
+ p
;
105 #define N (1 << EXP_TABLE_BITS)
106 #define InvLn2N __exp_data.invln2N
107 #define NegLn2hiN __exp_data.negln2hiN
108 #define NegLn2loN __exp_data.negln2loN
109 #define Shift __exp_data.shift
110 #define T __exp_data.tab
111 #define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
112 #define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
113 #define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
114 #define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
115 #define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
117 /* Handle cases that may overflow or underflow when computing the result that
118 is scale*(1+TMP) without intermediate rounding. The bit representation of
119 scale is in SBITS, however it has a computed exponent that may have
120 overflown into the sign bit so that needs to be adjusted before using it as
121 a double. (int32_t)KI is the k used in the argument reduction and exponent
122 adjustment of scale, positive k here means the result may overflow and
123 negative k means the result may underflow. */
124 static inline double specialcase(double_t tmp
, uint64_t sbits
, uint64_t ki
)
128 if ((ki
& 0x80000000) == 0) {
129 /* k > 0, the exponent of scale might have overflowed by <= 460. */
130 sbits
-= 1009ull << 52;
131 scale
= asdouble(sbits
);
132 y
= 0x1p
1009 * (scale
+ scale
* tmp
);
133 return eval_as_double(y
);
135 /* k < 0, need special care in the subnormal range. */
136 sbits
+= 1022ull << 52;
137 /* Note: sbits is signed scale. */
138 scale
= asdouble(sbits
);
139 y
= scale
+ scale
* tmp
;
141 /* Round y to the right precision before scaling it into the subnormal
142 range to avoid double rounding that can cause 0.5+E/2 ulp error where
143 E is the worst-case ulp error outside the subnormal range. So this
144 is only useful if the goal is better than 1 ulp worst-case error. */
145 double_t hi
, lo
, one
= 1.0;
148 lo
= scale
- y
+ scale
* tmp
;
150 lo
= one
- hi
+ y
+ lo
;
151 y
= eval_as_double(hi
+ lo
) - one
;
152 /* Fix the sign of 0. */
154 y
= asdouble(sbits
& 0x8000000000000000);
155 /* The underflow exception needs to be signaled explicitly. */
156 fp_force_eval(fp_barrier(0x1p
-1022) * 0x1p
-1022);
159 return eval_as_double(y
);
162 #define SIGN_BIAS (0x800 << EXP_TABLE_BITS)
164 /* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
165 The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */
166 static inline double exp_inline(double_t x
, double_t xtail
, uint32_t sign_bias
)
169 uint64_t ki
, idx
, top
, sbits
;
170 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
171 double_t kd
, z
, r
, r2
, scale
, tail
, tmp
;
173 abstop
= top12(x
) & 0x7ff;
174 if (predict_false(abstop
- top12(0x1p
-54) >=
175 top12(512.0) - top12(0x1p
-54))) {
176 if (abstop
- top12(0x1p
-54) >= 0x80000000) {
177 /* Avoid spurious underflow for tiny x. */
178 /* Note: 0 is common input. */
179 double_t one
= WANT_ROUNDING
? 1.0 + x
: 1.0;
180 return sign_bias
? -one
: one
;
182 if (abstop
>= top12(1024.0)) {
183 /* Note: inf and nan are already handled. */
184 if (asuint64(x
) >> 63)
185 return __math_uflow(sign_bias
);
187 return __math_oflow(sign_bias
);
189 /* Large x is special cased below. */
193 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
194 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
198 ki
= converttoint(z
);
199 #elif EXP_USE_TOINT_NARROW
200 /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */
201 kd
= eval_as_double(z
+ Shift
);
202 ki
= asuint64(kd
) >> 16;
203 kd
= (double_t
)(int32_t)ki
;
205 /* z - kd is in [-1, 1] in non-nearest rounding modes. */
206 kd
= eval_as_double(z
+ Shift
);
210 r
= x
+ kd
* NegLn2hiN
+ kd
* NegLn2loN
;
211 /* The code assumes 2^-200 < |xtail| < 2^-8/N. */
213 /* 2^(k/N) ~= scale * (1 + tail). */
215 top
= (ki
+ sign_bias
) << (52 - EXP_TABLE_BITS
);
216 tail
= asdouble(T
[idx
]);
217 /* This is only a valid scale when -1023*N < k < 1024*N. */
218 sbits
= T
[idx
+ 1] + top
;
219 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
220 /* Evaluation is optimized assuming superscalar pipelined execution. */
222 /* Without fma the worst case error is 0.25/N ulp larger. */
223 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
224 tmp
= tail
+ r
+ r2
* (C2
+ r
* C3
) + r2
* r2
* (C4
+ r
* C5
);
225 if (predict_false(abstop
== 0))
226 return specialcase(tmp
, sbits
, ki
);
227 scale
= asdouble(sbits
);
228 /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
229 is no spurious underflow here even without fma. */
230 return eval_as_double(scale
+ scale
* tmp
);
233 /* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is
234 the bit representation of a non-zero finite floating-point value. */
235 static inline int checkint(uint64_t iy
)
237 int e
= iy
>> 52 & 0x7ff;
242 if (iy
& ((1ULL << (0x3ff + 52 - e
)) - 1))
244 if (iy
& (1ULL << (0x3ff + 52 - e
)))
249 /* Returns 1 if input is the bit representation of 0, infinity or nan. */
250 static inline int zeroinfnan(uint64_t i
)
252 return 2 * i
- 1 >= 2 * asuint64(INFINITY
) - 1;
255 double pow(double x
, double y
)
257 uint32_t sign_bias
= 0;
265 if (predict_false(topx
- 0x001 >= 0x7ff - 0x001 ||
266 (topy
& 0x7ff) - 0x3be >= 0x43e - 0x3be)) {
267 /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0
268 and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */
269 /* Special cases: (x < 0x1p-126 or inf or nan) or
270 (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */
271 if (predict_false(zeroinfnan(iy
))) {
273 return issignaling_inline(x
) ? x
+ y
: 1.0;
274 if (ix
== asuint64(1.0))
275 return issignaling_inline(y
) ? x
+ y
: 1.0;
276 if (2 * ix
> 2 * asuint64(INFINITY
) ||
277 2 * iy
> 2 * asuint64(INFINITY
))
279 if (2 * ix
== 2 * asuint64(1.0))
281 if ((2 * ix
< 2 * asuint64(1.0)) == !(iy
>> 63))
282 return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */
285 if (predict_false(zeroinfnan(ix
))) {
287 if (ix
>> 63 && checkint(iy
) == 1)
289 /* Without the barrier some versions of clang hoist the 1/x2 and
290 thus division by zero exception can be signaled spuriously. */
291 return iy
>> 63 ? fp_barrier(1 / x2
) : x2
;
293 /* Here x and y are non-zero finite. */
296 int yint
= checkint(iy
);
298 return __math_invalid(x
);
300 sign_bias
= SIGN_BIAS
;
301 ix
&= 0x7fffffffffffffff;
304 if ((topy
& 0x7ff) - 0x3be >= 0x43e - 0x3be) {
305 /* Note: sign_bias == 0 here because y is not odd. */
306 if (ix
== asuint64(1.0))
308 if ((topy
& 0x7ff) < 0x3be) {
309 /* |y| < 2^-65, x^y ~= 1 + y*log(x). */
311 return ix
> asuint64(1.0) ? 1.0 + y
:
316 return (ix
> asuint64(1.0)) == (topy
< 0x800) ?
321 /* Normalize subnormal x so exponent becomes negative. */
322 ix
= asuint64(x
* 0x1p
52);
323 ix
&= 0x7fffffffffffffff;
329 double_t hi
= log_inline(ix
, &lo
);
333 elo
= y
* lo
+ __builtin_fma(y
, hi
, -ehi
);
335 double_t yhi
= asdouble(iy
& -1ULL << 27);
336 double_t ylo
= y
- yhi
;
337 double_t lhi
= asdouble(asuint64(hi
) & -1ULL << 27);
338 double_t llo
= hi
- lhi
+ lo
;
340 elo
= ylo
* lhi
+ y
* llo
; /* |elo| < |ehi| * 2^-25. */
342 return exp_inline(ehi
, elo
, sign_bias
);