Loading...
Searching...
No Matches
CertifiedProduct.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <cfenv>
6#include <cmath>
7#include <cstdint>
8#include <cstring>
9#include <limits>
10
11namespace gpr::linalg {
12
13inline double twoSumResidual(double a, double b, double rounded)
14{
15 return std::abs(a) >= std::abs(b) ? (a - rounded) + b : (b - rounded) + a;
16}
17
18// The bounded operands keep products, FMA residuals, and all partial sums
19// normal or zero for an int-sized dot. Other inputs use the exact backend.
20// The certificate encloses the exact sum within one binary64 rounding cell.
21inline bool tryCertifiedProduct(const double* left, const double* right,
22 int length, double& result)
23{
24 if (length < 0 || std::fegetround() != FE_TONEAREST) return false;
25 double sum = 0.0;
26 double correction = 0.0;
27 double absolute_residuals = 0.0;
28 const auto add = [&](double value) {
29 const double next = sum + value;
30 const double residual = twoSumResidual(sum, value, next);
31 correction += residual;
32 absolute_residuals += std::abs(residual);
33 sum = next;
34 };
35 for (int i = 0; i < length; ++i) {
36 const double a = left[i], b = right[i];
37 uint64_t bits_a, bits_b;
38 std::memcpy(&bits_a, &a, sizeof(a));
39 std::memcpy(&bits_b, &b, sizeof(b));
40 bits_a &= UINT64_C(0x7fffffffffffffff);
41 bits_b &= UINT64_C(0x7fffffffffffffff);
42 if (bits_a >= UINT64_C(0x7ff0000000000000) ||
43 bits_b >= UINT64_C(0x7ff0000000000000))
44 return false;
45 if (bits_a == 0 || bits_b == 0) continue;
46 if (std::abs(a) < 0x1p-400 || std::abs(a) > 0x1p400 ||
47 std::abs(b) < 0x1p-400 || std::abs(b) > 0x1p400)
48 return false;
49 const double product = a * b;
50 add(product);
51 add(std::fma(a, b, -product));
52 }
53 if (absolute_residuals == 0.0) {
54 result = sum;
55 return true;
56 }
57
58 // For m <= 2n additions and u = 2^-53, the error in correction is
59 // bounded by gamma_(m-1)/(1-gamma_(m-1)) times absolute_residuals.
60 // With n <= INT_MAX this factor is strictly below 4nu = n*2^-51.
61 const double infinity = std::numeric_limits<double>::infinity();
62 const double bound = std::nextafter(
63 (static_cast<double>(length) * 0x1p-51) * absolute_residuals, infinity);
64 const double rounded = sum + correction;
65 const double residual = twoSumResidual(sum, correction, rounded);
66 const double previous = std::nextafter(rounded, -infinity);
67 const double following = std::nextafter(rounded, infinity);
68 if (!std::isfinite(previous) || !std::isfinite(following)) return false;
69 const double lower_radius = (rounded - previous) * 0.5;
70 const double upper_radius = (following - rounded) * 0.5;
71 const double lower = std::nextafter(residual - bound, -infinity);
72 const double upper = std::nextafter(residual + bound, infinity);
73 if (lower > -lower_radius && upper < upper_radius) {
74 result = rounded;
75 return true;
76 }
77 return false;
78}
79
80} // namespace gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
bool tryCertifiedProduct(const double *left, const double *right, int length, double &result)
double twoSumResidual(double a, double b, double rounded)