Skip to content

Commit 294f06b

Browse files
committed
Fix bit-reverse for 64-bit integers
1 parent c9782e4 commit 294f06b

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

gcc-test-backend/src/main.rs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,45 @@ fn main() {
193193
assert_eq!(r.saturating_pow(3), -8 as i128);
194194
assert_eq!(r.saturating_pow(0), 1 as i128);*/
195195

196-
use std::i128::{MAX, MIN};
197-
assert_eq!((0 as i128).saturating_neg(), 0);
198-
assert_eq!((123 as i128).saturating_neg(), -123);
199-
assert_eq!((-123 as i128).saturating_neg(), 123);
200-
assert_eq!((MAX - 2).saturating_neg(), MIN + 3);
201-
assert_eq!((MAX - 1).saturating_neg(), MIN + 2);
202-
assert_eq!(MAX.saturating_neg(), MIN + 1);
203-
assert_eq!((MIN + 2).saturating_neg(), MAX - 1);
204-
assert_eq!((MIN + 1).saturating_neg(), MAX);
205-
assert_eq!(MIN.saturating_neg(), MAX);
196+
const A: u64 = 0b0101100;
197+
const B: u64 = 0b0100001;
198+
const C: u64 = 0b1111001;
199+
200+
const _0: u64 = 0;
201+
const _1: u64 = !0;
202+
203+
assert_eq!(A.reverse_bits().reverse_bits(), A);
204+
assert_eq!(B.reverse_bits().reverse_bits(), B);
205+
assert_eq!(C.reverse_bits().reverse_bits(), C);
206+
207+
// Swapping these should make no difference
208+
assert_eq!(_0.reverse_bits(), _0);
209+
assert_eq!(_1.reverse_bits(), _1);
210+
211+
/*const _0: i128 = 0;
212+
const _1: i128 = !0;
213+
214+
use std::i128::MAX;
215+
216+
let a: i128 = 0b0101_1111;
217+
assert_eq!(a.trailing_ones(), 5);
218+
assert_eq!((!a).leading_ones(), i128::BITS - 7);
219+
220+
assert_eq!(a.reverse_bits().leading_ones(), 5);
221+
222+
assert_eq!(_1.leading_ones(), i128::BITS);
223+
assert_eq!(_1.trailing_ones(), i128::BITS);
224+
225+
assert_eq!((_1 << 1).trailing_ones(), 0);
226+
assert_eq!(MAX.leading_ones(), 0);
227+
228+
assert_eq!((_1 << 1).leading_ones(), i128::BITS - 1);
229+
assert_eq!(MAX.trailing_ones(), i128::BITS - 1);
230+
231+
assert_eq!(_0.leading_ones(), 0);
232+
assert_eq!(_0.trailing_ones(), 0);
233+
234+
let x: i128 = 0b0010_1100;
235+
assert_eq!(x.leading_ones(), 0);
236+
assert_eq!(x.trailing_ones(), 0);*/
206237
}

src/intrinsic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,14 +677,14 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
677677
64 => {
678678
// First step.
679679
let left = self.shl(value, context.new_rvalue_from_long(typ, 32));
680-
let right = self.lshr(left, context.new_rvalue_from_long(typ, 32));
680+
let right = self.lshr(value, context.new_rvalue_from_long(typ, 32));
681681
let step1 = self.or(left, right);
682682

683683
// Second step.
684684
let left = self.and(step1, context.new_rvalue_from_long(typ, 0x0001FFFF0001FFFF));
685685
let left = self.shl(left, context.new_rvalue_from_long(typ, 15));
686686
let right = self.and(step1, context.new_rvalue_from_long(typ, 0xFFFE0000FFFE0000u64 as i64)); // TODO: transmute the number instead?
687-
let right = self.shl(right, context.new_rvalue_from_long(typ, 17));
687+
let right = self.lshr(right, context.new_rvalue_from_long(typ, 17));
688688
let step2 = self.or(left, right);
689689

690690
// Third step.

0 commit comments

Comments
 (0)