| 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 | |
| 9 | use 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. |
| 13 | struct Foo; |
| 14 | |
| 15 | impl 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)] |
| 27 | fn 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 | |
| 33 | // currently requires `safe_wrappers` feature |
| 34 | fn do_something_on_neon(_level: Level) -> f32 { |
| 35 | #[cfg(all(feature = "safe_wrappers", target_arch = "aarch64"))] |
| 36 | if let Some(neon) = _level.as_neon() { |
| 37 | return neon.vectorize( |
| 38 | #[inline(always)] |
| 39 | || { |
| 40 | let v = neon.neon.vdupq_n_f32(42.0); |
| 41 | neon.neon.vgetq_lane_f32::<0>(v) |
| 42 | }, |
| 43 | ); |
| 44 | } |
| 45 | 0.0 |
| 46 | } |
| 47 | |
| 48 | fn main() { |
| 49 | let level = Level::new(); |
| 50 | let x = level.dispatch(Foo); |
| 51 | let y = dispatch!(level, simd => foo(simd, 42.0)); |
| 52 | let z = do_something_on_neon(level); |
| 53 | |
| 54 | println!("level = {level:?}, x = {x}, y = {y}, z = {z}"); |
| 55 | } |