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