Skip to content

Implement and fix rotate intrinsic #31

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 1 commit into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 52 additions & 15 deletions gcc-test-backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
#![feature(is_sorted)]

fn main() {
let b: u16 = 42;
assert_eq!(b, 42);
let empty: [i32; 0] = [];

assert!([1, 2, 2, 9].is_sorted());
assert!(![1, 3, 2].is_sorted());
assert!([42].is_sorted());
assert!(empty.is_sorted());
assert!(![0.0, 1.0, f32::NAN].is_sorted());
assert!([-2, -1, 0, 3].is_sorted());
assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs()));
assert!(!["c", "bb", "aaa"].is_sorted());
assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len()));
const A: i8 = 0b0101100;
const B: i8 = 0b0100001;
const C: i8 = 0b1111001;

const _0: i8 = 0;
const _1: i8 = !0;

assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);

/*fn rotate_left(x: u32, mut n: u32) -> u32 {
//(x << n) | (x >> (-(n as i32) & 31))
//println!("{}", 0 << 32);
let second_shift = 32 - (n % 32);
n %= 32;
(x << n) | (x >> second_shift)
}

fn irotate_left(x: i32, mut n: u32) -> i32 {
let x = x as u32;
let second_shift = 32 - (n % 32);
n %= 32;
((x << n) | (x >> second_shift)) as i32
}

assert_eq!(rotate_left(2, 48), 131072);
assert_eq!(irotate_left(2, 48), 131072);
assert_eq!(irotate_left(-2, 48), -65537);
assert_eq!(irotate_left(_1, 124), _1);*/

/*const _2: u32 = 0;
assert_eq!(rotate_left(_2, 16), _2);
assert_eq!(rotate_left(_2, 32), _2);
assert_eq!(_2.rotate_left(32), _2);*/

// Rotating these should make no difference
//
// We test using 124 bits because to ensure that overlong bit shifts do
// not cause undefined behaviour. See #10183.
assert_eq!(_0.rotate_left(124), _0);
println!("{:b}", _1);
assert_eq!(_1.rotate_left(124), _1);

// Rotating by 0 should have no effect
assert_eq!(A.rotate_left(0), A);
assert_eq!(B.rotate_left(0), B);
assert_eq!(C.rotate_left(0), C);
// Rotating by a multiple of word size should also have no effect
assert_eq!(A.rotate_left(128), A);
assert_eq!(B.rotate_left(128), B);
assert_eq!(C.rotate_left(128), C);
}
40 changes: 0 additions & 40 deletions patches/0022-core-Disable-not-compiling-tests.patch
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,6 @@ index a35897e..f0bf645 100644

pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
match decode(v).1 {
diff --git a/library/core/tests/num/int_macros.rs b/library/core/tests/num/int_macros.rs
index 0475aeb..9558198 100644
--- a/library/core/tests/num/int_macros.rs
+++ b/library/core/tests/num/int_macros.rs
@@ -88,6 +88,7 @@ mod tests {
assert_eq!(x.trailing_ones(), 0);
}

+ /*
#[test]
fn test_rotate() {
assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
@@ -112,6 +113,7 @@ mod tests {
assert_eq!(B.rotate_left(128), B);
assert_eq!(C.rotate_left(128), C);
}
+ */

#[test]
fn test_swap_bytes() {
diff --git a/library/core/tests/num/uint_macros.rs b/library/core/tests/num/uint_macros.rs
index 04ed14f..a6e372e 100644
--- a/library/core/tests/num/uint_macros.rs
+++ b/library/core/tests/num/uint_macros.rs
@@ -52,6 +52,7 @@ mod tests {
assert_eq!(x.trailing_ones(), 0);
}

+ /*
#[test]
fn test_rotate() {
assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
@@ -76,6 +77,7 @@ mod tests {
assert_eq!(B.rotate_left(128), B);
assert_eq!(C.rotate_left(128), C);
}
+ */

#[test]
fn test_swap_bytes() {
diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs
index 6609bc3..241b497 100644
--- a/library/core/tests/slice.rs
Expand Down
27 changes: 18 additions & 9 deletions src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
self.rotate_left(val, raw_shift, width)
}
else {
// TODO: implement now to enable more tests.
match width {
8 => unimplemented!(),
16 => unimplemented!(),
32 => unimplemented!(),
64 => unimplemented!(),
_ => unimplemented!("rotate for integer of size {}", width),
}
self.rotate_right(val, raw_shift, width)
}
},
sym::saturating_add => {
Expand Down Expand Up @@ -935,13 +928,29 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {

// Algorithm from: https://blog.regehr.org/archives/1063
fn rotate_left(&mut self, value: RValue<'gcc>, shift: RValue<'gcc>, width: u64) -> RValue<'gcc> {
let max = self.context.new_rvalue_from_long(shift.get_type(), width as i64);
let shift = shift % max;
let lhs = self.shl(value, shift);
let result_and =
self.and(
self.context.new_unary_op(None, UnaryOp::Minus, shift.get_type(), shift),
self.context.new_rvalue_from_long(shift.get_type(), width as i64 - 1),
);
let rhs = self.lshr(shift, result_and);
let rhs = self.lshr(value, result_and);
self.or(lhs, rhs)
}

// Algorithm from: https://blog.regehr.org/archives/1063
fn rotate_right(&mut self, value: RValue<'gcc>, shift: RValue<'gcc>, width: u64) -> RValue<'gcc> {
let max = self.context.new_rvalue_from_long(shift.get_type(), width as i64);
let shift = shift % max;
let lhs = self.lshr(value, shift);
let result_and =
self.and(
self.context.new_unary_op(None, UnaryOp::Minus, shift.get_type(), shift),
self.context.new_rvalue_from_long(shift.get_type(), width as i64 - 1),
);
let rhs = self.shl(value, result_and);
self.or(lhs, rhs)
}

Expand Down