multidouble 5.2.0

Double-double, quad-double and octo-double arithmetic, scalar and 8-lane SIMD (AVX-512, AVX2, SSE2, NEON, portable) with bitwise identical results, dual numbers and LU solves
Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! `exp` and `ln` of the 8-lane types `DV`, `QV` and `OV` against the scalar functions of `DD`, `QD` and `OD`, bit for
//! bit in every lane, NaNs included, on every backend this CPU runs. Most vectors mix kinds of argument, so that their
//! lanes take different reductions k, squaring counts, Taylor degrees, branches of ln and special values in one call;
//! every fourth takes one kind in all lanes. The kinds: for exp, random over the range, near 0 (subnormal included),
//! near k ln 2 and (k + 1/2) ln 2, within 3 ulps of ln f64::MAX with tails near half an ulp, near underflow, subnormal,
//! moderate, and the special values with NaN payloads; for ln, random over the range, near 1, near sqrt(2) 2^k,
//! subnormal, within 3 ulps of f64::MAX with tails near half an ulp (the start's x/2 branch), powers of 2, moderate,
//! and the special values; with the special values, arguments off the ladder whose run makes NaNs (exp with a second
//! part far above the first's ulp, and in a release build ln of parts that cancel). Lower parts are absent, on the
//! ladder with gaps, or at the top of the ladder. Every eighth vector checks each lane's own powers of two in the exact
//! scalings: lane 0 takes e^0 or ln 1, which scale by 1, and the other lanes take arguments whose results' last parts
//! lie near the bottom of the range, where a scaling by another lane's power of two changes them (exp of one part near
//! 2^-197, ln of 1 and one or two parts near 2^-330).
//!
//! Control (feature `mutate`): with `explog::mutate::LANE_STEP`, each lane skipping the first step of its Horner sum,
//! the check fails for every type and function, and so it does with `LANE_NAN`, the lanes with a NaN part left as the
//! lanes' arithmetic gives them with their NaNs' signs flipped; with `LANE_POW2_POLY`, `LANE_POW2_FT` and
//! `LANE_POW2_SERIES`, every lane taking lane 0's powers of two in one scaling, the octo-double check fails for the
//! function that scales (the double-double and quad-double results end above the subnormals, so a wrong power of two
//! cannot show there); with each of the scalar functions' mutations, which the lanes share, it still passes.
//!
//! ```text
//! cargo test --release --test lanes_explog
//! cargo test --release --test lanes_explog --features mutate
//! ```
#![allow(clippy::needless_range_loop)]
mod common;
use common::Rng;
use multidouble::dd::DD;
use multidouble::dv::DV;
use multidouble::od::OD;
use multidouble::ov::OV;
use multidouble::qd::QD;
use multidouble::qv::QV;
use multidouble::simd::{self, Kernel, Simd};

/// vectors per function in a release build: double-double, quad-double, octo-double (a debug build runs an eighth)
const COUNT: [usize; 3] = [16000, 8000, 2400];

fn same(a: f64, b: f64) -> bool {
    a.to_bits() == b.to_bits()
}

/// 2^e for -1074 <= e <= 1023, from its bits
fn pow2i(e: i32) -> f64 {
    if e >= -1022 {
        f64::from_bits(((e + 1023) as u64) << 52)
    } else {
        f64::from_bits(1 << (e + 1074))
    }
}

