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 some common shuffles #98

Merged
merged 3 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
83 changes: 83 additions & 0 deletions crates/core_simd/src/permute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,89 @@ macro_rules! impl_shuffle_lane {
pub fn shuffle<const IDX: [u32; $n]>(self, second: Self) -> Self {
unsafe { crate::intrinsics::$fn(self, second, IDX) }
}

/// Reverse the order of the lanes in the vector.
#[inline]
pub fn reverse(self) -> Self {
const fn idx() -> [u32; $n] {
let mut idx = [0u32; $n];
let mut i = 0;
while i < $n {
idx[i] = ($n - i - 1) as u32;
i += 1;
}
idx
}
self.shuffle::<{ idx() }>(self)
workingjubilee marked this conversation as resolved.
Show resolved Hide resolved
}

/// Interleave two vectors.
///
/// The even lanes of the first result contain the lower half of `self`, and the odd
/// lanes contain the lower half of `other`.
///
/// The even lanes of the second result contain the upper half of `self`, and the odd
/// lanes contain the upper half of `other`.
#[inline]
pub fn interleave(self, other: Self) -> (Self, Self) {
Copy link
Member

Choose a reason for hiding this comment

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

We should address the case of lane numbering ("are we indexing from zero or one?") explicitly here if we are going to use "even" or "odd". Likewise, "upper" and "lower" need an explicit meaning.

"Interleave" feels slightly uncommon as a vocabulary word, even though it is a technically correct term, so in the documentation itself consider using "alternating" to describe this phenomenon: as-is, going by these remarks, I have to visualize the elements moving before I realize it is in fact alternating.

I don't thiiink I would want to use "zip" like Arm does to describe this, but am unsure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree that all of those definitions are a little vague. There's probably a clearer way to describe it, but I'm not sure what that is.

const fn lo() -> [u32; $n] {
let mut idx = [0u32; $n];
let mut i = 0;
while i < $n {
let offset = i / 2;
idx[i] = if i % 2 == 0 {
offset
} else {
$n + offset
} as u32;
i += 1;
}
idx
}
const fn hi() -> [u32; $n] {
let mut idx = [0u32; $n];
let mut i = 0;
while i < $n {
let offset = ($n + i) / 2;
idx[i] = if i % 2 == 0 {
offset
} else {
$n + offset
} as u32;
i += 1;
}
idx
}
(self.shuffle::<{ lo() }>(other), self.shuffle::<{ hi() }>(other))
}

/// Deinterleave two vectors.
///
/// The first result contains the even lanes of `self` and `other` concatenated.
///
/// The second result contains the odd lanes of `self` and `other` concatenated.
calebzulawski marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn deinterleave(self, other: Self) -> (Self, Self) {
const fn even() -> [u32; $n] {
let mut idx = [0u32; $n];
let mut i = 0;
while i < $n {
idx[i] = 2 * i as u32;
i += 1;
}
idx
}
const fn odd() -> [u32; $n] {
let mut idx = [0u32; $n];
let mut i = 0;
while i < $n {
idx[i] = 1 + 2 * i as u32;
i += 1;
}
idx
}
(self.shuffle::<{ even() }>(other), self.shuffle::<{ odd() }>(other))
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions crates/core_simd/tests/permute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@ fn simple_shuffle() {
let b = a;
assert_eq!(a.shuffle::<{ [3, 1, 4, 6] }>(b).to_array(), [9, 4, 2, 1]);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn reverse() {
let a = SimdU32::from_array([0, 1, 2, 3, 4, 5, 6, 7]);
assert_eq!(a.reverse().to_array(), [7, 6, 5, 4, 3, 2, 1, 0]);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn interleave() {
let a = SimdU32::from_array([0, 1, 2, 3, 4, 5, 6, 7]);
let b = SimdU32::from_array([8, 9, 10, 11, 12, 13, 14, 15]);
let (lo, hi) = a.interleave(b);
assert_eq!(lo.to_array(), [0, 8, 1, 9, 2, 10, 3, 11]);
assert_eq!(hi.to_array(), [4, 12, 5, 13, 6, 14, 7, 15]);
let (even, odd) = lo.deinterleave(hi);
assert_eq!(even, a);
assert_eq!(odd, b);
}