-
Notifications
You must be signed in to change notification settings - Fork 288
add rotate_left/right #496
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1271a3f
add rotate_left/right
a71eb28
rotate instr tests
be0b786
better match intel pseudocode
32021c8
Revert "better match intel pseudocode"
86b1f48
try instr prefix
31823d7
always inline rotates
cc6c887
try avx512vl
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//! Implements integer rotates. | ||
#![allow(unused)] | ||
|
||
// inline(always) to encourage the compiler to generate rotate instructions | ||
// where available | ||
macro_rules! impl_vector_rotates { | ||
($id:ident, $elem_ty:ident) => { | ||
impl $id { | ||
/// Shifts the bits of each lane to the left by the specified amount in | ||
/// the corresponding lane of `n`, wrapping the truncated bits to | ||
/// the end of the resulting integer. | ||
/// | ||
/// Please note this isn't the same operation as `<<`!. Also note it | ||
/// isn't equivalent to `slice::rotate_left`, it doesn't move the vector's | ||
/// lanes around. (that can be implemented with vector shuffles). | ||
#[inline(always)] | ||
pub fn rotate_left(self, n: $id) -> $id { | ||
const LANE_WIDTH: $elem_ty = ::mem::size_of::<$elem_ty>() as $elem_ty * 8; | ||
// Protect against undefined behavior for over-long bit shifts | ||
let n = n % LANE_WIDTH; | ||
(self << n) | (self >> ((LANE_WIDTH - n) % LANE_WIDTH)) | ||
} | ||
|
||
/// Shifts the bits of each lane to the right by the specified amount in | ||
/// the corresponding lane of `n`, wrapping the truncated bits to | ||
/// the beginning of the resulting integer. | ||
/// | ||
/// Please note this isn't the same operation as `>>`!. Also note it | ||
/// isn't similar to `slice::rotate_right`, it doesn't move the vector's | ||
/// lanes around. (that can be implemented with vector shuffles). | ||
#[inline(always)] | ||
pub fn rotate_right(self, n: $id) -> $id { | ||
const LANE_WIDTH: $elem_ty = ::mem::size_of::<$elem_ty>() as $elem_ty * 8; | ||
// Protect against undefined behavior for over-long bit shifts | ||
let n = n % LANE_WIDTH; | ||
(self >> n) | (self << ((LANE_WIDTH - n) % LANE_WIDTH)) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
#[cfg(test)] | ||
macro_rules! test_vector_rotate_ops { | ||
($id:ident, $elem_ty:ident) => { | ||
#[test] | ||
fn rotate_ops() { | ||
use coresimd::simd::$id; | ||
use std::mem; | ||
let z = $id::splat(0 as $elem_ty); | ||
let o = $id::splat(1 as $elem_ty); | ||
let t = $id::splat(2 as $elem_ty); | ||
let f = $id::splat(4 as $elem_ty); | ||
|
||
let max = | ||
$id::splat((mem::size_of::<$elem_ty>() * 8 - 1) as $elem_ty); | ||
|
||
// rotate_right | ||
assert_eq!(z.rotate_right(z), z); | ||
assert_eq!(z.rotate_right(o), z); | ||
assert_eq!(z.rotate_right(t), z); | ||
|
||
assert_eq!(o.rotate_right(z), o); | ||
assert_eq!(t.rotate_right(z), t); | ||
assert_eq!(f.rotate_right(z), f); | ||
assert_eq!(f.rotate_right(max), f << 1); | ||
|
||
assert_eq!(o.rotate_right(o), o << max); | ||
assert_eq!(t.rotate_right(o), o); | ||
assert_eq!(t.rotate_right(t), o << max); | ||
assert_eq!(f.rotate_right(o), t); | ||
assert_eq!(f.rotate_right(t), o); | ||
|
||
// rotate_left | ||
assert_eq!(z.rotate_left(z), z); | ||
assert_eq!(o.rotate_left(z), o); | ||
assert_eq!(t.rotate_left(z), t); | ||
assert_eq!(f.rotate_left(z), f); | ||
assert_eq!(f.rotate_left(max), t); | ||
|
||
assert_eq!(o.rotate_left(o), t); | ||
assert_eq!(o.rotate_left(t), f); | ||
assert_eq!(t.rotate_left(o), f); | ||
} | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! rotate instruction tests | ||
|
||
#![feature(stdsimd)] | ||
#![feature(proc_macro)] | ||
#![feature(avx512_target_feature)] | ||
#![feature(abi_vectorcall)] | ||
|
||
extern crate stdsimd; | ||
extern crate stdsimd_test; | ||
|
||
use stdsimd::simd::*; | ||
use stdsimd_test::assert_instr; | ||
|
||
// Verify that supported hardware compiles rotates into single instructions | ||
|
||
#[inline] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), target_feature(enable = "avx512f"))] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), target_feature(enable = "avx512vl"))] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), assert_instr(vpro))] | ||
unsafe fn rotate_right_variable(x: u64x8) -> u64x8 { | ||
x.rotate_right(u64x8::new(0, 1, 2, 3, 4, 5, 6, 7)) | ||
} | ||
|
||
#[inline] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), target_feature(enable = "avx512f"))] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), target_feature(enable = "avx512vl"))] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), assert_instr(vpro))] | ||
unsafe fn rotate_left_variable(x: u64x8) -> u64x8 { | ||
x.rotate_left(u64x8::new(0, 1, 2, 3, 4, 5, 6, 7)) | ||
} | ||
|
||
#[inline] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), target_feature(enable = "avx512f"))] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), assert_instr(vpro))] | ||
unsafe fn rotate_right(x: u64x8) -> u64x8 { | ||
x.rotate_right(u64x8::splat(12)) | ||
} | ||
|
||
#[inline] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), target_feature(enable = "avx512f"))] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), assert_instr(vpro))] | ||
unsafe fn rotate_left(x: u64x8) -> u64x8 { | ||
x.rotate_left(u64x8::splat(12)) | ||
} | ||
|
||
#[inline] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), target_feature(enable = "avx512f"))] | ||
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), assert_instr(vpro))] | ||
unsafe fn rotate_left_x2(x: u64x2) -> u64x2 { | ||
x.rotate_left(u64x2::splat(12)) | ||
} |
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.
Could you add tests here for 128-bit wide and 256-bit wide vectors as well?