edge-rs 0.3.1

Negative binomial differential expression for bulk and single-cell RNA-seq: the edgeR numerical stack and NEBULA mixed models.
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! SIMD kernels behind the `EdgeSimd` trait.
//!
//! The optimiser core (`glm::levenberg`) calls `f64::dot_simd(..)` and
//! `f64::exp_in_place_simd(..)` directly, since likelihoods and optimisers work
//! in f64 regardless of the crate's `T: EdgeFloat` data layer. The vector width
//! lives here and nowhere else; no intrinsics or `wide` types appear outside
//! this file.
//!
//! Each operation exists at four widths: scalar, 128-bit, 256-bit and 512-bit.
//! [`detect_simd_level`] caches what the machine supports and the trait impls
//! dispatch on it. A tier vectorises what it can at its own width, then hands
//! the remainder down to the tier below. That ladder matters here because the
//! hot caller is `linear_predictor`, which dots design rows of two to ten
//! elements: a single-width kernel would drop those straight to scalar.
//!
//! The reductions run one accumulator, not several. Multiple accumulators break
//! the `fadd` dependency chain and pay from a few hundred elements up, but the
//! block loop that drives them needs `lanes * accumulators` elements before it
//! can fire at all, which at two to ten coefficients it never does. Dropping the
//! four-accumulator version moved `glm_fit` by under three per cent either way
//! at two and four coefficients and made it five per cent faster at eight, so it
//! was buying code and nothing else.

use std::sync::OnceLock;

use wide::{f32x4, f32x8, f64x2, f64x4};

#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
use wide::{f32x16, f64x8};

//////////////
// Dispatch //
//////////////

/// Widest vector width available on the running machine.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SimdLevel {
    /// No vector unit worth dispatching to.
    Scalar,
    /// 128-bit. Also covers NEON, which every aarch64 target has.
    Sse,
    /// 256-bit.
    Avx2,
    /// 512-bit.
    Avx512,
}

/// Cached result of the CPU feature probe.
static SIMD_LEVEL: OnceLock<SimdLevel> = OnceLock::new();

/// Detects the widest vector width this machine supports.
///
/// The probe runs once and is cached, so the `match` in the trait impls costs a
/// load and a branch rather than a `cpuid`.
///
/// ### Returns
///
/// The [`SimdLevel`] to dispatch on.
pub fn detect_simd_level() -> SimdLevel {
    *SIMD_LEVEL.get_or_init(|| {
        #[cfg(target_arch = "x86_64")]
        {
            if is_x86_feature_detected!("avx512f") {
                return SimdLevel::Avx512;
            }
            if is_x86_feature_detected!("avx2") {
                return SimdLevel::Avx2;
            }
            if is_x86_feature_detected!("sse4.1") {
                return SimdLevel::Sse;
            }
            SimdLevel::Scalar
        }

        #[cfg(target_arch = "aarch64")]
        {
            SimdLevel::Sse
        }

        #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
        {
            SimdLevel::Scalar
        }
    })
}

/// SIMD-accelerated primitives over slices of a float type.
///
/// Implemented once per concrete float type; algorithm code never dispatches on
/// the type itself.
pub trait EdgeSimd: Sized + Copy {
    /// Dot product of two equal-length slices.
    ///
    /// ### Params
    ///
    /// * `a` - Left operand
    /// * `b` - Right operand, must be the same length as `a`
    ///
    /// ### Returns
    ///
    /// The sum of the elementwise products. Excess elements beyond `b`'s length
    /// are ignored, so a length mismatch truncates rather than panics.
    fn dot_simd(a: &[Self], b: &[Self]) -> Self;

