11#include "thirdparty/fdlibm_erf_coefficients.h"
20double exp(
double x)
noexcept
24double log(
double x)
noexcept
36template <std::
size_t N>
37double pairedPolynomial(
const std::array<double, N>& coefficients,
double x)
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];
46 pair +=
x * coefficients[index + 1];
48 result += powers[index / 2] * pair;
53double nearZeroRatio(
double x)
55 const double square =
x *
x;
56 return pairedPolynomial(fdlibm::kNearZeroNumerator, square) /
57 pairedPolynomial(fdlibm::kNearZeroDenominator, square);
60double nearOneRatio(
double magnitude)
62 const double offset = magnitude - 1.0;
63 return pairedPolynomial(fdlibm::kNearOneNumerator, offset) /
64 pairedPolynomial(fdlibm::kNearOneDenominator, offset);
69double positiveErfcTail(
double x, std::uint32_t breakpoint)
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);
81 const double high = std::bit_cast<double>(bits & 0xffffffff00000000ULL);
83 exp(-high * high - 0.5625) *
exp((high -
x) * (high +
x) + correction);
88double erf(
double x)
noexcept
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;
99 return x +
x * nearZeroRatio(
x);
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;
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;
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));
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;
127 if (magnitude >= 28.0 || (negative && magnitude >= 6.0)) {
128 return negative ? 2.0 : 0.0;
130 const double tail = positiveErfcTail(magnitude, 0x4006db6dU);
131 return negative ? 2.0 - tail : tail;
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