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
705
706
707
708
709
710
711
//! `numeric-array` is a wrapper around

//! [`generic-array`](https://github.com/fizyk20/generic-array) that adds

//! efficient numeric trait implementations, often times making use of

//! SIMD instructions and compile-time evaluations.

//!

//! All stable `core::ops` traits are implemented for `NumericArray` itself,

//! plus the thin `NumericConstant` type, which is required to

//! differeniate constant values from `NumericArray` itself.

//!

//! Additionally, most of `num_traits` are implemented,

//! including `Num` itself. So you can even use a whole array as a generic number.

//!

//! Example:

//!

//! ```rust

//! extern crate num_traits;

//! #[macro_use]

//! extern crate generic_array;

//! #[macro_use]

//! extern crate numeric_array;

//!

//! use num_traits::Float;

//! use numeric_array::NumericArray;

//!

//! fn main() {

//!     let a = narr![f32; 1, 2, 3, 4];

//!     let b = narr![f32; 5, 6, 7, 8];

//!     let c = narr![f32; 9, 1, 2, 3];

//!

//!     // Compiles to a single vfmadd213ps instruction on my machine

//!     let d = a.mul_add(b, c);

//!

//!     assert_eq!(d, narr![f32; 14, 13, 23, 35]);

//! }

//! ```

//!

//! When used with `RUSTFLAGS = "-C opt-level=3 -C target-cpu=native"`,

//! then Rust and LLVM are smart enough to turn almost all operations

//! into SIMD instructions, or even just evaluate them at compile time.

//! The above example is actually evaluated at compile time,

//! so if you were to view the assembly it would show the result only.

//! Rust is pretty smart.

//!

//! Therefore, this is ideal for situations where simple component-wise

//! operations are required for arrays.

//!


#![deny(missing_docs)]
#![no_std]

extern crate num_traits;

#[cfg_attr(test, macro_use)]
extern crate generic_array;

pub use generic_array::{typenum, ArrayLength};

#[cfg(feature = "serde1")]
extern crate serde;

use core::{cmp, ptr, slice};

use core::borrow::{Borrow, BorrowMut};
use core::ops::{Deref, DerefMut, Index, IndexMut};
use core::ops::{Range, RangeFrom, RangeFull, RangeTo};

use core::iter::FromIterator;

use core::fmt::{Debug, Formatter, Result as FmtResult};

use generic_array::functional::*;
use generic_array::sequence::*;
use generic_array::{GenericArray, GenericArrayIter};

#[cfg(feature = "serde1")]
mod impl_serde;

pub mod geometry;
pub mod impls;
pub mod simd;

/// A numeric wrapper for a `GenericArray`, allowing for easy numerical operations

/// on the whole sequence.

///

/// This has the added bonus of allowing SIMD optimizations for almost all operations

/// when compiled with `RUSTFLAGS = "-C opt-level=3 -C target-cpu=native"`

///

/// For example, adding together four-element `NumericArray`'s will result

/// in a single SIMD instruction for all elements at once.

#[repr(transparent)]
pub struct NumericArray<T, N: ArrayLength<T>>(GenericArray<T, N>);

/// Sugar for `NumericArray::new(arr![...])`

///

/// ```ignore

/// #[macro_use]

/// extern crate generic_array;

/// ```

///

/// is required to use this, as it still uses the `arr!` macro internally.

#[macro_export]
macro_rules! narr {
    ($($t:tt)*) => {
        $crate::NumericArray::new(arr!($($t)*))
    }
}

unsafe impl<T, N: ArrayLength<T>> GenericSequence<T> for NumericArray<T, N> {
    type Length = N;
    type Sequence = Self;

    fn generate<F>(f: F) -> Self
    where
        F: FnMut(usize) -> T,
    {
        NumericArray(GenericArray::generate(f))
    }
}

/// This is required to allow `NumericArray` to be operated on by both other `NumericArray`

/// instances and constants, with generic types,

/// because some type `U` supercedes `NumericArray<U, N>`

///

/// As a result, constants must be wrapped in this totally

/// transparent wrapper type to differentiate the types to Rust.

#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct NumericConstant<T>(pub T);

/// Creates a new `NumericConstant` from the given expression.

#[macro_export]
macro_rules! nconstant {
    ($value:expr) => {
        $crate::NumericConstant($value)
    };
}