    /// Exponentiates a slice in place.
    ///
    /// This is the `mu = exp(eta)` step of every GLM fit in the crate, so it
    /// sits on the hottest path there is.
    ///
    /// ### Params
    ///
    /// * `x` - Values to exponentiate, modified in place
    ///
    /// Note that each tier vectorises what it can and hands the remainder down,
    /// and the last tier calls libm. `wide`'s polynomial and libm disagree in
    /// the last ulp, so the same input value can come back with two different
    /// bit patterns depending on where in the slice it sits, on the slice
    /// length, and on which tier the machine dispatched to: `vec![0.37; n]`
    /// gives two distinct patterns for every odd `n`. This is elementwise rather
    /// than a reassociation, so it is a stronger caveat than the one on
    /// [`EdgeSimd::dot_simd`], but the magnitude is still one ulp and nothing in
    /// the crate depends on it. The vector lanes also flush a deep underflow to
    /// zero where libm returns a subnormal: `exp(-745.0)` is exactly 0 in a
    /// lane, 5e-324 in the tail.
    fn exp_in_place_simd(x: &mut [Self]);
}

/////////////////
// Dot product //
/////////////////

//-----------//
// f64 tiers //
//-----------//

/// Dot product of two `f64` slices, scalar.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[inline]
fn dot_f64_scalar(a: &[f64], b: &[f64]) -> f64 {
    a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}

/// Dot product of two `f64` slices, 128-bit.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[inline]
fn dot_f64_sse(a: &[f64], b: &[f64]) -> f64 {
    const LANES: usize = 2;

    let n = a.len().min(b.len());
    let mut total = f64x2::ZERO;
    let mut i = 0;

    while i + LANES <= n {
        total += f64x2::from(&a[i..i + LANES]) * f64x2::from(&b[i..i + LANES]);
        i += LANES;
    }

    total.reduce_add() + dot_f64_scalar(&a[i..n], &b[i..n])
}

/// Dot product of two `f64` slices, 256-bit.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[inline]
fn dot_f64_avx2(a: &[f64], b: &[f64]) -> f64 {
    const LANES: usize = 4;

    let n = a.len().min(b.len());
    let mut total = f64x4::ZERO;
    let mut i = 0;

    while i + LANES <= n {
        total += f64x4::from(&a[i..i + LANES]) * f64x4::from(&b[i..i + LANES]);
        i += LANES;
    }

    total.reduce_add() + dot_f64_sse(&a[i..n], &b[i..n])
}

/// Dot product of two `f64` slices, 512-bit.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
#[inline]
fn dot_f64_avx512(a: &[f64], b: &[f64]) -> f64 {
    const LANES: usize = 8;

    let n = a.len().min(b.len());
    let mut total = f64x8::ZERO;
    let mut i = 0;

    while i + LANES <= n {
        total += f64x8::from(&a[i..i + LANES]) * f64x8::from(&b[i..i + LANES]);
        i += LANES;
    }

    total.reduce_add() + dot_f64_avx2(&a[i..n], &b[i..n])
}

/// Fallback when the crate is not built with AVX-512, so `f64x8` would be two
/// 256-bit halves and buy nothing over the 256-bit tier.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx512f")))]
#[inline]
fn dot_f64_avx512(a: &[f64], b: &[f64]) -> f64 {
    dot_f64_avx2(a, b)
}

//-----------//
// f32 tiers //
//-----------//

/// Dot product of two `f32` slices, scalar.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[inline]
fn dot_f32_scalar(a: &[f32], b: &[f32]) -> f32 {
    a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}

/// Dot product of two `f32` slices, 128-bit.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[inline]
fn dot_f32_sse(a: &[f32], b: &[f32]) -> f32 {
    const LANES: usize = 4;

    let n = a.len().min(b.len());
    let mut total = f32x4::ZERO;
    let mut i = 0;

    while i + LANES <= n {
        total += f32x4::from(&a[i..i + LANES]) * f32x4::from(&b[i..i + LANES]);
        i += LANES;
    }

    total.reduce_add() + dot_f32_scalar(&a[i..n], &b[i..n])
}

