1*0928368fSKristof Beyls /*
2*0928368fSKristof Beyls * Double-precision 2^x function.
3*0928368fSKristof Beyls *
4*0928368fSKristof Beyls * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*0928368fSKristof Beyls * See https://llvm.org/LICENSE.txt for license information.
6*0928368fSKristof Beyls * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*0928368fSKristof Beyls */
8*0928368fSKristof Beyls
9*0928368fSKristof Beyls #include <float.h>
10*0928368fSKristof Beyls #include <math.h>
11*0928368fSKristof Beyls #include <stdint.h>
12*0928368fSKristof Beyls #include "math_config.h"
13*0928368fSKristof Beyls
14*0928368fSKristof Beyls #define N (1 << EXP_TABLE_BITS)
15*0928368fSKristof Beyls #define Shift __exp_data.exp2_shift
16*0928368fSKristof Beyls #define T __exp_data.tab
17*0928368fSKristof Beyls #define C1 __exp_data.exp2_poly[0]
18*0928368fSKristof Beyls #define C2 __exp_data.exp2_poly[1]
19*0928368fSKristof Beyls #define C3 __exp_data.exp2_poly[2]
20*0928368fSKristof Beyls #define C4 __exp_data.exp2_poly[3]
21*0928368fSKristof Beyls #define C5 __exp_data.exp2_poly[4]
22*0928368fSKristof Beyls #define C6 __exp_data.exp2_poly[5]
23*0928368fSKristof Beyls
24*0928368fSKristof Beyls /* Handle cases that may overflow or underflow when computing the result that
25*0928368fSKristof Beyls is scale*(1+TMP) without intermediate rounding. The bit representation of
26*0928368fSKristof Beyls scale is in SBITS, however it has a computed exponent that may have
27*0928368fSKristof Beyls overflown into the sign bit so that needs to be adjusted before using it as
28*0928368fSKristof Beyls a double. (int32_t)KI is the k used in the argument reduction and exponent
29*0928368fSKristof Beyls adjustment of scale, positive k here means the result may overflow and
30*0928368fSKristof Beyls negative k means the result may underflow. */
31*0928368fSKristof Beyls static inline double
specialcase(double_t tmp,uint64_t sbits,uint64_t ki)32*0928368fSKristof Beyls specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
33*0928368fSKristof Beyls {
34*0928368fSKristof Beyls double_t scale, y;
35*0928368fSKristof Beyls
36*0928368fSKristof Beyls if ((ki & 0x80000000) == 0)
37*0928368fSKristof Beyls {
38*0928368fSKristof Beyls /* k > 0, the exponent of scale might have overflowed by 1. */
39*0928368fSKristof Beyls sbits -= 1ull << 52;
40*0928368fSKristof Beyls scale = asdouble (sbits);
41*0928368fSKristof Beyls y = 2 * (scale + scale * tmp);
42*0928368fSKristof Beyls return check_oflow (eval_as_double (y));
43*0928368fSKristof Beyls }
44*0928368fSKristof Beyls /* k < 0, need special care in the subnormal range. */
45*0928368fSKristof Beyls sbits += 1022ull << 52;
46*0928368fSKristof Beyls scale = asdouble (sbits);
47*0928368fSKristof Beyls y = scale + scale * tmp;
48*0928368fSKristof Beyls if (y < 1.0)
49*0928368fSKristof Beyls {
50*0928368fSKristof Beyls /* Round y to the right precision before scaling it into the subnormal
51*0928368fSKristof Beyls range to avoid double rounding that can cause 0.5+E/2 ulp error where
52*0928368fSKristof Beyls E is the worst-case ulp error outside the subnormal range. So this
53*0928368fSKristof Beyls is only useful if the goal is better than 1 ulp worst-case error. */
54*0928368fSKristof Beyls double_t hi, lo;
55*0928368fSKristof Beyls lo = scale - y + scale * tmp;
56*0928368fSKristof Beyls hi = 1.0 + y;
57*0928368fSKristof Beyls lo = 1.0 - hi + y + lo;
58*0928368fSKristof Beyls y = eval_as_double (hi + lo) - 1.0;
59*0928368fSKristof Beyls /* Avoid -0.0 with downward rounding. */
60*0928368fSKristof Beyls if (WANT_ROUNDING && y == 0.0)
61*0928368fSKristof Beyls y = 0.0;
62*0928368fSKristof Beyls /* The underflow exception needs to be signaled explicitly. */
63*0928368fSKristof Beyls force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
64*0928368fSKristof Beyls }
65*0928368fSKristof Beyls y = 0x1p-1022 * y;
66*0928368fSKristof Beyls return check_uflow (eval_as_double (y));
67*0928368fSKristof Beyls }
68*0928368fSKristof Beyls
69*0928368fSKristof Beyls /* Top 12 bits of a double (sign and exponent bits). */
70*0928368fSKristof Beyls static inline uint32_t
top12(double x)71*0928368fSKristof Beyls top12 (double x)
72*0928368fSKristof Beyls {
73*0928368fSKristof Beyls return asuint64 (x) >> 52;
74*0928368fSKristof Beyls }
75*0928368fSKristof Beyls
76*0928368fSKristof Beyls double
exp2(double x)77*0928368fSKristof Beyls exp2 (double x)
78*0928368fSKristof Beyls {
79*0928368fSKristof Beyls uint32_t abstop;
80*0928368fSKristof Beyls uint64_t ki, idx, top, sbits;
81*0928368fSKristof Beyls /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
82*0928368fSKristof Beyls double_t kd, r, r2, scale, tail, tmp;
83*0928368fSKristof Beyls
84*0928368fSKristof Beyls abstop = top12 (x) & 0x7ff;
85*0928368fSKristof Beyls if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
86*0928368fSKristof Beyls {
87*0928368fSKristof Beyls if (abstop - top12 (0x1p-54) >= 0x80000000)
88*0928368fSKristof Beyls /* Avoid spurious underflow for tiny x. */
89*0928368fSKristof Beyls /* Note: 0 is common input. */
90*0928368fSKristof Beyls return WANT_ROUNDING ? 1.0 + x : 1.0;
91*0928368fSKristof Beyls if (abstop >= top12 (1024.0))
92*0928368fSKristof Beyls {
93*0928368fSKristof Beyls if (asuint64 (x) == asuint64 (-INFINITY))
94*0928368fSKristof Beyls return 0.0;
95*0928368fSKristof Beyls if (abstop >= top12 (INFINITY))
96*0928368fSKristof Beyls return 1.0 + x;
97*0928368fSKristof Beyls if (!(asuint64 (x) >> 63))
98*0928368fSKristof Beyls return __math_oflow (0);
99*0928368fSKristof Beyls else if (asuint64 (x) >= asuint64 (-1075.0))
100*0928368fSKristof Beyls return __math_uflow (0);
101*0928368fSKristof Beyls }
102*0928368fSKristof Beyls if (2 * asuint64 (x) > 2 * asuint64 (928.0))
103*0928368fSKristof Beyls /* Large x is special cased below. */
104*0928368fSKristof Beyls abstop = 0;
105*0928368fSKristof Beyls }
106*0928368fSKristof Beyls
107*0928368fSKristof Beyls /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)]. */
108*0928368fSKristof Beyls /* x = k/N + r, with int k and r in [-1/2N, 1/2N]. */
109*0928368fSKristof Beyls kd = eval_as_double (x + Shift);
110*0928368fSKristof Beyls ki = asuint64 (kd); /* k. */
111*0928368fSKristof Beyls kd -= Shift; /* k/N for int k. */
112*0928368fSKristof Beyls r = x - kd;
113*0928368fSKristof Beyls /* 2^(k/N) ~= scale * (1 + tail). */
114*0928368fSKristof Beyls idx = 2 * (ki % N);
115*0928368fSKristof Beyls top = ki << (52 - EXP_TABLE_BITS);
116*0928368fSKristof Beyls tail = asdouble (T[idx]);
117*0928368fSKristof Beyls /* This is only a valid scale when -1023*N < k < 1024*N. */
118*0928368fSKristof Beyls sbits = T[idx + 1] + top;
119*0928368fSKristof Beyls /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1). */
120*0928368fSKristof Beyls /* Evaluation is optimized assuming superscalar pipelined execution. */
121*0928368fSKristof Beyls r2 = r * r;
122*0928368fSKristof Beyls /* Without fma the worst case error is 0.5/N ulp larger. */
123*0928368fSKristof Beyls /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp. */
124*0928368fSKristof Beyls #if EXP2_POLY_ORDER == 4
125*0928368fSKristof Beyls tmp = tail + r * C1 + r2 * C2 + r * r2 * (C3 + r * C4);
126*0928368fSKristof Beyls #elif EXP2_POLY_ORDER == 5
127*0928368fSKristof Beyls tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
128*0928368fSKristof Beyls #elif EXP2_POLY_ORDER == 6
129*0928368fSKristof Beyls tmp = tail + r * C1 + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
130*0928368fSKristof Beyls #endif
131*0928368fSKristof Beyls if (unlikely (abstop == 0))
132*0928368fSKristof Beyls return specialcase (tmp, sbits, ki);
133*0928368fSKristof Beyls scale = asdouble (sbits);
134*0928368fSKristof Beyls /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there
135*0928368fSKristof Beyls is no spurious underflow here even without fma. */
136*0928368fSKristof Beyls return eval_as_double (scale + scale * tmp);
137*0928368fSKristof Beyls }
138*0928368fSKristof Beyls #if USE_GLIBC_ABI
strong_alias(exp2,__exp2_finite)139*0928368fSKristof Beyls strong_alias (exp2, __exp2_finite)
140*0928368fSKristof Beyls hidden_alias (exp2, __ieee754_exp2)
141*0928368fSKristof Beyls # if LDBL_MANT_DIG == 53
142*0928368fSKristof Beyls long double exp2l (long double x) { return exp2 (x); }
143*0928368fSKristof Beyls # endif
144*0928368fSKristof Beyls #endif
145