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