Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy_to_slice #331

Merged
merged 2 commits into from
Mar 11, 2023
Merged
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
33 changes: 32 additions & 1 deletion crates/core_simd/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ where
///
/// Panics if the slice's length is less than the vector's `Simd::LANES`.
///
/// # Examples
/// # Example
///
/// ```
/// # #![feature(portable_simd)]
Expand All @@ -174,12 +174,43 @@ where
slice.len() >= LANES,
"slice length must be at least the number of lanes"
);
assert!(core::mem::size_of::<Self>() == LANES * core::mem::size_of::<T>());
// Safety:
// - We've checked the length is sufficient.
// - `T` and `Simd<T, N>` are Copy types.
unsafe { slice.as_ptr().cast::<Self>().read_unaligned() }
}

/// Writes a SIMD vector to the first `LANES` elements of a slice.
///
/// # Panics
///
/// Panics if the slice's length is less than the vector's `Simd::LANES`.
///
/// # Example
///
/// ```
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::u32x4;
/// let mut dest = vec![0; 6];
/// let v = u32x4::from_array([1, 2, 3, 4]);
/// v.copy_to_slice(&mut dest);
/// assert_eq!(&dest, &[1, 2, 3, 4, 0, 0]);
/// ```
pub fn copy_to_slice(self, slice: &mut [T]) {
assert!(
slice.len() >= LANES,
"slice length must be at least the number of lanes"
);
assert!(core::mem::size_of::<Self>() == LANES * core::mem::size_of::<T>());
// Safety:
// - We've checked the length is sufficient
// - `T` and `Simd<T, N>` are Copy types.
unsafe { slice.as_mut_ptr().cast::<Self>().write_unaligned(self) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the simd type has padding (e.g. for non-power-of-2 types where #319 isn't fixed yet)?

imho we should add an assert that the simd type and corresponding array type have the same size, otherwise write_unaligned is permitted to overwrite after-the-end elements with undef padding -- unsound. panicking is better than UB and the assert will never fire once #319 is fixed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, you might want an explicit Copy bound since we might add Simd<&mut _, _> support -- which isn't Copy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about the padding. I think I'll leave out the Copy bound for because basically every one of Simd's associated functions fail if the element isn't Copy, that would probably need a much more significant overhaul.

}

/// Performs lanewise conversion of a SIMD vector's elements to another SIMD-valid type.
///
/// This follows the semantics of Rust's `as` conversion for casting
Expand Down