1/* Single-precision e^x function.
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#ifdef __expf
20# undef libm_hidden_proto
21# define libm_hidden_proto(ignored)
22#endif
23
24#include <math.h>
25#include <stdint.h>
26#include <shlib-compat.h>
27#include <libm-alias-float.h>
28#include "math_config.h"
29
30/*
31EXP2F_TABLE_BITS = 5
32EXP2F_POLY_ORDER = 3
33
34ULP error: 0.502 (nearest rounding.)
35Relative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.)
36Wrong count: 170635 (all nearest rounding wrong results with fma.)
37Non-nearest ULP error: 1 (rounded ULP error)
38*/
39
40#define N (1 << EXP2F_TABLE_BITS)
41#define InvLn2N __exp2f_data.invln2_scaled
42#define T __exp2f_data.tab
43#define C __exp2f_data.poly_scaled
44
45static inline uint32_t
46top12 (float x)
47{
48 return asuint (x) >> 20;
49}
50
51float
52__expf (float x)
53{
54 uint32_t abstop;
55 uint64_t ki, t;
56 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
57 double_t kd, xd, z, r, r2, y, s;
58
59 xd = (double_t) x;
60 abstop = top12 (x) & 0x7ff;
61 if (__glibc_unlikely (abstop >= top12 (88.0f)))
62 {
63 /* |x| >= 88 or x is nan. */
64 if (asuint (x) == asuint (-INFINITY))
65 return 0.0f;
66 if (abstop >= top12 (INFINITY))
67 return x + x;
68 if (x > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */
69 return __math_oflowf (0);
70 if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */
71 return __math_uflowf (0);
72#if WANT_ERRNO_UFLOW
73 if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */
74 return __math_may_uflowf (0);
75#endif
76 }
77
78 /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */
79 z = InvLn2N * xd;
80
81 /* Round and convert z to int, the result is in [-150*N, 128*N] and
82 ideally ties-to-even rule is used, otherwise the magnitude of r
83 can be bigger which gives larger approximation error. */
84#if TOINT_INTRINSICS
85 kd = roundtoint (z);
86 ki = converttoint (z);
87#elif TOINT_RINT
88 kd = rint (z);
89 ki = (long) kd;
90#elif TOINT_SHIFT
91# define SHIFT __exp2f_data.shift
92 kd = math_narrow_eval ((double) (z + SHIFT)); /* Needs to be double. */
93 ki = asuint64 (kd);
94 kd -= SHIFT;
95#endif
96 r = z - kd;
97
98 /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
99 t = T[ki % N];
100 t += ki << (52 - EXP2F_TABLE_BITS);
101 s = asdouble (t);
102 z = C[0] * r + C[1];
103 r2 = r * r;
104 y = C[2] * r + 1;
105 y = z * r2 + y;
106 y = y * s;
107 return (float) y;
108}
109
110#ifndef __expf
111hidden_def (__expf)
112strong_alias (__expf, __ieee754_expf)
113strong_alias (__expf, __expf_finite)
114versioned_symbol (libm, __expf, expf, GLIBC_2_27);
115libm_alias_float_other (__exp, exp)
116#endif
117