-
Notifications
You must be signed in to change notification settings - Fork 105
Remove Simd: Index
#498
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
Open
scottmcm
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
scottmcm:no-index-trait
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Remove Simd: Index
#498
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -173,6 +173,11 @@ where | |||||
|
|
||||||
| /// Returns an array reference containing the entire SIMD vector. | ||||||
| /// | ||||||
| /// While this exists and *can* be used to read an arbitrary element from a | ||||||
| /// vector, like `v.as_array()[i]`, that's discouraged because it forces the | ||||||
| /// vector to memory which tends to perform poorly. Instead, use | ||||||
| /// [`Self::get`]. (This is also why `Index` is not implemented.) | ||||||
| /// | ||||||
| /// # Examples | ||||||
| /// | ||||||
| /// ``` | ||||||
|
|
@@ -193,6 +198,11 @@ where | |||||
| } | ||||||
|
|
||||||
| /// Returns a mutable array reference containing the entire SIMD vector. | ||||||
| /// | ||||||
| /// While this exists and *can* be used to write an arbitrary element into a | ||||||
| /// vector, like `v.as_mut_array()[i] = x`, that's discouraged because it | ||||||
| /// forces the vector to memory which tends to perform poorly. Instead, use | ||||||
| /// [`Self::set`]. (This is also why `IndexMut` is not implemented.) | ||||||
| #[inline] | ||||||
| pub const fn as_mut_array(&mut self) -> &mut [T; N] { | ||||||
| // SAFETY: `Simd<T, N>` is just an overaligned `[T; N]` with | ||||||
|
|
@@ -204,6 +214,147 @@ where | |||||
| unsafe { &mut *(self as *mut Self as *mut [T; N]) } | ||||||
| } | ||||||
|
|
||||||
| /// # Safety | ||||||
| /// `idx` must be in-bounds (`idx < N`) | ||||||
| #[inline] | ||||||
| unsafe fn get_inner(self, idx: usize) -> T { | ||||||
| // FIXME: This is a workaround for a CI failure in #498 | ||||||
| if cfg!(target_family = "wasm") { | ||||||
| return self.as_array()[idx]; | ||||||
| } | ||||||
|
|
||||||
| // SAFETY: our precondition is also that the value is in-bounds | ||||||
| // and this type is a simd type of the correct element type. | ||||||
| unsafe { core::intrinsics::simd::simd_extract_dyn(self, idx as u32) } | ||||||
| } | ||||||
|
|
||||||
| /// Gets the value at `idx` to `val`. | ||||||
| /// | ||||||
| /// This typically faster than reading via `as_array`. | ||||||
| /// | ||||||
| /// # Panics | ||||||
| /// | ||||||
| /// If `idx` is out of bounds (aka if `idx >= N`). | ||||||
| /// | ||||||
| /// # Examples | ||||||
| /// | ||||||
| /// ``` | ||||||
| /// # #![feature(portable_simd)] | ||||||
| /// # #[cfg(feature = "as_crate")] use core_simd::simd; | ||||||
| /// # #[cfg(not(feature = "as_crate"))] use core::simd; | ||||||
| /// # use simd::{Simd, u64x4}; | ||||||
| /// let v: u64x4 = Simd::from_array([3, 5, 7, 11]); | ||||||
| /// assert_eq!(v.get(2), 7); | ||||||
| /// ``` | ||||||
| #[inline] | ||||||
| pub fn get(self, idx: usize) -> T { | ||||||
| assert!(idx < N); | ||||||
| // SAFETY: just checked in-bounds with the assert | ||||||
| unsafe { self.get_inner(idx) } | ||||||
| } | ||||||
|
|
||||||
| /// Gets the value at `idx` to `val`, or `None` if `idx >= N`. | ||||||
| /// | ||||||
| /// This typically faster than reading via `as_array`. | ||||||
| #[inline] | ||||||
| pub fn get_checked(self, idx: usize) -> Option<T> { | ||||||
| if idx < N { | ||||||
| // SAFETY: just checked in-bounds with the if | ||||||
| Some(unsafe { self.get_inner(idx) }) | ||||||
| } else { | ||||||
| None | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Gets the value at `idx` to `val`. | ||||||
| /// | ||||||
| /// This typically faster than reading via `as_array`. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
also for the other occurrences |
||||||
| /// | ||||||
| /// # Safety | ||||||
| /// `idx` must be in-bounds (`idx < N`) | ||||||
| #[inline] | ||||||
| pub unsafe fn get_unchecked(self, idx: usize) -> T { | ||||||
| // SAFETY: our precondition is also that the value is in-bounds | ||||||
| unsafe { | ||||||
| core::hint::assert_unchecked(idx < N); | ||||||
| self.get_inner(idx) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// # Safety | ||||||
| /// `idx` must be in-bounds (`idx < N`) | ||||||
| #[inline] | ||||||
| #[must_use = "This returns a new vector, rather than updating in-place"] | ||||||
| unsafe fn set_inner(self, idx: usize, val: T) -> Self { | ||||||
| // FIXME: This is a workaround for a CI failure in #498 | ||||||
| if cfg!(target_family = "wasm") { | ||||||
| let mut temp = self; | ||||||
| temp.as_mut_array()[idx] = val; | ||||||
| return temp; | ||||||
| } | ||||||
|
|
||||||
| // SAFETY: our precondition is also that the value is in-bounds | ||||||
| // and this type is a simd type of the correct element type. | ||||||
| unsafe { core::intrinsics::simd::simd_insert_dyn(self, idx as u32, val) } | ||||||
| } | ||||||
|
|
||||||
| /// Sets the value at `idx` to `val`, returning the updated vector. | ||||||
| /// | ||||||
| /// This typically faster than updating via `as_mut_array`. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
also for the other occurrences |
||||||
| /// | ||||||
| /// # Panics | ||||||
| /// | ||||||
| /// If `idx` is out of bounds (aka if `idx >= N`). | ||||||
| /// | ||||||
| /// # Examples | ||||||
| /// | ||||||
| /// ``` | ||||||
| /// # #![feature(portable_simd)] | ||||||
| /// # #[cfg(feature = "as_crate")] use core_simd::simd; | ||||||
| /// # #[cfg(not(feature = "as_crate"))] use core::simd; | ||||||
| /// # use simd::{Simd, u64x4}; | ||||||
| /// let v: u64x4 = Simd::from_array([3, 5, 7, 11]); | ||||||
| /// assert_eq!(v.set(2, 99).as_array(), &[3, 5, 99, 11]); | ||||||
| /// ``` | ||||||
| #[inline] | ||||||
| #[must_use = "This returns a new vector, rather than updating in-place"] | ||||||
| pub fn set(self, idx: usize, val: T) -> Self { | ||||||
| assert!(idx < N); | ||||||
| // SAFETY: just checked in-bounds with the assert | ||||||
| unsafe { self.set_inner(idx, val) } | ||||||
| } | ||||||
|
|
||||||
| /// Sets the value at `idx` to `val` and returns the updated vector | ||||||
| /// or returns `None` if `idx >= N`. | ||||||
| /// | ||||||
| /// This typically faster than updating via `as_mut_array`. | ||||||
| #[inline] | ||||||
| #[must_use = "This returns a new vector, rather than updating in-place"] | ||||||
| pub fn set_checked(self, idx: usize, val: T) -> Option<Self> { | ||||||
| if idx < N { | ||||||
| // SAFETY: just checked in-bounds with the if | ||||||
| Some(unsafe { self.set_inner(idx, val) }) | ||||||
| } else { | ||||||
| None | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Sets the value at `idx` to `val`, returning the updated vector. | ||||||
| /// | ||||||
| /// This typically faster than updating via `as_mut_array`. | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// `idx` must be in-bounds (`idx < N`) | ||||||
| #[inline] | ||||||
| #[must_use = "This returns a new vector, rather than updating in-place"] | ||||||
| pub unsafe fn set_unchecked(self, idx: usize, val: T) -> Self { | ||||||
| // SAFETY: our precondition is also that the value is in-bounds | ||||||
| unsafe { | ||||||
| core::hint::assert_unchecked(idx < N); | ||||||
| self.set_inner(idx, val) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Loads a vector from an array of `T`. | ||||||
| /// | ||||||
| /// This function is necessary since `repr(simd)` has padding for non-power-of-2 vectors (at the time of writing). | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea what the problem was, but LLVM was very unhappy for some reason