impl<T> Deref for NumericConstant<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T> DerefMut for NumericConstant<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T: Debug, N: ArrayLength<T>> Debug for NumericArray<T, N> {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.debug_tuple("NumericArray").field(&self.0).finish()
    }
}

impl<X, T, N: ArrayLength<T>> From<X> for NumericArray<T, N>
where
    X: Into<GenericArray<T, N>>,
{
    fn from(x: X) -> NumericArray<T, N> {
        NumericArray::new(x.into())
    }
}

impl<T: Clone, N: ArrayLength<T>> Clone for NumericArray<T, N> {
    fn clone(&self) -> NumericArray<T, N> {
        NumericArray(self.0.clone())
    }
}

impl<T: Copy, N: ArrayLength<T>> Copy for NumericArray<T, N> where N::ArrayType: Copy {}

impl<T, N: ArrayLength<T>> Deref for NumericArray<T, N> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl<T, N: ArrayLength<T>> DerefMut for NumericArray<T, N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_slice()
    }
}

impl<T, U, N: ArrayLength<T> + ArrayLength<U>> PartialEq<NumericArray<U, N>> for NumericArray<T, N>
where
    T: PartialEq<U>,
{
    fn eq(&self, rhs: &NumericArray<U, N>) -> bool {
        **self == **rhs
    }
}

impl<T, U, N: ArrayLength<T> + ArrayLength<U>> PartialEq<GenericArray<U, N>> for NumericArray<T, N>
where
    T: PartialEq<U>,
{
    fn eq(&self, rhs: &GenericArray<U, N>) -> bool {
        **self == **rhs
    }
}

impl<T, N: ArrayLength<T>> cmp::Eq for NumericArray<T, N> where T: cmp::Eq {}

impl<T, N: ArrayLength<T>> PartialOrd<Self> for NumericArray<T, N>
where
    T: PartialOrd,
{
    #[inline]
    fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
        PartialOrd::partial_cmp(&self.0, &rhs.0)
    }

    #[inline]
    fn lt(&self, rhs: &Self) -> bool {
        PartialOrd::lt(&self.0, &rhs.0)
    }

    #[inline]
    fn le(&self, rhs: &Self) -> bool {
        PartialOrd::le(&self.0, &rhs.0)
    }

    #[inline]
    fn gt(&self, rhs: &Self) -> bool {
        PartialOrd::gt(&self.0, &rhs.0)
    }

    #[inline]
    fn ge(&self, rhs: &Self) -> bool {
        PartialOrd::ge(&self.0, &rhs.0)
    }
}

impl<T, N: ArrayLength<T>> PartialOrd<GenericArray<T, N>> for NumericArray<T, N>
where
    T: PartialOrd,
{
    #[inline]
    fn partial_cmp(&self, rhs: &GenericArray<T, N>) -> Option<cmp::Ordering> {
        PartialOrd::partial_cmp(&self.0, rhs)
    }

    #[inline]
    fn lt(&self, rhs: &GenericArray<T, N>) -> bool {
        PartialOrd::lt(&self.0, rhs)
    }

    #[inline]
    fn le(&self, rhs: &GenericArray<T, N>) -> bool {
        PartialOrd::le(&self.0, rhs)
    }

    #[inline]
    fn gt(&self, rhs: &GenericArray<T, N>) -> bool {
        PartialOrd::gt(&self.0, rhs)
    }

    #[inline]
    fn ge(&self, rhs: &GenericArray<T, N>) -> bool {
        PartialOrd::ge(&self.0, rhs)
    }
}

impl<T, N: ArrayLength<T>> cmp::Ord for NumericArray<T, N>
where
    T: cmp::Ord,
{
    #[inline]
    fn cmp(&self, rhs: &Self) -> cmp::Ordering {
        cmp::Ord::cmp(&self.0, &rhs.0)
    }
}

impl<T, N: ArrayLength<T>> NumericArray<T, N> {
    /// Creates a new `NumericArray` instance from a `GenericArray` instance.

    ///

    /// Example:

    ///

    /// ```

    /// #[macro_use]

    /// extern crate generic_array;

    /// extern crate numeric_array;

    ///

    /// use numeric_array::NumericArray;

    ///

    /// fn main() {

