1*0928368fSKristof Beyls /*
2*0928368fSKristof Beyls * Double-precision e^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 InvLn2N __exp_data.invln2N
16*0928368fSKristof Beyls #define NegLn2hiN __exp_data.negln2hiN
17*0928368fSKristof Beyls #define NegLn2loN __exp_data.negln2loN
18*0928368fSKristof Beyls #define Shift __exp_data.shift
19*0928368fSKristof Beyls #define T __exp_data.tab
20*0928368fSKristof Beyls #define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
21*0928368fSKristof Beyls #define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
22*0928368fSKristof Beyls #define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
23*0928368fSKristof Beyls #define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
24*0928368fSKristof Beyls #define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
25*0928368fSKristof Beyls
26*0928368fSKristof Beyls /* Handle cases that may overflow or underflow when computing the result that
27*0928368fSKristof Beyls is scale*(1+TMP) without intermediate rounding. The bit representation of
28*0928368fSKristof Beyls scale is in SBITS, however it has a computed exponent that may have
29*0928368fSKristof Beyls overflown into the sign bit so that needs to be adjusted before using it as
30*0928368fSKristof Beyls a double. (int32_t)KI is the k used in the argument reduction and exponent
31*0928368fSKristof Beyls adjustment of scale, positive k here means the result may overflow and
32*0928368fSKristof Beyls negative k means the result may underflow. */
33*0928368fSKristof Beyls static inline double
specialcase(double_t tmp,uint64_t sbits,uint64_t ki)34*0928368fSKristof Beyls specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
35*0928368fSKristof Beyls {
36*0928368fSKristof Beyls double_t scale, y;
37*0928368fSKristof Beyls
38*0928368fSKristof Beyls if ((ki & 0x80000000) == 0)
39*0928368fSKristof Beyls {
40*0928368fSKristof Beyls /* k > 0, the exponent of scale might have overflowed by <= 460. */
41*0928368fSKristof Beyls sbits -= 1009ull << 52;
42*0928368fSKristof Beyls scale = asdouble (sbits);
43*0928368fSKristof Beyls y = 0x1p1009 * (scale + scale * tmp);
44*0928368fSKristof Beyls return check_oflow (eval_as_double (y));
45*0928368fSKristof Beyls }
46*0928368fSKristof Beyls /* k < 0, need special care in the subnormal range. */
47*0928368fSKristof Beyls sbits += 1022ull << 52;
48*0928368fSKristof Beyls scale = asdouble (sbits);
49*0928368fSKristof Beyls y = scale + scale * tmp;
50*0928368fSKristof Beyls if (y < 1.0)
51*0928368fSKristof Beyls {
52*0928368fSKristof Beyls /* Round y to the right precision before scaling it into the subnormal
53*0928368fSKristof Beyls range to avoid double rounding that can cause 0.5+E/2 ulp error where
54*0928368fSKristof Beyls E is the worst-case ulp error outside the subnormal range. So this
55*0928368fSKristof Beyls is only useful if the goal is better than 1 ulp worst-case error. */
56*0928368fSKristof Beyls double_t hi, lo;
57*0928368fSKristof Beyls lo = scale - y + scale * tmp;
58*0928368fSKristof Beyls hi = 1.0 + y;
59*0928368fSKristof Beyls lo = 1.0 - hi + y + lo;
60*0928368fSKristof Beyls y = eval_as_double (hi + lo) - 1.0;
61*0928368fSKristof Beyls /* Avoid -0.0 with downward rounding. */
62*0928368fSKristof Beyls if (WANT_ROUNDING && y == 0.0)
63*0928368fSKristof Beyls y = 0.0;
64*0928368fSKristof Beyls /* The underflow exception needs to be signaled explicitly. */
65*0928368fSKristof Beyls force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
66*0928368fSKristof Beyls }
67*0928368fSKristof Beyls y = 0x1p-1022 * y;
68*0928368fSKristof Beyls return check_uflow (eval_as_double (y));
69*0928368fSKristof Beyls }
70*0928368fSKristof Beyls
71*0928368fSKristof Beyls /* Top 12 bits of a double (sign and exponent bits). */
72*0928368fSKristof Beyls static inline uint32_t
top12(double x)73*0928368fSKristof Beyls top12 (double x)
74*0928368fSKristof Beyls {
75*0928368fSKristof Beyls return asuint64 (x) >> 52;
76*0928368fSKristof Beyls }
77*0928368fSKristof Beyls
78*0928368fSKristof Beyls /* Computes exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
79*0928368fSKristof Beyls If hastail is 0 then xtail is assumed to be 0 too. */
80*0928368fSKristof Beyls static inline double
exp_inline(double x,double xtail,int hastail)81*0928368fSKristof Beyls exp_inline (double x, double xtail, int hastail)
82*0928368fSKristof Beyls {
83*0928368fSKristof Beyls uint32_t abstop;
84*0928368fSKristof Beyls uint64_t ki, idx, top, sbits;
85*0928368fSKristof Beyls /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
86*0928368fSKristof Beyls double_t kd, z, r, r2, scale, tail, tmp;
87*0928368fSKristof Beyls
88*0928368fSKristof Beyls abstop = top12 (x) & 0x7ff;
89*0928368fSKristof Beyls if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
90*0928368fSKristof Beyls {
91*0928368fSKristof Beyls if (abstop - top12 (0x1p-54) >= 0x80000000)
92*0928368fSKristof Beyls /* Avoid spurious underflow for tiny x. */
93*0928368fSKristof Beyls /* Note: 0 is common input. */
94*0928368fSKristof Beyls return WANT_ROUNDING ? 1.0 + x : 1.0;
95*0928368fSKristof Beyls if (abstop >= top12 (1024.0))
96*0928368fSKristof Beyls {
97*0928368fSKristof Beyls if (asuint64 (x) == asuint64 (-INFINITY))
98*0928368fSKristof Beyls return 0.0;
99*0928368fSKristof Beyls if (abstop >= top12 (INFINITY))
100*0928368fSKristof Beyls return 1.0 + x;
101*0928368fSKristof Beyls if (asuint64 (x) >> 63)
102*0928368fSKristof Beyls return __math_uflow (0);
103*0928368fSKristof Beyls else
104*0928368fSKristof Beyls return __math_oflow (0);
105*0928368fSKristof Beyls }
106*0928368fSKristof Beyls /* Large x is special cased below. */
107*0928368fSKristof Beyls abstop = 0;
108*0928368fSKristof Beyls }
109*0928368fSKristof Beyls
110*0928368fSKristof Beyls /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
111*0928368fSKristof Beyls /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
112*0928368fSKristof Beyls z = InvLn2N * x;
113*0928368fSKristof Beyls #if TOINT_INTRINSICS
114*0928368fSKristof Beyls kd = roundtoint (z);
115*0928368fSKristof Beyls ki = converttoint (z);
116*0928368fSKristof Beyls #elif EXP_USE_TOINT_NARROW
117*0928368fSKristof Beyls /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */
118*0928368fSKristof Beyls kd = eval_as_double (z + Shift);
119*0928368fSKristof Beyls ki = asuint64 (kd) >> 16;
120*0928368fSKristof Beyls kd = (double_t) (int32_t) ki;
121*0928368fSKristof Beyls #else
122*0928368fSKristof Beyls /* z - kd is in [-1, 1] in non-nearest rounding modes. */
123*0928368fSKristof Beyls kd = eval_as_double (z + Shift);
124*0928368fSKristof Beyls ki = asuint64 (kd);
125*0928368fSKristof Beyls kd -= Shift;
126*0928368fSKristof Beyls #endif
127*0928368fSKristof Beyls r = x + kd * NegLn2hiN + kd * NegLn2loN;
128*0928368fSKristof Beyls /* The code assumes 2^-200 < |xtail| < 2^-8/N. */
129*0928368fSKristof Beyls if (hastail)
130*0928368fSKristof Beyls r += xtail;
131*0928368fSKristof Beyls /* 2^(k/N) ~= scale * (1 + tail). */
132*0928368fSKristof Beyls idx = 2 * (ki % N);
133*0928368fSKristof Beyls top = ki << (52 - EXP_TABLE_BITS);
134*0928368fSKristof Beyls tail = asdouble (T[idx]);
135*0928368fSKristof Beyls /* This is only a valid scale when -1023*N < k < 1024*N. */
136*0928368fSKristof Beyls sbits = T[idx + 1] + top;
137*0928368fSKristof Beyls /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
138*0928368fSKristof Beyls /* Evaluation is optimized assuming superscalar pipelined execution. */
139*0928368fSKristof Beyls r2 = r * r;
140*0928368fSKristof Beyls /* Without fma the worst case error is 0.25/N ulp larger. */
141*0928368fSKristof Beyls /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
142*0928368fSKristof Beyls #if EXP_POLY_ORDER == 4
143*0928368fSKristof Beyls tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
144*0928368fSKristof Beyls #elif EXP_POLY_ORDER == 5
145*0928368fSKristof Beyls tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
146*0928368fSKristof Beyls #elif EXP_POLY_ORDER == 6
147*0928368fSKristof Beyls tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
148*0928368fSKristof Beyls #endif
149*0928368fSKristof Beyls if (unlikely (abstop == 0))
150*0928368fSKristof Beyls return specialcase (tmp, sbits, ki);
151*0928368fSKristof Beyls scale = asdouble (sbits);
152*0928368fSKristof Beyls /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
153*0928368fSKristof Beyls is no spurious underflow here even without fma. */
154*0928368fSKristof Beyls return eval_as_double (scale + scale * tmp);
155*0928368fSKristof Beyls }
156*0928368fSKristof Beyls
157*0928368fSKristof Beyls double
exp(double x)158*0928368fSKristof Beyls exp (double x)
159*0928368fSKristof Beyls {
160*0928368fSKristof Beyls return exp_inline (x, 0, 0);
161*0928368fSKristof Beyls }
162*0928368fSKristof Beyls
163*0928368fSKristof Beyls /* May be useful for implementing pow where more than double
164*0928368fSKristof Beyls precision input is needed. */
165*0928368fSKristof Beyls double
__exp_dd(double x,double xtail)166*0928368fSKristof Beyls __exp_dd (double x, double xtail)
167*0928368fSKristof Beyls {
168*0928368fSKristof Beyls return exp_inline (x, xtail, 1);
169*0928368fSKristof Beyls }
170*0928368fSKristof Beyls #if USE_GLIBC_ABI
strong_alias(exp,__exp_finite)171*0928368fSKristof Beyls strong_alias (exp, __exp_finite)
172*0928368fSKristof Beyls hidden_alias (exp, __ieee754_exp)
173*0928368fSKristof Beyls hidden_alias (__exp_dd, __exp1)
174*0928368fSKristof Beyls # if LDBL_MANT_DIG == 53
175*0928368fSKristof Beyls long double expl (long double x) { return exp (x); }
176*0928368fSKristof Beyls # endif
177*0928368fSKristof Beyls #endif
178