46 releases (17 breaking)

0.18.10 Apr 12, 2024
0.18.9 Mar 15, 2024
0.18.8 Feb 14, 2024
0.18.6 Nov 19, 2023
0.10.3 Nov 28, 2022

#32 in Hardware support

Download history 7960/week @ 2024-01-05 9407/week @ 2024-01-12 8564/week @ 2024-01-19 10327/week @ 2024-01-26 9993/week @ 2024-02-02 12101/week @ 2024-02-09 11517/week @ 2024-02-16 11916/week @ 2024-02-23 15284/week @ 2024-03-01 13539/week @ 2024-03-08 17273/week @ 2024-03-15 20134/week @ 2024-03-22 17766/week @ 2024-03-29 15622/week @ 2024-04-05 17099/week @ 2024-04-12 13516/week @ 2024-04-19

66,873 downloads per month
Used in 108 crates (19 directly)

MIT license

1MB
25K SLoC

pulp is a safe abstraction over SIMD instructions, that allows you to write a function once and dispatch to equivalent vectorized versions based on the features detected at runtime.

Documentation Crate

Autovectorization example

use pulp::Arch;

let mut v = (0..1000).map(|i| i as f64).collect::<Vec<_>>();
let arch = Arch::new();

arch.dispatch(|| {
    for x in &mut v {
        *x *= 2.0;
    }
});

for (i, x) in v.into_iter().enumerate() {
    assert_eq!(x, 2.0 * i as f64);
}

Manual vectorization example

use pulp::{Arch, Simd, WithSimd};

struct TimesThree<'a>(&'a mut [f64]);
impl<'a> WithSimd for TimesThree<'a> {
    type Output = ();

    #[inline(always)]
    fn with_simd<S: Simd>(self, simd: S) -> Self::Output {
        let v = self.0;
        let (head, tail) = S::f64s_as_mut_simd(v);

        let three = simd.f64s_splat(3.0);
        for x in head {
            *x = simd.f64s_mul(three, *x);
        }

        for x in tail {
            *x = *x * 3.0;
        }
    }
}

let mut v = (0..1000).map(|i| i as f64).collect::<Vec<_>>();
let arch = Arch::new();

arch.dispatch(TimesThree(&mut v));

for (i, x) in v.into_iter().enumerate() {
    assert_eq!(x, 3.0 * i as f64);
}

Less boilerplate using pulp::with_simd

Only available with the macro feature.

Requires the first non-lifetime generic parameter, as well as the function's first input parameter to be the SIMD type.

#[pulp::with_simd(sum = pulp::Arch::new())]
#[inline(always)]
fn sum_with_simd<'a, S: Simd>(simd: S, v: &'a mut [f64]) {
    let (head, tail) = S::f64s_as_mut_simd(v);
    let three = simd.f64s_splat(3.0);
    for x in head {
        *x = simd.f64s_mul(three, *x);
    }
    for x in tail {
        *x = *x * 3.0;
    }
}

let mut v = (0..1000).map(|i| i as f64).collect::<Vec<_>>();
sum(&mut v);

for (i, x) in v.into_iter().enumerate() {
    assert_eq!(x, 3.0 * i as f64);
}

Dependencies

~1MB
~19K SLoC