Enum Level
#[non_exhaustive]
pub enum Level
The level enum with the specific SIMD capabilities available.
The contained values serve as a proof that the associated target feature is available.
Variants
-
Sse2(Sse2) The SSE2 instruction set on (32 and 64 bit) x86.
This is the baseline for i686 and x86-64 targets.
-
Sse4_2(Sse4_2) The SSE4.2 instruction set on (32 and 64 bit) x86, plus
popcntandcmpxchg16b. Also known as x86-64-v2.All production CPUs with SSE4.2 also support the other two extensions, so it is safe to require them.
-
Avx512(Avx512) Ice Lake-class AVX-512 on (32 and 64 bit) x86.
-
Avx2(Avx2) The x86-64-v3 instruction set on (32 and 64 bit) x86, including AVX2 and FMA.
Implementations
impl Level
fn new() -> SelfReturn the best SIMD level available on the CPU. This value should be passed to
dispatch.On x86 and x86-64 targets, this detects the available CPU features on the first call and caches the result. Other targets return their strongest statically supported level. This may change in the future if runtime-detected levels for other platforms are added.
This function requires the standard library on targets other than wasm32. On wasm32, the available level is known statically, so the standard library isn't required.
On x86-64, it is sometimes possible to detect the available features on
#[no_std]by parsing the output ofcpuidinstruction, but this function does not do that. If you do this, you can create the SIMD token viaassume_supportedand then get the level from it.Libraries that use SIMD on
#[no_std]should let the user pass the appropriate SIMD level the user detected through other means (e.g.cpuid), to avoid using the fallback level when a better SIMD level is available in hardware.fn try_detect() -> Option<Self>Get the target feature level suitable for this run.
Should be used in libraries if they wish to handle the case where target features cannot be detected at runtime. Most users should prefer
new. This is discussed in more detail innew's documentation.fn is_fallback(self) -> boolCheck whether this is the
Fallbacklevel; that is, whether no better feature level could be statically or dynamically detected. This is useful if there's a scalarized version of your algorithm that runs faster if SIMD isn't supported.This method is always available, even when the fallback backend is not compiled. In that case, it always returns
false.fn as_sse2(self) -> Option<Sse2>If this is a proof that SSE2 (or better) is available, access that instruction set.
See
Sse2::assume_supportedfor the exact list of CPU features this token enables.This method should be preferred over matching against the
Sse2variant of self, because if the CPU supports a superset of SSE2 (e.g. SSE4.2, AVX2, or AVX-512), this method will return the SSE2 token even if that "better" instruction set is available.This can be used in combination with the [kernel] macro to safely access level-specific SIMD intrinsics.
fn as_sse4_2(self) -> Option<Sse4_2>If this is a proof that x86-64-v2 feature set (or better) is available, access that instruction set.
See
Sse4_2::assume_supportedfor the exact list of CPU features this token enables.This method should be preferred over matching against the
Sse4_2variant of self, because if the CPU supports a superset of SSE4.2 (e.g. AVX2 or AVX-512), this method will return the SSE4.2 token even if that "better" instruction set is available.This can be used in combination with the [kernel] macro to safely access level-specific SIMD intrinsics.
fn as_avx2(self) -> Option<Avx2>If this is a proof that the x86-64-v3 feature set (or better) is available, access that instruction set.
See
Avx2::assume_supportedfor the exact list of CPU features this token enables.This method should be preferred over matching against the
Avx2variant of self, because if the CPU supports a superset of AVX2 (e.g. AVX-512), this method will return the AVX2 token even if that "better" instruction set is available.This can be used in combination with the [kernel] macro to safely access level-specific SIMD intrinsics.
fn as_avx512(self) -> Option<Avx512>If this is a proof that the Ice Lake AVX-512 feature set is available, access that instruction set.
See
Avx512::assume_supportedfor the exact list of CPU features this token enables.This can be used in combination with the [kernel] macro to safely access level-specific SIMD intrinsics.
const fn baseline() -> SelfGet the strongest statically supported SIMD level.
That is, if your compilation run ambiently declares that a target feature is enabled, this method will take that into account. In most cases, you should use
Level::neworLevel::try_detect. This method is mainly useful for libraries, where:- Your crate features request that you not use the standard library, i.e. doesn't enable
your
"std"crate feature reason (so you can't useLevel::newandLevel::try_detectreturnsNone); AND - Your caller does not provide a
Level; AND - The library doesn't want to panic when it can't find a SIMD level.
Note that in these cases, the library should clearly inform the integrator that it is using a fallback and so not getting optimal performance (e.g. by panicking if
debug_assertionsare enabled, and emitting a log with the "error" level otherwise). The messages given should also provide actionable fixes, such as pointing to the entry-point which provides aLevel, or your"std"feature.Note that this is unaffected by the
force-support-fallbackfeature. Instead, you should useLevel::fallbackif you require the fallback level.- Your crate features request that you not use the standard library, i.e. doesn't enable
your
Trait Implementations
impl Clone for Level
fn clone(&self) -> Self
impl Copy for Level
impl Debug for Level
fn fmt(&self, f: &mut Formatter<'_>) -> Result