/// the gap below |h| to the next f64, for an |h| that is not a power of two
fn ulp(h: f64) -> f64 {
    h.abs() - f64::from_bits(h.abs().to_bits() - 1)
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum F {
    Exp,
    Ln,
}

/// the kinds of argument of each function drawn at random; the last is the special values
const KINDS: u64 = 9;
const SPECIAL: u64 = KINDS - 1;
/// lane 0 of the scaling vectors: e^0 or ln 1, whose powers of two in the scalings are 1
const COMPANION: u64 = KINDS;
/// the other lanes of the scaling vectors (see the module doc)
const SCALING: u64 = KINDS + 1;

/// exp's special values and range ends
const EXP_SPECIAL: [f64; 13] = [
    f64::NAN,
    f64::from_bits(0x7ff8_0000_0000_1234),
    f64::from_bits(0xfff8_0000_0000_0001),
    f64::INFINITY,
    f64::NEG_INFINITY,
    0.0,
    -0.0,
    709.8,
    -745.3,
    709.79,
    -745.2,
    f64::MAX,
    -f64::MAX,
];
/// ln's special values, and 1
const LN_SPECIAL: [f64; 11] = [
    f64::NAN,
    f64::from_bits(0x7ff8_0000_0000_1234),
    f64::from_bits(0xfff8_0000_0000_0001),
    -1.0,
    -0.0,
    0.0,
    f64::INFINITY,
    f64::NEG_INFINITY,
    -f64::from_bits(1),
    -f64::MAX,
    1.0,
];

/// one value's n parts of kind `kind` (see the module doc)
fn arg(r: &mut Rng, f: F, kind: u64, n: usize) -> [f64; 8] {
    let mut p = [0.0; 8];
    let sign = |r: &mut Rng| if r.next_u64() & 1 == 0 { 1.0 } else { -1.0 };
    // a leading part near the top of the range, with a second part near half its ulp
    let edge = |r: &mut Rng, x0: f64, p: &mut [f64; 8]| {
        p[0] = f64::from_bits(x0.to_bits() - r.next_u64() % 4);
        p[1] = ulp(p[0]) / 2.0
            * match r.next_u64() % 3 {
                0 => -1.0,
                1 => 1.0 - r.unit() * pow2i(-40),
                _ => 1.0,
            };
        if n > 2 && r.next_u64() & 1 == 0 {
            p[2] = r.sym() * ulp(p[0]) * pow2i(-60 - (r.next_u64() % 40) as i32);
        }
    };
    let x0 = match (f, kind) {
        (_, SPECIAL) => {
            if r.next_u64().is_multiple_of(4) {
                // off the ladder, where the run can overflow and make NaNs: exp with a second part far above the
                // first's ulp, and in a release build ln of a value that cancels to nothing (its NaN start reaches
                // the scalar powers of two, which a debug build checks)
                if f == F::Exp {
                    p[0] = -700.0 + 1400.0 * r.unit();
                    p[1] = sign(r) * p[0].abs() * pow2i(30 + (r.next_u64() % 30) as i32);
                    return p;
                }
                if !cfg!(debug_assertions) {
                    p[0] = (1.0 + r.unit()) * pow2i(-1000 - (r.next_u64() % 22) as i32);
                    (p[1], p[2]) = (-p[0], -0.0);
                    return p;
                }
            }
            let s = if f == F::Exp { &EXP_SPECIAL[..] } else { &LN_SPECIAL[..] };
            p[0] = s[(r.next_u64() % s.len() as u64) as usize];
            return p;
        }
        (F::Exp, COMPANION) => return p,
        (F::Ln, COMPANION) => {
            p[0] = 1.0;
            return p;
        }
        (F::Exp, SCALING) => {
            // 1 + x + x^2/2 + ... in eight parts ends near 2^-1022
            p[0] = sign(r) * (1.0 + r.unit()) * pow2i(-195 - (r.next_u64() % 4) as i32);
            return p;
        }
        (F::Ln, SCALING) => {
            // ln(1 + e) = e - e^2/2 + ... has parts near 2^-330 k, so its later parts are subnormal
            let e = -320 - (r.next_u64() % 22) as i32;
            (p[0], p[1]) = (1.0, sign(r) * (1.0 + r.unit()) * pow2i(e));
            if r.next_u64() & 1 == 0 {
                p[2] = sign(r) * (1.0 + r.unit()) * pow2i(e - 53 - (r.next_u64() % 30) as i32);
            }
            return p;
        }
        (F::Exp, 0) => -745.0 + 1455.0 * r.unit(),
        (F::Exp, 1) => sign(r) * (1.0 + r.unit()) * pow2i(-((r.next_u64() % 1075) as i32)),
        (F::Exp, 2 | 3) => {
            let k = (r.next_u64() % 2048) as f64 - 1075.0 + 0.5 * (kind - 2) as f64;
            k * std::f64::consts::LN_2 * (1.0 + r.sym() * pow2i(-30 - (r.next_u64() % 23) as i32))
        }
        (F::Exp, 4) => {
            edge(r, f64::from_bits(709.782712893384f64.to_bits() + 2), &mut p);
            return p;
        }
        (F::Exp, 5) => -708.0 - 37.5 * r.unit(),
        (F::Exp, 6) => sign(r) * f64::from_bits(1 + r.next_u64() % (1 << 52)),
        (F::Exp, _) => r.sym() * pow2i(3 - (r.next_u64() % 16) as i32),
        (F::Ln, 0) => (1.0 + r.unit()) * pow2i((r.next_u64() % 2045) as i32 - 1022),
        (F::Ln, 1) => {
            // 1 itself in a quarter, with the lower parts alone
            let d = r.sym() * pow2i(-((r.next_u64() % 60) as i32) - 1);
            1.0 + if r.next_u64().is_multiple_of(4) { 0.0 } else { d }
        }
        (F::Ln, 2) => {
            let k = (r.next_u64() % 2000) as i32 - 1000;
            std::f64::consts::SQRT_2 * pow2i(k) * (1.0 + r.sym() * pow2i(-40))
        }
        (F::Ln, 3) => f64::from_bits(1 + r.next_u64() % (1 << 52)),
        (F::Ln, 4) => {
            edge(r, f64::MAX, &mut p);
            return p;
        }
        (F::Ln, 5) => {
            p[0] = pow2i((r.next_u64() % 2098) as i32 - 1074);
            return p;
        }
        (F::Ln, _) => (r.sym() * 30.0).exp(),
    };
    p[0] = x0;
    if x0.abs() < pow2i(-900) {
        return p;
    }
    match r.next_u64() % 4 {
        // no lower parts
        0 => {}
        // at the top of the ladder, |x_i| in ((2u)^i / 2, (2u)^i] |x_0|, overlapping by a bit
        1 if n > 2 => {
            for i in 1..n {
                p[i] = sign(r) * (0.5 + 0.5 * r.unit()) * x0.abs() * pow2i(-52 * i as i32);
            }
        }
        // on the ladder with gaps
        _ => {
            for i in 1..n {
                if r.next_u64().is_multiple_of(3) {
                    continue;
                }
                p[i] = r.sym() * x0.abs() * pow2i(-53 * i as i32 - (r.next_u64() % 60) as i32);
            }
        }
    }
    if n == 2 {
        // a double-word
        let d = DD::new(p[0], p[1]);
        (p[0], p[1]) = (d.hi, d.lo);
    }
    p
}

/// count vectors of n parts for f, each lane's kind with it; all scaling vectors if `scaling`
fn vectors(r: &mut Rng, f: F, n: usize, count: usize, scaling: bool) -> Vec<([[f64; 8]; 8], [u64; 8])> {
    (0..count)
        .map(|i| {
            let k0 = r.next_u64() % KINDS;
            let kinds: [u64; 8] = std::array::from_fn(|j| match (scaling || i % 8 == 5, j) {
                (true, 0) => COMPANION,
                (true, _) => SCALING,
                _ if i % 4 == 3 => k0,
                _ => r.next_u64() % KINDS,
            });
            (std::array::from_fn(|j| arg(r, f, kinds[j], n)), kinds)
        })
        .collect()
}

/// a lane type with its scalar functions, through the lanes' parts
trait Lanes<S: Simd>: Copy {
    const NAME: &'static str;
    /// parts per value
    const N: usize;
    /// index into COUNT
    const IX: usize;
    fn load(x: &[[f64; 8]; 8]) -> Self;
    fn parts(self) -> [[f64; 8]; 8];
    fn exp(self) -> Self;
    fn ln(self) -> Self;
    /// the scalar function f of one value's parts
    fn scalar(f: F, x: &[f64; 8]) -> [f64; 8];
}

impl<S: Simd> Lanes<S> for DV<S> {
    const NAME: &'static str = "dv";
    const N: usize = 2;
    const IX: usize = 0;
    fn load(x: &[[f64; 8]; 8]) -> Self {
        let (hi, lo): (Vec<f64>, Vec<f64>) = x.iter().map(|p| (p[0], p[1])).unzip();
        DV::load(&hi, &lo)
    }
    fn parts(self) -> [[f64; 8]; 8] {
        let (hi, lo) = (self.hi.to_array(), self.lo.to_array());
        std::array::from_fn(|j| [hi[j], lo[j], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
    }
    fn exp(self) -> Self {
        DV::exp(self)
    }
    fn ln(self) -> Self {
        DV::ln(self)
    }
    fn scalar(f: F, x: &[f64; 8]) -> [f64; 8] {
        let x = DD { hi: x[0], lo: x[1] };
        let y = if f == F::Exp { x.exp() } else { x.ln() };
        [y.hi, y.lo, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    }
}

impl<S: Simd> Lanes<S> for QV<S> {
    const NAME: &'static str = "qv";
    const N: usize = 4;
    const IX: usize = 1;
    fn load(x: &[[f64; 8]; 8]) -> Self {
        QV::from_lanes(&std::array::from_fn(|j| QD {
            x: [x[j][0], x[j][1], x[j][2], x[j][3]],
        }))
    }
    fn parts(self) -> [[f64; 8]; 8] {
        self.lanes()
            .map(|q| [q.x[0], q.x[1], q.x[2], q.x[3], 0.0, 0.0, 0.0, 0.0])
    }
    fn exp(self) -> Self {
        QV::exp(self)
    }
    fn ln(self) -> Self {
        QV::ln(self)
    }
    fn scalar(f: F, x: &[f64; 8]) -> [f64; 8] {
        let x = QD {
            x: [x[0], x[1], x[2], x[3]],
        };
        let y = if f == F::Exp { x.exp() } else { x.ln() };
        [y.x[0], y.x[1], y.x[2], y.x[3], 0.0, 0.0, 0.0, 0.0]
    }
}

impl<S: Simd> Lanes<S> for OV<S> {
    const NAME: &'static str = "ov";
    const N: usize = 8;
    const IX: usize = 2;
    fn load(x: &[[f64; 8]; 8]) -> Self {
        OV::from_lanes(&std::array::from_fn(|j| OD { x: x[j] }))
    }
    fn parts(self) -> [[f64; 8]; 8] {
        self.lanes().map(|o| o.x)
    }
    fn exp(self) -> Self {
        OV::exp(self)
    }
    fn ln(self) -> Self {
        OV::ln(self)
    }
    fn scalar(f: F, x: &[f64; 8]) -> [f64; 8] {
        let x = OD { x: *x };
        (if f == F::Exp { x.exp() } else { x.ln() }).x
    }
}

/// one type and function on one backend
#[derive(Debug)]
struct Row {
    what: String,
    lanes: usize,
    /// lanes that differ from the scalar function
    misses: usize,
    /// vectors with special and other lanes
    mixed: usize,
    first: Vec<String>,
}

/// V's exp and ln against the scalar functions, on COUNT[V::IX] / div vectors each, all scaling vectors if `scaling`
fn rows<S: Simd, V: Lanes<S>>(div: usize, scaling: bool) -> Vec<Row> {
    let div = if cfg!(debug_assertions) { 8 * div } else { div };
    let mut out = Vec::new();
    for (s, f) in [F::Exp, F::Ln].into_iter().enumerate() {
        let mut r = Rng(100 + 10 * V::IX as u64 + s as u64);
        let mut row = Row {
            what: format!("{} {f:?}", V::NAME),
            lanes: 0,
            misses: 0,
            mixed: 0,
            first: Vec::new(),
        };
        for (x, kinds) in vectors(&mut r, f, V::N, (COUNT[V::IX] / div).max(8), scaling) {
            let v = V::load(&x);
            let y = if f == F::Exp { v.exp() } else { v.ln() }.parts();
            let special = kinds.iter().filter(|&&k| k == SPECIAL).count();
            row.mixed += (special > 0 && special < 8) as usize;
            for j in 0..8 {
                let want = V::scalar(f, &x[j]);
                row.lanes += 1;
                if !(0..V::N).all(|i| same(y[j][i], want[i])) {
                    row.misses += 1;
                    if row.first.len() < 3 {
                        row.first.push(format!(
                            "lane {j} kind {} of {kinds:?}: x {:?} lanes {:?} scalar {:?}",
                            kinds[j],
                            &x[j][..V::N],
                            &y[j][..V::N],
                            &want[..V::N]
                        ));
                    }
                }
            }
        }
        out.push(row);
    }
    out
}

/// the three types' rows on one backend, each type on 1/div of its vectors, all scaling vectors if `scaling`
#[derive(Clone)]
struct LanesVsScalar {
    div: usize,
    scaling: bool,
}

impl Kernel for LanesVsScalar {
    type Output = Vec<Row>;
    #[inline(always)]
    fn run<S: Simd>(self) -> Vec<Row> {
        let mut rows_ = rows::<S, DV<S>>(self.div, self.scaling);
        rows_.extend(rows::<S, QV<S>>(self.div, self.scaling));
        rows_.extend(rows::<S, OV<S>>(self.div, self.scaling));
        rows_
    }
}

/// every backend's rows, each named with its backend
fn every_backend(div: usize, scaling: bool) -> Vec<(&'static str, Row)> {
    let ran = simd::for_each_available(LanesVsScalar { div, scaling });
    assert_eq!(ran.iter().map(|r| r.0).collect::<Vec<_>>(), simd::available());
    ran.into_iter()
        .flat_map(|(isa, rows)| rows.into_iter().map(move |r| (isa, r)))
        .collect()
}

#[test]
fn lanes_match_scalar_bit_for_bit() {
    common::big_stack(|| {
        let rows = every_backend(1, false);
        for (isa, r) in &rows {
            println!(
                "{isa:<8} {:<7} {:>6} lanes, {:>5} vectors mixing special and other lanes, {} differ",
                r.what, r.lanes, r.mixed, r.misses
            );
        }
        let bad: Vec<_> = rows.iter().filter(|(_, r)| r.misses > 0).collect();
        assert!(bad.is_empty(), "{bad:#?}");
        // a fair share of the vectors mixes special values with other lanes in one call
        assert!(rows.iter().all(|(_, r)| r.mixed * 4 >= r.lanes / 8), "{rows:#?}");
    });
}

/// The controls (feature `mutate`): the lanes' own breaks fail the check on every backend, the skipped Horner step and
/// the flipped NaN signs for every type and function and lane 0's powers of two for the octo-double function that
/// scales, and the scalar functions' breaks, which the lanes take alike, leave it passing.
#[cfg(feature = "mutate")]
#[test]
fn control_fires() {
    common::big_stack(|| {
        use multidouble::explog::mutate;
        for id in 1..=mutate::N {
            mutate::set(id);
            let rows = every_backend(4, false);
            mutate::set(0);
            let bad: Vec<_> = rows.iter().filter(|(_, r)| r.misses > 0).collect();
            assert!(bad.is_empty(), "under '{}': {bad:#?}", mutate::NAMES[id as usize]);
        }
        for id in [mutate::LANE_STEP, mutate::LANE_NAN] {
            mutate::set(id);
            let rows = every_backend(4, false);
            mutate::set(0);
            for (isa, r) in &rows {
                println!(
                    "control '{}': {isa:<8} {:<7} {} of {} lanes differ",
                    mutate::NAMES[id as usize],
                    r.what,
                    r.misses,
                    r.lanes
                );
            }
            let silent: Vec<_> = rows.iter().filter(|(_, r)| r.misses == 0).collect();
            assert!(silent.is_empty(), "the control did not fire: {silent:#?}");
        }
        for (id, row) in [
            (mutate::LANE_POW2_POLY, "ov Exp"),
            (mutate::LANE_POW2_FT, "ov Ln"),
            (mutate::LANE_POW2_SERIES, "ov Ln"),
        ] {
            mutate::set(id);
            let rows = every_backend(4, true);
            mutate::set(0);
            let fired: Vec<_> = rows.iter().filter(|(_, r)| r.what == row).collect();
            assert_eq!(fired.len(), simd::available().len());
            for (isa, r) in &fired {
                println!(
                    "control '{}': {isa:<8} {:<7} {} of {} lanes differ",
                    mutate::NAMES[id as usize],
                    r.what,
                    r.misses,
                    r.lanes
                );
            }
            let silent: Vec<_> = fired.iter().filter(|(_, r)| r.misses == 0).collect();
            assert!(silent.is_empty(), "the control did not fire: {silent:#?}");
        }
    });
}