diff options
| author | Antoni Boucher <bouanto@zoho.com> | 2022-03-19 12:15:26 -0400 |
|---|---|---|
| committer | Antoni Boucher <bouanto@zoho.com> | 2022-03-19 12:15:26 -0400 |
| commit | 0fb350f37c71fc0fb9537dd0736449d0fffb8653 (patch) | |
| tree | fdbdf3ffaaf424bd2056283947a20a8e5712bbaf | |
| parent | d63ea3e037a143964b19b40a3f5f0b745a85745e (diff) | |
| download | rust-0fb350f37c71fc0fb9537dd0736449d0fffb8653.tar.gz rust-0fb350f37c71fc0fb9537dd0736449d0fffb8653.zip | |
Fix shift of unsigned integer by signed integer
| -rw-r--r-- | src/int.rs | 9 | ||||
| -rw-r--r-- | tests/run/int.rs | 2 |
2 files changed, 4 insertions, 7 deletions
diff --git a/src/int.rs b/src/int.rs index d2df9d2dcb6..c3ed71ff730 100644 --- a/src/int.rs +++ b/src/int.rs @@ -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 } diff --git a/tests/run/int.rs b/tests/run/int.rs index 7a62fc7d1f7..49376012c40 100644 --- a/tests/run/int.rs +++ b/tests/run/int.rs @@ -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); |
