1 /*
2 * Single-precision e^x function.
3 *
4 * Copyright (c) 2017-2018, Arm Limited.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #if WANT_SINGLEPREC
9 #include "single/e_expf.c"
10 #else
11
12 #include <math.h>
13 #include <stdint.h>
14 #include "math_config.h"
15
16 /*
17 EXP2F_TABLE_BITS = 5
18 EXP2F_POLY_ORDER = 3
19
20 ULP error: 0.502 (nearest rounding.)
21 Relative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.)
22 Wrong count: 170635 (all nearest rounding wrong results with fma.)
23 Non-nearest ULP error: 1 (rounded ULP error)
24 */
25
26 #define N (1 << EXP2F_TABLE_BITS)
27 #define InvLn2N __exp2f_data.invln2_scaled
28 #define T __exp2f_data.tab
29 #define C __exp2f_data.poly_scaled
30
31 static inline uint32_t
top12(float x)32 top12 (float x)
33 {
34 return asuint (x) >> 20;
35 }
36
37 float
expf(float x)38 expf (float x)
39 {
40 uint32_t abstop;
41 uint64_t ki, t;
42 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
43 double_t kd, xd, z, r, r2, y, s;
44
45 xd = (double_t) x;
46 abstop = top12 (x) & 0x7ff;
47 if (unlikely (abstop >= top12 (88.0f)))
48 {
49 /* |x| >= 88 or x is nan. */
50 if (asuint (x) == asuint (-INFINITY))
51 return 0.0f;
52 if (abstop >= top12 (INFINITY))
53 return x + x;
54 if (x > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */
55 return __math_oflowf (0);
56 if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */
57 return __math_uflowf (0);
58 #if WANT_ERRNO_UFLOW
59 if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */
60 return __math_may_uflowf (0);
61 #endif
62 }
63
64 /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */
65 z = InvLn2N * xd;
66
67 /* Round and convert z to int, the result is in [-150*N, 128*N] and
68 ideally nearest int is used, otherwise the magnitude of r can be
69 bigger which gives larger approximation error. */
70 #if TOINT_INTRINSICS
71 kd = roundtoint (z);
72 ki = converttoint (z);
73 #else
74 # define SHIFT __exp2f_data.shift
75 kd = eval_as_double (z + SHIFT);
76 ki = asuint64 (kd);
77 kd -= SHIFT;
78 #endif
79 r = z - kd;
80
81 /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
82 t = T[ki % N];
83 t += ki << (52 - EXP2F_TABLE_BITS);
84 s = asdouble (t);
85 z = C[0] * r + C[1];
86 r2 = r * r;
87 y = C[2] * r + 1;
88 y = z * r2 + y;
89 y = y * s;
90 return eval_as_float (y);
91 }
92 #if USE_GLIBC_ABI
93 strong_alias (expf, __expf_finite)
94 hidden_alias (expf, __ieee754_expf)
95 #endif
96 #endif
97