    ///     let arr = NumericArray::new(arr![i32; 1, 2, 3, 4]);

    ///

    ///     println!("{:?}", arr); // Prints 'NumericArray([1, 2, 3, 4])'

    /// }

    /// ```

    #[inline]
    pub fn new(arr: GenericArray<T, N>) -> NumericArray<T, N> {
        NumericArray(arr)
    }

    /// Creates a new array filled with a single value.

    ///

    /// Example:

    ///

    /// ```ignore

    /// let a = NumericArray::new(arr![i32; 5, 5, 5, 5]);

    /// let b = NumericArray::splat(5);

    ///

    /// assert_eq!(a, b);

    /// ```

    #[inline]
    pub fn splat(t: T) -> NumericArray<T, N>
    where
        T: Clone,
    {
        NumericArray(GenericArray::generate(|_| t.clone()))
    }

    /// Convert all elements of the `NumericArray` to another `NumericArray` using `From`

    pub fn convert<U: From<T>>(self) -> NumericArray<U, N>
    where
        N: ArrayLength<U>,
    {
        self.0.map(From::from).into()
    }

    /// Consumes self and returns the internal `GenericArray` instance

    #[inline]
    pub fn into_array(self) -> GenericArray<T, N> {
        self.0
    }

    /// Get reference to underlying `GenericArray` instance.

    #[inline]
    pub fn as_array(&self) -> &GenericArray<T, N> {
        &self.0
    }

    /// Get mutable reference to underlying `GenericArray` instance.

    #[inline]
    pub fn as_mut_array(&mut self) -> &mut GenericArray<T, N> {
        &mut self.0
    }

    /// Extracts a slice containing the entire array.

    #[inline]
    pub fn as_slice(&self) -> &[T] {
        &self.0
    }

    /// Extracts a mutable slice containing the entire array.

    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.0
    }

    /// Converts slice to a numeric array reference with inferred length;

    ///

    /// Length of the slice must be equal to the length of the array.

    #[inline]
    pub fn from_slice(slice: &[T]) -> &NumericArray<T, N> {
        slice.into()
    }

    /// Converts mutable slice to a mutable numeric array reference

    ///

    /// Length of the slice must be equal to the length of the array.

    #[inline]
    pub fn from_mut_slice(slice: &mut [T]) -> &mut NumericArray<T, N> {
        slice.into()
    }
}

use core::ops::Sub;
use typenum::{bit::B1 as True, Diff, IsGreaterOrEqual};

impl<T, N: ArrayLength<T>> NumericArray<T, N> {
    /// Offset the numeric array and cast it into a shorter array

    #[inline(always)]
    pub fn offset<V: ArrayLength<T>, O: ArrayLength<T>>(&self) -> &NumericArray<T, V>
    where
        N: Sub<O>,
        Diff<N, O>: IsGreaterOrEqual<V, Output = True>,
    {
        unsafe { &*((self as *const _ as *const T).add(O::USIZE) as *const NumericArray<T, V>) }
    }

    /// Offset the numeric array and cast it into a shorter array

    #[inline(always)]
    pub fn offset_mut<V: ArrayLength<T>, O: ArrayLength<T>>(&mut self) -> &mut NumericArray<T, V>
    where
        N: Sub<O>,
        Diff<N, O>: IsGreaterOrEqual<V, Output = True>,
    {
        unsafe { &mut *((self as *mut _ as *mut T).add(O::USIZE) as *mut NumericArray<T, V>) }
    }
}

impl<'a, T, N: ArrayLength<T>> From<&'a [T]> for &'a NumericArray<T, N> {
    /// Converts slice to a numeric array reference with inferred length;

    ///

    /// Length of the slice must be equal to the length of the array.

    #[inline]
    fn from(slice: &[T]) -> &NumericArray<T, N> {
        debug_assert_eq!(slice.len(), N::to_usize());

        unsafe { &*(slice.as_ptr() as *const NumericArray<T, N>) }
    }
}

impl<'a, T, N: ArrayLength<T>> From<&'a mut [T]> for &'a mut NumericArray<T, N> {
    /// Converts mutable slice to a mutable numeric array reference

    ///

    /// Length of the slice must be equal to the length of the array.

    #[inline]
    fn from(slice: &mut [T]) -> &mut NumericArray<T, N> {
        debug_assert_eq!(slice.len(), N::to_usize());

        unsafe { &mut *(slice.as_mut_ptr() as *mut NumericArray<T, N>) }
    }
}

