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