/// Dot product of two `f32` slices, 256-bit.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[inline]
fn dot_f32_avx2(a: &[f32], b: &[f32]) -> f32 {
    const LANES: usize = 8;

    let n = a.len().min(b.len());
    let mut total = f32x8::ZERO;
    let mut i = 0;

    while i + LANES <= n {
        total += f32x8::from(&a[i..i + LANES]) * f32x8::from(&b[i..i + LANES]);
        i += LANES;
    }

    total.reduce_add() + dot_f32_sse(&a[i..n], &b[i..n])
}

/// Dot product of two `f32` slices, 512-bit.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
#[inline]
fn dot_f32_avx512(a: &[f32], b: &[f32]) -> f32 {
    const LANES: usize = 16;

    let n = a.len().min(b.len());
    let mut total = f32x16::ZERO;
    let mut i = 0;

    while i + LANES <= n {
        total += f32x16::from(&a[i..i + LANES]) * f32x16::from(&b[i..i + LANES]);
        i += LANES;
    }

    total.reduce_add() + dot_f32_avx2(&a[i..n], &b[i..n])
}

/// Fallback when the crate is not built with AVX-512.
///
/// ### Params
///
/// * `a` - Left operand
/// * `b` - Right operand
///
/// ### Returns
///
/// The sum of elementwise products, truncated to the shorter operand.
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx512f")))]
#[inline]
fn dot_f32_avx512(a: &[f32], b: &[f32]) -> f32 {
    dot_f32_avx2(a, b)
}

/////////////////
// Exponential //
/////////////////

//-----------//
// f64 tiers //
//-----------//

/// In-place exponential over an `f64` slice, scalar.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[inline]
fn exp_f64_scalar(x: &mut [f64]) {
    for v in x.iter_mut() {
        *v = v.exp();
    }
}

/// In-place exponential over an `f64` slice, 128-bit.
///
/// Worth taking down to two lanes: `wide`'s polynomial beats a pair of libm
/// calls, and `n_samples` in the GLM path is often odd and small.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[inline]
fn exp_f64_sse(x: &mut [f64]) {
    const LANES: usize = 2;

    let n = x.len();
    let mut i = 0;

    while i + LANES <= n {
        let v = f64x2::from(&x[i..i + LANES]).exp();
        x[i..i + LANES].copy_from_slice(v.as_array());
        i += LANES;
    }

    exp_f64_scalar(&mut x[i..]);
}

/// In-place exponential over an `f64` slice, 256-bit.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[inline]
fn exp_f64_avx2(x: &mut [f64]) {
    const LANES: usize = 4;

    let n = x.len();
    let mut i = 0;

    while i + LANES <= n {
        let v = f64x4::from(&x[i..i + LANES]).exp();
        x[i..i + LANES].copy_from_slice(v.as_array());
        i += LANES;
    }

    exp_f64_sse(&mut x[i..]);
}

/// In-place exponential over an `f64` slice, 512-bit.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
#[inline]
fn exp_f64_avx512(x: &mut [f64]) {
    const LANES: usize = 8;

    let n = x.len();
    let mut i = 0;

    while i + LANES <= n {
        let v = f64x8::from(&x[i..i + LANES]).exp();
        x[i..i + LANES].copy_from_slice(v.as_array());
        i += LANES;
    }

    exp_f64_avx2(&mut x[i..]);
}

/// Fallback when the crate is not built with AVX-512.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx512f")))]
#[inline]
fn exp_f64_avx512(x: &mut [f64]) {
    exp_f64_avx2(x)
}

//-----------//
// f32 tiers //
//-----------//

/// In-place exponential over an `f32` slice, scalar.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[inline]
fn exp_f32_scalar(x: &mut [f32]) {
    for v in x.iter_mut() {
        *v = v.exp();
    }
}

