Skip to content

Commit 0b3589b

Browse files
committed
intrinsified python_cext PyLong_XXX
1 parent 2ae65fd commit 0b3589b

File tree

4 files changed

+182
-30
lines changed

4 files changed

+182
-30
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_long.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@
4141
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
4242
__dir__ = __file__.rpartition("/")[0]
4343

44-
44+
def raise_Py6_SystemError():
45+
if sys.version_info.minor >= 6:
46+
raise SystemError
47+
else:
48+
return -1
49+
4550
def _reference_aslong(args):
4651
# We cannot be sure if we are on 32-bit or 64-bit architecture. So, assume the smaller one.
4752
n = int(args[0])
@@ -88,6 +93,14 @@ def _reference_fromlong(args):
8893
n = args[0]
8994
return n
9095

96+
def _reference_sign(args):
97+
n = args[0]
98+
if n==0:
99+
return 0
100+
elif n < 0:
101+
return -1
102+
else:
103+
return 1
91104

92105
class DummyNonInt():
93106
pass
@@ -395,3 +408,19 @@ def compile_module(self, name):
395408
argspec="OniiO",
396409
arguments=["PyObject* object", "Py_ssize_t n", "int little_endian", "int is_signed", "PyObject* unused"],
397410
)
411+
412+
test__PyLong_Sign = CPyExtFunction(
413+
_reference_sign,
414+
lambda: (
415+
(0,),
416+
(-1,),
417+
(0xffffffff,),
418+
(0xfffffffffffffffffffffff,),
419+
(True,),
420+
(False,),
421+
),
422+
resultspec="i",
423+
argspec='O',
424+
arguments=["PyObject* o"],
425+
cmpfunc=unhandled_error_compare
426+
)

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

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
import com.oracle.graal.python.builtins.objects.dict.PDict;
214214
import com.oracle.graal.python.builtins.objects.ellipsis.PEllipsis;
215215
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
216+
import com.oracle.graal.python.builtins.objects.floats.FloatBuiltins.IntNode;
216217
import com.oracle.graal.python.builtins.objects.frame.PFrame;
217218
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
218219
import com.oracle.graal.python.builtins.objects.function.PArguments;
@@ -221,6 +222,9 @@
221222
import com.oracle.graal.python.builtins.objects.function.PKeyword;
222223
import com.oracle.graal.python.builtins.objects.function.Signature;
223224
import com.oracle.graal.python.builtins.objects.getsetdescriptor.GetSetDescriptor;
225+
import com.oracle.graal.python.builtins.objects.ints.IntBuiltins.GtNode;
226+
import com.oracle.graal.python.builtins.objects.ints.IntBuiltins.LtNode;
227+
import com.oracle.graal.python.builtins.objects.ints.IntBuiltins.NegNode;
224228
import com.oracle.graal.python.builtins.objects.ints.PInt;
225229
import com.oracle.graal.python.builtins.objects.iterator.PSequenceIterator;
226230
import com.oracle.graal.python.builtins.objects.list.ListBuiltins;
@@ -2215,7 +2219,151 @@ protected boolean isListSubtype(VirtualFrame frame, Object obj, GetClassNode get
22152219
return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PList);
22162220
}
22172221
}
2222+
2223+
///////////// long /////////////
22182224

