Loading...
Searching...
No Matches
gpr::linalg::ExactSum Class Reference

#include <gpr/linalg/ExactSum.h>

Public Member Functions

 ExactSum () noexcept
void add (double x) noexcept
void addProduct (double a, double b) noexcept
void addTripleProduct (double a, double b, double c) noexcept
void assignLimbs (const std::int64_t *digits) noexcept
void merge (const ExactSum &other) noexcept
 Adds another accumulator exactly.
void normalize () noexcept
std::int64_t * limbs () noexcept
 Canonical digits for message passing: normalize(), then limbs().
const std::int64_t * limbs () const noexcept
double nonfinite () const noexcept
 Sum of the non-finite addends (0 when every addend was finite).
void setNonfinite (double value) noexcept
double value () const noexcept
 The exact sum rounded once to the nearest double, ties to even.

Static Public Attributes

static constexpr int kLimbs = 169
static constexpr int kOrigin = 2148
 Bit 0 has weight 2^-kOrigin.
static constexpr int kDoubleShift = 1074
 A finite double's least bit lies this many bits above bit 0.

Detailed Description

Definition at line 34 of file ExactSum.h.

Constructor & Destructor Documentation

◆ ExactSum()

gpr::linalg::ExactSum::ExactSum ( )
inlinenoexcept

Definition at line 47 of file ExactSum.h.

48 {
49 std::memset(limb_, 0, sizeof(limb_));
50 }

Member Function Documentation

◆ add()

void gpr::linalg::ExactSum::add ( double x)
inlinenoexcept

Adds a double exactly. Non-finite addends are tracked separately and propagate to value().

Definition at line 54 of file ExactSum.h.

55 {
56 const Factor factor = factorOf(x);
57 if (factor.nonfinite) {
58 nonfinite_ += x;
59 return;
60 }
61 addMagnitude(factor.significand, factor.lsb + kDoubleShift,
62 factor.negative);
63 }
@ x
Definition Enums.h:28
static constexpr int kDoubleShift
A finite double's least bit lies this many bits above bit 0.
Definition ExactSum.h:45

◆ addProduct()

void gpr::linalg::ExactSum::addProduct ( double a,
double b )
inlinenoexcept

Adds the exact product a * b. The integer significands are multiplied in full, including when fl(a*b) overflows or underflows. A rounded product of 2^1023 with itself is infinite, and the TwoProduct residual is then not a binary64; the significand product still cancels and leaves a representable sum such as 2^-1074.

Definition at line 70 of file ExactSum.h.

71 {
72 const Factor left = factorOf(a);
73 const Factor right = factorOf(b);
74 if (left.nonfinite || right.nonfinite) {
75 nonfinite_ += a * b;
76 return;
77 }
78 addMagnitude(left.significand * right.significand, left.lsb + right.lsb,
79 left.negative != right.negative);
80 }

◆ addTripleProduct()

void gpr::linalg::ExactSum::addTripleProduct ( double a,
double b,
double c )
inlinenoexcept

Adds the exact product a * b * c. The three integer significands are multiplied in full. A rounded a * b that overflows is infinite, and the TwoProduct residual is then not a binary64, so the product is not formed in binary64 first.

Definition at line 86 of file ExactSum.h.

87 {
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;
93 return;
94 }
95 const ExactWide product = left.significand * middle.significand;
96 ExactWide low_magnitude =
97 (product & static_cast<ExactWide>(~std::uint64_t{0})) *
98 right.significand;
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;
104 // Bits below the origin are not stored. A nonzero residue stays as a
105 // sticky bit at the origin so a later rounding still sees it. The
106 // limb window holds every finite triple product, so a product that
107 // still does not fit is the only one reported as infinite here.
108 const auto align = [](ExactWide& magnitude, int& bit) noexcept {
109 if (magnitude == 0) return true;
110 if (bit < 0) {
111 const int drop = -bit;
112 bool sticky = true;
113 if (drop < 128) {
114 sticky = (magnitude & ((ExactWide{1} << drop) - 1)) != 0;
115 magnitude >>= drop;
116 } else {
117 magnitude = 0;
118 }
119 if (sticky) magnitude |= ExactWide{1};
120 bit = 0;
121 if (magnitude == 0) return true;
122 }
123 const int digit = bit >> 5;
124 return digit >= 0 && digit + 4 < kLimbs;
125 };
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();
130 return;
131 }
132 addMagnitude(low_magnitude, low_bit, negative);
133 addMagnitude(high_magnitude, high_bit, negative);
134 }
static constexpr int kLimbs
Definition ExactSum.h:41

◆ assignLimbs()

void gpr::linalg::ExactSum::assignLimbs ( const std::int64_t * digits)
inlinenoexcept

Replaces the digits with canonical digits received from message passing (see limbs()).

Definition at line 138 of file ExactSum.h.

139 {
140 std::memcpy(limb_, digits, sizeof(limb_));
141 pending_ = 0;
142 normalize();
143 }
void normalize() noexcept
Definition ExactSum.h:159

◆ limbs() [1/2]

const std::int64_t * gpr::linalg::ExactSum::limbs ( ) const
inlinenoexcept

