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