Skip to content

Commit

Permalink
Use pointer reads for better codegen in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sp00ph committed Apr 22, 2023
1 parent 52833cc commit f1b86ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 0 additions & 1 deletion crates/core_simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![feature(
const_ptr_read,
const_refs_to_cell,
const_transmute_copy,
convert_float_to_int,
decl_macro,
intra_doc_pointers,
Expand Down
18 changes: 14 additions & 4 deletions crates/core_simd/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,31 @@ where
/// Converts an array to a SIMD vector.
pub const fn from_array(array: [T; LANES]) -> Self {
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
// is always valid.
// is always valid. We need to use `read_unaligned` here, since
// the array may have a lower alignment than the vector.
//
// FIXME: We currently use a pointer read instead of `transmute_copy` because
// it results in better codegen with optimizations disabled, but we should
// probably just use `transmute` once that works on const generic types.
//
// NOTE: This deliberately doesn't just use `Self(array)`, see the comment
// on the struct definition for details.
unsafe { core::mem::transmute_copy(&array) }
unsafe { (&array as *const [T; LANES] as *const Self).read_unaligned() }
}

/// Converts a SIMD vector to an array.
pub const fn to_array(self) -> [T; LANES] {
// SAFETY: Transmuting between `Simd<T, LANES>` and `[T; LANES]`
// is always valid.
// is always valid. No need to use `read_unaligned` here, since
// the vector never has a lower alignment than the array.
//
// FIXME: We currently use a pointer read instead of `transmute_copy` because
// it results in better codegen with optimizations disabled, but we should
// probably just use `transmute` once that works on const generic types.
//
// NOTE: This deliberately doesn't just use `self.0`, see the comment
// on the struct definition for details.
unsafe { core::mem::transmute_copy(&self) }
unsafe { (&self as *const Self as *const [T; LANES]).read() }
}

/// Converts a slice to a SIMD vector containing `slice[..LANES]`.
Expand Down

0 comments on commit f1b86ba

Please sign in to comment.