Skip to content

Commit af70373

Browse files
committed
Implement bit-reverse for 128-bit integers
1 parent 294f06b commit af70373

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

gcc-test-backend/src/main.rs

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

196-
const A: u64 = 0b0101100;
197-
const B: u64 = 0b0100001;
198-
const C: u64 = 0b1111001;
196+
{
197+
const A: u128 = 0b0101100;
198+
const B: u128 = 0b0100001;
199+
const C: u128 = 0b1111001;
199200

200-
const _0: u64 = 0;
201-
const _1: u64 = !0;
201+
const _0: u128 = 0;
202+
const _1: u128 = !0;
202203

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);
204+
assert_eq!(A.reverse_bits().reverse_bits(), A);
205+
assert_eq!(B.reverse_bits().reverse_bits(), B);
206+
assert_eq!(C.reverse_bits().reverse_bits(), C);
206207

207-
// Swapping these should make no difference
208-
assert_eq!(_0.reverse_bits(), _0);
209-
assert_eq!(_1.reverse_bits(), _1);
208+
// Swapping these should make no difference
209+
assert_eq!(_0.reverse_bits(), _0);
210+
assert_eq!(_1.reverse_bits(), _1);
211+
}
210212

211-
/*const _0: i128 = 0;
213+
const _0: i128 = 0;
212214
const _1: i128 = !0;
213215

214216
use std::i128::MAX;
@@ -233,5 +235,5 @@ fn main() {
233235

234236
let x: i128 = 0b0010_1100;
235237
assert_eq!(x.leading_ones(), 0);
236-
assert_eq!(x.trailing_ones(), 0);*/
238+
assert_eq!(x.trailing_ones(), 0);
237239
}

src/intrinsic.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,18 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
717717
step5
718718
},
719719
128 => {
720-
// TODO
721-
value
720+
// TODO: find a more efficient implementation?
721+
let sixty_four = self.context.new_rvalue_from_long(typ, 64);
722+
let high = self.context.new_cast(None, value >> sixty_four, self.u64_type);
723+
let low = self.context.new_cast(None, value, self.u64_type);
724+
725+
let reversed_high = self.bit_reverse(64, high);
726+
let reversed_low = self.bit_reverse(64, low);
727+
728+
let new_low = self.context.new_cast(None, reversed_high, typ);
729+
let new_high = self.context.new_cast(None, reversed_low, typ) << sixty_four;
730+
731+
new_low | new_high
722732
},
723733
_ => {
724734
panic!("cannot bit reverse with width = {}", width);

0 commit comments

Comments
 (0)