Debian Codegraph @

README.md

raw symbols SCIP
1 <div align="center">
2
3 # Fearless SIMD
4
5 **Safer and easier SIMD**
6
7 [![Latest published version.](https://img.shields.io/crates/v/fearless_simd.svg)](https://crates.io/crates/fearless_simd)
8 [![Documentation build status.](https://img.shields.io/docsrs/fearless_simd.svg)](https://docs.rs/fearless_simd)
9 [![Apache 2.0 or MIT license.](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue.svg)](#license)
10 \
11 [![Linebender Zulip, #simd channel.](https://img.shields.io/badge/Linebender-%23simd-blue?logo=Zulip)](https://xi.zulipchat.com/#narrow/channel/514230-simd)
12 [![GitHub Actions CI status.](https://img.shields.io/github/actions/workflow/status/linebender/fearless_simd/ci.yml?logo=github&label=CI)](https://github.com/linebender/fearless_simd/actions)
13 [![Dependency staleness status.](https://deps.rs/crate/fearless_simd/latest/status.svg)](https://deps.rs/crate/fearless_simd/)
14
15 </div>
16
17 > [!CAUTION]
18 > Fearless SIMD is in extremely early experimental development. As such, there are no stability
19 > guarantees, APIs are incomplete, and architectures have missing implementations. Fearless SIMD is
20 > being developed in conjunction with the [Vello Sparse
21 > Strips](https://github.com/linebender/vello/) renderer.
22
23 <!-- We use cargo-rdme to update the README with the contents of lib.rs.
24 To edit the following section, update it in lib.rs, then run:
25 cargo rdme --workspace-project=fearless_simd --heading-base-level=0
26 Full documentation at https://github.com/orium/cargo-rdme -->
27
28 <!-- Intra-doc links used in lib.rs should be evaluated here.
29 See https://linebender.org/blog/doc-include/ for related discussion. -->
30
31 [libm]: https://crates.io/crates/libm
32 [`f32x4`]: https://docs.rs/fearless_simd/latest/fearless_simd/generated/simd_types/struct.f32x4.html
33 [`Simd`]: https://docs.rs/fearless_simd/0.2.0/fearless_simd/generated/simd_trait/trait.Simd.html
34 [`SimdFrom`]: https://docs.rs/fearless_simd/0.2.0/fearless_simd/traits/trait.SimdFrom.html
35 [SimdBase::from_slice]: https://docs.rs/fearless_simd/0.2.0/fearless_simd/generated/simd_trait/trait.SimdBase.html#tymethod.from_slice
36 [`dispatch`]: https://docs.rs/fearless_simd/0.2.0/fearless_simd/macros/macro.dispatch.html
37 [`Level`]: https://docs.rs/fearless_simd/0.2.0/fearless_simd/enum.Level.html
38 [`Level::new`]: https://docs.rs/fearless_simd/0.2.0/fearless_simd/enum.Level.html#method.new
39 [`std::simd`]: https://doc.rust-lang.org/std/simd/index.html
40 <!-- cargo-rdme start -->
41
42 A helper library to make SIMD more friendly.
43
44 Fearless SIMD exposes safe SIMD with ergonomic multi-versioning in Rust.
45
46 Fearless SIMD uses "marker values" which serve as proofs of which target features are available on the current CPU.
47 These each implement the [`Simd`] trait, which exposes a core set of SIMD operations which are implemented as
48 efficiently as possible on each target platform.
49
50 Additionally, there are types for packed vectors of a specific width and element type (such as [`f32x4`]).
51 Fearless SIMD does not currently support vectors of less than 128 bits.
52 These vector types implement some standard arithmetic traits (i.e. they can be added together using
53 `+`, multiplied by a scalar using `*`, among others), which are implemented as efficiently
54 as possible using SIMD instructions.
55 These can be created in a SIMD context using the [`SimdFrom`] trait, or the
56 [`from_slice`][SimdBase::from_slice] associated function.
57
58 To call a function with the best available target features and get the associated `Simd`
59 implementation, use the [`dispatch!()`] macro:
60
61 ```rust
62 use fearless_simd::{Level, Simd, dispatch};
63
64 #[inline(always)]
65 fn sigmoid<S: Simd>(simd: S, x: &[f32], out: &mut [f32]) { /* ... */ }
66
67 // The stored level, which you should only construct once in your application.
68 let level = Level::new();
69
70 dispatch!(level, simd => sigmoid(simd, &[/*...*/], &mut [/*...*/]));
71 ```
72
73 A few things to note:
74
75 1) `sigmoid` is generic over any `Simd` type.
76 2) The [`dispatch`] macro is used to invoke the given function with the target features associated with the supplied [`Level`].
77 3) The function or closure passed to [`dispatch!()`] should be `#[inline(always)]`.
78 The performance of the SIMD implementation may be poor if that isn't the case. See [the section on inlining for details](#inlining)
79
80 The first parameter to [`dispatch!()`] is the [`Level`].
81 If you are writing an application, you should create this once (using [`Level::new`]), and pass it to any function which wants to use SIMD.
82 This type stores which instruction sets are available for the current process, which is used
83 in the macro to dispatch to the most optimal variant of the supplied function for this process.
84
85 # Inlining
86
87 Fearless SIMD relies heavily on Rust's inlining support to create functions which have the
88 given target features enabled.
89 As such, most functions which you write when using Fearless SIMD should have the `#[inline(always)]` attribute.
90
91 There is a rule of thumb for how to achieve things in Fearless SIMD:
92
93 - All SIMD functions need `#[inline(always)]`.
94 - Use [`dispatch!`] when calling SIMD code from non-SIMD code.
95 - Use [`vectorize()`](Simd::vectorize) when calling SIMD from SIMD if you don't want to force inlining.
96
97 We currently don't have docs explaining why this is the case.
98 You can read [this Zulip conversation](https://xi.zulipchat.com/#narrow/channel/514230-simd/topic/inlining/with/546913433)
99 for some train of thought explanation.
100
101 <!--
102 TODO: Also have concrete examples of each of these.
103
104 TODO: This is a really subtle point, and we do need there to be a well-written explanation available.
105 E.g. We might want names for these, e.g.:
106
107 # Kernels vs not kernels
108
109 TODO: Talk about writing versions of functions which can be called in other `S: Simd` functions.
110 -->
111
112 # WebAssembly
113
114 WASM SIMD doesn't have feature detection, and so you need to compile two versions of your bundle for WASM, one with SIMD and one without,
115 then select the appropriate one for your user's browser. This can be done via [the `wasm-feature-detect`
116 library](https://github.com/GoogleChromeLabs/wasm-feature-detect).
117
118 You can compile WebAssembly with the SIMD128 feature enabled via the `RUSTFLAGS` environment variable
119 (`RUSTFLAGS="-Ctarget-feature=+simd128"`), or by adding the compiler flags in your [Cargo
120 config.toml](https://doc.rust-lang.org/cargo/reference/config.html):
121
122 ```toml
123 [target.'cfg(target_arch = "wasm32")']
124 rustflags = ["-Ctarget-feature=+simd128"]
125 rustdocflags = ["-Ctarget-feature=+simd128"]
126 ```
127
128 If you want to compile both SIMD and non-SIMD versions of your WebAssembly library, your best option right now is to create a shell script
129 that builds it once with the `RUSTFLAGS` specified, and once without. [Cargo currently does not allow specifying compiler flags
130 per-profile.](https://github.com/rust-lang/cargo/issues/10271)
131
132 ## Relaxed SIMD
133
134 Fearless SIMD can make use of the [relaxed SIMD](https://github.com/WebAssembly/relaxed-simd/blob/main/proposals/relaxed-simd/Overview.md)
135 WebAssembly instructions, if the requisite target feature is enabled. These instructions can return implementation-dependent results
136 depending on what is fastest on the underlying hardware. They are only used for operations where we already give hardware-dependent results.
137
138 At the time of writing, relaxed SIMD is only supported in Chrome. To make use of it, you'll need to build two versions of your library, one
139 with relaxed SIMD enabled (`RUSTFLAGS="-Ctarget-feature=+simd128,+relaxed-simd"`) and one with it disabled, and then feature-detect at
140 runtime.
141
142 # Credits
143
144 This crate was inspired by [`pulp`], [`std::simd`], among others in the Rust ecosystem, though makes many decisions differently.
145 It benefited from conversations with Luca Versari, though he is not responsible for any of the mistakes or bad decisions.
146
147 # Feature Flags
148
149 The following crate [feature flags](https://doc.rust-lang.org/cargo/reference/features.html#dependency-features) are available:
150
151 - `std` (enabled by default): Get floating point functions from the standard library (likely using your target's libc).
152 Also allows using [`Level::new`] on all platforms, to detect which target features are enabled.
153 - `libm`: Use floating point implementations from [libm].
154 - `safe_wrappers`: Include safe wrappers for (some) target feature specific intrinsics,
155 beyond the basic SIMD operations abstracted on all platforms.
156 - `force_support_fallback`: Force scalar fallback, to be supported, even if your compilation target has a better baseline.
157
158 At least one of `std` and `libm` is required; `std` overrides `libm`.
159
160 [`pulp`]: https://crates.io/crates/pulp
161
162 <!-- cargo-rdme end -->
163
164 ## Minimum supported Rust Version (MSRV)
165
166 This version of Fearless SIMD has been verified to compile with **Rust 1.88** and later.
167
168 Future versions of Fearless SIMD might increase the Rust version requirement.
169 It will not be treated as a breaking change and as such can even happen with small patch releases.
170
171 ## Community
172
173 [![Linebender Zulip, #simd channel.](https://img.shields.io/badge/Linebender-%23simd-blue?logo=Zulip)](https://xi.zulipchat.com/#narrow/channel/514230-simd)
174
175 Discussion of Fearless SIMD development happens in the [Linebender Zulip](https://xi.zulipchat.com/), specifically in [#simd](https://xi.zulipchat.com/#narrow/channel/514230-simd).
176 All public content can be read without logging in.
177
178 Contributions are welcome by pull request.
179 The [Rust code of conduct] applies.
180
181 ## License
182
183 Licensed under either of
184
185 - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
186 - MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
187
188 at your option.
189
190 [Rust Code of Conduct]: https://www.rust-lang.org/policies/code-of-conduct