Skip to content

Commit 19cd2c7

Browse files
committed
[GR-23373] Again: Fix rshift of int/long with large right operand.
PullRequest: graalpython/964
2 parents 78f9fe7 + 96b16ef commit 19cd2c7

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_binary_arithmetic.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,18 @@ def test_sub():
275275

276276
def test_rshift():
277277
assert 1 >> 64 == 0
278-
assert 0xffffffffffff >> 128 == 0
278+
assert 0xffffffffffff >> 128 == 0
279+
assert 0x7fffffec >> 24 == 0x7f
280+
assert 0x7fffffec >> 32 == 0
281+
assert 0x7fffffffffffffec >> 64 == 0
282+
bigint = 0xffffffffffffffffffffffffffffffff
283+
assert bigint >> 120 == 0xff
284+
assert bigint >> 128 == 0
285+
trimmed = bigint & 0x7fffffffffffffff
286+
assert trimmed >> 64 == 0
287+
288+
289+
def test_lshift():
290+
assert 1 << 32 == 0x100000000
291+
assert 1 << 64 == 0x10000000000000000
292+
assert 1 << 128 == 0x100000000000000000000000000000000

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ private void raiseNegativeShiftCount(boolean cond) {
12141214
@TypeSystemReference(PythonArithmeticTypes.class)
12151215
@GenerateNodeFactory
12161216
abstract static class RShiftNode extends PythonBinaryBuiltinNode {
1217-
@Specialization(guards = "right < 64")
1217+
@Specialization(guards = "right < 32")
12181218
int doIISmall(int left, int right) {
12191219
raiseNegativeShiftCount(right < 0);
12201220
return left >> right;
@@ -1225,10 +1225,10 @@ int doII(int left, int right) {
12251225
raiseNegativeShiftCount(right < 0);
12261226
// Note: according to JLS, if 'left' is an int, then only the 5 LSBs of 'right' are
12271227
// considered. However, Python would consider more bits, so do the max possible shift.
1228-
return left >> (right >= 64 ? 63 : right);
1228+
return left >> (right >= 32 ? 31 : right);
12291229
}
12301230

1231-
@Specialization(guards = "right < 128")
1231+
@Specialization(guards = "right < 64")
12321232
long doLLSmall(long left, long right) {
12331233
raiseNegativeShiftCount(right < 0);
12341234
return left >> right;
@@ -1238,7 +1238,7 @@ long doLLSmall(long left, long right) {
12381238
long doLL(long left, long right) {
12391239
raiseNegativeShiftCount(right < 0);
12401240
// for explanation, see 'doII'
1241-
return left >> (right >= 128 ? 127 : right);
1241+
return left >> (right >= 64 ? 63 : right);
12421242
}
12431243

12441244
@Specialization

0 commit comments

Comments
 (0)