1 //===-- Implementation header for expf --------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_H
10 #define LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_H
11
12 #include "exp_float_constants.h" // Lookup tables EXP_M1 and EXP_M2.
13 #include "src/__support/FPUtil/FEnvImpl.h"
14 #include "src/__support/FPUtil/FPBits.h"
15 #include "src/__support/FPUtil/PolyEval.h"
16 #include "src/__support/FPUtil/multiply_add.h"
17 #include "src/__support/FPUtil/nearest_integer.h"
18 #include "src/__support/FPUtil/rounding_mode.h"
19 #include "src/__support/common.h"
20 #include "src/__support/macros/config.h"
21 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
22
23 namespace LIBC_NAMESPACE_DECL {
24
25 namespace math {
26
expf(float x)27 static constexpr float expf(float x) {
28 using FPBits = typename fputil::FPBits<float>;
29 FPBits xbits(x);
30
31 uint32_t x_u = xbits.uintval();
32 uint32_t x_abs = x_u & 0x7fff'ffffU;
33
34 #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
35 // Exceptional values
36 if (LIBC_UNLIKELY(x_u == 0xc236'bd8cU)) { // x = -0x1.6d7b18p+5f
37 return 0x1.108a58p-66f - x * 0x1.0p-95f;
38 }
39 #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
40
41 // When |x| >= 89, |x| < 2^-25, or x is nan
42 if (LIBC_UNLIKELY(x_abs >= 0x42b2'0000U || x_abs <= 0x3280'0000U)) {
43 // |x| < 2^-25
44 if (xbits.get_biased_exponent() <= 101) {
45 return 1.0f + x;
46 }
47
48 // When x < log(2^-150) or nan
49 if (xbits.uintval() >= 0xc2cf'f1b5U) {
50 // exp(-Inf) = 0
51 if (xbits.is_inf())
52 return 0.0f;
53 // exp(nan) = nan
54 if (xbits.is_nan())
55 return x;
56 if (fputil::fenv_is_round_up())
57 return FPBits::min_subnormal().get_val();
58 fputil::set_errno_if_required(ERANGE);
59 fputil::raise_except_if_required(FE_UNDERFLOW);
60 return 0.0f;
61 }
62 // x >= 89 or nan
63 if (xbits.is_pos() && (xbits.uintval() >= 0x42b2'0000)) {
64 // x is finite
65 if (xbits.uintval() < 0x7f80'0000U) {
66 int rounding = fputil::quick_get_round();
67 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
68 return FPBits::max_normal().get_val();
69
70 fputil::set_errno_if_required(ERANGE);
71 fputil::raise_except_if_required(FE_OVERFLOW);
72 }
73 // x is +inf or nan
74 return x + FPBits::inf().get_val();
75 }
76 }
77 // For -104 < x < 89, to compute exp(x), we perform the following range
78 // reduction: find hi, mid, lo such that:
79 // x = hi + mid + lo, in which
80 // hi is an integer,
81 // mid * 2^7 is an integer
82 // -2^(-8) <= lo < 2^-8.
83 // In particular,
84 // hi + mid = round(x * 2^7) * 2^(-7).
85 // Then,
86 // exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo).
87 // We store exp(hi) and exp(mid) in the lookup tables EXP_M1 and EXP_M2
88 // respectively. exp(lo) is computed using a degree-4 minimax polynomial
89 // generated by Sollya.
90
91 // x_hi = (hi + mid) * 2^7 = round(x * 2^7).
92 float kf = fputil::nearest_integer(x * 0x1.0p7f);
93 // Subtract (hi + mid) from x to get lo.
94 double xd = static_cast<double>(fputil::multiply_add(kf, -0x1.0p-7f, x));
95 int x_hi = static_cast<int>(kf);
96 x_hi += 104 << 7;
97 // hi = x_hi >> 7
98 double exp_hi = EXP_M1[x_hi >> 7];
99 // mid * 2^7 = x_hi & 0x0000'007fU;
100 double exp_mid = EXP_M2[x_hi & 0x7f];
101 // Degree-4 minimax polynomial generated by Sollya with the following
102 // commands:
103 // > display = hexadecimal;
104 // > Q = fpminimax(expm1(x)/x, 3, [|D...|], [-2^-8, 2^-8]);
105 // > Q;
106 double exp_lo =
107 fputil::polyeval(xd, 0x1p0, 0x1.ffffffffff777p-1, 0x1.000000000071cp-1,
108 0x1.555566668e5e7p-3, 0x1.55555555ef243p-5);
109 return static_cast<float>(exp_hi * exp_mid * exp_lo);
110 }
111
112 } // namespace math
113
114 } // namespace LIBC_NAMESPACE_DECL
115
116 #endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_H
117