impl<T, N: ArrayLength<T>> AsRef<[T]> for NumericArray<T, N> {
    fn as_ref(&self) -> &[T] {
        self
    }
}

impl<T, N: ArrayLength<T>> Borrow<[T]> for NumericArray<T, N> {
    fn borrow(&self) -> &[T] {
        self
    }
}

impl<T, N: ArrayLength<T>> AsMut<[T]> for NumericArray<T, N> {
    fn as_mut(&mut self) -> &mut [T] {
        self
    }
}

impl<T, N: ArrayLength<T>> BorrowMut<[T]> for NumericArray<T, N> {
    fn borrow_mut(&mut self) -> &mut [T] {
        self
    }
}

impl<T, N: ArrayLength<T>> Index<usize> for NumericArray<T, N> {
    type Output = T;

    #[inline(always)]
    fn index(&self, index: usize) -> &T {
        self.0.index(index)
    }
}

impl<T, N: ArrayLength<T>> IndexMut<usize> for NumericArray<T, N> {
    #[inline(always)]
    fn index_mut(&mut self, index: usize) -> &mut T {
        self.0.index_mut(index)
    }
}

impl<T, N: ArrayLength<T>> Index<Range<usize>> for NumericArray<T, N> {
    type Output = [T];

    #[inline(always)]
    fn index(&self, index: Range<usize>) -> &[T] {
        self.0.index(index)
    }
}

impl<T, N: ArrayLength<T>> IndexMut<Range<usize>> for NumericArray<T, N> {
    #[inline(always)]
    fn index_mut(&mut self, index: Range<usize>) -> &mut [T] {
        self.0.index_mut(index)
    }
}

impl<T, N: ArrayLength<T>> Index<RangeTo<usize>> for NumericArray<T, N> {
    type Output = [T];

    #[inline(always)]
    fn index(&self, index: RangeTo<usize>) -> &[T] {
        self.0.index(index)
    }
}

impl<T, N: ArrayLength<T>> IndexMut<RangeTo<usize>> for NumericArray<T, N> {
    #[inline(always)]
    fn index_mut(&mut self, index: RangeTo<usize>) -> &mut [T] {
        self.0.index_mut(index)
    }
}

impl<T, N: ArrayLength<T>> Index<RangeFrom<usize>> for NumericArray<T, N> {
    type Output = [T];

    #[inline(always)]
    fn index(&self, index: RangeFrom<usize>) -> &[T] {
        self.0.index(index)
    }
}

impl<T, N: ArrayLength<T>> IndexMut<RangeFrom<usize>> for NumericArray<T, N> {
    #[inline(always)]
    fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut [T] {
        self.0.index_mut(index)
    }
}

impl<T, N: ArrayLength<T>> Index<RangeFull> for NumericArray<T, N> {
    type Output = [T];

    #[inline(always)]
    fn index(&self, _index: RangeFull) -> &[T] {
        self
    }
}

impl<T, N: ArrayLength<T>> IndexMut<RangeFull> for NumericArray<T, N> {
    #[inline(always)]
    fn index_mut(&mut self, _index: RangeFull) -> &mut [T] {
        self
    }
}

impl<'a, T, N: ArrayLength<T>> IntoIterator for &'a NumericArray<T, N> {
    type Item = &'a T;
    type IntoIter = slice::Iter<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T, N: ArrayLength<T>> IntoIterator for &'a mut NumericArray<T, N> {
    type Item = &'a mut T;
    type IntoIter = slice::IterMut<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<T, N: ArrayLength<T>> IntoIterator for NumericArray<T, N> {
    type Item = T;
    type IntoIter = GenericArrayIter<T, N>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<T, N: ArrayLength<T>> FromIterator<T> for NumericArray<T, N> {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        NumericArray(GenericArray::from_iter(iter))
    }
}

impl<T, N: ArrayLength<T>> Default for NumericArray<T, N>
where
    T: Default,
{
    fn default() -> Self {
        NumericArray(GenericArray::default())
    }
}

#[cfg(test)]
pub mod tests {
    // This stops the compiler from optimizing based on known data, only data types.

