Skip to main content

play/
play.rs

1// Copyright 2024 the Fearless_SIMD Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4#![expect(
5    missing_docs,
6    reason = "TODO: https://github.com/linebender/fearless_simd/issues/40"
7)]
8
9use fearless_simd::{Level, dispatch, prelude::*};
10
11// The WithSimd idea is adapted from pulp but is clunky; we
12// will probably prefer the `dispatch!` macro.
13struct Foo;
14
15impl WithSimd for Foo {
16    type Output = f32;
17
18    #[inline(always)]
19    fn with_simd<S: Simd>(self, simd: S) -> Self::Output {
20        let a = simd.splat_f32x4(42.0);
21        let b = a + a;
22        b[0]
23    }
24}
25
26#[inline(always)]
27fn foo<S: Simd>(simd: S, x: f32) -> f32 {
28    let n = S::f32s::N;
29    println!("n = {n}");
30    simd.splat_f32x4(x).sqrt()[0]
31}
32
33fn main() {
34    let level = Level::new();
35    let x = level.dispatch(Foo);
36    let y = dispatch!(level, simd => foo(simd, 42.0));
37
38    println!("level = {level:?}, x = {x}, y = {y}");
39}