|
| 1 | +//! Implements integer rotates. |
| 2 | +#![allow(unused)] |
| 3 | + |
| 4 | +macro_rules! impl_vector_rotates { |
| 5 | + ($id:ident, $elem_ty:ident) => { |
| 6 | + impl $id { |
| 7 | + /// Shifts the bits of each lane to the left by the specified amount in |
| 8 | + /// the corresponding lane of `n`, wrapping the truncated bits to |
| 9 | + /// the end of the resulting integer. |
| 10 | + /// |
| 11 | + /// Please note this isn't the same operation as `<<`!. Also note it |
| 12 | + /// isn't equivalent to `slice::rotate_left` (that can be implemented |
| 13 | + /// with vector shuffles). |
| 14 | + #[inline] |
| 15 | + pub fn rotate_left(self, n: $id) -> $id { |
| 16 | + const LANE_WIDTH: $elem_ty = ::mem::size_of::<$elem_ty>() as $elem_ty * 8; |
| 17 | + // Protect against undefined behavior for over-long bit shifts |
| 18 | + let n = n % LANE_WIDTH; |
| 19 | + (self << n) | (self >> ((LANE_WIDTH - n) % LANE_WIDTH)) |
| 20 | + } |
| 21 | + |
| 22 | + /// Shifts the bits of each lane to the right by the specified amount in |
| 23 | + /// the corresponding lane of `n`, wrapping the truncated bits to |
| 24 | + /// the beginning of the resulting integer. |
| 25 | + /// |
| 26 | + /// Please note this isn't the same operation as `>>`!. Also note it |
| 27 | + /// isn't similar to `slice::rotate_right`, it doesn't move the vector's |
| 28 | + /// lanes around. (that can be implemented with vector shuffles). |
| 29 | + #[inline] |
| 30 | + pub fn rotate_right(self, n: $id) -> $id { |
| 31 | + const LANE_WIDTH: $elem_ty = ::mem::size_of::<$elem_ty>() as $elem_ty * 8; |
| 32 | + // Protect against undefined behavior for over-long bit shifts |
| 33 | + let n = n % LANE_WIDTH; |
| 34 | + (self >> n) | (self << ((LANE_WIDTH - n) % LANE_WIDTH)) |
| 35 | + } |
| 36 | + } |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +#[cfg(test)] |
| 41 | +macro_rules! test_vector_rotate_ops { |
| 42 | + ($id:ident, $elem_ty:ident) => { |
| 43 | + #[test] |
| 44 | + fn rotate_ops() { |
| 45 | + use coresimd::simd::$id; |
| 46 | + use std::mem; |
| 47 | + let z = $id::splat(0 as $elem_ty); |
| 48 | + let o = $id::splat(1 as $elem_ty); |
| 49 | + let t = $id::splat(2 as $elem_ty); |
| 50 | + let f = $id::splat(4 as $elem_ty); |
| 51 | + |
| 52 | + let max = |
| 53 | + $id::splat((mem::size_of::<$elem_ty>() * 8 - 1) as $elem_ty); |
| 54 | + |
| 55 | + // rotate_right |
| 56 | + assert_eq!(z.rotate_right(z), z); |
| 57 | + assert_eq!(z.rotate_right(o), z); |
| 58 | + assert_eq!(z.rotate_right(t), z); |
| 59 | + |
| 60 | + assert_eq!(o.rotate_right(z), o); |
| 61 | + assert_eq!(t.rotate_right(z), t); |
| 62 | + assert_eq!(f.rotate_right(z), f); |
| 63 | + assert_eq!(f.rotate_right(max), f << 1); |
| 64 | + |
| 65 | + assert_eq!(o.rotate_right(o), o << max); |
| 66 | + assert_eq!(t.rotate_right(o), o); |
| 67 | + assert_eq!(t.rotate_right(t), o << max); |
| 68 | + assert_eq!(f.rotate_right(o), t); |
| 69 | + assert_eq!(f.rotate_right(t), o); |
| 70 | + |
| 71 | + // rotate_left |
| 72 | + assert_eq!(z.rotate_left(z), z); |
| 73 | + assert_eq!(o.rotate_left(z), o); |
| 74 | + assert_eq!(t.rotate_left(z), t); |
| 75 | + assert_eq!(f.rotate_left(z), f); |
| 76 | + assert_eq!(f.rotate_left(max), t); |
| 77 | + |
| 78 | + assert_eq!(o.rotate_left(o), t); |
| 79 | + assert_eq!(o.rotate_left(t), f); |
| 80 | + assert_eq!(t.rotate_left(o), f); |
| 81 | + } |
| 82 | + }; |
| 83 | +} |
0 commit comments