Loading...
Searching...
No Matches
PortableMath.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#include "PortableMath.h"
5
6#include <array>
7#include <bit>
8#include <cmath>
9#include <cstdint>
10
11#include "thirdparty/fdlibm_erf_coefficients.h"
12
13extern "C" {
14double gpr_core_exp(double);
15double gpr_core_log(double);
16double gpr_core_lgamma(double);
17}
18
19namespace gpr::numerics {
20double exp(double x) noexcept
21{
22 return gpr_core_exp(x);
23}
24double log(double x) noexcept
25{
26 return gpr_core_log(x);
27}
28double lgamma(double x) noexcept
29{
30 return gpr_core_lgamma(x);
31}
32
33namespace {
34// Evaluate pairs of coefficients in increasing degree with separate products
35// and sums. The order is part of the quadrature reference arithmetic.
36template <std::size_t N>
37double pairedPolynomial(const std::array<double, N>& coefficients, double x)
38{
39 const double x2 = x * x;
40 const double x4 = x2 * x2;
41 const std::array powers{1.0, x2, x4, x4 * x2, x4 * x4};
42 double result = coefficients[0] + x * coefficients[1];
43 for (std::size_t index = 2; index < N; index += 2) {
44 double pair = coefficients[index];
45 if (index + 1 < N) {
46 pair += x * coefficients[index + 1];
47 }
48 result += powers[index / 2] * pair;
49 }
50 return result;
51}
52
53double nearZeroRatio(double x)
54{
55 const double square = x * x;
56 return pairedPolynomial(fdlibm::kNearZeroNumerator, square) /
57 pairedPolynomial(fdlibm::kNearZeroDenominator, square);
58}
59
60double nearOneRatio(double magnitude)
61{
62 const double offset = magnitude - 1.0;
63 return pairedPolynomial(fdlibm::kNearOneNumerator, offset) /
64 pairedPolynomial(fdlibm::kNearOneDenominator, offset);
65}
66
67// The scalar reference selects the two tail approximants by the high word
68// near 20/7. Each function retains its own cutoff.
69double positiveErfcTail(double x, std::uint32_t breakpoint)
70{
71 const double inverse_square = 1.0 / (x * x);
72 const auto bits = std::bit_cast<std::uint64_t>(x);
73 const double correction =
74 (bits >> 32) < breakpoint
75 ? pairedPolynomial(fdlibm::kNearTailNumerator, inverse_square) /
76 pairedPolynomial(fdlibm::kNearTailDenominator, inverse_square)
77 : pairedPolynomial(fdlibm::kFarTailNumerator, inverse_square) /
78 pairedPolynomial(fdlibm::kFarTailDenominator, inverse_square);
79 // Split the exponent at a truncated input so the tail avoids rounding
80 // the whole square before evaluating the two exponential factors.
81 const double high = std::bit_cast<double>(bits & 0xffffffff00000000ULL);
82 const double value =
83 exp(-high * high - 0.5625) * exp((high - x) * (high + x) + correction);
84 return value / x;
85}
86} // namespace
87
88double erf(double x) noexcept
89{
90 const double magnitude = std::abs(x);
91 if (std::isnan(x) || x == 0.0) return x;
92 if (magnitude < 0.84375) {
93 if (magnitude < 0x1p-28) {
94 return magnitude < 0x1p-1015
95 ? 0.0625 * (16.0 * x +
96 (16.0 * fdlibm::kNearZeroCorrection) * x)
97 : x + fdlibm::kNearZeroCorrection * x;
98 }
99 return x + x * nearZeroRatio(x);
100 }
101 const bool negative = std::signbit(x);
102 if (magnitude < 1.25) {
103 const double ratio = nearOneRatio(magnitude);
104 return negative ? -fdlibm::kErfOneAnchor - ratio
105 : fdlibm::kErfOneAnchor + ratio;
106 }
107 if (magnitude >= 6.0) return negative ? -1.0 : 1.0;
108 const double tail = positiveErfcTail(magnitude, 0x4006db6eU);
109 return negative ? tail - 1.0 : 1.0 - tail;
110}
111
112double erfc(double x) noexcept
113{
114 const double magnitude = std::abs(x);
115 if (std::isnan(x)) return x;
116 if (magnitude < 0.84375) {
117 if (magnitude < 0x1p-56) return 1.0 - x;
118 const double scaled = x * nearZeroRatio(x);
119 return x < 0.25 ? 1.0 - (x + scaled) : 0.5 - (scaled + (x - 0.5));
120 }
121 const bool negative = std::signbit(x);
122 if (magnitude < 1.25) {
123 const double ratio = nearOneRatio(magnitude);
124 return negative ? 1.0 + (fdlibm::kErfOneAnchor + ratio)
125 : (1.0 - fdlibm::kErfOneAnchor) - ratio;
126 }
127 if (magnitude >= 28.0 || (negative && magnitude >= 6.0)) {
128 return negative ? 2.0 : 0.0;
129 }
130 const double tail = positiveErfcTail(magnitude, 0x4006db6dU);
131 return negative ? 2.0 - tail : tail;
132}
133} // namespace gpr::numerics
@ x
Definition Enums.h:28
double gpr_core_log(double)
double gpr_core_lgamma(double)
double gpr_core_exp(double)
double erf(double x) noexcept
double lgamma(double x) noexcept
double erfc(double x) noexcept
double log(double x) noexcept
double exp(double x) noexcept