1//===----------------------------------------------------------------------===//
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/// \file
10/// Double-precision implementation of expf.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_DOUBLE_EVAL_H
15#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_DOUBLE_EVAL_H
16
17#include "src/__support/FPUtil/FPBits.h"
18#include "src/__support/FPUtil/except_value_utils.h"
19#include "src/__support/FPUtil/multiply_add.h"
20#include "src/__support/FPUtil/nearest_integer.h"
21#include "src/__support/FPUtil/polyeval.h"
22#include "src/__support/common.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h"
25#include "src/__support/math/check/exp_exceptions.h"
26#include "src/__support/math/exp_float_constants.h" // Lookup tables EXP_M1 and EXP_M2.
27
28namespace LIBC_NAMESPACE_DECL {
29namespace math {
30namespace double_eval {
31
32LIBC_INLINE float expf(float x) {
33 using FPBits = fputil::FPBits<float>;
34 using FPBounds = math::check::exp_internal::Bounds<float>;
35
36 FPBits xbits(x);
37
38 uint32_t x_u = xbits.uintval();
39 uint32_t x_abs = x_u & 0x7fff'ffffU;
40
41#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
42 // Exceptional values
43 if (LIBC_UNLIKELY(x_u == 0xc236'bd8cU)) { // x = -0x1.6d7b18p+5f
44 return 0x1.108a58p-66f - x * 0x1.0p-95f;
45 }
46#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
47
48 // When |x| >= log(2^127 * (2 - 2^-23)), |x| < 2^-25, or x is nan
49 if (LIBC_UNLIKELY(x_abs >= FPBounds::UPPER_BITS || x_abs <= 0x3280'0000U)) {
50 // |x| < 2^-25
51 if (xbits.get_biased_exponent() <= 101) {
52 return 1.0f + x;
53 }
54
55 // When x < log(2^-150) or nan
56 if (xbits.uintval() >= 0xc2cf'f1b5U) {
57 // exp(-Inf) = 0
58 if (xbits.is_inf())
59 return 0.0f;
60 // exp(nan) = nan
61 if (xbits.is_nan())
62 return x;
63 return fputil::set_underflow<float>();
64 }
65 // x >= log(2^127 * (2 - 2^-23)) or nan
66 if (xbits.is_pos() && (xbits.uintval() >= FPBounds::UPPER_BITS)) {
67 // x is finite
68 if (xbits.uintval() < 0x7f80'0000U)
69 return fputil::set_overflow<float>();
70 // x is +inf or nan
71 return x + FPBits::inf().get_val();
72 }
73 }
74 // For -104 < x < 89, to compute exp(x), we perform the following range
75 // reduction: find hi, mid, lo such that:
76 // x = hi + mid + lo, in which
77 // hi is an integer,
78 // mid * 2^7 is an integer
79 // -2^(-8) <= lo < 2^-8.
80 // In particular,
81 // hi + mid = round(x * 2^7) * 2^(-7).
82 // Then,
83 // exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo).
84 // We store exp(hi) and exp(mid) in the lookup tables EXP_M1 and EXP_M2
85 // respectively. exp(lo) is computed using a degree-4 minimax polynomial
86 // generated by Sollya.
87
88 // x_hi = (hi + mid) * 2^7 = round(x * 2^7).
89 float kf = fputil::nearest_integer(x: x * 0x1.0p7f);
90 // Subtract (hi + mid) from x to get lo.
91 double xd = static_cast<double>(fputil::sub_multiply(x, y: kf, z: 0x1.0p-7f));
92 int x_hi = static_cast<int>(kf);
93 x_hi += 104 << 7;
94 // hi = x_hi >> 7
95 double exp_hi = EXP_M1[x_hi >> 7];
96 // mid * 2^7 = x_hi & 0x0000'007fU;
97 double exp_mid = EXP_M2[x_hi & 0x7f];
98 // Degree-4 minimax polynomial generated by Sollya with the following
99 // commands:
100 // > display = hexadecimal;
101 // > Q = fpminimax(expm1(x)/x, 3, [|D...|], [-2^-8, 2^-8]);
102 // > Q;
103 double exp_lo =
104 fputil::polyeval(x: xd, a0: 0x1p0, a: 0x1.ffffffffff777p-1, a: 0x1.000000000071cp-1,
105 a: 0x1.555566668e5e7p-3, a: 0x1.55555555ef243p-5);
106 return static_cast<float>(exp_hi * exp_mid * exp_lo);
107}
108
109} // namespace double_eval
110} // namespace math
111} // namespace LIBC_NAMESPACE_DECL
112
113#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_DOUBLE_EVAL_H
114