Skip to content

Commit 44c0204

Browse files
authored
Merge pull request rust-lang#141 from rust-lang/fix/unsigned-by-signed-shift
Fix shift of unsigned integer by signed integer
2 parents d63ea3e + 0fb350f commit 44c0204

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

src/int.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,9 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
6868
let a_native = self.is_native_int_type(a_type);
6969
let b_native = self.is_native_int_type(b_type);
7070
if a_native && b_native {
71-
// FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by an unsigned number.
71+
// FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by a signed number.
7272
// TODO(antoyo): cast to unsigned to do a logical shift if that does not work.
73-
if a_type.is_unsigned(self) && b_type.is_signed(self) {
74-
let a = self.context.new_cast(None, a, b_type);
75-
let result = a >> b;
76-
self.context.new_cast(None, result, a_type)
77-
}
78-
else if a_type.is_signed(self) && b_type.is_unsigned(self) {
73+
if a_type.is_signed(self) != b_type.is_signed(self) {
7974
let b = self.context.new_cast(None, b, a_type);
8075
a >> b
8176
}

tests/run/int.rs

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ fn main(argc: isize, _argv: *const *const u8) -> isize {
7171
assert_eq!(var3 << (argc + 62) as u128, 96618259944854013731572476686437974016);
7272
assert_eq!(var3 << (argc + 63) as u128, 193236519889708027463144953372875948032);
7373

74+
assert_eq!((2220326408_u32 + argc as u32) >> (32 - 6), 33);
75+
7476
assert_eq!(var >> (argc as u128 - 1), var);
7577
assert_eq!(var >> argc as u128, 67108928);
7678
assert_eq!(var >> (argc + 32) as u128, 0);

0 commit comments

Comments
 (0)