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 popcnt and cmpxchg16b. 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() -> Self

Return 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 of cpuid instruction, but this function does not do that. If you do this, you can create the SIMD token via assume_supported and 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 in new's documentation.

fn is_fallback(self) -> bool

Check whether this is the Fallback level; 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_supported for the exact list of CPU features this token enables.

This method should be preferred over matching against the Sse2 variant 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_supported for the exact list of CPU features this token enables.

This method should be preferred over matching against the Sse4_2 variant 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_supported for the exact list of CPU features this token enables.

This method should be preferred over matching against the Avx2 variant 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_supported for 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() -> Self

Get 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::new or Level::try_detect. This method is mainly useful for libraries, where:

  1. 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 use Level::new and Level::try_detect returns None); AND
  2. Your caller does not provide a Level; AND
  3. 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_assertions are 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 a Level, or your "std" feature.

Note that this is unaffected by the force-support-fallback feature. Instead, you should use Level::fallback if you require the fallback level.

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

Auto Trait Implementations

impl Freeze for Level

impl RefUnwindSafe for Level

impl Send for Level

impl Sync for Level

impl Unpin for Level

impl UnsafeUnpin for Level

impl UnwindSafe for Level

Blanket Implementations

impl<F, T, S> SimdInto<T, S> for Level where T: SimdFrom<F, S>, S: Simd,

impl<T> Any for Level where T: 'static + ?Sized,

impl<T> Borrow<T> for Level where T: ?Sized,

impl<T> BorrowMut<T> for Level where T: ?Sized,

impl<T> CloneToUninit for Level where T: Clone,

impl<T> From<T> for Level

impl<T> ToOwned for Level where T: Clone,

impl<T, S> SimdFrom<T, S> for Level where S: Simd,

impl<T, U> Into<U> for Level where U: From<T>,

impl<T, U> TryFrom<U> for Level where U: Into<T>,

impl<T, U> TryInto<U> for Level where U: TryFrom<T>,