Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 18 additions & 37 deletions crates/core_simd/src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,35 @@
use crate::simd::SimdElement;

mod sealed {
/// Cast vector elements to other types.
///
/// # Safety
/// Implementing this trait asserts that the type is a valid vector element for the `simd_cast`
/// or `simd_as` intrinsics.
pub unsafe trait Sealed {}
}
use sealed::Sealed;

/// Supporting trait for `Simd::cast`. Typically doesn't need to be used directly.
pub trait SimdCast: Sealed + SimdElement {}
///
/// # Safety
/// Implementing this trait asserts that the type is a valid vector element for the `simd_cast` or
/// `simd_as` intrinsics.
pub impl(self) unsafe trait SimdCast: SimdElement {}

// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for i8 {}
impl SimdCast for i8 {}
unsafe impl SimdCast for i8 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for i16 {}
impl SimdCast for i16 {}
unsafe impl SimdCast for i16 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for i32 {}
impl SimdCast for i32 {}
unsafe impl SimdCast for i32 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for i64 {}
impl SimdCast for i64 {}
unsafe impl SimdCast for i64 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for isize {}
impl SimdCast for isize {}
unsafe impl SimdCast for isize {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for u8 {}
impl SimdCast for u8 {}
unsafe impl SimdCast for u8 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for u16 {}
impl SimdCast for u16 {}
unsafe impl SimdCast for u16 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for u32 {}
impl SimdCast for u32 {}
unsafe impl SimdCast for u32 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for u64 {}
impl SimdCast for u64 {}
unsafe impl SimdCast for u64 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for usize {}
impl SimdCast for usize {}
unsafe impl SimdCast for usize {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for f16 {}
impl SimdCast for f16 {}
unsafe impl SimdCast for f16 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for f32 {}
impl SimdCast for f32 {}
unsafe impl SimdCast for f32 {}
// Safety: primitive number types can be cast to other primitive number types
unsafe impl Sealed for f64 {}
impl SimdCast for f64 {}
unsafe impl SimdCast for f64 {}
1 change: 1 addition & 0 deletions crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
f16,
core_intrinsics,
decl_macro,
impl_restriction,
repr_simd,
staged_api,
prelude_import,
Expand Down
15 changes: 9 additions & 6 deletions crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro_rules! impl_fix_endianness {

impl_fix_endianness! { u8, u16, u32, u64 }

mod sealed {
mod private_methods {
use super::*;

/// Not only does this seal the `MaskElement` trait, but these functions prevent other traits
Expand All @@ -38,7 +38,7 @@ mod sealed {
/// For example, `eq` could be provided by requiring `MaskElement: PartialEq`, but that would
/// prevent us from ever removing that bound, or from implementing `MaskElement` on
/// non-`PartialEq` types in the future.
pub trait Sealed {
pub trait PrivateMethods {
fn valid<const N: usize>(values: Simd<Self, N>) -> bool
where
Self: SimdElement;
Expand All @@ -55,17 +55,20 @@ mod sealed {
const FALSE: Self;
}
}
use sealed::Sealed;
use private_methods::PrivateMethods;

/// Marker trait for types that may be used as SIMD mask elements.
///
/// # Safety
/// Type must be a signed integer.
pub unsafe trait MaskElement: SimdElement<Mask = Self> + SimdCast + Sealed {}
pub impl(self) unsafe trait MaskElement:
SimdElement<Mask = Self> + SimdCast + PrivateMethods
{
}

macro_rules! impl_element {
{ $ty:ty, $unsigned:ty } => {
impl Sealed for $ty {
impl PrivateMethods for $ty {
#[inline]
fn valid<const N: usize>(value: Simd<Self, N>) -> bool
{
Expand Down Expand Up @@ -196,7 +199,7 @@ where
pub unsafe fn from_simd_unchecked(value: Simd<T, N>) -> Self {
// Safety: the caller must confirm this invariant
unsafe {
core::intrinsics::assume(<T as Sealed>::valid(value));
core::intrinsics::assume(<T as PrivateMethods>::valid(value));
}
Self(value)
}
Expand Down
4 changes: 0 additions & 4 deletions crates/core_simd/src/simd/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ mod float;
mod int;
mod uint;

mod sealed {
pub trait Sealed {}
}

pub use float::*;
pub use int::*;
pub use uint::*;
5 changes: 1 addition & 4 deletions crates/core_simd/src/simd/num/float.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::sealed::Sealed;
use crate::simd::{
Mask, Select, Simd, SimdCast, SimdElement,
cmp::{SimdPartialEq, SimdPartialOrd},
};

/// Operations on SIMD vectors of floats.
pub trait SimdFloat: Copy + Sealed {
pub impl(self) trait SimdFloat: Copy {
/// Mask type used for manipulating this SIMD vector type.
type Mask;

Expand Down Expand Up @@ -240,8 +239,6 @@ pub trait SimdFloat: Copy + Sealed {
macro_rules! impl_trait {
{ $($ty:ty { bits: $bits_ty:ty, mask: $mask_ty:ty }),* } => {
$(
impl<const N: usize> Sealed for Simd<$ty, N> {}

impl<const N: usize> SimdFloat for Simd<$ty, N>
{
type Mask = Mask<<$mask_ty as SimdElement>::Mask, N>;
Expand Down
5 changes: 1 addition & 4 deletions crates/core_simd/src/simd/num/int.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use super::sealed::Sealed;
use crate::simd::{
Mask, Select, Simd, SimdCast, SimdElement, cmp::SimdOrd, cmp::SimdPartialOrd, num::SimdUint,
};

/// Operations on SIMD vectors of signed integers.
pub trait SimdInt: Copy + Sealed {
pub impl(self) trait SimdInt: Copy {
/// Mask type used for manipulating this SIMD vector type.
type Mask;

Expand Down Expand Up @@ -241,8 +240,6 @@ pub trait SimdInt: Copy + Sealed {
macro_rules! impl_trait {
{ $($ty:ident ($unsigned:ident)),* } => {
$(
impl<const N: usize> Sealed for Simd<$ty, N> {}

impl<const N: usize> SimdInt for Simd<$ty, N> {
type Mask = Mask<<$ty as SimdElement>::Mask, N>;
type Scalar = $ty;
Expand Down
5 changes: 1 addition & 4 deletions crates/core_simd/src/simd/num/uint.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::sealed::Sealed;
use crate::simd::{Simd, SimdCast, SimdElement, cmp::SimdOrd};

/// Operations on SIMD vectors of unsigned integers.
pub trait SimdUint: Copy + Sealed {
pub impl(self) trait SimdUint: Copy {
/// Scalar type contained by this SIMD vector type.
type Scalar;

Expand Down Expand Up @@ -124,8 +123,6 @@ pub trait SimdUint: Copy + Sealed {
macro_rules! impl_trait {
{ $($ty:ident ($signed:ident)),* } => {
$(
impl<const N: usize> Sealed for Simd<$ty, N> {}

impl<const N: usize> SimdUint for Simd<$ty, N>
{
type Scalar = $ty;
Expand Down
4 changes: 0 additions & 4 deletions crates/core_simd/src/simd/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
mod const_ptr;
mod mut_ptr;

mod sealed {
pub trait Sealed {}
}

pub use const_ptr::*;
pub use mut_ptr::*;

Expand Down
5 changes: 1 addition & 4 deletions crates/core_simd/src/simd/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::sealed::Sealed;
use crate::simd::{Mask, Simd, cmp::SimdPartialEq, num::SimdUint};

/// Operations on SIMD vectors of constant pointers.
pub trait SimdConstPtr: Copy + Sealed {
pub impl(self) trait SimdConstPtr: Copy {
/// Vector of `usize` with the same number of elements.
type Usize;

Expand Down Expand Up @@ -65,8 +64,6 @@ pub trait SimdConstPtr: Copy + Sealed {
fn wrapping_sub(self, count: Self::Usize) -> Self;
}

impl<T, const N: usize> Sealed for Simd<*const T, N> {}

impl<T, const N: usize> SimdConstPtr for Simd<*const T, N> {
type Usize = Simd<usize, N>;
type Isize = Simd<isize, N>;
Expand Down
5 changes: 1 addition & 4 deletions crates/core_simd/src/simd/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::sealed::Sealed;
use crate::simd::{Mask, Simd, cmp::SimdPartialEq, num::SimdUint};

/// Operations on SIMD vectors of mutable pointers.
pub trait SimdMutPtr: Copy + Sealed {
pub impl(self) trait SimdMutPtr: Copy {
/// Vector of `usize` with the same number of elements.
type Usize;

Expand Down Expand Up @@ -65,8 +64,6 @@ pub trait SimdMutPtr: Copy + Sealed {
fn wrapping_sub(self, count: Self::Usize) -> Self;
}

impl<T, const N: usize> Sealed for Simd<*mut T, N> {}

impl<T, const N: usize> SimdMutPtr for Simd<*mut T, N> {
type Usize = Simd<usize, N>;
type Isize = Simd<isize, N>;
Expand Down
11 changes: 2 additions & 9 deletions crates/core_simd/src/to_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
use crate::simd::{
Simd, SimdElement,
Simd,
num::{SimdFloat, SimdInt, SimdUint},
};

mod sealed {
use super::*;
pub trait Sealed {}
impl<T: SimdElement, const N: usize> Sealed for Simd<T, N> {}
}
use sealed::Sealed;

/// Converts SIMD vectors to vectors of bytes
pub trait ToBytes: Sealed {
pub impl(self) trait ToBytes {
/// This type, reinterpreted as bytes.
type Bytes: Copy
+ Unpin
Expand Down
Loading
Loading