From a93ded542652cdff67e8b222c91a401d8e905777 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sun, 1 Oct 2023 21:28:03 -0400 Subject: [PATCH 1/4] Remove generic_const_exprs --- crates/core_simd/Cargo.toml | 1 - crates/core_simd/src/lib.rs | 2 - crates/core_simd/src/masks.rs | 5 +- crates/core_simd/src/masks/bitmask.rs | 2 - crates/core_simd/src/masks/full_masks.rs | 25 ++-- crates/core_simd/src/masks/to_bitmask.rs | 64 +++++----- crates/core_simd/src/mod.rs | 5 +- crates/core_simd/src/to_bytes.rs | 147 ++++++++++++++++------- crates/core_simd/tests/masks.rs | 1 - crates/core_simd/tests/to_bytes.rs | 6 +- 10 files changed, 148 insertions(+), 110 deletions(-) diff --git a/crates/core_simd/Cargo.toml b/crates/core_simd/Cargo.toml index d1a3a515a7e..b4a8fd70f4c 100644 --- a/crates/core_simd/Cargo.toml +++ b/crates/core_simd/Cargo.toml @@ -12,7 +12,6 @@ license = "MIT OR Apache-2.0" default = ["as_crate"] as_crate = [] std = [] -generic_const_exprs = [] all_lane_counts = [] [target.'cfg(target_arch = "wasm32")'.dev-dependencies] diff --git a/crates/core_simd/src/lib.rs b/crates/core_simd/src/lib.rs index 2d68e4cce85..dd3c546e014 100644 --- a/crates/core_simd/src/lib.rs +++ b/crates/core_simd/src/lib.rs @@ -14,8 +14,6 @@ strict_provenance, ptr_metadata )] -#![cfg_attr(feature = "generic_const_exprs", feature(generic_const_exprs))] -#![cfg_attr(feature = "generic_const_exprs", allow(incomplete_features))] #![warn(missing_docs, clippy::missing_inline_in_public_items)] // basically all items, really #![deny(unsafe_op_in_unsafe_fn, clippy::undocumented_unsafe_blocks)] #![allow(internal_features)] diff --git a/crates/core_simd/src/masks.rs b/crates/core_simd/src/masks.rs index fea687bdc1a..b6af9f83581 100644 --- a/crates/core_simd/src/masks.rs +++ b/crates/core_simd/src/masks.rs @@ -13,10 +13,7 @@ mod mask_impl; mod to_bitmask; -pub use to_bitmask::ToBitMask; - -#[cfg(feature = "generic_const_exprs")] -pub use to_bitmask::{bitmask_len, ToBitMaskArray}; +pub use to_bitmask::{ToBitMask, ToBitMaskArray}; use crate::simd::{intrinsics, LaneCount, Simd, SimdElement, SimdPartialEq, SupportedLaneCount}; use core::cmp::Ordering; diff --git a/crates/core_simd/src/masks/bitmask.rs b/crates/core_simd/src/masks/bitmask.rs index 20465ba9b07..a7df6304bc7 100644 --- a/crates/core_simd/src/masks/bitmask.rs +++ b/crates/core_simd/src/masks/bitmask.rs @@ -119,7 +119,6 @@ where unsafe { Self(intrinsics::simd_bitmask(value), PhantomData) } } - #[cfg(feature = "generic_const_exprs")] #[inline] #[must_use = "method returns a new array and does not mutate the original value"] pub fn to_bitmask_array(self) -> [u8; N] { @@ -129,7 +128,6 @@ where unsafe { core::mem::transmute_copy(&self.0) } } - #[cfg(feature = "generic_const_exprs")] #[inline] #[must_use = "method returns a new mask and does not mutate the original value"] pub fn from_bitmask_array(bitmask: [u8; N]) -> Self { diff --git a/crates/core_simd/src/masks/full_masks.rs b/crates/core_simd/src/masks/full_masks.rs index 1d13c45b8e7..4b36adece71 100644 --- a/crates/core_simd/src/masks/full_masks.rs +++ b/crates/core_simd/src/masks/full_masks.rs @@ -1,12 +1,9 @@ //! Masks that take up full SIMD vector registers. -use super::MaskElement; +use super::{to_bitmask::ToBitMaskArray, MaskElement}; use crate::simd::intrinsics; use crate::simd::{LaneCount, Simd, SupportedLaneCount, ToBitMask}; -#[cfg(feature = "generic_const_exprs")] -use crate::simd::ToBitMaskArray; - #[repr(transparent)] pub struct Mask(Simd) where @@ -145,23 +142,19 @@ where unsafe { Mask(intrinsics::simd_cast(self.0)) } } - #[cfg(feature = "generic_const_exprs")] #[inline] #[must_use = "method returns a new array and does not mutate the original value"] pub fn to_bitmask_array(self) -> [u8; N] where super::Mask: ToBitMaskArray, - [(); as ToBitMaskArray>::BYTES]: Sized, { - assert_eq!( as ToBitMaskArray>::BYTES, N); - - // Safety: N is the correct bitmask size + // Safety: Bytes is the right size array unsafe { // Compute the bitmask - let bitmask: [u8; as ToBitMaskArray>::BYTES] = + let bitmask: as ToBitMaskArray>::BitMaskArray = intrinsics::simd_bitmask(self.0); - // Transmute to the return type, previously asserted to be the same size + // Transmute to the return type let mut bitmask: [u8; N] = core::mem::transmute_copy(&bitmask); // LLVM assumes bit order should match endianness @@ -175,17 +168,13 @@ where } } - #[cfg(feature = "generic_const_exprs")] #[inline] #[must_use = "method returns a new mask and does not mutate the original value"] pub fn from_bitmask_array(mut bitmask: [u8; N]) -> Self where super::Mask: ToBitMaskArray, - [(); as ToBitMaskArray>::BYTES]: Sized, { - assert_eq!( as ToBitMaskArray>::BYTES, N); - - // Safety: N is the correct bitmask size + // Safety: Bytes is the right size array unsafe { // LLVM assumes bit order should match endianness if cfg!(target_endian = "big") { @@ -194,8 +183,8 @@ where } } - // Transmute to the bitmask type, previously asserted to be the same size - let bitmask: [u8; as ToBitMaskArray>::BYTES] = + // Transmute to the bitmask + let bitmask: as ToBitMaskArray>::BitMaskArray = core::mem::transmute_copy(&bitmask); // Compute the regular mask diff --git a/crates/core_simd/src/masks/to_bitmask.rs b/crates/core_simd/src/masks/to_bitmask.rs index 8e724c9de8c..7041d15164d 100644 --- a/crates/core_simd/src/masks/to_bitmask.rs +++ b/crates/core_simd/src/masks/to_bitmask.rs @@ -30,19 +30,18 @@ pub trait ToBitMask: Sealed { /// Converts masks to and from byte array bitmasks. /// /// Each bit of the bitmask corresponds to a mask lane, starting with the LSB of the first byte. -#[cfg(feature = "generic_const_exprs")] pub trait ToBitMaskArray: Sealed { - /// The length of the bitmask array. - const BYTES: usize; + /// The bitmask array. + type BitMaskArray; /// Converts a mask to a bitmask. - fn to_bitmask_array(self) -> [u8; Self::BYTES]; + fn to_bitmask_array(self) -> Self::BitMaskArray; /// Converts a bitmask to a mask. - fn from_bitmask_array(bitmask: [u8; Self::BYTES]) -> Self; + fn from_bitmask_array(bitmask: Self::BitMaskArray) -> Self; } -macro_rules! impl_integer_intrinsic { +macro_rules! impl_integer { { $(impl ToBitMask for Mask<_, $lanes:literal>)* } => { $( impl ToBitMask for Mask { @@ -62,7 +61,27 @@ macro_rules! impl_integer_intrinsic { } } -impl_integer_intrinsic! { +macro_rules! impl_array { + { $(impl ToBitMaskArray for Mask<_, $lanes:literal>)* } => { + $( + impl ToBitMaskArray for Mask { + type BitMaskArray = [u8; $int]; + + #[inline] + fn to_bitmask_array(self) -> Self::BitMaskArray { + self.0.to_bitmask_array() + } + + #[inline] + fn from_bitmask_array(bitmask: Self::BitMaskArray) -> Self { + Self(mask_impl::Mask::from_bitmask_array(bitmask)) + } + } + )* + } +} + +impl_integer! { impl ToBitMask for Mask<_, 1> impl ToBitMask for Mask<_, 2> impl ToBitMask for Mask<_, 4> @@ -72,27 +91,12 @@ impl_integer_intrinsic! { impl ToBitMask for Mask<_, 64> } -/// Returns the minimum number of bytes in a bitmask with `lanes` lanes. -#[cfg(feature = "generic_const_exprs")] -#[allow(clippy::missing_inline_in_public_items)] -pub const fn bitmask_len(lanes: usize) -> usize { - (lanes + 7) / 8 -} - -#[cfg(feature = "generic_const_exprs")] -impl ToBitMaskArray for Mask -where - LaneCount: SupportedLaneCount, -{ - const BYTES: usize = bitmask_len(LANES); - - #[inline] - fn to_bitmask_array(self) -> [u8; Self::BYTES] { - self.0.to_bitmask_array() - } - - #[inline] - fn from_bitmask_array(bitmask: [u8; Self::BYTES]) -> Self { - Mask(mask_impl::Mask::from_bitmask_array(bitmask)) - } +impl_array! { + impl ToBitMaskArray for Mask<_, 1> + impl ToBitMaskArray for Mask<_, 2> + impl ToBitMaskArray for Mask<_, 4> + impl ToBitMaskArray for Mask<_, 8> + impl ToBitMaskArray for Mask<_, 16> + impl ToBitMaskArray for Mask<_, 32> + impl ToBitMaskArray for Mask<_, 64> } diff --git a/crates/core_simd/src/mod.rs b/crates/core_simd/src/mod.rs index dd954b7cc48..f489ae36de4 100644 --- a/crates/core_simd/src/mod.rs +++ b/crates/core_simd/src/mod.rs @@ -3,9 +3,6 @@ mod swizzle; pub(crate) mod intrinsics; -#[cfg(feature = "generic_const_exprs")] -mod to_bytes; - mod alias; mod cast; mod elements; @@ -18,6 +15,7 @@ mod ops; mod ord; mod select; mod swizzle_dyn; +mod to_bytes; mod vector; mod vendor; @@ -37,5 +35,6 @@ pub mod simd { pub use crate::core_simd::ord::*; pub use crate::core_simd::swizzle::*; pub use crate::core_simd::swizzle_dyn::*; + pub use crate::core_simd::to_bytes::ToBytes; pub use crate::core_simd::vector::*; } diff --git a/crates/core_simd/src/to_bytes.rs b/crates/core_simd/src/to_bytes.rs index 5f1374fd5a5..5fe4a77d50d 100644 --- a/crates/core_simd/src/to_bytes.rs +++ b/crates/core_simd/src/to_bytes.rs @@ -1,72 +1,126 @@ -use crate::simd::SimdUint; +use crate::simd::{LaneCount, Simd, SimdElement, SimdFloat, SimdInt, SimdUint, SupportedLaneCount}; + +mod sealed { + use super::*; + pub trait Sealed {} + impl Sealed for Simd where LaneCount: SupportedLaneCount {} +} +use sealed::Sealed; + +/// Convert SIMD vectors to vectors of bytes +pub trait ToBytes: Sealed { + /// This type, reinterpreted as bytes. + type Bytes; + + /// Return the memory representation of this integer as a byte array in native byte + /// order. + fn to_ne_bytes(self) -> Self::Bytes; + + /// Return the memory representation of this integer as a byte array in big-endian + /// (network) byte order. + fn to_be_bytes(self) -> Self::Bytes; + + /// Return the memory representation of this integer as a byte array in little-endian + /// byte order. + fn to_le_bytes(self) -> Self::Bytes; + + /// Create a native endian integer value from its memory representation as a byte array + /// in native endianness. + fn from_ne_bytes(bytes: Self::Bytes) -> Self; + + /// Create an integer value from its representation as a byte array in big endian. + fn from_be_bytes(bytes: Self::Bytes) -> Self; + + /// Create an integer value from its representation as a byte array in little endian. + fn from_le_bytes(bytes: Self::Bytes) -> Self; +} + +macro_rules! swap_bytes { + { f32, $x:expr } => { Simd::from_bits($x.to_bits().swap_bytes()) }; + { f64, $x:expr } => { Simd::from_bits($x.to_bits().swap_bytes()) }; + { $ty:ty, $x:expr } => { $x.swap_bytes() } +} macro_rules! impl_to_bytes { - { $ty:ty, $size:literal } => { - impl crate::simd::Simd<$ty, LANES> - where - crate::simd::LaneCount: crate::simd::SupportedLaneCount, - crate::simd::LaneCount<{{ $size * LANES }}>: crate::simd::SupportedLaneCount, - { - /// Return the memory representation of this integer as a byte array in native byte - /// order. + { $ty:tt, $size:tt } => { + impl_to_bytes! { $ty, $size * 1 } + impl_to_bytes! { $ty, $size * 2 } + impl_to_bytes! { $ty, $size * 4 } + impl_to_bytes! { $ty, $size * 8 } + impl_to_bytes! { $ty, $size * 16 } + impl_to_bytes! { $ty, $size * 32 } + impl_to_bytes! { $ty, $size * 64 } + }; + + // multiply element size by number of elements + { $ty:tt, 1 * $elems:literal } => { impl_to_bytes! { @impl [$ty; $elems], $elems } }; + { $ty:tt, $size:literal * 1 } => { impl_to_bytes! { @impl [$ty; 1], $size } }; + { $ty:tt, 2 * 2 } => { impl_to_bytes! { @impl [$ty; 2], 4 } }; + { $ty:tt, 2 * 4 } => { impl_to_bytes! { @impl [$ty; 4], 8 } }; + { $ty:tt, 2 * 8 } => { impl_to_bytes! { @impl [$ty; 8], 16 } }; + { $ty:tt, 2 * 16 } => { impl_to_bytes! { @impl [$ty; 16], 32 } }; + { $ty:tt, 2 * 32 } => { impl_to_bytes! { @impl [$ty; 32], 64 } }; + { $ty:tt, 4 * 2 } => { impl_to_bytes! { @impl [$ty; 2], 8 } }; + { $ty:tt, 4 * 4 } => { impl_to_bytes! { @impl [$ty; 4], 16 } }; + { $ty:tt, 4 * 8 } => { impl_to_bytes! { @impl [$ty; 8], 32 } }; + { $ty:tt, 4 * 16 } => { impl_to_bytes! { @impl [$ty; 16], 64 } }; + { $ty:tt, 8 * 2 } => { impl_to_bytes! { @impl [$ty; 2], 16 } }; + { $ty:tt, 8 * 4 } => { impl_to_bytes! { @impl [$ty; 4], 32 } }; + { $ty:tt, 8 * 8 } => { impl_to_bytes! { @impl [$ty; 8], 64 } }; + + // unsupported number of lanes + { $ty:ty, $a:literal * $b:literal } => { }; + + { @impl [$ty:tt; $elem:literal], $bytes:literal } => { + impl ToBytes for Simd<$ty, $elem> { + type Bytes = Simd; + #[inline] - pub fn to_ne_bytes(self) -> crate::simd::Simd { + fn to_ne_bytes(self) -> Self::Bytes { // Safety: transmuting between vectors is safe - unsafe { core::mem::transmute_copy(&self) } + unsafe { core::mem::transmute(self) } } - /// Return the memory representation of this integer as a byte array in big-endian - /// (network) byte order. #[inline] - pub fn to_be_bytes(self) -> crate::simd::Simd { - let bytes = self.to_ne_bytes(); - if cfg!(target_endian = "big") { - bytes - } else { - bytes.swap_bytes() + fn to_be_bytes(mut self) -> Self::Bytes { + if !cfg!(target_endian = "big") { + self = swap_bytes!($ty, self); } + self.to_ne_bytes() } - /// Return the memory representation of this integer as a byte array in little-endian - /// byte order. #[inline] - pub fn to_le_bytes(self) -> crate::simd::Simd { - let bytes = self.to_ne_bytes(); - if cfg!(target_endian = "little") { - bytes - } else { - bytes.swap_bytes() + fn to_le_bytes(mut self) -> Self::Bytes { + if !cfg!(target_endian = "little") { + self = swap_bytes!($ty, self); } + self.to_ne_bytes() } - /// Create a native endian integer value from its memory representation as a byte array - /// in native endianness. #[inline] - pub fn from_ne_bytes(bytes: crate::simd::Simd) -> Self { + fn from_ne_bytes(bytes: Self::Bytes) -> Self { // Safety: transmuting between vectors is safe - unsafe { core::mem::transmute_copy(&bytes) } + unsafe { core::mem::transmute(bytes) } } - /// Create an integer value from its representation as a byte array in big endian. #[inline] - pub fn from_be_bytes(bytes: crate::simd::Simd) -> Self { - let bytes = if cfg!(target_endian = "big") { - bytes + fn from_be_bytes(bytes: Self::Bytes) -> Self { + let ret = Self::from_ne_bytes(bytes); + if cfg!(target_endian = "big") { + ret } else { - bytes.swap_bytes() - }; - Self::from_ne_bytes(bytes) + swap_bytes!($ty, ret) + } } - /// Create an integer value from its representation as a byte array in little endian. #[inline] - pub fn from_le_bytes(bytes: crate::simd::Simd) -> Self { - let bytes = if cfg!(target_endian = "little") { - bytes + fn from_le_bytes(bytes: Self::Bytes) -> Self { + let ret = Self::from_ne_bytes(bytes); + if cfg!(target_endian = "little") { + ret } else { - bytes.swap_bytes() - }; - Self::from_ne_bytes(bytes) + swap_bytes!($ty, ret) + } } } } @@ -89,3 +143,6 @@ impl_to_bytes! { i64, 8 } impl_to_bytes! { isize, 4 } #[cfg(target_pointer_width = "64")] impl_to_bytes! { isize, 8 } + +impl_to_bytes! { f32, 4 } +impl_to_bytes! { f64, 8 } diff --git a/crates/core_simd/tests/masks.rs b/crates/core_simd/tests/masks.rs index 9f8bad1c36c..7c1d4c7dd3f 100644 --- a/crates/core_simd/tests/masks.rs +++ b/crates/core_simd/tests/masks.rs @@ -125,7 +125,6 @@ macro_rules! test_mask_api { cast_impl::(); } - #[cfg(feature = "generic_const_exprs")] #[test] fn roundtrip_bitmask_array_conversion() { use core_simd::simd::ToBitMaskArray; diff --git a/crates/core_simd/tests/to_bytes.rs b/crates/core_simd/tests/to_bytes.rs index 7dd740d65dd..66a7981cdc3 100644 --- a/crates/core_simd/tests/to_bytes.rs +++ b/crates/core_simd/tests/to_bytes.rs @@ -1,8 +1,6 @@ -#![feature(portable_simd, generic_const_exprs, adt_const_params)] -#![allow(incomplete_features)] -#![cfg(feature = "generic_const_exprs")] +#![feature(portable_simd)] -use core_simd::simd::Simd; +use core_simd::simd::{Simd, ToBytes}; #[test] fn byte_convert() { From b070f0f657bbe4e8a24c3731fe6a230fda64cdd0 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sun, 1 Oct 2023 23:34:48 -0400 Subject: [PATCH 2/4] Fix cargo features in CI and enable them for testing --- .github/workflows/ci.yml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed1589be4f1..90543044ea8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -238,7 +238,7 @@ jobs: run: cross test --verbose --target=${{ matrix.target }} --release features: - name: "Check cargo features (${{ matrix.simd }} × ${{ matrix.features }})" + name: "Test cargo features (${{ matrix.simd }} × ${{ matrix.features }})" runs-on: ubuntu-latest strategy: fail-fast: false @@ -249,12 +249,8 @@ jobs: features: - "" - "--features std" - - "--features generic_const_exprs" - - "--features std --features generic_const_exprs" - "--features all_lane_counts" - - "--features all_lane_counts --features std" - - "--features all_lane_counts --features generic_const_exprs" - - "--features all_lane_counts --features std --features generic_const_exprs" + - "--all-features" steps: - uses: actions/checkout@v2 @@ -266,9 +262,9 @@ jobs: run: echo "CPU_FEATURE=$(lscpu | grep -o avx512[a-z]* | sed s/avx/+avx/ | tr '\n' ',' )" >> $GITHUB_ENV - name: Check build if: ${{ matrix.simd == '' }} - run: RUSTFLAGS="-Dwarnings" cargo check --all-targets --no-default-features ${{ matrix.features }} + run: RUSTFLAGS="-Dwarnings" cargo test --all-targets --no-default-features ${{ matrix.features }} - name: Check AVX if: ${{ matrix.simd == 'avx512' && contains(env.CPU_FEATURE, 'avx512') }} run: | echo "Found AVX features: $CPU_FEATURE" - RUSTFLAGS="-Dwarnings -Ctarget-feature=$CPU_FEATURE" cargo check --all-targets --no-default-features ${{ matrix.features }} + RUSTFLAGS="-Dwarnings -Ctarget-feature=$CPU_FEATURE" cargo test --all-targets --no-default-features ${{ matrix.features }} From b411cb401d97d128f87c47f3f58f615fa041d879 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Mon, 2 Oct 2023 16:15:37 -0400 Subject: [PATCH 3/4] Simplify macro --- crates/core_simd/src/to_bytes.rs | 45 +++++++++----------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/crates/core_simd/src/to_bytes.rs b/crates/core_simd/src/to_bytes.rs index 5fe4a77d50d..07a3efea01a 100644 --- a/crates/core_simd/src/to_bytes.rs +++ b/crates/core_simd/src/to_bytes.rs @@ -42,38 +42,18 @@ macro_rules! swap_bytes { } macro_rules! impl_to_bytes { - { $ty:tt, $size:tt } => { - impl_to_bytes! { $ty, $size * 1 } - impl_to_bytes! { $ty, $size * 2 } - impl_to_bytes! { $ty, $size * 4 } - impl_to_bytes! { $ty, $size * 8 } - impl_to_bytes! { $ty, $size * 16 } - impl_to_bytes! { $ty, $size * 32 } - impl_to_bytes! { $ty, $size * 64 } - }; - - // multiply element size by number of elements - { $ty:tt, 1 * $elems:literal } => { impl_to_bytes! { @impl [$ty; $elems], $elems } }; - { $ty:tt, $size:literal * 1 } => { impl_to_bytes! { @impl [$ty; 1], $size } }; - { $ty:tt, 2 * 2 } => { impl_to_bytes! { @impl [$ty; 2], 4 } }; - { $ty:tt, 2 * 4 } => { impl_to_bytes! { @impl [$ty; 4], 8 } }; - { $ty:tt, 2 * 8 } => { impl_to_bytes! { @impl [$ty; 8], 16 } }; - { $ty:tt, 2 * 16 } => { impl_to_bytes! { @impl [$ty; 16], 32 } }; - { $ty:tt, 2 * 32 } => { impl_to_bytes! { @impl [$ty; 32], 64 } }; - { $ty:tt, 4 * 2 } => { impl_to_bytes! { @impl [$ty; 2], 8 } }; - { $ty:tt, 4 * 4 } => { impl_to_bytes! { @impl [$ty; 4], 16 } }; - { $ty:tt, 4 * 8 } => { impl_to_bytes! { @impl [$ty; 8], 32 } }; - { $ty:tt, 4 * 16 } => { impl_to_bytes! { @impl [$ty; 16], 64 } }; - { $ty:tt, 8 * 2 } => { impl_to_bytes! { @impl [$ty; 2], 16 } }; - { $ty:tt, 8 * 4 } => { impl_to_bytes! { @impl [$ty; 4], 32 } }; - { $ty:tt, 8 * 8 } => { impl_to_bytes! { @impl [$ty; 8], 64 } }; - - // unsupported number of lanes - { $ty:ty, $a:literal * $b:literal } => { }; - - { @impl [$ty:tt; $elem:literal], $bytes:literal } => { - impl ToBytes for Simd<$ty, $elem> { - type Bytes = Simd; + { $ty:tt, 1 } => { impl_to_bytes! { $ty, 1 * [1, 2, 4, 8, 16, 32, 64] } }; + { $ty:tt, 2 } => { impl_to_bytes! { $ty, 2 * [1, 2, 4, 8, 16, 32] } }; + { $ty:tt, 4 } => { impl_to_bytes! { $ty, 4 * [1, 2, 4, 8, 16] } }; + { $ty:tt, 8 } => { impl_to_bytes! { $ty, 8 * [1, 2, 4, 8] } }; + { $ty:tt, 16 } => { impl_to_bytes! { $ty, 16 * [1, 2, 4] } }; + { $ty:tt, 32 } => { impl_to_bytes! { $ty, 32 * [1, 2] } }; + { $ty:tt, 64 } => { impl_to_bytes! { $ty, 64 * [1] } }; + + { $ty:tt, $size:literal * [$($elems:literal),*] } => { + $( + impl ToBytes for Simd<$ty, $elems> { + type Bytes = Simd; #[inline] fn to_ne_bytes(self) -> Self::Bytes { @@ -123,6 +103,7 @@ macro_rules! impl_to_bytes { } } } + )* } } From afe28b13e73da85c3b8e711e24dc709829a142b9 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Mon, 2 Oct 2023 16:27:18 -0400 Subject: [PATCH 4/4] Add various bounds --- crates/core_simd/src/masks/to_bitmask.rs | 11 ++++++++++- crates/core_simd/src/to_bytes.rs | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/core_simd/src/masks/to_bitmask.rs b/crates/core_simd/src/masks/to_bitmask.rs index 7041d15164d..12cb1771ce1 100644 --- a/crates/core_simd/src/masks/to_bitmask.rs +++ b/crates/core_simd/src/masks/to_bitmask.rs @@ -1,5 +1,6 @@ use super::{mask_impl, Mask, MaskElement}; use crate::simd::{LaneCount, SupportedLaneCount}; +use core::borrow::{Borrow, BorrowMut}; mod sealed { pub trait Sealed {} @@ -32,7 +33,15 @@ pub trait ToBitMask: Sealed { /// Each bit of the bitmask corresponds to a mask lane, starting with the LSB of the first byte. pub trait ToBitMaskArray: Sealed { /// The bitmask array. - type BitMaskArray; + type BitMaskArray: Copy + + Unpin + + Send + + Sync + + AsRef<[u8]> + + AsMut<[u8]> + + Borrow<[u8]> + + BorrowMut<[u8]> + + 'static; /// Converts a mask to a bitmask. fn to_bitmask_array(self) -> Self::BitMaskArray; diff --git a/crates/core_simd/src/to_bytes.rs b/crates/core_simd/src/to_bytes.rs index 07a3efea01a..3c93fe47404 100644 --- a/crates/core_simd/src/to_bytes.rs +++ b/crates/core_simd/src/to_bytes.rs @@ -10,7 +10,14 @@ use sealed::Sealed; /// Convert SIMD vectors to vectors of bytes pub trait ToBytes: Sealed { /// This type, reinterpreted as bytes. - type Bytes; + type Bytes: Copy + + Unpin + + Send + + Sync + + AsRef<[u8]> + + AsMut<[u8]> + + SimdUint + + 'static; /// Return the memory representation of this integer as a byte array in native byte /// order.