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