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