-
Notifications
You must be signed in to change notification settings - Fork 87
Add SIMD shuffles for SimdType{2,4,8,16,32,64} #62
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
Merged
Merged
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,8 @@ macro_rules! impl_vector { | |
Self::splat(value) | ||
} | ||
} | ||
|
||
impl_shuffle_2pow_lanes!{ $name } | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
macro_rules! impl_shuffle_lane { | ||
{ $name:ident, $fn:ident, $n:literal } => { | ||
impl $name<$n> { | ||
/// A const SIMD shuffle that takes 2 SIMD vectors and produces another vector, using | ||
/// the indices in the const parameter. The first or "self" vector will have its lanes | ||
/// indexed from 0, and the second vector will have its first lane indexed at $n. | ||
/// Indices must be in-bounds of either vector at compile time. | ||
/// | ||
/// Some SIMD shuffle instructions can be quite slow, so avoiding them by loading data | ||
/// into the desired patterns in advance is preferred, but shuffles are still faster | ||
/// than storing and reloading from memory. | ||
#[inline] | ||
pub fn shuffle<const IDX: [u32; $n]>(self, second: Self) -> Self { | ||
unsafe { crate::intrinsics::$fn(self, second, IDX) } | ||
workingjubilee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} | ||
|
||
macro_rules! impl_shuffle_2pow_lanes { | ||
{ $name:ident } => { | ||
impl_shuffle_lane!{ $name, simd_shuffle2, 2 } | ||
impl_shuffle_lane!{ $name, simd_shuffle4, 4 } | ||
impl_shuffle_lane!{ $name, simd_shuffle8, 8 } | ||
impl_shuffle_lane!{ $name, simd_shuffle16, 16 } | ||
impl_shuffle_lane!{ $name, simd_shuffle32, 32 } | ||
impl_shuffle_lane!{ $name, simd_shuffle64, 64 } | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use core_simd::SimdU32; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
use wasm_bindgen_test::*; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
#[test] | ||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] | ||
fn simple_shuffle() { | ||
let a = SimdU32::from_array([2, 4, 1, 9]); | ||
let b = a; | ||
assert_eq!(a.shuffle::<{ [3, 1, 4, 6] }>(b).to_array(), [9, 4, 2, 1]); | ||
} |
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.
So if we extend rustc to have
fn simd_shuffle<T, U, M>(x: T, y: T, idx: M) -> U
we could make this fully generic, right?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.
Possibly! I am currently poking people in the const generics crew to better understand what is required, so I'm optimistic but not completely certain what's going to happen next.