Skip to content

Commit

Permalink
Don't use direct field access in Simd functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sp00ph committed Apr 22, 2023
1 parent ceb2611 commit 095acb3
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions crates/core_simd/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,26 @@ where
/// assert_eq!(v.as_array(), &[0, 1, 2, 3]);
/// ```
pub const fn as_array(&self) -> &[T; LANES] {
&self.0
// SAFETY: `[T; LANES]` and `Simd<T, LANES>` have the same layout.
unsafe { &*core::ptr::from_ref(self).cast() }
}

/// Returns a mutable array reference containing the entire SIMD vector.
pub fn as_mut_array(&mut self) -> &mut [T; LANES] {
&mut self.0
// SAFETY: `[T; LANES]` and `Simd<T, LANES>` have the same layout.
unsafe { &mut *core::ptr::from_mut(self).cast() }
}

/// Converts an array to a SIMD vector.
pub const fn from_array(array: [T; LANES]) -> Self {
Self(array)
// SAFETY: `[T; LANES]` and `Simd<T, LANES>` have the same layout.
unsafe { core::mem::transmute_copy(&array) }
}

/// Converts a SIMD vector to an array.
pub const fn to_array(self) -> [T; LANES] {
self.0
// SAFETY: `[T; LANES]` and `Simd<T, LANES>` have the same layout.
unsafe { core::mem::transmute_copy(&self) }
}

/// Converts a slice to a SIMD vector containing `slice[..LANES]`.
Expand Down Expand Up @@ -735,7 +739,7 @@ where
{
#[inline]
fn as_ref(&self) -> &[T; LANES] {
&self.0
self.as_array()
}
}

Expand All @@ -746,7 +750,7 @@ where
{
#[inline]
fn as_mut(&mut self) -> &mut [T; LANES] {
&mut self.0
self.as_mut_array()
}
}

Expand All @@ -758,7 +762,7 @@ where
{
#[inline]
fn as_ref(&self) -> &[T] {
&self.0
self.as_array()
}
}

Expand All @@ -769,7 +773,7 @@ where
{
#[inline]
fn as_mut(&mut self) -> &mut [T] {
&mut self.0
self.as_mut_array()
}
}

Expand Down

0 comments on commit 095acb3

Please sign in to comment.