xref: /aosp_15_r20/external/llvm-libc/src/math/generic/exp2f_impl.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Single-precision 2^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 #ifndef LLVM_LIBC_SRC_MATH_GENERIC_EXP2F_IMPL_H
10 #define LLVM_LIBC_SRC_MATH_GENERIC_EXP2F_IMPL_H
11 
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/except_value_utils.h"
16 #include "src/__support/FPUtil/multiply_add.h"
17 #include "src/__support/FPUtil/nearest_integer.h"
18 #include "src/__support/FPUtil/rounding_mode.h"
19 #include "src/__support/common.h"
20 #include "src/__support/macros/config.h"
21 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
22 #include "src/__support/macros/properties/cpu_features.h"
23 
24 #include "explogxf.h"
25 
26 namespace LIBC_NAMESPACE_DECL {
27 namespace generic {
28 
exp2f(float x)29 LIBC_INLINE float exp2f(float x) {
30   constexpr uint32_t EXVAL1 = 0x3b42'9d37U;
31   constexpr uint32_t EXVAL2 = 0xbcf3'a937U;
32   constexpr uint32_t EXVAL_MASK = EXVAL1 & EXVAL2;
33 
34   using FPBits = typename fputil::FPBits<float>;
35   FPBits xbits(x);
36 
37   uint32_t x_u = xbits.uintval();
38   uint32_t x_abs = x_u & 0x7fff'ffffU;
39 
40   // When |x| >= 128, or x is nan, or |x| <= 2^-5
41   if (LIBC_UNLIKELY(x_abs >= 0x4300'0000U || x_abs <= 0x3d00'0000U)) {
42     // |x| <= 2^-5
43     if (x_abs <= 0x3d00'0000) {
44       // |x| < 2^-25
45       if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
46         return 1.0f + x;
47       }
48 
49       // Check exceptional values.
50       if (LIBC_UNLIKELY((x_u & EXVAL_MASK) == EXVAL_MASK)) {
51         if (LIBC_UNLIKELY(x_u == EXVAL1)) { // x = 0x1.853a6ep-9f
52           return fputil::round_result_slightly_down(0x1.00870ap+0f);
53         } else if (LIBC_UNLIKELY(x_u == EXVAL2)) { // x = -0x1.e7526ep-6f
54           return fputil::round_result_slightly_down(0x1.f58d62p-1f);
55         }
56       }
57 
58       // Minimax polynomial generated by Sollya with:
59       // > P = fpminimax((2^x - 1)/x, 5, [|D...|], [-2^-5, 2^-5]);
60       constexpr double COEFFS[] = {
61           0x1.62e42fefa39f3p-1, 0x1.ebfbdff82c57bp-3,  0x1.c6b08d6f2d7aap-5,
62           0x1.3b2ab6fc92f5dp-7, 0x1.5d897cfe27125p-10, 0x1.43090e61e6af1p-13};
63       double xd = static_cast<double>(x);
64       double xsq = xd * xd;
65       double c0 = fputil::multiply_add(xd, COEFFS[1], COEFFS[0]);
66       double c1 = fputil::multiply_add(xd, COEFFS[3], COEFFS[2]);
67       double c2 = fputil::multiply_add(xd, COEFFS[5], COEFFS[4]);
68       double p = fputil::polyeval(xsq, c0, c1, c2);
69       double r = fputil::multiply_add(p, xd, 1.0);
70       return static_cast<float>(r);
71     }
72 
73     // x >= 128
74     if (xbits.is_pos()) {
75       // x is finite
76       if (x_u < 0x7f80'0000U) {
77         int rounding = fputil::quick_get_round();
78         if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
79           return FPBits::max_normal().get_val();
80 
81         fputil::set_errno_if_required(ERANGE);
82         fputil::raise_except_if_required(FE_OVERFLOW);
83       }
84       // x is +inf or nan
85       return x + FPBits::inf().get_val();
86     }
87     // x <= -150
88     if (x_u >= 0xc316'0000U) {
89       // exp(-Inf) = 0
90       if (xbits.is_inf())
91         return 0.0f;
92       // exp(nan) = nan
93       if (xbits.is_nan())
94         return x;
95       if (fputil::fenv_is_round_up())
96         return FPBits::min_subnormal().get_val();
97       if (x != 0.0f) {
98         fputil::set_errno_if_required(ERANGE);
99         fputil::raise_except_if_required(FE_UNDERFLOW);
100       }
101       return 0.0f;
102     }
103   }
104 
105   // For -150 < x < 128, to compute 2^x, we perform the following range
106   // reduction: find hi, mid, lo such that:
107   //   x = hi + mid + lo, in which
108   //     hi is an integer,
109   //     0 <= mid * 2^5 < 32 is an integer
110   //     -2^(-6) <= lo <= 2^-6.
111   // In particular,
112   //   hi + mid = round(x * 2^5) * 2^(-5).
113   // Then,
114   //   2^x = 2^(hi + mid + lo) = 2^hi * 2^mid * 2^lo.
115   // 2^mid is stored in the lookup table of 32 elements.
116   // 2^lo is computed using a degree-5 minimax polynomial
117   // generated by Sollya.
118   // We perform 2^hi * 2^mid by simply add hi to the exponent field
119   // of 2^mid.
120 
121   // kf = (hi + mid) * 2^5 = round(x * 2^5)
122   float kf;
123   int k;
124 #ifdef LIBC_TARGET_CPU_HAS_NEAREST_INT
125   kf = fputil::nearest_integer(x * 32.0f);
126   k = static_cast<int>(kf);
127 #else
128   constexpr float HALF[2] = {0.5f, -0.5f};
129   k = static_cast<int>(fputil::multiply_add(x, 32.0f, HALF[x < 0.0f]));
130   kf = static_cast<float>(k);
131 #endif // LIBC_TARGET_CPU_HAS_NEAREST_INT
132 
133   // dx = lo = x - (hi + mid) = x - kf * 2^(-5)
134   double dx = fputil::multiply_add(-0x1.0p-5f, kf, x);
135 
136   // hi = floor(kf * 2^(-4))
137   // exp_hi = shift hi to the exponent field of double precision.
138   int64_t exp_hi =
139       static_cast<int64_t>(static_cast<uint64_t>(k >> ExpBase::MID_BITS)
140                            << fputil::FPBits<double>::FRACTION_LEN);
141   // mh = 2^hi * 2^mid
142   // mh_bits = bit field of mh
143   int64_t mh_bits = ExpBase::EXP_2_MID[k & ExpBase::MID_MASK] + exp_hi;
144   double mh = fputil::FPBits<double>(uint64_t(mh_bits)).get_val();
145 
146   // Degree-5 polynomial approximating (2^x - 1)/x generating by Sollya with:
147   // > P = fpminimax((2^x - 1)/x, 5, [|D...|], [-1/32. 1/32]);
148   constexpr double COEFFS[5] = {0x1.62e42fefa39efp-1, 0x1.ebfbdff8131c4p-3,
149                                 0x1.c6b08d7061695p-5, 0x1.3b2b1bee74b2ap-7,
150                                 0x1.5d88091198529p-10};
151   double dx_sq = dx * dx;
152   double c1 = fputil::multiply_add(dx, COEFFS[0], 1.0);
153   double c2 = fputil::multiply_add(dx, COEFFS[2], COEFFS[1]);
154   double c3 = fputil::multiply_add(dx, COEFFS[4], COEFFS[3]);
155   double p = fputil::multiply_add(dx_sq, c3, c2);
156   // 2^x = 2^(hi + mid + lo)
157   //     = 2^(hi + mid) * 2^lo
158   //     ~ mh * (1 + lo * P(lo))
159   //     = mh + (mh*lo) * P(lo)
160   return static_cast<float>(fputil::multiply_add(p, dx_sq * mh, c1 * mh));
161 }
162 
163 } // namespace generic
164 } // namespace LIBC_NAMESPACE_DECL
165 
166 #endif // LLVM_LIBC_SRC_MATH_GENERIC_EXP2F_IMPL_H
167