/// In-place exponential over an `f32` slice, 128-bit.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[inline]
fn exp_f32_sse(x: &mut [f32]) {
    const LANES: usize = 4;

    let n = x.len();
    let mut i = 0;

    while i + LANES <= n {
        let v = f32x4::from(&x[i..i + LANES]).exp();
        x[i..i + LANES].copy_from_slice(v.as_array());
        i += LANES;
    }

    exp_f32_scalar(&mut x[i..]);
}

/// In-place exponential over an `f32` slice, 256-bit.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[inline]
fn exp_f32_avx2(x: &mut [f32]) {
    const LANES: usize = 8;

    let n = x.len();
    let mut i = 0;

    while i + LANES <= n {
        let v = f32x8::from(&x[i..i + LANES]).exp();
        x[i..i + LANES].copy_from_slice(v.as_array());
        i += LANES;
    }

    exp_f32_sse(&mut x[i..]);
}

/// In-place exponential over an `f32` slice, 512-bit.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
#[inline]
fn exp_f32_avx512(x: &mut [f32]) {
    const LANES: usize = 16;

    let n = x.len();
    let mut i = 0;

    while i + LANES <= n {
        let v = f32x16::from(&x[i..i + LANES]).exp();
        x[i..i + LANES].copy_from_slice(v.as_array());
        i += LANES;
    }

    exp_f32_avx2(&mut x[i..]);
}

/// Fallback when the crate is not built with AVX-512.
///
/// ### Params
///
/// * `x` - Values to exponentiate
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx512f")))]
#[inline]
fn exp_f32_avx512(x: &mut [f32]) {
    exp_f32_avx2(x)
}

/////////////////////
// Implementations //
/////////////////////

impl EdgeSimd for f64 {
    #[inline]
    fn dot_simd(a: &[f64], b: &[f64]) -> f64 {
        match detect_simd_level() {
            SimdLevel::Avx512 => dot_f64_avx512(a, b),
            SimdLevel::Avx2 => dot_f64_avx2(a, b),
            SimdLevel::Sse => dot_f64_sse(a, b),
            SimdLevel::Scalar => dot_f64_scalar(a, b),
        }
    }

    #[inline]
    fn exp_in_place_simd(x: &mut [f64]) {
        match detect_simd_level() {
            SimdLevel::Avx512 => exp_f64_avx512(x),
            SimdLevel::Avx2 => exp_f64_avx2(x),
            SimdLevel::Sse => exp_f64_sse(x),
            SimdLevel::Scalar => exp_f64_scalar(x),
        }
    }
}

impl EdgeSimd for f32 {
    #[inline]
    fn dot_simd(a: &[f32], b: &[f32]) -> f32 {
        match detect_simd_level() {
            SimdLevel::Avx512 => dot_f32_avx512(a, b),
            SimdLevel::Avx2 => dot_f32_avx2(a, b),
            SimdLevel::Sse => dot_f32_sse(a, b),
            SimdLevel::Scalar => dot_f32_scalar(a, b),
        }
    }

    #[inline]
    fn exp_in_place_simd(x: &mut [f32]) {
        match detect_simd_level() {
            SimdLevel::Avx512 => exp_f32_avx512(x),
            SimdLevel::Avx2 => exp_f32_avx2(x),
            SimdLevel::Sse => exp_f32_sse(x),
            SimdLevel::Scalar => exp_f32_scalar(x),
        }
    }
}

