1//===-- Single-precision general exp/log functions ------------------------===//
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_EXPLOGXF_H
10#define LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
11
12#include "common_constants.h"
13#include "src/__support/CPP/bit.h"
14#include "src/__support/CPP/optional.h"
15#include "src/__support/FPUtil/FEnvImpl.h"
16#include "src/__support/FPUtil/FPBits.h"
17#include "src/__support/FPUtil/PolyEval.h"
18#include "src/__support/FPUtil/nearest_integer.h"
19#include "src/__support/common.h"
20#include "src/__support/macros/properties/cpu_features.h"
21
22#include <errno.h>
23
24namespace LIBC_NAMESPACE {
25
26struct ExpBase {
27 // Base = e
28 static constexpr int MID_BITS = 5;
29 static constexpr int MID_MASK = (1 << MID_BITS) - 1;
30 // log2(e) * 2^5
31 static constexpr double LOG2_B = 0x1.71547652b82fep+0 * (1 << MID_BITS);
32 // High and low parts of -log(2) * 2^(-5)
33 static constexpr double M_LOGB_2_HI = -0x1.62e42fefa0000p-1 / (1 << MID_BITS);
34 static constexpr double M_LOGB_2_LO =
35 -0x1.cf79abc9e3b3ap-40 / (1 << MID_BITS);
36 // Look up table for bit fields of 2^(i/32) for i = 0..31, generated by Sollya
37 // with:
38 // > for i from 0 to 31 do printdouble(round(2^(i/32), D, RN));
39 static constexpr int64_t EXP_2_MID[1 << MID_BITS] = {
40 0x3ff0000000000000, 0x3ff059b0d3158574, 0x3ff0b5586cf9890f,
41 0x3ff11301d0125b51, 0x3ff172b83c7d517b, 0x3ff1d4873168b9aa,
42 0x3ff2387a6e756238, 0x3ff29e9df51fdee1, 0x3ff306fe0a31b715,
43 0x3ff371a7373aa9cb, 0x3ff3dea64c123422, 0x3ff44e086061892d,
44 0x3ff4bfdad5362a27, 0x3ff5342b569d4f82, 0x3ff5ab07dd485429,
45 0x3ff6247eb03a5585, 0x3ff6a09e667f3bcd, 0x3ff71f75e8ec5f74,
46 0x3ff7a11473eb0187, 0x3ff82589994cce13, 0x3ff8ace5422aa0db,
47 0x3ff93737b0cdc5e5, 0x3ff9c49182a3f090, 0x3ffa5503b23e255d,
48 0x3ffae89f995ad3ad, 0x3ffb7f76f2fb5e47, 0x3ffc199bdd85529c,
49 0x3ffcb720dcef9069, 0x3ffd5818dcfba487, 0x3ffdfc97337b9b5f,
50 0x3ffea4afa2a490da, 0x3fff50765b6e4540,
51 };
52
53 // Approximating e^dx with degree-5 minimax polynomial generated by Sollya:
54 // > Q = fpminimax(expm1(x)/x, 4, [|1, D...|], [-log(2)/64, log(2)/64]);
55 // Then:
56 // e^dx ~ P(dx) = 1 + dx + COEFFS[0] * dx^2 + ... + COEFFS[3] * dx^5.
57 static constexpr double COEFFS[4] = {
58 0x1.ffffffffe5bc8p-2, 0x1.555555555cd67p-3, 0x1.5555c2a9b48b4p-5,
59 0x1.11112a0e34bdbp-7};
60
61 LIBC_INLINE static double powb_lo(double dx) {
62 using fputil::multiply_add;
63 double dx2 = dx * dx;
64 double c0 = 1.0 + dx;
65 // c1 = COEFFS[0] + COEFFS[1] * dx
66 double c1 = multiply_add(x: dx, y: ExpBase::COEFFS[1], z: ExpBase::COEFFS[0]);
67 // c2 = COEFFS[2] + COEFFS[3] * dx
68 double c2 = multiply_add(x: dx, y: ExpBase::COEFFS[3], z: ExpBase::COEFFS[2]);
69 // r = c4 + c5 * dx^4
70 // = 1 + dx + COEFFS[0] * dx^2 + ... + COEFFS[5] * dx^7
71 return fputil::polyeval(x: dx2, a0: c0, a: c1, a: c2);
72 }
73};
74
75struct Exp10Base : public ExpBase {
76 // log2(10) * 2^5
77 static constexpr double LOG2_B = 0x1.a934f0979a371p1 * (1 << MID_BITS);
78 // High and low parts of -log10(2) * 2^(-5).
79 // Notice that since |x * log2(10)| < 150:
80 // |k| = |round(x * log2(10) * 2^5)| < 2^8 * 2^5 = 2^13
81 // So when the FMA instructions are not available, in order for the product
82 // k * M_LOGB_2_HI
83 // to be exact, we only store the high part of log10(2) up to 38 bits
84 // (= 53 - 15) of precision.
85 // It is generated by Sollya with:
86 // > round(log10(2), 44, RN);
87 static constexpr double M_LOGB_2_HI = -0x1.34413509f8p-2 / (1 << MID_BITS);
88 // > round(log10(2) - 0x1.34413509f8p-2, D, RN);
89 static constexpr double M_LOGB_2_LO = 0x1.80433b83b532ap-44 / (1 << MID_BITS);
90
91 // Approximating 10^dx with degree-5 minimax polynomial generated by Sollya:
92 // > Q = fpminimax((10^x - 1)/x, 4, [|D...|], [-log10(2)/2^6, log10(2)/2^6]);
93 // Then:
94 // 10^dx ~ P(dx) = 1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5.
95 static constexpr double COEFFS[5] = {0x1.26bb1bbb55515p1, 0x1.53524c73bd3eap1,
96 0x1.0470591dff149p1, 0x1.2bd7c0a9fbc4dp0,
97 0x1.1429e74a98f43p-1};
98
99 static double powb_lo(double dx) {
100 using fputil::multiply_add;
101 double dx2 = dx * dx;
102 // c0 = 1 + COEFFS[0] * dx
103 double c0 = multiply_add(x: dx, y: Exp10Base::COEFFS[0], z: 1.0);
104 // c1 = COEFFS[1] + COEFFS[2] * dx
105 double c1 = multiply_add(x: dx, y: Exp10Base::COEFFS[2], z: Exp10Base::COEFFS[1]);
106 // c2 = COEFFS[3] + COEFFS[4] * dx
107 double c2 = multiply_add(x: dx, y: Exp10Base::COEFFS[4], z: Exp10Base::COEFFS[3]);
108 // r = c0 + dx^2 * (c1 + c2 * dx^2)
109 // = c0 + c1 * dx^2 + c2 * dx^4
110 // = 1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5.
111 return fputil::polyeval(x: dx2, a0: c0, a: c1, a: c2);
112 }
113};
114
115constexpr int LOG_P1_BITS = 6;
116constexpr int LOG_P1_SIZE = 1 << LOG_P1_BITS;
117
118// N[Table[Log[2, 1 + x], {x, 0/64, 63/64, 1/64}], 40]
119extern const double LOG_P1_LOG2[LOG_P1_SIZE];
120
121// N[Table[1/(1 + x), {x, 0/64, 63/64, 1/64}], 40]
122extern const double LOG_P1_1_OVER[LOG_P1_SIZE];
123
124// Taylor series expansion for Log[2, 1 + x] splitted to EVEN AND ODD numbers
125// K_LOG2_ODD starts from x^3
126extern const double K_LOG2_ODD[4];
127extern const double K_LOG2_EVEN[4];
128
129// Output of range reduction for exp_b: (2^(mid + hi), lo)
130// where:
131// b^x = 2^(mid + hi) * b^lo
132struct exp_b_reduc_t {
133 double mh; // 2^(mid + hi)
134 double lo;
135};
136
137// The function correctly calculates b^x value with at least float precision
138// in a limited range.
139// Range reduction:
140// b^x = 2^(hi + mid) * b^lo
141// where:
142// x = (hi + mid) * log_b(2) + lo
143// hi is an integer,
144// 0 <= mid * 2^MID_BITS < 2^MID_BITS is an integer
145// -2^(-MID_BITS - 1) <= lo * log2(b) <= 2^(-MID_BITS - 1)
146// Base class needs to provide the following constants:
147// - MID_BITS : number of bits after decimal points used for mid
148// - MID_MASK : 2^MID_BITS - 1, mask to extract mid bits
149// - LOG2_B : log2(b) * 2^MID_BITS for scaling
150// - M_LOGB_2_HI : high part of -log_b(2) * 2^(-MID_BITS)
151// - M_LOGB_2_LO : low part of -log_b(2) * 2^(-MID_BITS)
152// - EXP_2_MID : look up table for bit fields of 2^mid
153// Return:
154// { 2^(hi + mid), lo }
155template <class Base> LIBC_INLINE exp_b_reduc_t exp_b_range_reduc(float x) {
156 double xd = static_cast<double>(x);
157 // kd = round((hi + mid) * log2(b) * 2^MID_BITS)
158 double kd = fputil::nearest_integer(Base::LOG2_B * xd);
159 // k = round((hi + mid) * log2(b) * 2^MID_BITS)
160 int k = static_cast<int>(kd);
161 // hi = floor(kd * 2^(-MID_BITS))
162 // exp_hi = shift hi to the exponent field of double precision.
163 int64_t exp_hi = static_cast<int64_t>((k >> Base::MID_BITS))
164 << fputil::FPBits<double>::FRACTION_LEN;
165 // mh = 2^hi * 2^mid
166 // mh_bits = bit field of mh
167 int64_t mh_bits = Base::EXP_2_MID[k & Base::MID_MASK] + exp_hi;
168 double mh = fputil::FPBits<double>(uint64_t(mh_bits)).get_val();
169 // dx = lo = x - (hi + mid) * log(2)
170 double dx = fputil::multiply_add(
171 kd, Base::M_LOGB_2_LO, fputil::multiply_add(kd, Base::M_LOGB_2_HI, xd));
172 return {.mh: mh, .lo: dx};
173}
174
175// The function correctly calculates sinh(x) and cosh(x) by calculating exp(x)
176// and exp(-x) simultaneously.
177// To compute e^x, we perform the following range
178// reduction: find hi, mid, lo such that:
179// x = (hi + mid) * log(2) + lo, in which
180// hi is an integer,
181// 0 <= mid * 2^5 < 32 is an integer
182// -2^(-6) <= lo * log2(e) <= 2^-6.
183// In particular,
184// hi + mid = round(x * log2(e) * 2^5) * 2^(-5).
185// Then,
186// e^x = 2^(hi + mid) * e^lo = 2^hi * 2^mid * e^lo.
187// 2^mid is stored in the lookup table of 32 elements.
188// e^lo is computed using a degree-5 minimax polynomial
189// generated by Sollya:
190// e^lo ~ P(lo) = 1 + lo + c2 * lo^2 + ... + c5 * lo^5
191// = (1 + c2*lo^2 + c4*lo^4) + lo * (1 + c3*lo^2 + c5*lo^4)
192// = P_even + lo * P_odd
193// We perform 2^hi * 2^mid by simply add hi to the exponent field
194// of 2^mid.
195// To compute e^(-x), notice that:
196// e^(-x) = 2^(-(hi + mid)) * e^(-lo)
197// ~ 2^(-(hi + mid)) * P(-lo)
198// = 2^(-(hi + mid)) * (P_even - lo * P_odd)
199// So:
200// sinh(x) = (e^x - e^(-x)) / 2
201// ~ 0.5 * (2^(hi + mid) * (P_even + lo * P_odd) -
202// 2^(-(hi + mid)) * (P_even - lo * P_odd))
203// = 0.5 * (P_even * (2^(hi + mid) - 2^(-(hi + mid))) +
204// lo * P_odd * (2^(hi + mid) + 2^(-(hi + mid))))
205// And similarly:
206// cosh(x) = (e^x + e^(-x)) / 2
207// ~ 0.5 * (P_even * (2^(hi + mid) + 2^(-(hi + mid))) +
208// lo * P_odd * (2^(hi + mid) - 2^(-(hi + mid))))
209// The main point of these formulas is that the expensive part of calculating
210// the polynomials approximating lower parts of e^(x) and e^(-x) are shared
211// and only done once.
212template <bool is_sinh> LIBC_INLINE double exp_pm_eval(float x) {
213 double xd = static_cast<double>(x);
214
215 // kd = round(x * log2(e) * 2^5)
216 // k_p = round(x * log2(e) * 2^5)
217 // k_m = round(-x * log2(e) * 2^5)
218 double kd;
219 int k_p, k_m;
220
221#ifdef LIBC_TARGET_CPU_HAS_NEAREST_INT
222 kd = fputil::nearest_integer(x: ExpBase::LOG2_B * xd);
223 k_p = static_cast<int>(kd);
224 k_m = -k_p;
225#else
226 constexpr double HALF_WAY[2] = {0.5, -0.5};
227
228 k_p = static_cast<int>(
229 fputil::multiply_add(xd, ExpBase::LOG2_B, HALF_WAY[x < 0.0f]));
230 k_m = -k_p;
231 kd = static_cast<double>(k_p);
232#endif // LIBC_TARGET_CPU_HAS_NEAREST_INT
233
234 // hi = floor(kf * 2^(-5))
235 // exp_hi = shift hi to the exponent field of double precision.
236 int64_t exp_hi_p = static_cast<int64_t>((k_p >> ExpBase::MID_BITS))
237 << fputil::FPBits<double>::FRACTION_LEN;
238 int64_t exp_hi_m = static_cast<int64_t>((k_m >> ExpBase::MID_BITS))
239 << fputil::FPBits<double>::FRACTION_LEN;
240 // mh_p = 2^(hi + mid)
241 // mh_m = 2^(-(hi + mid))
242 // mh_bits_* = bit field of mh_*
243 int64_t mh_bits_p = ExpBase::EXP_2_MID[k_p & ExpBase::MID_MASK] + exp_hi_p;
244 int64_t mh_bits_m = ExpBase::EXP_2_MID[k_m & ExpBase::MID_MASK] + exp_hi_m;
245 double mh_p = fputil::FPBits<double>(uint64_t(mh_bits_p)).get_val();
246 double mh_m = fputil::FPBits<double>(uint64_t(mh_bits_m)).get_val();
247 // mh_sum = 2^(hi + mid) + 2^(-(hi + mid))
248 double mh_sum = mh_p + mh_m;
249 // mh_diff = 2^(hi + mid) - 2^(-(hi + mid))
250 double mh_diff = mh_p - mh_m;
251
252 // dx = lo = x - (hi + mid) * log(2)
253 double dx =
254 fputil::multiply_add(x: kd, y: ExpBase::M_LOGB_2_LO,
255 z: fputil::multiply_add(x: kd, y: ExpBase::M_LOGB_2_HI, z: xd));
256 double dx2 = dx * dx;
257
258 // c0 = 1 + COEFFS[0] * lo^2
259 // P_even = (1 + COEFFS[0] * lo^2 + COEFFS[2] * lo^4) / 2
260 double p_even = fputil::polyeval(x: dx2, a0: 0.5, a: ExpBase::COEFFS[0] * 0.5,
261 a: ExpBase::COEFFS[2] * 0.5);
262 // P_odd = (1 + COEFFS[1] * lo^2 + COEFFS[3] * lo^4) / 2
263 double p_odd = fputil::polyeval(x: dx2, a0: 0.5, a: ExpBase::COEFFS[1] * 0.5,
264 a: ExpBase::COEFFS[3] * 0.5);
265
266 double r;
267 if constexpr (is_sinh)
268 r = fputil::multiply_add(x: dx * mh_sum, y: p_odd, z: p_even * mh_diff);
269 else
270 r = fputil::multiply_add(x: dx * mh_diff, y: p_odd, z: p_even * mh_sum);
271 return r;
272}
273
274// x should be positive, normal finite value
275LIBC_INLINE static double log2_eval(double x) {
276 using FPB = fputil::FPBits<double>;
277 FPB bs(x);
278
279 double result = 0;
280 result += bs.get_exponent();
281
282 int p1 = (bs.get_mantissa() >> (FPB::FRACTION_LEN - LOG_P1_BITS)) &
283 (LOG_P1_SIZE - 1);
284
285 bs.set_uintval(bs.uintval() & (FPB::FRACTION_MASK >> LOG_P1_BITS));
286 bs.set_biased_exponent(FPB::EXP_BIAS);
287 double dx = (bs.get_val() - 1.0) * LOG_P1_1_OVER[p1];
288
289 // Taylor series for log(2,1+x)
290 double c1 = fputil::multiply_add(x: dx, y: K_LOG2_ODD[0], z: K_LOG2_EVEN[0]);
291 double c2 = fputil::multiply_add(x: dx, y: K_LOG2_ODD[1], z: K_LOG2_EVEN[1]);
292 double c3 = fputil::multiply_add(x: dx, y: K_LOG2_ODD[2], z: K_LOG2_EVEN[2]);
293 double c4 = fputil::multiply_add(x: dx, y: K_LOG2_ODD[3], z: K_LOG2_EVEN[3]);
294
295 // c0 = dx * (1.0 / ln(2)) + LOG_P1_LOG2[p1]
296 double c0 = fputil::multiply_add(x: dx, y: 0x1.71547652b82fep+0, z: LOG_P1_LOG2[p1]);
297 result += LIBC_NAMESPACE::fputil::polyeval(x: dx * dx, a0: c0, a: c1, a: c2, a: c3, a: c4);
298 return result;
299}
300
301// x should be positive, normal finite value
302LIBC_INLINE static double log_eval(double x) {
303 // For x = 2^ex * (1 + mx)
304 // log(x) = ex * log(2) + log(1 + mx)
305 using FPB = fputil::FPBits<double>;
306 FPB bs(x);
307
308 double ex = static_cast<double>(bs.get_exponent());
309
310 // p1 is the leading 7 bits of mx, i.e.
311 // p1 * 2^(-7) <= m_x < (p1 + 1) * 2^(-7).
312 int p1 = static_cast<int>(bs.get_mantissa() >> (FPB::FRACTION_LEN - 7));
313
314 // Set bs to (1 + (mx - p1*2^(-7))
315 bs.set_uintval(bs.uintval() & (FPB::FRACTION_MASK >> 7));
316 bs.set_biased_exponent(FPB::EXP_BIAS);
317 // dx = (mx - p1*2^(-7)) / (1 + p1*2^(-7)).
318 double dx = (bs.get_val() - 1.0) * ONE_OVER_F[p1];
319
320 // Minimax polynomial of log(1 + dx) generated by Sollya with:
321 // > P = fpminimax(log(1 + x)/x, 6, [|D...|], [0, 2^-7]);
322 const double COEFFS[6] = {-0x1.ffffffffffffcp-2, 0x1.5555555552ddep-2,
323 -0x1.ffffffefe562dp-3, 0x1.9999817d3a50fp-3,
324 -0x1.554317b3f67a5p-3, 0x1.1dc5c45e09c18p-3};
325 double dx2 = dx * dx;
326 double c1 = fputil::multiply_add(x: dx, y: COEFFS[1], z: COEFFS[0]);
327 double c2 = fputil::multiply_add(x: dx, y: COEFFS[3], z: COEFFS[2]);
328 double c3 = fputil::multiply_add(x: dx, y: COEFFS[5], z: COEFFS[4]);
329
330 double p = fputil::polyeval(x: dx2, a0: dx, a: c1, a: c2, a: c3);
331 double result =
332 fputil::multiply_add(x: ex, /*log(2)*/ y: 0x1.62e42fefa39efp-1, z: LOG_F[p1] + p);
333 return result;
334}
335
336// Rounding tests for 2^hi * (mid + lo) when the output might be denormal. We
337// assume further that 1 <= mid < 2, mid + lo < 2, and |lo| << mid.
338// Notice that, if 0 < x < 2^-1022,
339// double(2^-1022 + x) - 2^-1022 = double(x).
340// So if we scale x up by 2^1022, we can use
341// double(1.0 + 2^1022 * x) - 1.0 to test how x is rounded in denormal range.
342LIBC_INLINE cpp::optional<double> ziv_test_denorm(int hi, double mid, double lo,
343 double err) {
344 using FPBits = typename fputil::FPBits<double>;
345
346 // Scaling factor = 1/(min normal number) = 2^1022
347 int64_t exp_hi = static_cast<int64_t>(hi + 1022) << FPBits::FRACTION_LEN;
348 double mid_hi = cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: mid));
349 double lo_scaled =
350 (lo != 0.0) ? cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: lo))
351 : 0.0;
352
353 double extra_factor = 0.0;
354 uint64_t scale_down = 0x3FE0'0000'0000'0000; // 1022 in the exponent field.
355
356 // Result is denormal if (mid_hi + lo_scale < 1.0).
357 if ((1.0 - mid_hi) > lo_scaled) {
358 // Extra rounding step is needed, which adds more rounding errors.
359 err += 0x1.0p-52;
360 extra_factor = 1.0;
361 scale_down = 0x3FF0'0000'0000'0000; // 1023 in the exponent field.
362 }
363
364 double err_scaled =
365 cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: err));
366
367 double lo_u = lo_scaled + err_scaled;
368 double lo_l = lo_scaled - err_scaled;
369
370 // By adding 1.0, the results will have similar rounding points as denormal
371 // outputs.
372 double upper = extra_factor + (mid_hi + lo_u);
373 double lower = extra_factor + (mid_hi + lo_l);
374
375 if (LIBC_LIKELY(upper == lower)) {
376 return cpp::bit_cast<double>(from: cpp::bit_cast<uint64_t>(from: upper) - scale_down);
377 }
378
379 return cpp::nullopt;
380}
381
382} // namespace LIBC_NAMESPACE
383
384#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
385

source code of libc/src/math/generic/explogxf.h