[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / math / generic / exp2f.cpp
blob15f35d0da82f5810321aa649c1c14db4b73ccd2b
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 //===----------------------------------------------------------------------===//
9 #include "src/math/exp2f.h"
10 #include "src/__support/FPUtil/FEnvImpl.h"
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/__support/FPUtil/PolyEval.h"
13 #include "src/__support/FPUtil/multiply_add.h"
14 #include "src/__support/FPUtil/nearest_integer.h"
15 #include "src/__support/common.h"
16 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
18 #include <errno.h>
20 #include "explogxf.h"
22 namespace __llvm_libc {
24 constexpr uint32_t EXVAL1 = 0x3b42'9d37U;
25 constexpr uint32_t EXVAL2 = 0xbcf3'a937U;
26 constexpr uint32_t EXVAL_MASK = EXVAL1 & EXVAL2;
28 LLVM_LIBC_FUNCTION(float, exp2f, (float x)) {
29 using FPBits = typename fputil::FPBits<float>;
30 FPBits xbits(x);
32 uint32_t x_u = xbits.uintval();
33 uint32_t x_abs = x_u & 0x7fff'ffffU;
35 // |x| < 2^-25
36 if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
37 return 1.0f + x;
40 // // When |x| >= 128, or x is nan
41 if (LIBC_UNLIKELY(x_abs >= 0x4300'0000U)) {
43 // x >= 128
44 if (!xbits.get_sign()) {
45 // x is finite
46 if (x_u < 0x7f80'0000U) {
47 int rounding = fputil::get_round();
48 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
49 return static_cast<float>(FPBits(FPBits::MAX_NORMAL));
51 fputil::set_errno_if_required(ERANGE);
52 fputil::raise_except_if_required(FE_OVERFLOW);
54 // x is +inf or nan
55 return x + FPBits::inf().get_val();
57 // x <= -150
58 if (x_u >= 0xc316'0000U) {
59 // exp(-Inf) = 0
60 if (xbits.is_inf())
61 return 0.0f;
62 // exp(nan) = nan
63 if (xbits.is_nan())
64 return x;
65 if (fputil::get_round() == FE_UPWARD)
66 return FPBits(FPBits::MIN_SUBNORMAL).get_val();
67 if (x != 0.0f) {
68 fputil::set_errno_if_required(ERANGE);
69 fputil::raise_except_if_required(FE_UNDERFLOW);
71 return 0.0f;
75 // Check exceptional values.
76 if (LIBC_UNLIKELY((x_u & EXVAL_MASK) == EXVAL_MASK)) {
77 if (LIBC_UNLIKELY(x_u == EXVAL1)) { // x = 0x1.853a6ep-9f
78 if (fputil::get_round() == FE_TONEAREST)
79 return 0x1.00870ap+0f;
80 } else if (LIBC_UNLIKELY(x_u == EXVAL2)) { // x = -0x1.e7526ep-6f
81 if (fputil::get_round() == FE_TONEAREST)
82 return 0x1.f58d62p-1f;
86 // For -150 < x < 128, to compute 2^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 // 0 <= mid * 2^5 < 32 is an integer
91 // -2^(-6) <= lo <= 2^-6.
92 // In particular,
93 // hi + mid = round(x * 2^5) * 2^(-5).
94 // Then,
95 // 2^x = 2^(hi + mid + lo) = 2^hi * 2^mid * 2^lo.
96 // 2^mid is stored in the lookup table of 32 elements.
97 // 2^lo is computed using a degree-5 minimax polynomial
98 // generated by Sollya.
99 // We perform 2^hi * 2^mid by simply add hi to the exponent field
100 // of 2^mid.
102 // kf = (hi + mid) * 2^5 = round(x * 2^5)
103 float kf = fputil::nearest_integer(x * 32.0f);
104 // dx = lo = x - (hi + mid) = x - kf * 2^(-5)
105 double dx = fputil::multiply_add(-0x1.0p-5f, kf, x);
107 int k = static_cast<int>(kf);
108 // hi = floor(kf * 2^(-4))
109 // exp_hi = shift hi to the exponent field of double precision.
110 int64_t exp_hi = static_cast<int64_t>(k >> ExpBase::MID_BITS)
111 << fputil::FloatProperties<double>::MANTISSA_WIDTH;
112 // mh = 2^hi * 2^mid
113 // mh_bits = bit field of mh
114 int64_t mh_bits = ExpBase::EXP_2_MID[k & ExpBase::MID_MASK] + exp_hi;
115 double mh = fputil::FPBits<double>(uint64_t(mh_bits)).get_val();
117 // Degree-5 polynomial approximating (2^x - 1)/x generating by Sollya with:
118 // > P = fpminimax((2^x - 1)/x, 5, [|D...|], [-1/32. 1/32]);
119 constexpr double COEFFS[5] = {0x1.62e42fefa39efp-1, 0x1.ebfbdff8131c4p-3,
120 0x1.c6b08d7061695p-5, 0x1.3b2b1bee74b2ap-7,
121 0x1.5d88091198529p-10};
122 double dx_sq = dx * dx;
123 double c1 = fputil::multiply_add(dx, COEFFS[0], 1.0);
124 double c2 = fputil::multiply_add(dx, COEFFS[2], COEFFS[1]);
125 double c3 = fputil::multiply_add(dx, COEFFS[4], COEFFS[3]);
126 double p = fputil::multiply_add(dx_sq, c3, c2);
127 // 2^x = 2^(hi + mid + lo)
128 // = 2^(hi + mid) * 2^lo
129 // ~ mh * (1 + lo * P(lo))
130 // = mh + (mh*lo) * P(lo)
131 return static_cast<float>(fputil::multiply_add(p, dx_sq * mh, c1 * mh));
134 } // namespace __llvm_libc