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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//! Error-free transformations: floating-point sums and products together with
//! their exact rounding errors.
//!
//! Every function returns a pair `(x, y)` where `x` is the correctly rounded
//! floating-point result of the operation and `y` its rounding error, so that
//! `x + y` equals the real result exactly. This holds under the requirement
//! documented on each function, assuming the default round-to-nearest-even
//! mode. NaN and infinite inputs are not supported.
//!
//! The algorithms are those of Knuth, Dekker and Veltkamp as presented by
//! Shewchuk, "Adaptive Precision Floating-Point Arithmetic and Fast Robust
//! Geometric Predicates" (1997). When the target has a hardware fused
//! multiply-add (see [`HAS_FMA`]), the exact product uses it instead.
// On 32-bit x86 without SSE2, `f64` arithmetic runs on the x87 unit, whose
// excess precision and double rounding break every error-free
// transformation here.
compile_error!;
/// Selects code by whether the target has a hardware fused multiply-add that
/// the crate can use: x86_64 compiled with the `fma` target feature, or
/// aarch64 with the `neon` target feature. This is the single definition of
/// that condition.
///
/// - `fma_target!(has)` is `true` or `false` according to the condition;
/// - `fma_target!(item)` keeps `item` only when the condition holds;
/// - `fma_target!(if { a } else { b })` evaluates `a` when the condition
/// holds and `b` otherwise; only the chosen expression is compiled.
/// True when `two_product` uses a fused multiply-add on this target.
///
/// That is the case on x86_64 when compiled with the `fma` target feature
/// (for example with `-C target-feature=+fma` or `-C target-cpu=native` on a
/// CPU that has it), and on aarch64 with the `neon` target feature, which
/// every aarch64 target enables by default except the soft-float ones.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::HAS_FMA;
///
/// if HAS_FMA {
/// // `two_product` and `two_square` use `two_product_fma`.
/// }
/// ```
pub const HAS_FMA: bool = fma_target!;
/// `2^-480`, the smallest nonzero magnitude in the product domain. See
/// [`in_product_domain`].
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::PRODUCT_OPERAND_MIN;
///
/// assert_eq!(PRODUCT_OPERAND_MIN, 2f64.powi(-480));
/// ```
pub const PRODUCT_OPERAND_MIN: f64 = f64from_bits;
/// `2^480`, the largest magnitude in the product domain. See
/// [`in_product_domain`].
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::PRODUCT_OPERAND_MAX;
///
/// assert_eq!(PRODUCT_OPERAND_MAX, 2f64.powi(480));
/// ```
pub const PRODUCT_OPERAND_MAX: f64 = f64from_bits;
/// Returns `true` if `x` lies in the *product domain*: it is zero of either
/// sign, or its magnitude is in
/// [`PRODUCT_OPERAND_MIN`]` ..= `[`PRODUCT_OPERAND_MAX`], that is
/// `2^-480 ..= 2^480`.
///
/// Every operand of the exact products ([`two_product`],
/// [`two_product_dekker`], `two_product_fma` and [`two_square`]) must lie in
/// this domain, where neither the product nor its rounding error can
/// overflow or underflow. Outside it the error they return can be inexact,
/// without any other symptom. Debug builds check the domain on every exact
/// product and panic outside it; release builds do not check.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::{PRODUCT_OPERAND_MAX, PRODUCT_OPERAND_MIN, in_product_domain};
///
/// assert!(in_product_domain(-0.0));
/// assert!(in_product_domain(PRODUCT_OPERAND_MIN) && in_product_domain(-PRODUCT_OPERAND_MAX));
/// assert!(!in_product_domain(PRODUCT_OPERAND_MIN / 2.0));
/// assert!(!in_product_domain(f64::MAX));
/// assert!(!in_product_domain(f64::NAN));
/// ```
/// `2^27 + 1`: multiplying by it splits an `f64` into two halves of at most
/// 26 significant bits each (Veltkamp's splitting).
const SPLITTER: f64 = 134_217_729.0;
/// Computes `a + b` and its rounding error (Knuth's two-sum).
///
/// Requirement: `a + b` does not overflow.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::two_sum;
///
/// let (x, y) = two_sum(1.0, 1e-20);
/// assert_eq!(x, 1.0);
/// assert_eq!(y, 1e-20);
/// ```
/// Computes `a + b` and its rounding error, faster than [`two_sum`] when the
/// magnitudes are known to be ordered (Dekker's fast-two-sum).
///
/// Requirement: `|a| >= |b|` or `a == 0`, and `a + b` does not overflow.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::fast_two_sum;
///
/// let (x, y) = fast_two_sum(1.0, 1e-20);
/// assert_eq!(x, 1.0);
/// assert_eq!(y, 1e-20);
/// ```
/// Computes `a - b` and its rounding error.
///
/// Requirement: `a - b` does not overflow.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::two_diff;
///
/// let (x, y) = two_diff(1.0, 1e-20);
/// assert_eq!(x, 1.0);
/// assert_eq!(y, -1e-20);
/// ```
/// Computes `a * b` and its rounding error with Dekker's algorithm, using
/// only additions and multiplications.
///
/// Requirement: `a` and `b` lie in the product domain (see
/// [`in_product_domain`]); debug builds panic if they do not.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::two_product_dekker;
///
/// let a = 1.0 + f64::EPSILON;
/// let (x, y) = two_product_dekker(a, a);
/// assert_eq!(x, 1.0 + 2.0 * f64::EPSILON);
/// assert_eq!(y, f64::EPSILON * f64::EPSILON);
/// ```
fma_target!
/// Computes `a * b` and its rounding error with the crate's selected
/// implementation: `two_product_fma` when [`HAS_FMA`] is true, and
/// [`two_product_dekker`] otherwise. Both give equal values.
///
/// Requirement: `a` and `b` lie in the product domain (see
/// [`in_product_domain`]); debug builds panic if they do not.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::two_product;
///
/// let a = 1.0 + f64::EPSILON;
/// let (x, y) = two_product(a, 1.0 - f64::EPSILON);
/// assert_eq!(x, 1.0);
/// assert_eq!(y, -f64::EPSILON * f64::EPSILON);
/// ```
/// Computes `a * a` and its rounding error: with `two_product_fma` when
/// [`HAS_FMA`] is true, and otherwise with fewer operations than
/// [`two_product_dekker`].
///
/// Requirement: `a` lies in the product domain (see [`in_product_domain`]);
/// debug builds panic if it does not.
///
/// # Examples
///
/// ```
/// use implicit_predicates_core::eft::two_square;
///
/// let a = 1.0 + f64::EPSILON;
/// let (x, y) = two_square(a);
/// assert_eq!(x, 1.0 + 2.0 * f64::EPSILON);
/// assert_eq!(y, f64::EPSILON * f64::EPSILON);
/// ```
/// Panics in debug builds unless both operands of an exact product lie in
/// the product domain.
/// Veltkamp's splitting: returns `(hi, lo)` with `hi + lo == a` exactly and
/// each half fitting in 26 significant bits, so that products of halves are
/// exact. Requires `|a| * SPLITTER` not to overflow.