Fix shift of unsigned integer by signed integer

This commit is contained in:
Antoni Boucher 2022-03-19 12:15:26 -04:00
parent d63ea3e037
commit 0fb350f37c
2 changed files with 4 additions and 7 deletions

View File

@ -68,14 +68,9 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
let a_native = self.is_native_int_type(a_type);
let b_native = self.is_native_int_type(b_type);
if a_native && b_native {
// FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by an unsigned number.
// FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by a signed number.
// TODO(antoyo): cast to unsigned to do a logical shift if that does not work.
if a_type.is_unsigned(self) && b_type.is_signed(self) {
let a = self.context.new_cast(None, a, b_type);
let result = a >> b;
self.context.new_cast(None, result, a_type)
}
else if a_type.is_signed(self) && b_type.is_unsigned(self) {
if a_type.is_signed(self) != b_type.is_signed(self) {
let b = self.context.new_cast(None, b, a_type);
a >> b
}

View File

@ -71,6 +71,8 @@ fn main(argc: isize, _argv: *const *const u8) -> isize {
assert_eq!(var3 << (argc + 62) as u128, 96618259944854013731572476686437974016);
assert_eq!(var3 << (argc + 63) as u128, 193236519889708027463144953372875948032);
assert_eq!((2220326408_u32 + argc as u32) >> (32 - 6), 33);
assert_eq!(var >> (argc as u128 - 1), var);
assert_eq!(var >> argc as u128, 67108928);
assert_eq!(var >> (argc + 32) as u128, 0);