///////////
// Tests //
///////////

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;

    /// Lengths chosen to straddle every rung of the ladder: below a vector, one
    /// vector, and vectors plus awkward remainders that force the delegation
    /// down through 256, 128 and scalar.
    const SIZES: [usize; 16] = [0, 1, 2, 3, 5, 6, 7, 8, 9, 15, 16, 17, 31, 32, 33, 45];

    fn ramp_f64(n: usize) -> Vec<f64> {
        (0..n).map(|i| (i as f64) * 0.5 - 3.0).collect()
    }

    fn ramp_f32(n: usize) -> Vec<f32> {
        (0..n).map(|i| (i as f32) * 0.5 - 3.0).collect()
    }

    /// Every tier must agree with the scalar reference. Dispatch on this host
    /// only ever reaches one tier, so the tier functions are called directly.
    #[test]
    fn test_dot_tiers_match_scalar_f64() {
        for n in SIZES {
            let a = ramp_f64(n);
            let b: Vec<f64> = ramp_f64(n).iter().map(|v| v * 1.7 + 0.3).collect();
            let expected = dot_f64_scalar(&a, &b);
            assert_relative_eq!(dot_f64_sse(&a, &b), expected, epsilon = 1e-12);
            assert_relative_eq!(dot_f64_avx2(&a, &b), expected, epsilon = 1e-12);
            assert_relative_eq!(dot_f64_avx512(&a, &b), expected, epsilon = 1e-12);
            assert_relative_eq!(f64::dot_simd(&a, &b), expected, epsilon = 1e-12);
        }
    }

    #[test]
    fn test_dot_tiers_match_scalar_f32() {
        for n in SIZES {
            let a = ramp_f32(n);
            let b: Vec<f32> = ramp_f32(n).iter().map(|v| v * 1.7 + 0.3).collect();
            let expected = dot_f32_scalar(&a, &b);
            assert_relative_eq!(dot_f32_sse(&a, &b), expected, epsilon = 1e-4);
            assert_relative_eq!(dot_f32_avx2(&a, &b), expected, epsilon = 1e-4);
            assert_relative_eq!(dot_f32_avx512(&a, &b), expected, epsilon = 1e-4);
            assert_relative_eq!(f32::dot_simd(&a, &b), expected, epsilon = 1e-4);
        }
    }

    #[test]
    fn test_exp_tiers_match_scalar() {
        for n in SIZES {
            let mut expected = ramp_f64(n);
            exp_f64_scalar(&mut expected);

            for kernel in [
                exp_f64_sse as fn(&mut [f64]),
                exp_f64_avx2,
                exp_f64_avx512,
                f64::exp_in_place_simd,
            ] {
                let mut x = ramp_f64(n);
                kernel(&mut x);
                for (got, want) in x.iter().zip(expected.iter()) {
                    assert_relative_eq!(got, want, max_relative = 1e-12);
                }
            }

            let mut expected = ramp_f32(n);
            exp_f32_scalar(&mut expected);

            for kernel in [
                exp_f32_sse as fn(&mut [f32]),
                exp_f32_avx2,
                exp_f32_avx512,
                f32::exp_in_place_simd,
            ] {
                let mut x = ramp_f32(n);
                kernel(&mut x);
                for (got, want) in x.iter().zip(expected.iter()) {
                    assert_relative_eq!(got, want, max_relative = 1e-5);
                }
            }
        }
    }

    /// A dot product of unequal lengths truncates to the shorter operand rather
    /// than reading out of bounds. Documented behaviour, and the delegation has
    /// to preserve it at every rung, so pin all tiers.
    #[test]
    fn test_dot_truncates_on_length_mismatch() {
        let a = vec![1.0_f64; 20];
        let b = vec![2.0_f64; 5];
        assert_relative_eq!(dot_f64_scalar(&a, &b), 10.0, epsilon = 1e-12);
        assert_relative_eq!(dot_f64_sse(&a, &b), 10.0, epsilon = 1e-12);
        assert_relative_eq!(dot_f64_avx2(&a, &b), 10.0, epsilon = 1e-12);
        assert_relative_eq!(dot_f64_avx512(&a, &b), 10.0, epsilon = 1e-12);
        assert_relative_eq!(f64::dot_simd(&a, &b), 10.0, epsilon = 1e-12);
    }

    /// The probe is cached, so repeated calls must agree.
    #[test]
    fn test_detect_simd_level_is_stable() {
        assert_eq!(detect_simd_level(), detect_simd_level());
    }
}