Add new pow implementation
[glibc.git] / sysdeps / ieee754 / dbl-64 / e_exp.c
blob37fdafcfa006d1f1f5275d83b410c282710a4457
1 /* Double-precision e^x function.
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <math.h>
20 #include <stdint.h>
21 #include <math-barriers.h>
22 #include <math-narrow-eval.h>
23 #include "math_config.h"
25 #define N (1 << EXP_TABLE_BITS)
26 #define InvLn2N __exp_data.invln2N
27 #define NegLn2hiN __exp_data.negln2hiN
28 #define NegLn2loN __exp_data.negln2loN
29 #define Shift __exp_data.shift
30 #define T __exp_data.tab
31 #define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
32 #define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
33 #define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
34 #define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
36 /* Handle cases that may overflow or underflow when computing the result that
37 is scale*(1+TMP) without intermediate rounding. The bit representation of
38 scale is in SBITS, however it has a computed exponent that may have
39 overflown into the sign bit so that needs to be adjusted before using it as
40 a double. (int32_t)KI is the k used in the argument reduction and exponent
41 adjustment of scale, positive k here means the result may overflow and
42 negative k means the result may underflow. */
43 static inline double
44 specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
46 double_t scale, y;
48 if ((ki & 0x80000000) == 0)
50 /* k > 0, the exponent of scale might have overflowed by <= 460. */
51 sbits -= 1009ull << 52;
52 scale = asdouble (sbits);
53 y = 0x1p1009 * (scale + scale * tmp);
54 return check_oflow (y);
56 /* k < 0, need special care in the subnormal range. */
57 sbits += 1022ull << 52;
58 scale = asdouble (sbits);
59 y = scale + scale * tmp;
60 if (y < 1.0)
62 /* Round y to the right precision before scaling it into the subnormal
63 range to avoid double rounding that can cause 0.5+E/2 ulp error where
64 E is the worst-case ulp error outside the subnormal range. So this
65 is only useful if the goal is better than 1 ulp worst-case error. */
66 double_t hi, lo;
67 lo = scale - y + scale * tmp;
68 hi = 1.0 + y;
69 lo = 1.0 - hi + y + lo;
70 y = math_narrow_eval (hi + lo) - 1.0;
71 /* Avoid -0.0 with downward rounding. */
72 if (WANT_ROUNDING && y == 0.0)
73 y = 0.0;
74 /* The underflow exception needs to be signaled explicitly. */
75 math_force_eval (math_opt_barrier (0x1p-1022) * 0x1p-1022);
77 y = 0x1p-1022 * y;
78 return check_uflow (y);
81 /* Top 12 bits of a double (sign and exponent bits). */
82 static inline uint32_t
83 top12 (double x)
85 return asuint64 (x) >> 52;
88 #ifndef SECTION
89 # define SECTION
90 #endif
92 double
93 SECTION
94 __ieee754_exp (double x)
96 uint32_t abstop;
97 uint64_t ki, idx, top, sbits;
98 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
99 double_t kd, z, r, r2, scale, tail, tmp;
101 abstop = top12 (x) & 0x7ff;
102 if (__glibc_unlikely (abstop - top12 (0x1p-54)
103 >= top12 (512.0) - top12 (0x1p-54)))
105 if (abstop - top12 (0x1p-54) >= 0x80000000)
106 /* Avoid spurious underflow for tiny x. */
107 /* Note: 0 is common input. */
108 return WANT_ROUNDING ? 1.0 + x : 1.0;
109 if (abstop >= top12 (1024.0))
111 if (asuint64 (x) == asuint64 (-INFINITY))
112 return 0.0;
113 if (abstop >= top12 (INFINITY))
114 return 1.0 + x;
115 if (asuint64 (x) >> 63)
116 return __math_uflow (0);
117 else
118 return __math_oflow (0);
120 /* Large x is special cased below. */
121 abstop = 0;
124 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
125 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
126 z = InvLn2N * x;
127 #if TOINT_INTRINSICS
128 kd = roundtoint (z);
129 ki = converttoint (z);
130 #else
131 /* z - kd is in [-1, 1] in non-nearest rounding modes. */
132 kd = math_narrow_eval (z + Shift);
133 ki = asuint64 (kd);
134 kd -= Shift;
135 #endif
136 r = x + kd * NegLn2hiN + kd * NegLn2loN;
137 /* 2^(k/N) ~= scale * (1 + tail). */
138 idx = 2 * (ki % N);
139 top = ki << (52 - EXP_TABLE_BITS);
140 tail = asdouble (T[idx]);
141 /* This is only a valid scale when -1023*N < k < 1024*N. */
142 sbits = T[idx + 1] + top;
143 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
144 /* Evaluation is optimized assuming superscalar pipelined execution. */
145 r2 = r * r;
146 /* Without fma the worst case error is 0.25/N ulp larger. */
147 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
148 tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
149 if (__glibc_unlikely (abstop == 0))
150 return specialcase (tmp, sbits, ki);
151 scale = asdouble (sbits);
152 /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-739, so there
153 is no spurious underflow here even without fma. */
154 return scale + scale * tmp;
156 #ifndef __ieee754_exp
157 strong_alias (__ieee754_exp, __exp_finite)
158 #endif