17#ifndef GPR_ML_GP_EXTENDED_REAL_H_
18#define GPR_ML_GP_EXTENDED_REAL_H_
45 :
hi(static_cast<double>(value)),
lo(0.0)
52 :
hi(static_cast<double>(value)),
53 lo(static_cast<double>(value - static_cast<long double>(
hi)))
57 explicit operator double()
const
80inline void twoSum(
double a,
double b,
double& sum,
double& error)
83 const double b_virtual = sum - a;
84 const double a_virtual = sum - b_virtual;
85 error = (a - a_virtual) + (b - b_virtual);
88inline void twoProd(
double a,
double b,
double& product,
double& error)
91 error = std::fma(a, b, -product);
97 twoSum(hi, lo, sum, error);
107 return renorm(sum, error);
122 double product, error;
124 error += a.
hi * b.
lo;
125 error += a.
lo * b.
hi;
126 error += a.
lo * b.
lo;
127 return renorm(product, error);
133 const double quotient = a.
hi / b.
hi;
136 const double q1 = a.
hi / b.
hi;
138 const double q2 = remainder.
hi / b.
hi;
140 const double q3 = remainder.
hi / b.
hi;
142 twoSum(q1, q2, sum, error);
144 return renorm(sum, error);
178 return std::isnan(a.
hi) || !std::isfinite(a.
lo);
184 return a.
hi == b.
hi && (!std::isfinite(a.
hi) || a.
lo == b.
lo);
195 if (a.
hi != b.
hi)
return a.
hi < b.
hi;
196 return std::isfinite(a.
hi) && a.
lo < b.
lo;
206 return a < b || a == b;
211 return b < a || a == b;
216 if (a.
hi < 0.0 || (a.
hi == 0.0 && a.
lo < 0.0))
225 return DoubleDouble(std::numeric_limits<double>::quiet_NaN(), 0.0);
227 const double root = std::sqrt(a.
hi);
236 return std::isfinite(a.
hi) && std::isfinite(a.
lo);
241 return std::isnan(a.
hi) || std::isnan(a.
lo);
246 return std::numeric_limits<long double>::digits > 53 &&
247 std::numeric_limits<long double>::radix == 2 &&
248 std::numeric_limits<long double>::is_iec559;
253 return std::numeric_limits<double>::digits == 53 &&
254 std::numeric_limits<double>::radix == 2 &&
255 std::numeric_limits<double>::is_iec559;
258#if defined(GPR_SIMULATE_NO_EXTENDED)
264#elif defined(GPR_FORCE_DOUBLE_DOUBLE)
290 std::numeric_limits<long double>::digits ==
291 std::numeric_limits<double>::digits &&
292 std::numeric_limits<long double>::max_exponent ==
293 std::numeric_limits<double>::max_exponent;
297 return 0.5L * std::numeric_limits<long double>::epsilon();
300 static long double nextUp(
long double value)
303 return std::nextafter(
static_cast<double>(value),
304 std::numeric_limits<double>::infinity());
305 return std::nextafter(value,
306 std::numeric_limits<long double>::infinity());
312 return std::nextafter(
static_cast<double>(value),
313 -std::numeric_limits<double>::infinity());
314 return std::nextafter(value,
315 -std::numeric_limits<long double>::infinity());
331 if (!std::isfinite(value.
hi))
return value;
332 const double magnitude = std::abs(value.
hi);
337 if (!(step > 0.0)) step = std::numeric_limits<double>::denorm_min();
343 if (!std::isfinite(value.
hi))
return value;
344 const double magnitude = std::abs(value.
hi);
346 if (!(step > 0.0)) step = std::numeric_limits<double>::denorm_min();
355template <
typename Work>
380 return std::isfinite(value.
lo);
383template <
typename Work>
386 const double result =
static_cast<double>(value);
388 throw std::range_error(
389 "a working-precision value is not finite and has no binary64 "
394template <
typename Work>
398 if (
static_cast<Work
>(result) > value)
400 std::nextafter(result, -std::numeric_limits<double>::infinity());
404template <
typename Work>
408 if (
static_cast<Work
>(result) < value)
410 std::nextafter(result, std::numeric_limits<double>::infinity());
433 return std::nextafter(
x, std::numeric_limits<double>::infinity());
438 return std::nextafter(
x, -std::numeric_limits<double>::infinity());
449 return std::isinf(rounded) && std::isfinite(a) && std::isfinite(b);
452inline double addUp(
double a,
double b)
454 const double s = a + b;
455 if (!std::isfinite(s))
457 ? std::numeric_limits<double>::lowest()
462 if (std::fabs(b) > std::fabs(a)) std::swap(a, b);
463 const double err = b - (s - a);
464 return err > 0.0 ?
stepUp(s) : s;
469 const double s = a + b;
470 if (!std::isfinite(s))
472 ? std::numeric_limits<double>::max()
474 if (std::fabs(b) > std::fabs(a)) std::swap(a, b);
475 const double err = b - (s - a);
479inline double subUp(
double a,
double b)
489inline double mulUp(
double a,
double b)
491 const double p = a * b;
492 if (!std::isfinite(p))
494 ? std::numeric_limits<double>::lowest()
496 if (a == 0.0 || b == 0.0)
return 0.0;
498 return p == 0.0 && std::signbit(a) != std::signbit(b) ? -0.0
500 return std::fma(a, b, -p) > 0.0 ?
stepUp(p) : p;
505 const double p = a * b;
506 if (!std::isfinite(p))
508 ? std::numeric_limits<double>::max()
510 if (a == 0.0 || b == 0.0)
return 0.0;
512 return p == 0.0 && std::signbit(a) == std::signbit(b) ? 0.0
514 return std::fma(a, b, -p) < 0.0 ?
stepDown(p) : p;
521inline double divUp(
double a,
double b)
523 if (a != 0.0 && std::fabs(a) <
kUnderflowGuard && std::fabs(b) < 0x1p+800)
524 return divUp(a * 0x1p+200, b * 0x1p+200);
525 const double q = a / b;
526 if (!std::isfinite(q))
528 ? std::numeric_limits<double>::lowest()
530 if (a == 0.0 || std::isinf(b))
return q;
532 return q == 0.0 && std::signbit(a) != std::signbit(b) ? -0.0
534 const double remainder = std::fma(-q, b, a);
535 const bool exact_above = (remainder > 0.0) == (b > 0.0) && remainder != 0.0;
536 return exact_above ?
stepUp(q) : q;
544template <
typename Work>
549 if (total <= 0 || total > (1LL << 53))
550 return Work(std::numeric_limits<double>::infinity());
551 const Work k(
static_cast<double>(total));
552 const Work u = Prec::unitRoundoff();
553 if (!(k * u < Work(1)))
554 return Work(std::numeric_limits<double>::infinity());
555 return (k * u) / (Work(1) - k * u);
562template <
typename Work>
567 return Work(std::numeric_limits<double>::infinity());
568 if (!(value > Work(0)))
return Work(0);
569 return Prec::nextUp(value *
580 std::frexp(value, &exponent);
587 std::frexp(value.
hi != 0.0 ? value.
hi : value.
lo, &exponent);
596 const long double scaled = std::ldexp(value, shift);
597 if (std::ldexp(
scaled, -shift) != value) exact =
false;
604 const double hi = std::ldexp(value.
hi, shift);
605 const double lo = std::ldexp(value.
lo, shift);
606 if (std::ldexp(hi, -shift) != value.
hi ||
607 std::ldexp(lo, -shift) != value.
lo)
613template <
typename Work>
616 if constexpr (std::is_same_v<Work, DoubleDouble>)
617 return Work(std::numeric_limits<double>::denorm_min());
619 return std::numeric_limits<Work>::denorm_min();
632template <
typename Work>
659template <
typename Work,
typename Plain>
665 bool nonzero =
false;
668 for (Eigen::Index j = 0; j < matrix.cols(); ++j) {
669 for (Eigen::Index i = 0; i < matrix.rows(); ++i) {
670 const Work value = matrix(i, j);
675 if (value == Work(0))
continue;
678 top = nonzero ? std::max(top, exponent) : exponent;
682 if (!finite || !nonzero || safe)
685 const Work dropped_below(std::ldexp(1.0,
kDroppedExp));
687 for (Eigen::Index j = 0; j < matrix.cols(); ++j) {
688 for (Eigen::Index i = 0; i < matrix.rows(); ++i) {
689 const Work value = matrix(i, j);
690 if (value == Work(0))
continue;
693 if (
y < dropped_below)
continue;
697 sum += Work(std::ldexp(
static_cast<double>(matrix.size()), -917));
726template <
typename Work,
typename ComputedMatrix,
typename AbsMatrix>
728 const AbsMatrix& abs_terms,
732 const long long norm_ops =
static_cast<long long>(computed.size()) + 2;
733 const Work inf(std::numeric_limits<double>::infinity());
734 const Work u = Prec::unitRoundoff();
735 const Work m(
static_cast<double>(entry_ops));
736 if (entry_ops <= 0 || !(m * u < Work(1)))
return inf;
740 const auto computed_magnitude = computed.cwiseAbs().eval();
741 const auto abs_magnitude = abs_terms.cwiseAbs().eval();
746 const Work gamma_entry = (m * u) / (Work(1) - m * u);
750 gamma_entry * abs_norm.
mantissa, 3 + abs_norm.
ops + entry_ops + 1);
753 const bool has_head = head > Work(0);
754 const bool has_tail = tail > Work(0);
756 if (has_head && has_tail)
762 bool aligned_exact =
true;
763 const Work head_at_top =
767 const Work tail_at_top =
771 Work sum = head_at_top + tail_at_top;
772 long long sum_ops = 1;
773 if (!aligned_exact) {
774 sum += Work(std::ldexp(1.0, -1072));
778 if (top == 0)
return bound_at_top;
779 bool unscaled_exact =
true;
783 if (unscaled_exact)
return bound;
790template <
typename Work>
793 if constexpr (std::is_same_v<Work, DoubleDouble>)
794 return Work(std::ldexp(1.0, -968));
796 return std::numeric_limits<Work>::min();
805template <
typename Work>
808 if constexpr (std::is_same_v<Work, DoubleDouble>)
809 return Work(std::ldexp(1.0, -1068));
811 return Work(64) * std::numeric_limits<Work>::denorm_min();
814template <
typename Work>
816 Eigen::Matrix<Work, Eigen::Dynamic, Eigen::Dynamic>
B;
848template <
typename Work,
typename DiagScale,
typename Vectors,
typename Values>
850 const Vectors& evecs_asc,
851 const Values& evals_asc, Eigen::Index rank,
856 const Eigen::Index n = evecs_asc.rows();
858 out.B.resize(n, rank);
862 bool scaled_norm =
false;
863 for (Eigen::Index j = 0; j < rank; ++j) {
864 const Eigen::Index src = n - 1 - j;
865 const Work s =
sqrt(evals_asc(src));
866 const bool root_small =
abs(evals_asc(src)) < floor2;
867 for (Eigen::Index i = 0; i < n; ++i) {
868 const Work product = diag_scale(i) * evecs_asc(i, src);
869 const Work entry = product * s;
871 if (entry != Work(0)) {
877 const bool exact_zero =
878 diag_scale(i) == Work(0) || evecs_asc(i, src) == Work(0);
879 if (exact_zero)
continue;
880 const bool product_small =
abs(product) < floor2;
881 const bool entry_small =
abs(entry) < floor2;
882 if (!(product_small || root_small || entry_small))
continue;
883 out.underflow =
true;
884 if (product_small) allowance += Work(2) *
abs(s);
885 if (root_small) allowance += Work(2) * (
abs(product) + floor2) / s;
886 if (entry_small) allowance += Work(1);
891 if (!(gamma < Work(1))) {
892 out.gram_error = Work(std::numeric_limits<double>::infinity());
893 out.norm =
out.gram_error;
896 const Work gap = Work(1) - gamma;
898 if (!
out.underflow && !scaled_norm) {
899 out.gram_error = bb *
out.B.squaredNorm();
904 const long long entries =
static_cast<long long>(
out.B.size());
907 out.B,
decltype(
out.B)::Zero(n, rank), 1);
912 const Work total = norm + H;
914 const Work absolute =
919 out.underflow =
true;
927template <
typename Work>
934template <
typename Work>
940 if (!std::isfinite(y_scale) || !(y_scale > 0.0) ||
941 !std::isfinite(calibration) || !(calibration > 0.0))
943 const Work
y(y_scale);
944 const Work square =
y *
y;
945 const Work c(calibration);
946 out.lower = Prec::nextDown(Prec::nextDown(square) * c);
947 out.upper = Prec::nextUp(Prec::nextUp(square) * c);
962template <
typename Work>
965 if (std::isnan(value))
966 throw std::range_error(
"a NaN has no scaled binary64 bound");
967 if (value == 0.0 || std::isinf(value))
return value == 0.0 ? 0.0 : value;
969 const Work factor = value > 0.0 ? scale.lower : scale.upper;
970 const Work product = Work(value) * factor;
972 return value > 0.0 ? std::numeric_limits<double>::max()
973 : -std::numeric_limits<double>::infinity();
974 const Work below = Prec::nextDown(product);
976 return value > 0.0 ? std::numeric_limits<double>::max()
977 : -std::numeric_limits<double>::infinity();
979 return value > 0.0 && std::signbit(bound) ? 0.0 : bound;
982template <
typename Work>
985 if (std::isnan(value))
986 throw std::range_error(
"a NaN has no scaled binary64 bound");
987 if (value == 0.0 || std::isinf(value))
return value == 0.0 ? 0.0 : value;
989 const Work factor = value > 0.0 ? scale.upper : scale.lower;
990 const Work product = Work(value) * factor;
992 return value > 0.0 ? std::numeric_limits<double>::infinity()
993 : std::numeric_limits<double>::lowest();
994 const Work above = Prec::nextUp(product);
996 return value > 0.0 ? std::numeric_limits<double>::infinity()
997 : std::numeric_limits<double>::lowest();
999 return value < 0.0 && !std::signbit(bound) ? -0.0 : bound;
1018struct numeric_limits<
gpr::extended::DoubleDouble> {
1034 numeric_limits<double>::min_exponent10;
1036 numeric_limits<double>::max_exponent10;
1056 return value_type(numeric_limits<double>::infinity(), 0.0);
1060 return value_type(numeric_limits<double>::quiet_NaN(), 0.0);
1068 return value_type(numeric_limits<double>::denorm_min(), 0.0);
1072 return value_type(numeric_limits<double>::min(), 0.0);
1076 return value_type(numeric_limits<double>::max(), 0.0);
1087struct NumTraits<
gpr::extended::DoubleDouble>
1088 : GenericNumTraits<gpr::extended::DoubleDouble> {
1128 return Real(std::numeric_limits<double>::max(), 0.0);
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double subUp(double a, double b)
double addUp(double a, double b)
bool finiteOverflow(double a, double b, double rounded)
constexpr double kUnderflowGuard
double divUp(double a, double b)
double subDown(double a, double b)
double mulUp(double a, double b)
double mulDown(double a, double b)
double stepDown(double x)
double addDown(double a, double b)
constexpr int kDroppedExp
ScaledNorm< Work > scaledFrobeniusNorm(const Plain &matrix, long long norm_ops)
long double scaleByPowerOfTwo(long double value, int shift, bool &exact)
int leadingExponent(long double value)
bool isfinite(DoubleDouble a)
bool lowLimbFinite(double)
constexpr int kDoubleDoubleDigits
DoubleDouble operator*(DoubleDouble a, DoubleDouble b)
DoubleDouble renorm(double hi, double lo)
Work frobeniusResidualUpperBound(const ComputedMatrix &computed, const AbsMatrix &abs_terms, long long entry_ops)
DoubleDouble operator-(DoubleDouble a, DoubleDouble b)
Work formedEntryAbsoluteError()
bool unordered(DoubleDouble a)
void twoProd(double a, double b, double &product, double &error)
double modelUnitRoundoff()
constexpr bool nativeLongDoubleIsExtended()
Work directedGammaUpper(long long expression_ops)
bool operator!=(DoubleDouble a, DoubleDouble b)
std::conditional_t< nativeLongDoubleIsExtended(), long double, DoubleDouble > WorkingReal
void twoSum(double a, double b, double &sum, double &error)
constexpr int kDoubleDoubleUnitRoundoffExp
void outwardProduct(double a, double b, double &down, double &up)
double roundDownToDouble(Work value)
constexpr int kDoubleDoubleEpsilonExp
FormedFactor< Work > formScaledFactor(const DiagScale &diag_scale, const Vectors &evecs_asc, const Values &evals_asc, Eigen::Index rank, int entry_ops)
DoubleDouble sqrt(DoubleDouble a)
bool isnan(DoubleDouble a)
DoubleDouble operator+(DoubleDouble a, DoubleDouble b)
DoubleDouble & operator-=(DoubleDouble &a, DoubleDouble b)
bool operator<=(DoubleDouble a, DoubleDouble b)
DoubleDouble abs(DoubleDouble a)
DoubleDouble & operator/=(DoubleDouble &a, DoubleDouble b)
bool operator>=(DoubleDouble a, DoubleDouble b)
VarianceScaleEnclosure< Work > varianceScaleEnclosure(double y_scale, double calibration)
DoubleDouble operator/(DoubleDouble a, DoubleDouble b)
double scaledUpperBound(double value, const VarianceScaleEnclosure< Work > &scale)
constexpr bool certificationAvailable()
Work directedUpperBound(Work value, long long expression_ops)
bool operator==(DoubleDouble a, DoubleDouble b)
bool workingFinite(Work value)
bool operator<(DoubleDouble a, DoubleDouble b)
constexpr bool binary64SupportsDoubleDouble()
double roundedToDouble(Work value)
double roundUpToDouble(Work value)
double scaledLowerBound(double value, const VarianceScaleEnclosure< Work > &scale)
DoubleDouble & operator+=(DoubleDouble &a, DoubleDouble b)
Work formedEntryRelativeFloor()
constexpr int kDirectedScaleOps
bool operator>(DoubleDouble a, DoubleDouble b)
DoubleDouble & operator*=(DoubleDouble &a, DoubleDouble b)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
gpr::extended::DoubleDouble Real
gpr::extended::DoubleDouble Nested
static Real dummy_precision()
gpr::extended::DoubleDouble NonInteger
gpr::extended::DoubleDouble Literal
constexpr DoubleDouble(double high, double low)
constexpr DoubleDouble(long value)
constexpr DoubleDouble(long long value)
constexpr DoubleDouble(int value)
constexpr DoubleDouble()=default
DoubleDouble(long double value)
constexpr DoubleDouble(double value)
static DoubleDouble nextUp(DoubleDouble value)
static DoubleDouble unitRoundoff()
static DoubleDouble nextDown(DoubleDouble value)
static long double nextUp(long double value)
static long double nextDown(long double value)
static constexpr bool kBinary64
static long double unitRoundoff()
static constexpr int max_digits10
static value_type round_error()
static constexpr int min_exponent
static value_type epsilon()
static constexpr bool is_bounded
static constexpr bool has_denorm_loss
static value_type denorm_min()
gpr::extended::DoubleDouble value_type
static constexpr bool has_signaling_NaN
static constexpr bool is_modulo
static constexpr int max_exponent10
static value_type signaling_NaN()
static constexpr float_round_style round_style
static constexpr int radix
static constexpr bool is_signed
static constexpr bool has_infinity
static constexpr bool is_specialized
static value_type quiet_NaN()
static constexpr int digits
static value_type infinity()
static constexpr bool traps
static constexpr bool has_quiet_NaN
static constexpr bool is_iec559
static constexpr bool is_integer
static constexpr float_denorm_style has_denorm
static constexpr int min_exponent10
static constexpr bool tinyness_before
static value_type lowest()
static constexpr int max_exponent
static constexpr int digits10
static constexpr bool is_exact