Skip to content

Commit e313e0d

Browse files
committed
Specialize 'PyLong_FromLongLong'.
1 parent 870e9e7 commit e313e0d

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,4 +1863,39 @@ Object createCache(TruffleObject ptrToResolveHandle) {
18631863
return new HandleCache(ptrToResolveHandle);
18641864
}
18651865
}
1866+
1867+
@Builtin(name = "PyLong_FromLongLong", fixedNumOfPositionalArgs = 2)
1868+
@GenerateNodeFactory
1869+
abstract static class PyLong_FromLongLong extends PythonBinaryBuiltinNode {
1870+
@Specialization(guards = "signed != 0")
1871+
Object doSignedInt(int n, @SuppressWarnings("unused") int signed,
1872+
@Cached("create()") CExtNodes.ToSulongNode toSulongNode) {
1873+
return toSulongNode.execute(n);
1874+
}
1875+
1876+
@Specialization(guards = "signed == 0")
1877+
Object doUnsignedInt(int n, @SuppressWarnings("unused") int signed,
1878+
@Cached("create()") CExtNodes.ToSulongNode toSulongNode) {
1879+
if (n < 0) {
1880+
return toSulongNode.execute(n & 0xFFFFFFFFL);
1881+
}
1882+
return toSulongNode.execute(n);
1883+
}
1884+
1885+
@Specialization(guards = "signed != 0")
1886+
Object doSignedLong(long n, @SuppressWarnings("unused") int signed,
1887+
@Cached("create()") CExtNodes.ToSulongNode toSulongNode) {
1888+
return toSulongNode.execute(n);
1889+
}
1890+
1891+
@Specialization(guards = "signed == 0")
1892+
Object doUnsignedLong(long n, @SuppressWarnings("unused") int signed,
1893+
@Cached("create()") CExtNodes.ToSulongNode toSulongNode) {
1894+
if (n < 0) {
1895+
CompilerDirectives.transferToInterpreter();
1896+
throw new UnsupportedOperationException();
1897+
}
1898+
return toSulongNode.execute(n);
1899+
}
1900+
}
18661901
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,6 @@ def PyList_Size(listObj):
314314

315315
##################### LONG
316316

317-
@may_raise
318-
def PyLong_FromLongLong(n, signed):
319-
if signed:
320-
return int(n)
321-
else:
322-
return int(n & 0xffffffffffffffff)
323-
324-
325317
@may_raise(-1)
326318
def PyLong_AsPrimitive(n, signed, size, descr):
327319
if isinstance(n, int):

0 commit comments

Comments
 (0)