1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! # Chapter 11.1 — Error-free transformations
//!
//! ## Purpose
//! The three primitives of the crate. Each rewrites the exact result of one
//! `f64` operation as a pair of `f64` values: the rounded result and the
//! rounding error, which — the fact everything else rests on — is itself an
//! `f64`.
//!
//! ## Notation
//! `F` is the set of `f64` values, `RN(·)` rounding to nearest (ties to
//! even), `u = 2⁻⁵³`, `η = 2⁻¹⁰⁷⁴` the smallest positive subnormal, `Ω` the
//! largest finite `f64`. For binary64 the precision is `p = 53` and the
//! minimal exponent `e_min = −1022`.
//!
//! ## Algorithms
//! **Algorithm S (TwoSum; Knuth, after Møller).** Input `a, b ∈ F`.
//!
//! S1. `s ← RN(a + b)`.
//! S2. `a′ ← RN(s − b)`; `b′ ← RN(s − a′)`.
//! S3. `δ_a ← RN(a − a′)`; `δ_b ← RN(b − b′)`.
//! S4. `e ← RN(δ_a + δ_b)`. Return `(s, e)`.
//!
//! Six operations, no branch, no condition on the operands.
//!
//! **Algorithm F (Fast2Sum; Dekker).** Input `a, b ∈ F` with `e_a ≥ e_b`
//! (the exponents; a zero has the least one), which holds in particular
//! when `|a| ≥ |b|` or `a = 0` — the form in which the requirement is
//! usually stated. The weaker form matters: under cancellation the
//! additions of Chapter 11.2 call F with `|a| < |b|` in the same binade
//! (`a = −2⁻⁵²`, `b ≈ −1.23 · 2⁻⁵²` in a test of this crate), where the
//! proofs of Joldes, Muller & Popescu rely on the exponent condition.
//!
//! F1. `s ← RN(a + b)`.
//! F2. `z ← RN(s − a)`.
//! F3. `e ← RN(b − z)`. Return `(s, e)`.
//!
//! **Algorithm P (2ProdFMA).** Input `a, b ∈ F`.
//!
//! P1. `p ← RN(a b)`.
//! P2. `e ← RN(a b − p)`, one fused multiply-add. Return `(p, e)`.
//!
//! ## Invariants
//! (T1) *Sums.* If `s` is finite, Algorithm F returns `s = RN(a + b)` and
//! `s + e = a + b` exactly, and so does Algorithm S unless `|a| = Ω`
//! — also when subnormal numbers occur: an `f64` addition whose
//! result is subnormal is exact, so the rounding error of a sum is
//! always representable. The exception: with `a = ±Ω` step S2 can
//! overflow although S1 did not (`a′ = RN(a + δ)` for the rounding
//! error `δ` of S1, which reaches `ulp(Ω)/2`: `a = Ω`, `b = −3 ·
//! 2⁹⁷⁰`), and `e` is then not finite. `DoubleDouble::from_sum` and
//! the additions of Chapter 11.2 detect it (their step E).
//! (T2) *Products.* If `p` is finite and `a b = 0` or `|a b| ≥ 2⁻⁹⁶⁹`,
//! Algorithm P returns `p = RN(a b)` and `p + e = a b` exactly. The
//! exact condition is `e_a + e_b ≥ e_min + p − 1 = −970` on the
//! exponents of `a` and `b` (the error `a b − p` is a multiple of
//! `2^(e_a + e_b − 2p + 2)`, which must be at least `η`); `|a b| ≥
//! 2⁻⁹⁶⁹` implies it, and it can fail for any smaller bound. Otherwise
//! `e` is the *rounded* error and `|a b − (p + e)| ≤ η/2`.
//! (T3) *Normalized.* In (T1), and in (T2) when the product is exact, `|e|
//! ≤ ulp(s)/2` (resp. `ulp(p)/2`) and `RN(s + e) = s`: the pair is a
//! normalized double-double (invariant D1 of Chapter 11.2). When the
//! error of a product is *rounded* (below the range of (T2)) it may
//! become exactly `±ulp(p)/2` with `p` odd, and `(p, e)` is then not
//! normalized: `DoubleDouble::from_product` adds one Fast2Sum, which
//! is exact there (T1).
//! (T4) *Non-finite.* If `s` (resp. `p`) is infinite or NaN, `e` is NaN or
//! infinite and carries no information; the callers of this crate
//! test the leading component (invariant D2).
//!
//! ## References
//! Møller, "Quasi double-precision in floating point addition", BIT 5
//! (1965); Knuth, TAOCP vol. 2, §4.2.2, Theorem B; Dekker, "A
//! floating-point technique for extending the available precision", Numer.
//! Math. 18 (1971); Ogita, Rump & Oishi, "Accurate sum and dot product",
//! SIAM J. Sci. Comput. 26 (2005), Theorem 3.4 and Algorithm 3.5
//! (TwoProductFMA); Joldes, Muller & Popescu, ACM TOMS 44 (2017),
//! Algorithms 1–3 (the condition of (T2) is that of their Fast2Mult);
//! Muller et al., *Handbook of Floating-Point Arithmetic*, 2nd ed. (2018),
//! §4.3–4.4.
/// `(s, e)` with `s = fl(a + b)` and `s + e = a + b` exactly (Knuth's
/// TwoSum: 6 flops, no branch, no condition on the operands). Exact unless
/// `s` overflows — or `a = ±Ω`, where an intermediate difference can
/// overflow and leave `e` non-finite (invariant T1 of Chapter 11.1).
/// The binary exponent field of `x`, subnormals and zeros sharing the
/// least one.
/// As [`two_sum`] in 3 flops, **requires** `|a| ≥ |b|` or `a = 0` (Dekker's
/// Fast2Sum); the requirement is a `debug_assert!`.
///
/// Precisely, the algorithm is exact whenever the exponent of `a` is at
/// least that of `b` (Algorithm F of Chapter 11.1), and that — the weaker
/// condition, which `|a| ≥ |b|` implies — is what the assertion tests.
// the precondition is an exact comparison
/// `(p, e)` with `p = fl(a · b)` and `p + e = a · b` exactly, by one fused
/// multiply-add. Exact when `a b = 0` or `|a b| ≥ 2⁻⁹⁶⁹` and `p` does not
/// overflow; otherwise `|a b − (p + e)| ≤ η`.
// L2.4 (Chapter 11.11): the tests may use the libm of `f64` (clippy.toml)
// the tests compare exact values