2225+
@Builtin(name = "_PyLong_Sign", minNumOfPositionalArgs = 1)
2226+
@GenerateNodeFactory
2227+
abstract static class PyLongSignNode extends PythonUnaryBuiltinNode {
2228+
2229+
@SuppressWarnings("unused")
2230+
@Specialization(guards = "n == 0")
2231+
int sign(int n) {
2232+
return 0;
2233+
}
2234+
2235+
@SuppressWarnings("unused")
2236+
@Specialization(guards = "n < 0")
2237+
int signNeg(int n) {
2238+
return -1;
2239+
}
2240+
2241+
@SuppressWarnings("unused")
2242+
@Specialization(guards = "n > 0")
2243+
int signPos(int n) {
2244+
return 1;
2245+
}
2246+
2247+
@SuppressWarnings("unused")
2248+
@Specialization(guards = "n == 0")
2249+
int sign(long n) {
2250+
return 0;
2251+
}
2252+
2253+
@SuppressWarnings("unused")
2254+
@Specialization(guards = "n < 0")
2255+
int signNeg(long n) {
2256+
return -1;
2257+
}
2258+
2259+
@SuppressWarnings("unused")
2260+
@Specialization(guards = "n > 0")
2261+
int signPos(long n) {
2262+
return 1;
2263+
}
2264+
2265+
@SuppressWarnings("unused")
2266+
@Specialization(guards = "b")
2267+
int signTrue(boolean b) {
2268+
return 1;
2269+
}
2270+
2271+
@SuppressWarnings("unused")
2272+
@Specialization(guards = "!b")
2273+
int signFalse(boolean b) {
2274+
return 0;
2275+
}
2276+
2277+
@Specialization
2278+
int sign(VirtualFrame frame, PInt n,
2279+
@Cached LtNode ltNode,
2280+
@Cached GtNode gtNode,
2281+
@Cached BranchProfile gtProfile,
2282+
@Cached BranchProfile ltProfile) {
2283+
if ((boolean) gtNode.execute(frame, n, 0)) {
2284+
gtProfile.enter();
2285+
return 1;
2286+
} else if ((boolean) ltNode.execute(frame, n, 0)) {
2287+
ltProfile.enter();
2288+
return -1;
2289+
} else {
2290+
return 0;
2291+
}
2292+
}
2293+
2294+
@SuppressWarnings("unused")
2295+
@Specialization(guards = {"!canBeInteger(obj)", "isPIntSubtype(frame, obj, getClassNode, isSubtypeNode)"})
2296+
public Object signNative(VirtualFrame frame, Object obj,
2297+
@Cached GetClassNode getClassNode,
2298+
@Cached IsSubtypeNode isSubtypeNode) {
2299+
// function returns int, but -1 is expected result for 'n < 0'
2300+
throw CompilerDirectives.shouldNotReachHere("not yet implemented");
2301+
}
2302+
2303+
@Specialization(guards = {"!isInteger(obj)", "!isPInt(obj)", "!isPIntSubtype(frame, obj,getClassNode,isSubtypeNode)"})
2304+
public Object sign(VirtualFrame frame, Object obj,
2305+
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
2306+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode) {
2307+
// assert(PyLong_Check(v));
2308+
throw CompilerDirectives.shouldNotReachHere();
2309+
}
2310+
2311+
protected boolean isPIntSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) {
2312+
return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PInt);
2313+
}
2314+
}
2315+
2316+
@Builtin(name = "PyLong_FromDouble", minNumOfPositionalArgs = 1)
2317+
@GenerateNodeFactory
2318+
abstract static class PyLongFromDoubleNode extends PythonUnaryBuiltinNode {
2319+
2320+
@Specialization
2321+
Object fromDouble(VirtualFrame frame, double d,
2322+
@Cached IntNode intNode,
2323+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
2324+
@Cached GetNativeNullNode getNativeNullNode) {
2325+
try {
2326+
return intNode.execute(frame, d);
2327+
} catch (PException e) {
2328+
transformExceptionToNativeNode.execute(e);
2329+
return getNativeNullNode.execute();
2330+
}
2331+
}
2332+
}
2333+
2334+
@Builtin(name = "PyLong_FromString", minNumOfPositionalArgs = 3)
2335+
@TypeSystemReference(PythonTypes.class)
2336+
@GenerateNodeFactory
2337+
abstract static class PyLongFromStringNode extends PythonTernaryBuiltinNode {
2338+
2339+
@Specialization(guards = "negative == 0")
2340+
Object fromString(VirtualFrame frame, String s, long base, @SuppressWarnings("unused") long negative,
2341+
@Cached com.oracle.graal.python.builtins.modules.BuiltinConstructors.IntNode intNode,
2342+
@Shared("transforEx") @Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
2343+
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
2344+
try {
2345+
return intNode.executeWith(frame, s, base);
2346+
} catch (PException e) {
2347+
transformExceptionToNativeNode.execute(e);
2348+
return getNativeNullNode.execute();
2349+
}
2350+
}
2351+
2352+
@Specialization(guards = "negative != 0")
2353+
Object fromString(VirtualFrame frame, String s, long base, @SuppressWarnings("unused") long negative,
2354+
@Cached com.oracle.graal.python.builtins.modules.BuiltinConstructors.IntNode intNode,
2355+
@Cached NegNode negNode,
2356+
@Shared("transforEx") @Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
2357+
@Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) {
2358+
try {
2359+
return negNode.execute(frame, intNode.executeWith(frame, s, base));
2360+
} catch (PException e) {
2361+
transformExceptionToNativeNode.execute(e);
2362+
return getNativeNullNode.execute();
2363+
}
2364+
}
2365+
}
2366+
22192367
/**
22202368
* This is used in the ExternalFunctionNode below, so all arguments passed from Python code into
22212369
* a C function are automatically unwrapped if they are wrapped. This function is also called

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
@@ -1247,7 +1247,7 @@ PInt pos(PInt arg) {
12471247
@Builtin(name = SpecialMethodNames.__NEG__, minNumOfPositionalArgs = 1)
12481248
@GenerateNodeFactory
12491249
@TypeSystemReference(PythonArithmeticTypes.class)
1250-
abstract static class NegNode extends PythonUnaryBuiltinNode {
1250+
public abstract static class NegNode extends PythonUnaryBuiltinNode {
12511251
@Specialization(rewriteOn = ArithmeticException.class)
12521252
static int neg(int arg) {
12531253
return Math.negateExact(arg);
@@ -1924,7 +1924,7 @@ static PNotImplemented eq(Object a, Object b) {
19241924
@Builtin(name = __LT__, minNumOfPositionalArgs = 2)
19251925
@GenerateNodeFactory
19261926
@TypeSystemReference(PythonArithmeticTypes.class)
1927-
abstract static class LtNode extends PythonBinaryBuiltinNode {
1927+
public abstract static class LtNode extends PythonBinaryBuiltinNode {
19281928
@Specialization
19291929
static boolean doII(int left, int right) {
19301930
return left < right;
@@ -2045,7 +2045,7 @@ static PNotImplemented doGeneric(Object a, Object b) {
20452045
@Builtin(name = SpecialMethodNames.__GT__, minNumOfPositionalArgs = 2)
20462046
@GenerateNodeFactory
20472047
@TypeSystemReference(PythonArithmeticTypes.class)
2048-
abstract static class GtNode extends PythonBinaryBuiltinNode {
2048+
public abstract static class GtNode extends PythonBinaryBuiltinNode {
20492049

20502050
@Specialization
20512051
static boolean doII(int left, int right) {
@@ -2832,7 +2832,7 @@ Object getPI(PInt self) {
28322832
@Builtin(name = SpecialMethodNames.__FLOAT__, minNumOfPositionalArgs = 1)
28332833
@GenerateNodeFactory
28342834
@TypeSystemReference(PythonArithmeticTypes.class)
2835-
abstract static class FloatNode extends PythonUnaryBuiltinNode {
2835+
public abstract static class FloatNode extends PythonUnaryBuiltinNode {
28362836
@Specialization
28372837
static double doBoolean(boolean self) {
28382838
return self ? 1.0 : 0.0;

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,6 @@ def PyDictProxy_New(mapping):
6666
mappingproxy = type(type.__dict__)
6767
return mappingproxy(mapping)
6868

69-
##################### LONG
70-
71-
def _PyLong_Sign(n):
72-
if n==0:
73-
return 0
74-
elif n < 0:
75-
return -1
76-
else:
77-
return 1
78-
79-
80-
@may_raise
81-
def PyLong_FromDouble(d):
82-
return int(d)
83-
84-
85-
@may_raise
86-
def PyLong_FromString(string, base, negative):
87-
result = int(string, base)
88-
if negative:
89-
return -result
90-
else:
91-
return result
92-
93-
9469
##################### FLOAT
9570

9671
@may_raise

0 commit comments

Comments
 (0)