Definition at line 174 of file ExactSum.h.

175 {
176 return limb_;
177 }

◆ limbs() [2/2]

std::int64_t * gpr::linalg::ExactSum::limbs ( )
inlinenoexcept

Canonical digits for message passing: normalize(), then limbs().

Definition at line 170 of file ExactSum.h.

171 {
172 return limb_;
173 }

◆ merge()

void gpr::linalg::ExactSum::merge ( const ExactSum & other)
inlinenoexcept

Adds another accumulator exactly.

Definition at line 146 of file ExactSum.h.

147 {
148 normalize();
149 ExactSum copy = other;
150 copy.normalize();
151 for (int k = 0; k < kLimbs; ++k)
152 limb_[k] += copy.limb_[k];
153 nonfinite_ += copy.nonfinite_;
154 normalize();
155 }
ExactSum() noexcept
Definition ExactSum.h:47

◆ nonfinite()

double gpr::linalg::ExactSum::nonfinite ( ) const
inlinenoexcept

Sum of the non-finite addends (0 when every addend was finite).

Definition at line 180 of file ExactSum.h.

181 {
182 return nonfinite_;
183 }

◆ normalize()

void gpr::linalg::ExactSum::normalize ( )
inlinenoexcept

Carry-propagates so that digits 0..kLimbs-2 lie in [0, 2^32). The representation is then canonical: equal sums have equal digits.

Definition at line 159 of file ExactSum.h.

160 {
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;
165 }
166 pending_ = 0;
167 }

◆ setNonfinite()

void gpr::linalg::ExactSum::setNonfinite ( double value)
inlinenoexcept

Definition at line 184 of file ExactSum.h.

185 {
186 nonfinite_ = value;
187 }
double value() const noexcept
The exact sum rounded once to the nearest double, ties to even.
Definition ExactSum.h:190

◆ value()

double gpr::linalg::ExactSum::value ( ) const
inlinenoexcept

The exact sum rounded once to the nearest double, ties to even.

Definition at line 190 of file ExactSum.h.

191 {
192 if (nonfinite_ != 0.0 || std::isnan(nonfinite_)) return nonfinite_;
193 ExactSum work = *this;
194 work.normalize();
195 bool negative = work.limb_[kLimbs - 1] < 0;
196 if (negative) {
197 for (int k = 0; k < kLimbs; ++k)
198 work.limb_[k] = -work.limb_[k];
199 work.normalize();
200 }
201 int top = kLimbs - 1;
202 while (top >= 0 && work.limb_[top] == 0)
203 --top;
204 if (top < 0) return 0.0;
205 int top_bit = 63;
206 while (!((static_cast<std::uint64_t>(work.limb_[top]) >> top_bit) & 1u))
207 --top_bit;
208 const long msb = 32L * top + top_bit; // weight 2^(msb - kOrigin)
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]) >>
212 (index & 31)) &
213 1u;
214 };
215 // A subnormal double has quantum 2^-1074. Rounding to a 53-bit
216 // significand and then again inside ldexp is one ulp low, so the
217 // quantum decision is made once, ties to even.
218 constexpr long kSubnormalLimit = kOrigin - 1022;
219 if (msb < kSubnormalLimit) {
220 std::uint64_t quantum = 0;
221 if (msb >= kDoubleShift) {
222 for (long i = msb; i >= kDoubleShift; --i)
223 quantum = (quantum << 1) | bit(i);
224 const std::uint64_t round_bit = bit(kDoubleShift - 1);
225 bool sticky = false;
226 for (long i = kDoubleShift - 2; i >= 0 && !sticky; --i)
227 sticky = bit(i) != 0;
228 if (round_bit && (sticky || (quantum & 1u))) ++quantum;
229 } else if (msb == kDoubleShift - 1) {
230 bool sticky = false;
231 for (long i = msb - 1; i >= 0 && !sticky; --i)
232 sticky = bit(i) != 0;
233 if (sticky) quantum = 1;
234 }
235 const double magnitude =
236 std::ldexp(static_cast<double>(quantum), -kDoubleShift);
237 return negative ? -magnitude : magnitude;
238 }
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);
243 bool sticky = false;
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;
251 }
static constexpr int kOrigin
Bit 0 has weight 2^-kOrigin.
Definition ExactSum.h:43

Member Data Documentation

◆ kDoubleShift

int gpr::linalg::ExactSum::kDoubleShift = 1074
staticconstexpr

A finite double's least bit lies this many bits above bit 0.

Definition at line 45 of file ExactSum.h.

◆ kLimbs

int gpr::linalg::ExactSum::kLimbs = 169
staticconstexpr

Digits of 32 bits; digit k has weight 2^(32 k - 2148). Bit 0 is the product of two minimum subnormals. A product of two maximum finite doubles sits below 2^2048. A product of three sits below 2^3072, and a long sum of those products stays below 2^3104. Digits above that window absorb carries and the sign.

Definition at line 41 of file ExactSum.h.

◆ kOrigin

int gpr::linalg::ExactSum::kOrigin = 2148
staticconstexpr

Bit 0 has weight 2^-kOrigin.

Definition at line 43 of file ExactSum.h.


The documentation for this class was generated from the following file: