From 4be18f5cdf77181cbae45fa0303787a5528ef9b8 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Mon, 9 Nov 2026 16:11:53 +0500 Subject: [PATCH 1/2] [mypyc] Fix native int shifts by negative or too large counts Shifts of native ints (i64, i32, i16, u32, u16, u8) were compiled to a plain C shift, which is undefined behavior if the count is negative or at least the bit width. In practice the count was masked, so e.g. i64 `5 >> 64` gave 5 and `8 >> -1` gave 0, and a constant count such as `x >> 64` failed the C build with -Werror. Use the plain shift only if the count is in range. Otherwise a negative count raises ValueError, a left shift gives 0 and a right shift gives 0, or -1 for a negative signed value. Constant counts are resolved at compile time. The common case needs one extra unsigned comparison. Also cast an integer literal left operand to the native int type in C, since the C shift type follows the promoted left operand (`3 << n` for i64 was a 32-bit shift), and add the missing uint16_t cast for unsigned comparisons of i16 values. --- mypyc/codegen/emitfunc.py | 12 +++ mypyc/irbuild/ll_builder.py | 52 +++++++++++++ mypyc/test-data/irbuild-i64.test | 128 ++++++++++++++++++++++++++++--- mypyc/test-data/irbuild-u8.test | 54 +++++++++++++ mypyc/test-data/run-i16.test | 55 +++++++++++++ mypyc/test-data/run-i32.test | 55 +++++++++++++ mypyc/test-data/run-i64.test | 53 +++++++++++++ mypyc/test-data/run-u16.test | 39 ++++++++++ mypyc/test-data/run-u32.test | 39 ++++++++++ mypyc/test-data/run-u8.test | 39 ++++++++++ 10 files changed, 516 insertions(+), 10 deletions(-) diff --git a/mypyc/codegen/emitfunc.py b/mypyc/codegen/emitfunc.py index 14be734866f8a..3fdc91a6382dd 100644 --- a/mypyc/codegen/emitfunc.py +++ b/mypyc/codegen/emitfunc.py @@ -85,6 +85,7 @@ RType, RVec, is_bool_or_bit_rprimitive, + is_int16_rprimitive, is_int32_rprimitive, is_int64_rprimitive, is_int_rprimitive, @@ -937,6 +938,15 @@ def visit_int_op(self, op: IntOp) -> None: dest = self.reg(op) lhs = self.reg(op.lhs) rhs = self.reg(op.rhs) + if ( + op.op in (IntOp.LEFT_SHIFT, IntOp.RIGHT_SHIFT) + and isinstance(op.lhs, Integer) + and isinstance(op.type, RPrimitive) + and op.type.is_native_int + ): + # The type of a C shift is the (promoted) type of the left operand, + # so cast an integer literal to get e.g. a 64-bit shift. + lhs = f"({self.ctype(op.type)}){lhs}" if op.op == IntOp.RIGHT_SHIFT: # Signed right shift lhs = self.emit_signed_int_cast(op.lhs.type) + lhs @@ -1191,6 +1201,8 @@ def emit_unsigned_int_cast(self, type: RType) -> str: return "(uint32_t)" elif is_int64_rprimitive(type): return "(uint64_t)" + elif is_int16_rprimitive(type): + return "(uint16_t)" else: return "" diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 01ee6dc6442d9..26b5b46583cc8 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -2820,8 +2820,60 @@ def fixed_width_int_op( else: assert False, type return self.call_c(prim, [lhs, rhs], line) + if op in (IntOp.LEFT_SHIFT, IntOp.RIGHT_SHIFT): + return self.fixed_width_int_shift(type, lhs, rhs, op, line) return self.int_op(type, lhs, rhs, op, line) + def fixed_width_int_shift( + self, type: RPrimitive, lhs: Value, rhs: Value, op: int, line: int + ) -> Value: + """Generate a native int shift (<< or >>) using Python semantics. + + A C shift by a negative count or by at least the bit width is undefined + behavior, so only use a plain shift when the count is in range. A negative + count raises ValueError. A larger count produces 0 (or -1 for a right shift + of a negative signed value). + """ + bits = type.size * 8 + if isinstance(rhs, Integer): + count = rhs.numeric_value() + if 0 <= count < bits: + return self.int_op(type, lhs, rhs, op, line) + if count < 0: + self.check_for_negative_shift_count(rhs, type, line) + return self.large_shift_result(type, lhs, op, line) + res = Register(type) + in_range, out_of_range, done = BasicBlock(), BasicBlock(), BasicBlock() + # Unsigned comparison also catches negative counts + ok = self.add(ComparisonOp(rhs, Integer(bits, type), ComparisonOp.ULT, line)) + self.add(Branch(ok, in_range, out_of_range, Branch.BOOL)) + self.activate_block(in_range) + self.add(Assign(res, self.int_op(type, lhs, rhs, op, line))) + self.add(Goto(done)) + self.activate_block(out_of_range) + if type.is_signed: + self.check_for_negative_shift_count(rhs, type, line) + self.add(Assign(res, self.large_shift_result(type, lhs, op, line))) + self.add(Goto(done)) + self.activate_block(done) + return res + + def large_shift_result(self, type: RPrimitive, lhs: Value, op: int, line: int) -> Value: + """Return the result of lhs << n or lhs >> n, when n >= bit width.""" + if op == IntOp.RIGHT_SHIFT and type.is_signed: + # Either 0 or -1, depending on the sign of lhs + return self.int_op(type, lhs, Integer(type.size * 8 - 1, type), op, line) + return Integer(0, type) + + def check_for_negative_shift_count(self, rhs: Value, type: RType, line: int) -> None: + err, ok = BasicBlock(), BasicBlock() + is_negative = self.add(ComparisonOp(rhs, Integer(0, type), ComparisonOp.SLT, line)) + self.add(Branch(is_negative, err, ok, Branch.BOOL)) + self.activate_block(err) + self.add(RaiseStandardError(RaiseStandardError.VALUE_ERROR, "negative shift count", line)) + self.add(Unreachable()) + self.activate_block(ok) + def check_for_zero_division(self, rhs: Value, type: RType, line: int) -> None: err, ok = BasicBlock(), BasicBlock() is_zero = self.binary_op(rhs, Integer(0, type), "==", line) diff --git a/mypyc/test-data/irbuild-i64.test b/mypyc/test-data/irbuild-i64.test index b861406b43e78..a34b5ed8f806b 100644 --- a/mypyc/test-data/irbuild-i64.test +++ b/mypyc/test-data/irbuild-i64.test @@ -266,7 +266,16 @@ L0: a = r1 return a def others(a, b): - a, b, r0, r1, r2, r3, r4, r5, r6 :: i64 + a, b, r0, r1, r2, r3, r4 :: i64 + r5 :: bit + r6, r7 :: i64 + r8 :: bit + r9 :: bool + r10 :: bit + r11, r12 :: i64 + r13 :: bit + r14 :: bool + r15 :: i64 L0: r0 = a - b a = r0 @@ -278,12 +287,73 @@ L0: a = r3 r4 = a ^ b a = r4 - r5 = a << b - a = r5 - r6 = a >> b - a = r6 + r5 = b < 64 :: unsigned + if r5 goto L1 else goto L2 :: bool +L1: + r6 = a << b + r7 = r6 + goto L5 +L2: + r8 = b < 0 :: signed + if r8 goto L3 else goto L4 :: bool +L3: + r9 = raise ValueError('negative shift count') + unreachable +L4: + r7 = 0 +L5: + a = r7 + r10 = b < 64 :: unsigned + if r10 goto L6 else goto L7 :: bool +L6: + r11 = a >> b + r12 = r11 + goto L10 +L7: + r13 = b < 0 :: signed + if r13 goto L8 else goto L9 :: bool +L8: + r14 = raise ValueError('negative shift count') + unreachable +L9: + r15 = a >> 63 + r12 = r15 +L10: + a = r12 return a +[case testI64ShiftByConstant] +from mypy_extensions import i64 + +def shift(x: i64) -> i64: + a = x << 63 + b = x << 64 + c = x >> 63 + d = x >> 64 + return x >> -1 +[out] +def shift(x): + x, r0, a, b, r1, c, r2, d :: i64 + r3 :: bit + r4 :: bool + r5 :: i64 +L0: + r0 = x << 63 + a = r0 + b = 0 + r1 = x >> 63 + c = r1 + r2 = x >> 63 + d = r2 + r3 = -1 < 0 :: signed + if r3 goto L1 else goto L2 :: bool +L1: + r4 = raise ValueError('negative shift count') + unreachable +L2: + r5 = x >> 63 + return r5 + [case testI64BitwiseOps] from mypy_extensions import i64 @@ -321,7 +391,16 @@ L0: b = r4 return b def reverse(a, b): - a, b, r0, r1, r2, r3, r4 :: i64 + a, b, r0, r1, r2 :: i64 + r3 :: bit + r4, r5 :: i64 + r6 :: bit + r7 :: bool + r8 :: bit + r9, r10 :: i64 + r11 :: bit + r12 :: bool + r13 :: i64 L0: r0 = 1 & a b = r0 @@ -329,10 +408,39 @@ L0: a = r1 r2 = 3 ^ a b = r2 - r3 = 4 << b - a = r3 - r4 = 5 >> a - b = r4 + r3 = b < 64 :: unsigned + if r3 goto L1 else goto L2 :: bool +L1: + r4 = 4 << b + r5 = r4 + goto L5 +L2: + r6 = b < 0 :: signed + if r6 goto L3 else goto L4 :: bool +L3: + r7 = raise ValueError('negative shift count') + unreachable +L4: + r5 = 0 +L5: + a = r5 + r8 = a < 64 :: unsigned + if r8 goto L6 else goto L7 :: bool +L6: + r9 = 5 >> a + r10 = r9 + goto L10 +L7: + r11 = a < 0 :: signed + if r11 goto L8 else goto L9 :: bool +L8: + r12 = raise ValueError('negative shift count') + unreachable +L9: + r13 = 5 >> 63 + r10 = r13 +L10: + b = r10 return b def unary(a): a, r0 :: i64 diff --git a/mypyc/test-data/irbuild-u8.test b/mypyc/test-data/irbuild-u8.test index 14f691c9451f2..11887391f9513 100644 --- a/mypyc/test-data/irbuild-u8.test +++ b/mypyc/test-data/irbuild-u8.test @@ -148,6 +148,60 @@ L4: r5 = a % y return r5 +[case testU8ShiftByVariable] +from mypy_extensions import u8 + +def shift(x: u8, y: u8) -> u8: + a = x << y + return a >> y +[out] +def shift(x, y): + x, y :: u8 + r0 :: bit + r1, r2, a :: u8 + r3 :: bit + r4, r5 :: u8 +L0: + r0 = y < 8 :: unsigned + if r0 goto L1 else goto L2 :: bool +L1: + r1 = x << y + r2 = r1 + goto L3 +L2: + r2 = 0 +L3: + a = r2 + r3 = y < 8 :: unsigned + if r3 goto L4 else goto L5 :: bool +L4: + r4 = a >> y + r5 = r4 + goto L6 +L5: + r5 = 0 +L6: + return r5 + +[case testU8ShiftByConstant] +from mypy_extensions import u8 + +def shift(x: u8) -> u8: + a = x << 7 + b = x << 8 + c = x >> 7 + return c >> 100 +[out] +def shift(x): + x, r0, a, b, r1, c :: u8 +L0: + r0 = x << 7 + a = r0 + b = 0 + r1 = x >> 7 + c = r1 + return 0 + [case testU8BinaryOperationWithOutOfRangeOperand] from mypy_extensions import u8 diff --git a/mypyc/test-data/run-i16.test b/mypyc/test-data/run-i16.test index 5dd766871ebb1..dfa3e593b72bd 100644 --- a/mypyc/test-data/run-i16.test +++ b/mypyc/test-data/run-i16.test @@ -218,6 +218,61 @@ def test_mixed_comparisons() -> None: with assertRaises(ValueError): assert int_too_small < i16_3 +def truncate_i16(n: int) -> int: + n &= (1 << 16) - 1 + return n - (1 << 16) if n >= (1 << 15) else n + +def test_shift_count_out_of_range() -> None: + for x in 0, 1, 5, -1, -8, (1 << 15) - 1, -(1 << 15): + v: i16 = x + for n in 0, 1, 15, 16, 17, 64, 100: + c: i16 = n + assert v << c == truncate_i16(x << n) + assert v >> c == x >> n + nn: int = n + assert v << nn == truncate_i16(x << n) + assert v >> nn == x >> n + y = v + y <<= c + assert y == truncate_i16(x << n) + y = v + y >>= c + assert y == x >> n + assert v << 0 == x + assert v << 15 == truncate_i16(x << 15) + assert v << 16 == 0 + assert v << 17 == 0 + assert v << 100 == 0 + assert v >> 0 == x + assert v >> 15 == x >> 15 + assert v >> 16 == x >> 16 + assert v >> 17 == x >> 17 + assert v >> 100 == x >> 100 + y = v + y >>= 16 + assert y == x >> 16 + assert v << 64 == 0 + assert v >> 64 == x >> 64 + for n in -1, -16, -100: + c = n + with assertRaises(ValueError, "negative shift count"): + y = v << c + with assertRaises(ValueError, "negative shift count"): + y = v >> c + nn = n + with assertRaises(ValueError, "negative shift count"): + y = v << nn + with assertRaises(ValueError, "negative shift count"): + y = v >> nn + with assertRaises(ValueError, "negative shift count"): + y = v << -1 + with assertRaises(ValueError, "negative shift count"): + y = v >> -1 + for n in 0, 1, 15, 16, 17, 64, 100: + c = n + assert 3 << c == truncate_i16(3 << n) + assert -5 >> c == -5 >> n + def test_mixed_arithmetic_and_bitwise_ops() -> None: i16_3: i16 = int() + 3 int_5 = int() + 5 diff --git a/mypyc/test-data/run-i32.test b/mypyc/test-data/run-i32.test index 857279fac8172..1511def910b19 100644 --- a/mypyc/test-data/run-i32.test +++ b/mypyc/test-data/run-i32.test @@ -218,6 +218,61 @@ def test_mixed_comparisons() -> None: with assertRaises(ValueError): assert int_too_small < i32_3 +def truncate_i32(n: int) -> int: + n &= (1 << 32) - 1 + return n - (1 << 32) if n >= (1 << 31) else n + +def test_shift_count_out_of_range() -> None: + for x in 0, 1, 5, -1, -8, (1 << 31) - 1, -(1 << 31): + v: i32 = x + for n in 0, 1, 31, 32, 33, 64, 100: + c: i32 = n + assert v << c == truncate_i32(x << n) + assert v >> c == x >> n + nn: int = n + assert v << nn == truncate_i32(x << n) + assert v >> nn == x >> n + y = v + y <<= c + assert y == truncate_i32(x << n) + y = v + y >>= c + assert y == x >> n + assert v << 0 == x + assert v << 31 == truncate_i32(x << 31) + assert v << 32 == 0 + assert v << 33 == 0 + assert v << 100 == 0 + assert v >> 0 == x + assert v >> 31 == x >> 31 + assert v >> 32 == x >> 32 + assert v >> 33 == x >> 33 + assert v >> 100 == x >> 100 + y = v + y >>= 32 + assert y == x >> 32 + assert v << 64 == 0 + assert v >> 64 == x >> 64 + for n in -1, -32, -100: + c = n + with assertRaises(ValueError, "negative shift count"): + y = v << c + with assertRaises(ValueError, "negative shift count"): + y = v >> c + nn = n + with assertRaises(ValueError, "negative shift count"): + y = v << nn + with assertRaises(ValueError, "negative shift count"): + y = v >> nn + with assertRaises(ValueError, "negative shift count"): + y = v << -1 + with assertRaises(ValueError, "negative shift count"): + y = v >> -1 + for n in 0, 1, 31, 32, 33, 64, 100: + c = n + assert 3 << c == truncate_i32(3 << n) + assert -5 >> c == -5 >> n + def test_mixed_arithmetic_and_bitwise_ops() -> None: i32_3: i32 = int() + 3 int_5 = int() + 5 diff --git a/mypyc/test-data/run-i64.test b/mypyc/test-data/run-i64.test index b548e4ad0472a..64ce40b027817 100644 --- a/mypyc/test-data/run-i64.test +++ b/mypyc/test-data/run-i64.test @@ -501,6 +501,59 @@ def test_mixed_comparisons_32bit() -> None: assert int_smallest_short_i64 > int_small assert int_largest_short_i64 < int_big +def truncate_i64(n: int) -> int: + n &= (1 << 64) - 1 + return n - (1 << 64) if n >= (1 << 63) else n + +def test_shift_count_out_of_range() -> None: + for x in 0, 1, 5, -1, -8, (1 << 63) - 1, -(1 << 63): + v: i64 = x + for n in 0, 1, 63, 64, 65, 100: + c: i64 = n + assert v << c == truncate_i64(x << n) + assert v >> c == x >> n + nn: int = n + assert v << nn == truncate_i64(x << n) + assert v >> nn == x >> n + y = v + y <<= c + assert y == truncate_i64(x << n) + y = v + y >>= c + assert y == x >> n + assert v << 0 == x + assert v << 63 == truncate_i64(x << 63) + assert v << 64 == 0 + assert v << 65 == 0 + assert v << 100 == 0 + assert v >> 0 == x + assert v >> 63 == x >> 63 + assert v >> 64 == x >> 64 + assert v >> 65 == x >> 65 + assert v >> 100 == x >> 100 + y = v + y >>= 64 + assert y == x >> 64 + for n in -1, -64, -100: + c = n + with assertRaises(ValueError, "negative shift count"): + y = v << c + with assertRaises(ValueError, "negative shift count"): + y = v >> c + nn = n + with assertRaises(ValueError, "negative shift count"): + y = v << nn + with assertRaises(ValueError, "negative shift count"): + y = v >> nn + with assertRaises(ValueError, "negative shift count"): + y = v << -1 + with assertRaises(ValueError, "negative shift count"): + y = v >> -1 + for n in 0, 1, 63, 64, 65, 100: + c = n + assert 3 << c == truncate_i64(3 << n) + assert -5 >> c == -5 >> n + def test_mixed_arithmetic_and_bitwise_ops() -> None: i64_3: i64 = int() + 3 int_5 = int() + 5 diff --git a/mypyc/test-data/run-u16.test b/mypyc/test-data/run-u16.test index 7278cd8d5b1a7..0b1495cfa8a80 100644 --- a/mypyc/test-data/run-u16.test +++ b/mypyc/test-data/run-u16.test @@ -175,6 +175,45 @@ def test_mixed_comparisons() -> None: with assertRaises(ValueError): assert int_too_small < u16_3 +def truncate_u16(n: int) -> int: + return n & ((1 << 16) - 1) + +def test_shift_count_out_of_range() -> None: + for x in 0, 1, 5, (1 << 16) - 1: + v: u16 = x + for n in 0, 1, 15, 16, 17, 64, 100: + c: u16 = n + assert v << c == truncate_u16(x << n) + assert v >> c == x >> n + nn: int = n + assert v << nn == truncate_u16(x << n) + assert v >> nn == x >> n + y = v + y <<= c + assert y == truncate_u16(x << n) + y = v + y >>= c + assert y == x >> n + assert v << 0 == x + assert v << 15 == truncate_u16(x << 15) + assert v << 16 == 0 + assert v << 17 == 0 + assert v << 100 == 0 + assert v >> 0 == x + assert v >> 15 == x >> 15 + assert v >> 16 == x >> 16 + assert v >> 17 == x >> 17 + assert v >> 100 == x >> 100 + y = v + y >>= 16 + assert y == x >> 16 + assert v << 64 == 0 + assert v >> 64 == x >> 64 + for n in 0, 1, 15, 16, 17, 64, 100: + c = n + assert 3 << c == truncate_u16(3 << n) + assert 200 >> c == 200 >> n + def test_mixed_arithmetic_and_bitwise_ops() -> None: u16_3: u16 = int() + 3 int_5 = int() + 5 diff --git a/mypyc/test-data/run-u32.test b/mypyc/test-data/run-u32.test index 8f7b727aeb122..c0b98ba00c887 100644 --- a/mypyc/test-data/run-u32.test +++ b/mypyc/test-data/run-u32.test @@ -175,6 +175,45 @@ def test_mixed_comparisons() -> None: with assertRaises(ValueError): assert int_too_small < u32_3 +def truncate_u32(n: int) -> int: + return n & ((1 << 32) - 1) + +def test_shift_count_out_of_range() -> None: + for x in 0, 1, 5, (1 << 32) - 1: + v: u32 = x + for n in 0, 1, 31, 32, 33, 64, 100: + c: u32 = n + assert v << c == truncate_u32(x << n) + assert v >> c == x >> n + nn: int = n + assert v << nn == truncate_u32(x << n) + assert v >> nn == x >> n + y = v + y <<= c + assert y == truncate_u32(x << n) + y = v + y >>= c + assert y == x >> n + assert v << 0 == x + assert v << 31 == truncate_u32(x << 31) + assert v << 32 == 0 + assert v << 33 == 0 + assert v << 100 == 0 + assert v >> 0 == x + assert v >> 31 == x >> 31 + assert v >> 32 == x >> 32 + assert v >> 33 == x >> 33 + assert v >> 100 == x >> 100 + y = v + y >>= 32 + assert y == x >> 32 + assert v << 64 == 0 + assert v >> 64 == x >> 64 + for n in 0, 1, 31, 32, 33, 64, 100: + c = n + assert 3 << c == truncate_u32(3 << n) + assert 200 >> c == 200 >> n + def test_mixed_arithmetic_and_bitwise_ops() -> None: u32_3: u32 = int() + 3 int_5 = int() + 5 diff --git a/mypyc/test-data/run-u8.test b/mypyc/test-data/run-u8.test index 05844de8ecff7..b921d8c852f8d 100644 --- a/mypyc/test-data/run-u8.test +++ b/mypyc/test-data/run-u8.test @@ -175,6 +175,45 @@ def test_mixed_comparisons() -> None: with assertRaises(ValueError): assert int_too_small < u8_3 +def truncate_u8(n: int) -> int: + return n & ((1 << 8) - 1) + +def test_shift_count_out_of_range() -> None: + for x in 0, 1, 5, (1 << 8) - 1: + v: u8 = x + for n in 0, 1, 7, 8, 9, 64, 100: + c: u8 = n + assert v << c == truncate_u8(x << n) + assert v >> c == x >> n + nn: int = n + assert v << nn == truncate_u8(x << n) + assert v >> nn == x >> n + y = v + y <<= c + assert y == truncate_u8(x << n) + y = v + y >>= c + assert y == x >> n + assert v << 0 == x + assert v << 7 == truncate_u8(x << 7) + assert v << 8 == 0 + assert v << 9 == 0 + assert v << 100 == 0 + assert v >> 0 == x + assert v >> 7 == x >> 7 + assert v >> 8 == x >> 8 + assert v >> 9 == x >> 9 + assert v >> 100 == x >> 100 + y = v + y >>= 8 + assert y == x >> 8 + assert v << 64 == 0 + assert v >> 64 == x >> 64 + for n in 0, 1, 7, 8, 9, 64, 100: + c = n + assert 3 << c == truncate_u8(3 << n) + assert 200 >> c == 200 >> n + def test_mixed_arithmetic_and_bitwise_ops() -> None: u8_3: u8 = int() + 3 int_5 = int() + 5 From 4477a72d8a4fc1c5a0d50d989322168d77c468ea Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 16:01:52 +0500 Subject: [PATCH 2/2] [mypyc] Also fix shifts by negative or too large counts for u64 and i8 The u64 and i8 types were added after the shift fix was written. With master merged in, their shifts go through the same range-checked lowering, but for i8 the unsigned range check was emitted without a uint8_t cast. A negative i8 count was then promoted to a negative int, passed the check and was used as a C shift count (undefined behavior) instead of raising ValueError. Add the missing uint8_t cast for unsigned comparisons of i8 values, and add shift run tests for u64 and i8 plus irbuild tests, mirroring the existing ones for the other native int types. --- mypyc/codegen/emitfunc.py | 3 ++ mypyc/test-data/irbuild-i8.test | 54 +++++++++++++++++++++++++++++++ mypyc/test-data/irbuild-u64.test | 19 +++++++++++ mypyc/test-data/run-i8.test | 55 ++++++++++++++++++++++++++++++++ mypyc/test-data/run-u64.test | 37 +++++++++++++++++++++ 5 files changed, 168 insertions(+) diff --git a/mypyc/codegen/emitfunc.py b/mypyc/codegen/emitfunc.py index 3fdc91a6382dd..72c371f20483b 100644 --- a/mypyc/codegen/emitfunc.py +++ b/mypyc/codegen/emitfunc.py @@ -85,6 +85,7 @@ RType, RVec, is_bool_or_bit_rprimitive, + is_int8_rprimitive, is_int16_rprimitive, is_int32_rprimitive, is_int64_rprimitive, @@ -1203,6 +1204,8 @@ def emit_unsigned_int_cast(self, type: RType) -> str: return "(uint64_t)" elif is_int16_rprimitive(type): return "(uint16_t)" + elif is_int8_rprimitive(type): + return "(uint8_t)" else: return "" diff --git a/mypyc/test-data/irbuild-i8.test b/mypyc/test-data/irbuild-i8.test index e7b5bbd09f919..9f7313d123d67 100644 --- a/mypyc/test-data/irbuild-i8.test +++ b/mypyc/test-data/irbuild-i8.test @@ -15,6 +15,60 @@ L0: r2 = r0 + r1 return r2 +[case testI8ShiftByVariable] +from mypy_extensions import i8 + +def shift(x: i8, y: i8) -> i8: + a = x << y + return a >> y +[out] +def shift(x, y): + x, y :: i8 + r0 :: bit + r1, r2 :: i8 + r3 :: bit + r4 :: bool + a :: i8 + r5 :: bit + r6, r7 :: i8 + r8 :: bit + r9 :: bool + r10 :: i8 +L0: + r0 = y < 8 :: unsigned + if r0 goto L1 else goto L2 :: bool +L1: + r1 = x << y + r2 = r1 + goto L5 +L2: + r3 = y < 0 :: signed + if r3 goto L3 else goto L4 :: bool +L3: + r4 = raise ValueError('negative shift count') + unreachable +L4: + r2 = 0 +L5: + a = r2 + r5 = y < 8 :: unsigned + if r5 goto L6 else goto L7 :: bool +L6: + r6 = a >> y + r7 = r6 + goto L10 +L7: + r8 = y < 0 :: signed + if r8 goto L8 else goto L9 :: bool +L8: + r9 = raise ValueError('negative shift count') + unreachable +L9: + r10 = a >> 7 + r7 = r10 +L10: + return r7 + [case testI8BoxAndUnbox] from typing import Any from mypy_extensions import i8 diff --git a/mypyc/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index 765192e9390fd..16a6e6ebd8702 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -62,6 +62,25 @@ L0: f = r5 return 1 +[case testU64ShiftByConstant] +from mypy_extensions import u64 + +def shift(x: u64) -> u64: + a = x << 63 + b = x << 64 + c = x >> 63 + return c >> 100 +[out] +def shift(x): + x, r0, a, b, r1, c :: u64 +L0: + r0 = x << 63 + a = r0 + b = 0 + r1 = x >> 63 + c = r1 + return 0 + [case testU64DetectMoreOutOfRangeLiterals] from mypy_extensions import u64 diff --git a/mypyc/test-data/run-i8.test b/mypyc/test-data/run-i8.test index 1998821f812d4..f2019977dbbeb 100644 --- a/mypyc/test-data/run-i8.test +++ b/mypyc/test-data/run-i8.test @@ -54,6 +54,61 @@ def test_operations() -> None: assert y >> 2 == -3 assert ~x == -66 +def truncate_i8(n: int) -> int: + n &= (1 << 8) - 1 + return n - (1 << 8) if n >= (1 << 7) else n + +def test_shift_count_out_of_range() -> None: + for x in 0, 1, 5, -1, -8, (1 << 7) - 1, -(1 << 7): + v: i8 = x + for n in 0, 1, 7, 8, 9, 64, 100: + c: i8 = n + assert v << c == truncate_i8(x << n) + assert v >> c == x >> n + nn: int = n + assert v << nn == truncate_i8(x << n) + assert v >> nn == x >> n + y = v + y <<= c + assert y == truncate_i8(x << n) + y = v + y >>= c + assert y == x >> n + assert v << 0 == x + assert v << 7 == truncate_i8(x << 7) + assert v << 8 == 0 + assert v << 9 == 0 + assert v << 100 == 0 + assert v >> 0 == x + assert v >> 7 == x >> 7 + assert v >> 8 == x >> 8 + assert v >> 9 == x >> 9 + assert v >> 100 == x >> 100 + y = v + y >>= 8 + assert y == x >> 8 + assert v << 64 == 0 + assert v >> 64 == x >> 64 + for n in -1, -8, -100: + c = n + with assertRaises(ValueError, "negative shift count"): + y = v << c + with assertRaises(ValueError, "negative shift count"): + y = v >> c + nn = n + with assertRaises(ValueError, "negative shift count"): + y = v << nn + with assertRaises(ValueError, "negative shift count"): + y = v >> nn + with assertRaises(ValueError, "negative shift count"): + y = v << -1 + with assertRaises(ValueError, "negative shift count"): + y = v >> -1 + for n in 0, 1, 7, 8, 9, 64, 100: + c = n + assert 3 << c == truncate_i8(3 << n) + assert -5 >> c == -5 >> n + def test_conversions() -> None: assert to_i8(127) == 127 assert to_i8(128) == -128 diff --git a/mypyc/test-data/run-u64.test b/mypyc/test-data/run-u64.test index 012b26815c47e..85630dd066f0f 100644 --- a/mypyc/test-data/run-u64.test +++ b/mypyc/test-data/run-u64.test @@ -175,6 +175,43 @@ def test_mixed_comparisons() -> None: with assertRaises(ValueError): assert int_too_small < u64_3 +def truncate_u64(n: int) -> int: + return n & ((1 << 64) - 1) + +def test_shift_count_out_of_range() -> None: + for x in 0, 1, 5, (1 << 63), (1 << 64) - 1: + v: u64 = x + for n in 0, 1, 63, 64, 65, 100: + c: u64 = n + assert v << c == truncate_u64(x << n) + assert v >> c == x >> n + nn: int = n + assert v << nn == truncate_u64(x << n) + assert v >> nn == x >> n + y = v + y <<= c + assert y == truncate_u64(x << n) + y = v + y >>= c + assert y == x >> n + assert v << 0 == x + assert v << 63 == truncate_u64(x << 63) + assert v << 64 == 0 + assert v << 65 == 0 + assert v << 100 == 0 + assert v >> 0 == x + assert v >> 63 == x >> 63 + assert v >> 64 == x >> 64 + assert v >> 65 == x >> 65 + assert v >> 100 == x >> 100 + y = v + y >>= 64 + assert y == x >> 64 + for n in 0, 1, 63, 64, 65, 100: + c = n + assert 3 << c == truncate_u64(3 << n) + assert 200 >> c == 200 >> n + def test_mixed_arithmetic_and_bitwise_ops() -> None: u64_3: u64 = int() + 3 int_5 = int() + 5