    #[inline(never)]
    pub fn black_box<T>(val: T) -> T {
        use core::{mem, ptr};

        let ret = unsafe { ptr::read_volatile(&val) };
        mem::forget(val);
        ret
    }

    #[test]
    fn test_ops() {
        let a = black_box(narr![i32; 1, 3, 5, 7]);
        let b = black_box(narr![i32; 2, 4, 6, 8]);

        let c = a + b;
        let d = c * nconstant!(black_box(5));
        let e = d << nconstant!(1_usize);

        assert_eq!(e, narr![i32; 30, 70, 110, 150])
    }

    #[test]
    fn test_constants() {
        let a = black_box(narr![i32; 1, 3, 5, 7]);
        let b = black_box(narr![i32; 2, 4, 6, 8]);

        let c = a + b * nconstant!(2);

        assert_eq!(c, narr![i32; 5, 11, 17, 23]);
    }

    #[test]
    fn test_floats() {
        let a = black_box(narr![f32; 1.0, 3.0, 5.0, 7.0]);
        let b = black_box(narr![f32; 2.0, 4.0, 6.0, 8.0]);

        let c = a + b;

        black_box(c);
    }

    #[test]
    fn test_other() {
        use num_traits::Saturating;

        let a = black_box(narr![i32; 1, 3, 5, 7]);
        let b = black_box(narr![i32; 2, 4, 6, 8]);

        let c = a.saturating_add(b);

        black_box(c);
    }

    #[test]
    fn test_atan2() {
        use num_traits::Float;

        let a = black_box(narr![f32; 1, 2, 3, 4]);
        let b = black_box(narr![f32; 2, 3, 4, 5]);

        let c = a.atan2(b);

        assert_eq!(c, narr![f32; 0.4636476, 0.5880026, 0.6435011, 0.67474097]);
    }

    #[test]
    fn test_classify() {
        use core::num::FpCategory;
        use num_traits::Float;

        let nan = f32::nan();
        let infinity = f32::infinity();

        let any_nan = black_box(narr![f32; 1, 2, nan, 0]);
        let any_infinite = black_box(narr![f32; 1, infinity, 2, 3]);
        let any_mixed = black_box(narr![f32; 1, infinity, nan, 0]);
        let all_normal = black_box(narr![f32; 1, 2, 3, 4]);
        let all_zero = black_box(narr![f32; 0, 0, 0, 0]);

        let non_zero = black_box(narr![f32; 0, 1, 0, 0]);

        assert_eq!(any_nan.classify(), FpCategory::Nan);
        assert_eq!(any_mixed.classify(), FpCategory::Nan);
        assert_eq!(any_infinite.classify(), FpCategory::Infinite);
        assert_eq!(all_normal.classify(), FpCategory::Normal);
        assert_eq!(all_zero.classify(), FpCategory::Zero);

        assert_eq!(non_zero.classify(), FpCategory::Normal);

        assert_eq!(any_nan.is_infinite(), false);
        assert_eq!(any_mixed.is_infinite(), true);
        assert_eq!(any_nan.is_nan(), true);
        assert_eq!(any_mixed.is_nan(), true);
        assert_eq!(any_infinite.is_nan(), false);
    }

    #[test]
    fn test_tanh() {
        use num_traits::Float;

        let a = black_box(narr![f32; 1, 2, 3, 4]);

        black_box(a.tanh());
    }

    #[test]
    pub fn test_madd() {
        use num_traits::Float;

        let a = black_box(narr![f32; 1, 2, 3, 4]);
        let b = black_box(narr![f32; 5, 6, 7, 8]);
        let c = black_box(narr![f32; 9, 1, 2, 3]);

        let d = a.mul_add(b, c);

        assert_eq!(d, narr![f32; 14, 13, 23, 35]);
    }

    #[test]
    #[no_mangle]
    pub fn test_select() {
        use simd::Select;

        let mask = black_box(narr![bool; true, false, false, true]);

        let a = black_box(narr![i32; 1, 2, 3, 4]);
        let b = black_box(narr![i32; 5, 6, 7, 8]);

        // Compiles to vblendvps

        let selected = mask.select(a, b);

        assert_eq!(selected, narr![i32; 1, 6, 7, 4]);
    }
}