#array #simd #generic #numeric

no-std numeric-array

Wrapper around generic-array that adds efficient numeric trait implementations

20 releases

0.6.1 Jul 5, 2024
0.5.2 Oct 7, 2020
0.5.1 Aug 31, 2020
0.5.0 Apr 11, 2020
0.1.6 Sep 22, 2017

#399 in Data structures

Download history 253/week @ 2024-08-06 225/week @ 2024-08-13 185/week @ 2024-08-20 285/week @ 2024-08-27 274/week @ 2024-09-03 307/week @ 2024-09-10 316/week @ 2024-09-17 336/week @ 2024-09-24 209/week @ 2024-10-01 94/week @ 2024-10-08 183/week @ 2024-10-15 182/week @ 2024-10-22 250/week @ 2024-10-29 215/week @ 2024-11-05 254/week @ 2024-11-12 174/week @ 2024-11-19

926 downloads per month
Used in 14 crates (3 directly)

MIT/Apache

59KB
1.5K SLoC

numeric-array

crates.io Documentation MIT/Apache-2 licensed

numeric-array is a wrapper around generic-array that adds efficient numeric trait implementations, designed to encourage LLVM to autovectorize expressions into SIMD instructions and perform compile-time evaluation.

All stable core::ops traits are implemented for NumericArray itself, plus the thin NumericConstant type, which is required to differentiate 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:

use num_traits::Float;
use numeric_array::{NumericArray, narr};

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 autovectorize 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.

This is ideal for situations where simple component-wise operations are required for arrays.

Dependencies

~320–580KB
~13K SLoC