|
| 1 | +// This file is automatically generated by `fearless_simd_core_gen`. |
| 2 | +// Its template can be found in `fearless_simd_core/gen/templates`. |
| 3 | + |
| 4 | +//! The AVX2 target feature. |
| 5 | +
|
| 6 | +use crate::{TargetFeatureToken, trampoline}; |
| 7 | + |
| 8 | +use core::fmt::Debug; |
| 9 | + |
| 10 | +/// [AVX2] --- Advanced Vector Extensions 2 |
| 11 | +/// |
| 12 | +/// [AVX2]: https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#AVX2 |
| 13 | +/// |
| 14 | +/// A token indicating that the current CPU has the `avx2` target feature. |
| 15 | +/// |
| 16 | +/// # Example |
| 17 | +/// |
| 18 | +/// This can be used to [`trampoline!`] into functions like: |
| 19 | +/// |
| 20 | +/// ```rust |
| 21 | +/// #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 22 | +/// #[target_feature(enable = "avx2")] |
| 23 | +/// fn uses_avx2() { |
| 24 | +/// // ... |
| 25 | +/// } |
| 26 | +/// ``` |
| 27 | +#[derive(Copy, Clone, Hash, PartialEq, Eq)] |
| 28 | +pub struct Avx2 { |
| 29 | + // We don't use non_exhaustive because we don't want this struct to be constructible. |
| 30 | + // in different modules in this crate. |
| 31 | + _private: (), |
| 32 | +} |
| 33 | + |
| 34 | +impl Debug for Avx2 { |
| 35 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 36 | + write!(f, r#""avx2" enabled."#) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +unsafe impl TargetFeatureToken for Avx2 { |
| 41 | + const FEATURES: &[&str] = &[ |
| 42 | + "avx2", "avx", "sse", "sse2", "sse3", "sse4.1", "sse4.2", "ssse3", |
| 43 | + ]; |
| 44 | + |
| 45 | + #[inline(always)] |
| 46 | + fn vectorize<R>(self, f: impl FnOnce() -> R) -> R { |
| 47 | + // Because we want this constant to be eagerly evaluated. |
| 48 | + trampoline!([Avx2 = self] => "avx2", <(R)> fn<(R)>(f: impl FnOnce() -> R = f) -> R { f() }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl Avx2 { |
| 53 | + #[cfg(feature = "std")] |
| 54 | + /// Create a new token if the `"avx2"` target feature is detected as enabled. |
| 55 | + /// |
| 56 | + /// This does not do any caching internally, although note that the standard |
| 57 | + /// library does internally cache the features it detects. |
| 58 | + // TODO: Consider a manual override feature/env var? |
| 59 | + pub fn try_new() -> Option<Self> { |
| 60 | + // Feature flag required to make docs compile. |
| 61 | + // TODO: Extract into a (private) crate::x86::is_x86_feature_detected? |
| 62 | + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 63 | + if std::arch::is_x86_feature_detected!("avx2") { |
| 64 | + // Safety: The required CPU feature was detected. |
| 65 | + unsafe { Some(Self::new()) } |
| 66 | + } else { |
| 67 | + None |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + #[target_feature(enable = "avx2")] |
| 72 | + /// Create a new token for the "avx2" target feature. |
| 73 | + /// |
| 74 | + /// This method is useful to get a new token if you have an external proof that |
| 75 | + /// AVX2 is available. This could happen if you are in a target feature |
| 76 | + /// function called by an external library user. |
| 77 | + /// |
| 78 | + /// # Safety |
| 79 | + /// |
| 80 | + /// No conditions other than those inherited from the target feature attribute, |
| 81 | + /// i.e. that the "avx2" target feature is available. |
| 82 | + /// |
| 83 | + /// [implicitly enables]: https://doc.rust-lang.org/beta/reference/attributes/codegen.html?highlight=implicitly%20enabled#r-attributes.codegen.target_feature.safety-restrictions |
| 84 | + pub fn new() -> Self { |
| 85 | + Self { _private: () } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl From<Avx2> for crate::x86::avx::Avx { |
| 90 | + fn from(value: Avx2) -> Self { |
| 91 | + // This also serves as a correctness check of the implicitly enabled features. |
| 92 | + trampoline!([Avx2 = value] => "avx2", fn() -> crate::x86::avx::Avx { crate::x86::avx::Avx::new() }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl From<Avx2> for crate::x86::sse::Sse { |
| 97 | + fn from(value: Avx2) -> Self { |
| 98 | + // This also serves as a correctness check of the implicitly enabled features. |
| 99 | + trampoline!([Avx2 = value] => "avx2", fn() -> crate::x86::sse::Sse { crate::x86::sse::Sse::new() }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl From<Avx2> for crate::x86::sse::Sse2 { |
| 104 | + fn from(value: Avx2) -> Self { |
| 105 | + // This also serves as a correctness check of the implicitly enabled features. |
| 106 | + trampoline!([Avx2 = value] => "avx2", fn() -> crate::x86::sse::Sse2 { crate::x86::sse::Sse2::new() }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl From<Avx2> for crate::x86::sse::Sse3 { |
| 111 | + fn from(value: Avx2) -> Self { |
| 112 | + // This also serves as a correctness check of the implicitly enabled features. |
| 113 | + trampoline!([Avx2 = value] => "avx2", fn() -> crate::x86::sse::Sse3 { crate::x86::sse::Sse3::new() }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl From<Avx2> for crate::x86::sse::Sse4_1 { |
| 118 | + fn from(value: Avx2) -> Self { |
| 119 | + // This also serves as a correctness check of the implicitly enabled features. |
| 120 | + trampoline!([Avx2 = value] => "avx2", fn() -> crate::x86::sse::Sse4_1 { crate::x86::sse::Sse4_1::new() }) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +impl From<Avx2> for crate::x86::sse::Sse4_2 { |
| 125 | + fn from(value: Avx2) -> Self { |
| 126 | + // This also serves as a correctness check of the implicitly enabled features. |
| 127 | + trampoline!([Avx2 = value] => "avx2", fn() -> crate::x86::sse::Sse4_2 { crate::x86::sse::Sse4_2::new() }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl From<Avx2> for crate::x86::sse::SupplementalSse3 { |
| 132 | + fn from(value: Avx2) -> Self { |
| 133 | + // This also serves as a correctness check of the implicitly enabled features. |
| 134 | + trampoline!([Avx2 = value] => "avx2", fn() -> crate::x86::sse::SupplementalSse3 { crate::x86::sse::SupplementalSse3::new() }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +const _: () = { |
| 139 | + assert!( |
| 140 | + core::mem::size_of::<Avx2>() == 0, |
| 141 | + "Target feature tokens should be zero sized." |
| 142 | + ); |
| 143 | +}; |
0 commit comments