Skip to content

Commit ce2a59e

Browse files
committed
Properly implement unsigned case in 'PyLong_FromLongLong'.
1 parent bf8059a commit ce2a59e

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TruffleCextBuiltins.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
4545
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
4646

47+
import java.math.BigInteger;
4748
import java.nio.ByteBuffer;
4849
import java.nio.CharBuffer;
4950
import java.nio.charset.CharacterCodingException;
@@ -1897,16 +1898,23 @@ Object doSignedLong(long n, @SuppressWarnings("unused") int signed,
18971898
return toSulongNode.execute(n);
18981899
}
18991900

1900-
@Specialization(guards = "signed == 0")
1901-
Object doUnsignedLong(long n, @SuppressWarnings("unused") int signed,
1901+
@Specialization(guards = {"signed == 0", "n >= 0"})
1902+
Object doUnsignedLongPositive(long n, @SuppressWarnings("unused") int signed,
19021903
@Cached("create()") CExtNodes.ToSulongNode toSulongNode) {
1903-
if (n < 0) {
1904-
CompilerDirectives.transferToInterpreter();
1905-
throw new UnsupportedOperationException();
1906-
}
19071904
return toSulongNode.execute(n);
19081905
}
19091906

1907+
@Specialization(guards = {"signed == 0", "n < 0"})
1908+
Object doUnsignedLongNegative(long n, @SuppressWarnings("unused") int signed,
1909+
@Cached("create()") CExtNodes.ToSulongNode toSulongNode) {
1910+
return toSulongNode.execute(factory().createInt(convertToBigInteger(n)));
1911+
}
1912+
1913+
@TruffleBoundary
1914+
private static BigInteger convertToBigInteger(long n) {
1915+
return BigInteger.valueOf(n).add(BigInteger.ONE.shiftLeft(64));
1916+
}
1917+
19101918
@Specialization
19111919
Object doPointer(TruffleObject n, @SuppressWarnings("unused") int signed,
19121920
@Cached("create()") CExtNodes.ToSulongNode toSulongNode) {

0 commit comments

Comments
 (0)