23#ifndef GPR_LINALG_EXACT_SUM_H_
24#define GPR_LINALG_EXACT_SUM_H_
49 std::memset(limb_, 0,
sizeof(limb_));
54 void add(
double x)
noexcept
56 const Factor factor = factorOf(
x);
57 if (factor.nonfinite) {
61 addMagnitude(factor.significand, factor.lsb +
kDoubleShift,
72 const Factor left = factorOf(a);
73 const Factor right = factorOf(b);
74 if (left.nonfinite || right.nonfinite) {
78 addMagnitude(left.significand * right.significand, left.lsb + right.lsb,
79 left.negative != right.negative);
88 const Factor left = factorOf(a);
89 const Factor middle = factorOf(b);
90 const Factor right = factorOf(c);
91 if (left.nonfinite || middle.nonfinite || right.nonfinite) {
92 nonfinite_ += a * b * c;
95 const ExactWide product = left.significand * middle.significand;
96 ExactWide low_magnitude =
97 (product &
static_cast<ExactWide
>(
~std::uint64_t{0})) *
99 ExactWide high_magnitude = (product >> 64) * right.significand;
100 int low_bit = left.lsb + middle.lsb + right.lsb -
kDoubleShift;
101 int high_bit = low_bit + 64;
102 const bool negative =
103 (left.negative != middle.negative) != right.negative;
108 const auto align = [](ExactWide& magnitude,
int& bit)
noexcept {
109 if (magnitude == 0)
return true;
111 const int drop = -bit;
114 sticky = (magnitude & ((ExactWide{1} << drop) - 1)) != 0;
119 if (sticky) magnitude |= ExactWide{1};
121 if (magnitude == 0)
return true;
123 const int digit = bit >> 5;
124 return digit >= 0 && digit + 4 <
kLimbs;
126 if (!align(low_magnitude, low_bit) ||
127 !align(high_magnitude, high_bit)) {
128 nonfinite_ += negative ? -std::numeric_limits<double>::infinity()
129 : std::numeric_limits<double>::infinity();
132 addMagnitude(low_magnitude, low_bit, negative);
133 addMagnitude(high_magnitude, high_bit, negative);
140 std::memcpy(limb_, digits,
sizeof(limb_));
151 for (
int k = 0; k <
kLimbs; ++k)
152 limb_[k] += copy.limb_[k];
153 nonfinite_ += copy.nonfinite_;
161 for (
int k = 0; k + 1 <
kLimbs; ++k) {
162 const std::int64_t carry = limb_[k] >> 32;
163 limb_[k] -= carry * (std::int64_t{1} << 32);
164 limb_[k + 1] += carry;
174 const std::int64_t*
limbs() const noexcept
192 if (nonfinite_ != 0.0 || std::isnan(nonfinite_))
return nonfinite_;
195 bool negative = work.limb_[
kLimbs - 1] < 0;
197 for (
int k = 0; k <
kLimbs; ++k)
198 work.limb_[k] = -work.limb_[k];
202 while (top >= 0 && work.limb_[top] == 0)
204 if (top < 0)
return 0.0;
206 while (!((
static_cast<std::uint64_t
>(work.limb_[top]) >> top_bit) & 1u))
208 const long msb = 32L * top + top_bit;
209 const auto bit = [&](
long index) -> std::uint64_t {
210 if (index < 0)
return 0;
211 return (
static_cast<std::uint64_t
>(work.limb_[index >> 5]) >>
218 constexpr long kSubnormalLimit =
kOrigin - 1022;
219 if (msb < kSubnormalLimit) {
227 sticky = bit(i) != 0;
231 for (
long i = msb - 1; i >= 0 && !sticky; --i)
232 sticky = bit(i) != 0;
235 const double magnitude =
237 return negative ? -magnitude : magnitude;
239 std::uint64_t mantissa = 0;
240 for (
long i = msb; i > msb - 53; --i)
241 mantissa = (mantissa << 1) | bit(i);
242 const std::uint64_t round_bit = bit(msb - 53);
244 for (
long i = msb - 54; i >= 0 && !sticky; --i)
245 sticky = bit(i) != 0;
246 if (round_bit && (sticky || (mantissa & 1u))) ++mantissa;
247 const long scale = msb - 52 -
kOrigin;
248 const double magnitude =
249 std::ldexp(
static_cast<double>(mantissa),
static_cast<int>(scale));
250 return negative ? -magnitude : magnitude;
256 __extension__
typedef unsigned __int128 ExactWide;
259 ExactWide significand = 0;
261 bool negative =
false;
265 static Factor factorOf(
double x)
noexcept
268 std::uint64_t bits = 0;
269 std::memcpy(&bits, &
x,
sizeof(bits));
270 factor.negative = (bits >> 63) != 0;
271 const std::uint64_t exponent = (bits >> 52) & 0x7ffu;
272 if (exponent == 0x7ffu) {
273 factor.nonfinite =
true;
276 std::uint64_t mantissa = bits & ((std::uint64_t{1} << 52) - 1);
278 mantissa |= std::uint64_t{1} << 52;
279 factor.lsb =
static_cast<int>(exponent) - 1;
281 factor.significand = mantissa;
286 void addMagnitude(ExactWide magnitude,
int bit,
bool negative)
noexcept
288 if (magnitude == 0)
return;
289 std::uint32_t piece[4];
290 for (
int i = 0; i < 4; ++i) {
291 piece[i] =
static_cast<std::uint32_t
>(magnitude & 0xffffffffu);
294 const unsigned shift =
static_cast<unsigned>(bit & 31);
295 const int digit = bit >> 5;
296 std::uint64_t spill = 0;
297 for (
int i = 0; i < 4; ++i) {
298 const std::uint64_t wide =
299 (
static_cast<std::uint64_t
>(piece[i]) << shift) | spill;
300 const std::int64_t chunk =
301 static_cast<std::int64_t
>(wide & 0xffffffffu);
304 limb_[digit + i] -= chunk;
306 limb_[digit + i] += chunk;
309 const std::int64_t chunk =
static_cast<std::int64_t
>(spill);
311 limb_[digit + 4] -= chunk;
313 limb_[digit + 4] += chunk;
315 if (++pending_ >= kNormalizeInterval)
normalize();
318 static constexpr int kNormalizeInterval = 1 << 28;
320 std::int64_t limb_[
kLimbs];
321 double nonfinite_ = 0.0;
void addProduct(double a, double b) noexcept
double nonfinite() const noexcept
Sum of the non-finite addends (0 when every addend was finite).
void assignLimbs(const std::int64_t *digits) noexcept
void add(double x) noexcept
double value() const noexcept
The exact sum rounded once to the nearest double, ties to even.
const std::int64_t * limbs() const noexcept
void addTripleProduct(double a, double b, double c) noexcept
static constexpr int kOrigin
Bit 0 has weight 2^-kOrigin.
void setNonfinite(double value) noexcept
std::int64_t * limbs() noexcept
Canonical digits for message passing: normalize(), then limbs().
static constexpr int kLimbs
static constexpr int kDoubleShift
A finite double's least bit lies this many bits above bit 0.
void normalize() noexcept
void merge(const ExactSum &other) noexcept
Adds another accumulator exactly.
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.