|
NumericAL
A header-only C++20 lexer built on REAL
|
exp, exp2, expm1, tanh, sinh and cosh of double by a table of 2^(j/128) and a polynomial, atan by a table of atan(k/64) and a series, log1p by a table of ln(k/128) and a series, branch-free so that a loop of them runs in vectors: within 0.506 ulp of the exact value for a normal result of exp and exp2, 0.503 ulp for expm1 and sinh; the same bits from a vector lane as from a call.
More...
#include <algorithm>#include <array>#include <bit>#include <cmath>#include <cstddef>#include <cstdint>#include <limits>#include "simd.hpp"Go to the source code of this file.
Classes | |
| struct | numerical::detail::sine_terms |
| The terms table_sine adds for sin x = a + b·r + …: a = S and b = C for the sine, a = C and b = −S for the cosine (S, C the table's sin(jπ/32) and cos(jπ/32), each a head and a tail), and the value where |x| < 2⁻²⁷: x for the sine, 1 for the cosine. More... | |
Namespaces | |
| namespace | numerical |
| The NumericAL public API. | |
| namespace | numerical::detail |
Functions | |
| double | numerical::detail::blend (bool condition, double if_true, double if_false) noexcept |
if_true where condition holds, else if_false, by a mask of their bits: GCC turns a ?: between a value computed only for it and an argument into a branch around that computation, its table reads among it, and then vectorizes no loop of the function ("unsupported control flow in loop"). | |
| double | numerical::detail::table_exp (double x) noexcept |
exp(x) for an x whose result is normal or infinite (exp_normal_bound or above): x = (128 e + j) ln 2/128 + r, |r| ≤ ln 2/256, and exp(x) = 2^e · 2^(j/128) · exp(r), exp(r) − 1 by its Taylor polynomial of degree 6. Every multiply-add std::fma, written out, so that no compiler fuses otherwise; inlined into the loop that calls it (exp_into). Below the bound, its answer is not exp's; for NaN, NaN. | |
| void | numerical::detail::two_sum (double a, double b, double &sum, double &error) noexcept |
The sum of a and b as sum rounded and error exact (Knuth's two-sum: no order of magnitudes needed). | |
| std::int64_t | numerical::detail::expm1_parts (double clamped, double &sum, double &small) noexcept |
expm1(clamped) in units of 2^e, as sum + small unrounded, and e: table_exp's reduction, x = (128 e + j) ln 2/128 + r, then 2^(j/128) − 2^−e + 2^(j/128) r_head, each sum and product of them exact (two-sums and a fused product's error), into sum, and the small rest, 2^(j/128) (r − r_head + exp(r) − 1 − r) and the table's tail times exp(r), with the two-sums' errors, into small. For clamped from −40 to 710. | |
| double | numerical::detail::table_expm1 (double x) noexcept |
expm1(x) by table_exp's reduction, x = (128 e + j) ln 2/128 + r: in units of 2^e, 2^(j/128) − 2^−e | |
| double | numerical::detail::table_tanh (double x) noexcept |
tanh(x) = t / (t + 2), t = expm1(2|x|) as a pair (expm1_parts, its two parts summed exactly), the sum t + 2 a pair too, the quotient by the reciprocal of its head, corrected by its exact residual: within 0.502 ulp of the exact value. Past 22, ±1 (1 − tanh under half an ulp of it); x itself where |x| < 2^−27 (tanh(x) − x under half an ulp of it, ±0 kept) and for NaN. | |
| double | numerical::detail::hyperbolic_parts (double magnitude, double &d_high, double &d_low, double &u_high, double &u_low) noexcept |
The parts of sinh and cosh of magnitude (0 to 711, past which both overflow): t = expm1(magnitude) (expm1_parts, its two parts summed exactly) at most 2^64 in its place (past it, t · 2^(64 − e), whose 1 and 1/(t + 1) then weigh under 2^−63 of it), d = t + 1 and u = 1/d as pairs (d_high + d_low, u_high + u_low); sinh = (d − u)/2 and cosh = (d + u)/2, each times the power of two returned. Near 0, d − u loses no digit: d_high − u_high is exact and the pairs' tails carry the rest. | |
| double | numerical::detail::table_sinh (double x) noexcept |
sinh(x) = (d − u)/2 (hyperbolic_parts), the sign of x: within 0.503 ulp of the exact value up to the overflow (2^(e − 1) a normal double, where e^x alone would overflow from 709.78). x itself where |x| < 2^−27 (sinh(x) − x under half an ulp of it, ±0 kept) and for NaN. | |
| double | numerical::detail::table_cosh (double x) noexcept |
cosh(x) = (d + u)/2 (hyperbolic_parts): within 0.502 ulp of the exact value up to the overflow; NaN for NaN. | |
| double | numerical::detail::table_exp2 (double x) noexcept |
2^x for an x whose result is normal or infinite (−1022 or above): x = (128 e + j)/128 + f, |f| ≤ 1/256, each step exact (the scaling by 128 and f's difference), and 2^x = 2^e · 2^(j/128) · exp(f ln 2), as in table_exp. Within 0.506 ulp of the exact value, and exact at every integer (f = 0, j = 0). Below the bound, its answer is not 2^x's; for NaN, NaN. | |
| template<bool Cosine> | |
| double | numerical::detail::reduced_sine (double x) noexcept |
sin(x), or cos(x) where Cosine, for |x| ≤ sine_reduction_bound (another x gives a value of no use): k the integer nearest x·2/π, by the sum with 1.5·2⁵² whose low bits are k's (its quadrant); r = x − k·π/2 by three multiply-adds of half_pi_parts, |r| ≤ π/4; sin r and cos r by fdlibm's polynomials (within 2⁻⁵⁸ there), the quadrant's taken and signed by masks. A float's value rounded to float is within half its ulp and 2⁻²⁸: the floats below 2²⁰ come no nearer a multiple of π/2 than 2⁻²⁷·⁸ (252.898…, near 161·π/2), where the reduction errs by some 2⁻⁸⁰. ±0 kept. Every multiply-add std::fma, so the bits are the same in vectors. | |
| template<bool Cosine> | |
| sine_terms | numerical::detail::sine_terms_of (std::uint64_t entry, double x) noexcept |
sine_terms of the table's row at entry (sin head, sin tail, cos head, cos tail) for x: the sine's where not Cosine, the cosine's otherwise; one specialization each, no choice at run time. | |
| template<> | |
| sine_terms | numerical::detail::sine_terms_of< false > (std::uint64_t entry, double x) noexcept |
| The terms of the sine (sine_terms): a = S, b = C, x itself where tiny. | |
| template<> | |
| sine_terms | numerical::detail::sine_terms_of< true > (std::uint64_t entry, double) noexcept |
| The terms of the cosine (sine_terms): a = C, b = −S, 1 where tiny. | |
| template<bool Cosine> | |
| double | numerical::detail::table_sine (double x) noexcept |
sin(x), or cos(x) where Cosine, of a double within half an ulp and a few thousandths, for |x| ≤ sine_reduction_bound (another x gives a value of no use): j the integer nearest x·32/π (its low six bits the table's entry), r = x − jπ/32 by sine_step_parts as a pair r_hi + r_lo (|r| ≤ π/64); sin r − r and cos r − 1 by their Taylor series to r¹¹ and r¹⁰; then sin x = S + (C·r + S·(cos r − 1) + C·(sin r − r)), cos x = C + (−S·r + …) for S, C the table's sin(jπ/32) and cos(jπ/32), the first sum exact (a two-sum and a two-product), the small rest added once. x itself where |x| < 2⁻²⁷ (±0 kept), 1 for the cosine. Every product beside a sum fused explicitly and every other operation its own, so the bits are the same in vectors and under every compiler with the FMA instructions. Within 0.502 ulp of the exact value on 80 000 points to 2²⁰, a third of them near multiples of π/32 (the C library's of macOS: 0.86). | |
| double | numerical::detail::table_atan (double x) noexcept |
atan(x): y = |x|, or 1/|x| past 1 (its rounding error carried), c = k/64 the nearest point of atan_table, t = (y − c)/(1 + y c) as a pair, |t| ≤ 2^−7, atan(t) − t by its Taylor series to t^11; atan(c) + t summed exactly, π/2 less that past 1, the small rest added once. Within 0.501 ulp of the exact value; x itself where |x| < 2^−27 (±0 kept) and for NaN, ±π/2 past 2^54. Every product beside a sum fused explicitly, as table_expm1's. | |
| void | numerical::detail::log_parts (double u, double u_low, double &sum, double &small) noexcept |
ln(u + u_low) as sum + small unrounded, for u at least 2^−54 and finite and |u_low| at most half an ulp of it: u = 2^e m, m in [0.703125, 1.40625), c = k/128 the nearest point, r = m RN(1/c) − 1 + u_low RN(1/c)/2^e as a normalized pair; e ln 2 + (−ln RN(1/c)) + r − r²/2 summed exactly (an exact sum of multiples of 2^−42, two-sums, a fused square's error) into sum, and the rest, r³/3 − … − r¹⁰/10, the low parts and r's low part by the derivative 1 − r + r², into small. Near 0, u_low is as large as r, under which small would round to an ulp of the result. Every product beside a sum fused explicitly, as table_expm1's. | |
| double | numerical::detail::table_log1p (double x) noexcept |
ln(1 + x) by log_parts of 1 + x as a pair (its two-sum): near 0, where c = 1, r is x exactly. Within 0.501 ulp of the exact value; x itself where |x| < 2^−54 (±0 kept) and for NaN, −∞ at −1, +∞ at +∞, NaN below −1. | |
Variables | |
| constexpr double | numerical::detail::exp_inv_ln2_128 {0x1.71547652b82fep+7} |
128/ln 2 (tools/exp_table.py derives each constant here). | |
| constexpr double | numerical::detail::exp_ln2_128_high {0x1.62e42fee00000p-8} |
| ln 2/128 to 32 significant bits, toward zero: its product by an integer of 18 bits or fewer is exact, and so is that product's difference from an argument it reduces. | |
| constexpr double | numerical::detail::exp_ln2_128_low {0x1.a39ef35793c76p-40} |
What exp_ln2_128_high leaves out of ln 2/128, to the nearest double. | |
| constexpr double | numerical::detail::exp_normal_bound {-0x1.6232bdd7abcd2p+9} |
ln 2^-1022 toward zero: below it, exp is subnormal, where table_exp's two scalings would round twice. | |
| constexpr std::array< double, 256 > | numerical::detail::exp_table |
2^(j/128) for j = 0…127, at 2j the double nearest it and at 2j + 1 the double nearest what that one leaves out. Not inline: a copy in each translation unit, whose loads Clang's vectoriser bounds, where it kept a loop of table_exp scalar over an inline variable's ("cannot identify array bounds"). | |
| constexpr double | numerical::detail::exp2_ln2 {0x1.62e42fefa39efp-1} |
ln 2 to the nearest double. | |
| constexpr double | numerical::detail::atan_half_pi_high {0x1.921fb54442d18p+0} |
π/2, the double nearest it (tools/atan_table.py derives each constant here). | |
| constexpr double | numerical::detail::atan_half_pi_low {0x1.1a62633145c07p-54} |
What atan_half_pi_high leaves out of π/2, to the nearest double. | |
| constexpr std::array< double, 130 > | numerical::detail::atan_table |
atan(k/64) for k = 0…64, at 2k the double nearest it and at 2k + 1 the double nearest what that one leaves out. Not inline, as exp_table. | |
| constexpr std::array< double, 3 > | numerical::detail::half_pi_parts {0x1.921fb54442d18p+0, 0x1.1a62633145c07p-54, -0x1.f1976b7ed8fbcp-110} |
| π/2 in three parts: its double, then the double of the rest, then the double of what is left (their sum within 6·10⁻⁵⁰ of π/2). | |
| constexpr double | numerical::detail::sine_reduction_bound {0x1p20} |
| The magnitude to which reduced_sine reduces its argument: k below 2²⁰, k·π/2's parts exact enough. | |
| constexpr double | numerical::detail::sine_inverse_step {0x1.45f306dc9c883p+3} |
32/π, the double nearest it (tools/sine_table.py derives each constant here). | |
| constexpr std::array< double, 3 > | numerical::detail::sine_step_parts {0x1.921fb54442d18p-4, 0x1.1a62633145c07p-58, -0x1.f1976b7ed8fbcp-114} |
| π/32 in three parts: its double, then the double of the rest, then the double of what is left. | |
| constexpr std::array< double, 256 > | numerical::detail::sine_table |
For j = 0…63, at 4j sin(jπ/32) and at 4j + 1 what that double leaves out, at 4j + 2 cos(jπ/32) and at 4j + 3 what that one leaves out, each the double nearest. Not inline, as exp_table. | |
| constexpr double | numerical::detail::log_ln2_high {0x1.62e42fefa3800p-1} |
ln 2 to 42 significant bits, toward zero: its product by an exponent of 11 bits or fewer is exact (tools/log_table.py derives each constant here). | |
| constexpr double | numerical::detail::log_ln2_low {0x1.ef35793c76730p-45} |
What log_ln2_high leaves out of ln 2, to the nearest double. | |
| constexpr std::array< double, 91 > | numerical::detail::log_inverse |
For k = 90…180, at k − 90 the double nearest 128/k. Not inline, as exp_table. | |
| constexpr std::array< double, 182 > | numerical::detail::log_table |
For k = 90…180, at 2(k − 90) −ln of log_inverse's entry to the nearest multiple of 2^−42 (its sum with an exponent times log_ln2_high exact) and at 2(k − 90) + 1 the double nearest what that leaves out. Not inline, as exp_table. | |
exp, exp2, expm1, tanh, sinh and cosh of double by a table of 2^(j/128) and a polynomial, atan by a table of atan(k/64) and a series, log1p by a table of ln(k/128) and a series, branch-free so that a loop of them runs in vectors: within 0.506 ulp of the exact value for a normal result of exp and exp2, 0.503 ulp for expm1 and sinh; the same bits from a vector lane as from a call.
Definition in file elementary.hpp.