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/FMA.h"
14 #include "src/__support/FPUtil/FPBits.h"
15 #include "src/__support/FPUtil/PolyEval.h"
16 #include "src/__support/common.h"
17 
18 #include <errno.h>
19 
20 namespace __llvm_libc {
21 
22 LLVM_LIBC_FUNCTION(float, expf, (float x)) {
23   using FPBits = typename fputil::FPBits<float>;
24   FPBits xbits(x);
25 
26   uint32_t x_u = xbits.uintval();
27   uint32_t x_abs = x_u & 0x7fff'ffffU;
28 
29   // Exceptional values
30   if (unlikely(x_u == 0xc236'bd8cU)) { // x = -0x1.6d7b18p+5f
31     return 0x1.108a58p-66f - x * 0x1.0p-95f;
32   }
33 
34   // When |x| >= 89, |x| < 2^-25, or x is nan
35   if (unlikely(x_abs >= 0x42b2'0000U || x_abs <= 0x3280'0000U)) {
36     // |x| < 2^-25
37     if (xbits.get_unbiased_exponent() <= 101) {
38       return 1.0f + x;
39     }
40 
41     // When x < log(2^-150) or nan
42     if (xbits.uintval() >= 0xc2cf'f1b5U) {
43       // exp(-Inf) = 0
44       if (xbits.is_inf())
45         return 0.0f;
46       // exp(nan) = nan
47       if (xbits.is_nan())
48         return x;
49       if (fputil::get_round() == FE_UPWARD)
50         return static_cast<float>(FPBits(FPBits::MIN_SUBNORMAL));
51       errno = ERANGE;
52       return 0.0f;
53     }
54     // x >= 89 or nan
55     if (!xbits.get_sign() && (xbits.uintval() >= 0x42b2'0000)) {
56       // x is finite
57       if (xbits.uintval() < 0x7f80'0000U) {
58         int rounding = fputil::get_round();
59         if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
60           return static_cast<float>(FPBits(FPBits::MAX_NORMAL));
61 
62         errno = ERANGE;
63       }
64       // x is +inf or nan
65       return x + static_cast<float>(FPBits::inf());
66     }
67   }
68   // For -104 < x < 89, to compute exp(x), we perform the following range
69   // reduction: find hi, mid, lo such that:
70   //   x = hi + mid + lo, in which
71   //     hi is an integer,
72   //     mid * 2^7 is an integer
73   //     -2^(-8) <= lo < 2^-8.
74   // In particular,
75   //   hi + mid = round(x * 2^7) * 2^(-7).
76   // Then,
77   //   exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo).
78   // We store exp(hi) and exp(mid) in the lookup tables EXP_M1 and EXP_M2
79   // respectively.  exp(lo) is computed using a degree-4 minimax polynomial
80   // generated by Sollya.
81 
82   // x_hi = (hi + mid) * 2^7 = round(x * 2^7).
83   // The default rounding mode for float-to-int conversion in C++ is
84   // round-toward-zero. To make it round-to-nearest, we add (-1)^sign(x) * 0.5
85   // before conversion.
86   int x_hi = static_cast<int>(x * 0x1.0p7f + (xbits.get_sign() ? -0.5f : 0.5f));
87   // Subtract (hi + mid) from x to get lo.
88   x -= static_cast<float>(x_hi) * 0x1.0p-7f;
89   double xd = static_cast<double>(x);
90   x_hi += 104 << 7;
91   // hi = x_hi >> 7
92   double exp_hi = EXP_M1[x_hi >> 7];
93   // mid * 2^7 = x_hi & 0x0000'007fU;
94   double exp_mid = EXP_M2[x_hi & 0x7f];
95   // Degree-4 minimax polynomial generated by Sollya with the following
96   // commands:
97   //   > display = hexadecimal;
98   //   > Q = fpminimax(expm1(x)/x, 3, [|D...|], [-2^-8, 2^-8]);
99   //   > Q;
100   double exp_lo =
101       fputil::polyeval(xd, 0x1p0, 0x1.ffffffffff777p-1, 0x1.000000000071cp-1,
102                        0x1.555566668e5e7p-3, 0x1.55555555ef243p-5);
103   return static_cast<float>(exp_hi * exp_mid * exp_lo);
104 }
105 
106 } // namespace __llvm_libc
107