Skip to content

Commit 37e07bb

Browse files
committed
Simplify cast to double routine
1 parent 92611d0 commit 37e07bb

File tree

2 files changed

+75
-137
lines changed

2 files changed

+75
-137
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 52 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package com.oracle.graal.python.builtins.objects.floats;
2727

2828
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.OverflowError;
29-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
3029
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
3130
import static com.oracle.graal.python.nodes.BuiltinNames.J_FLOAT;
3231
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___ABS__;
@@ -92,11 +91,8 @@
9291
import com.oracle.graal.python.builtins.objects.floats.FloatBuiltinsClinicProviders.FormatNodeClinicProviderGen;
9392
import com.oracle.graal.python.builtins.objects.ints.PInt;
9493
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
95-
import com.oracle.graal.python.lib.PyFloatCheckNode;
96-
import com.oracle.graal.python.lib.PyLongCheckNode;
9794
import com.oracle.graal.python.lib.PyObjectHashNode;
9895
import com.oracle.graal.python.nodes.ErrorMessages;
99-
import com.oracle.graal.python.nodes.PRaiseNode;
10096
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;
10197
import com.oracle.graal.python.nodes.call.special.LookupAndCallVarargsNode;
10298
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
@@ -108,7 +104,6 @@
108104
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
109105
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
110106
import com.oracle.graal.python.nodes.object.GetClassNode;
111-
import com.oracle.graal.python.nodes.object.InlinedGetClassNode;
112107
import com.oracle.graal.python.nodes.object.InlinedGetClassNode.GetPythonObjectClassNode;
113108
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
114109
import com.oracle.graal.python.nodes.util.CannotCastException;
@@ -124,15 +119,12 @@
124119
import com.oracle.truffle.api.dsl.Cached.Shared;
125120
import com.oracle.truffle.api.dsl.Fallback;
126121
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
127-
import com.oracle.truffle.api.dsl.GenerateUncached;
128122
import com.oracle.truffle.api.dsl.ImportStatic;
129123
import com.oracle.truffle.api.dsl.NodeFactory;
130124
import com.oracle.truffle.api.dsl.ReportPolymorphism;
131125
import com.oracle.truffle.api.dsl.Specialization;
132126
import com.oracle.truffle.api.dsl.TypeSystemReference;
133127
import com.oracle.truffle.api.frame.VirtualFrame;
134-
import com.oracle.truffle.api.interop.InteropLibrary;
135-
import com.oracle.truffle.api.library.CachedLibrary;
136128
import com.oracle.truffle.api.nodes.Node;
137129
import com.oracle.truffle.api.nodes.UnexpectedResultException;
138130
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
@@ -298,68 +290,13 @@ static boolean isFloatSubtype(Node inliningTarget, PythonAbstractNativeObject ob
298290
}
299291
}
300292

301-
@GenerateUncached
302-
abstract static class ConvertToDoubleCheckNode extends Node {
303-
abstract boolean execute(Object obj);
304-
305-
@Specialization
306-
static boolean doDouble(@SuppressWarnings("unused") Double object) {
307-
return true;
308-
}
309-
310-
@Specialization
311-
static boolean doInt(@SuppressWarnings("unused") Integer object) {
312-
return true;
313-
}
314-
315-
@Specialization
316-
static boolean doLong(@SuppressWarnings("unused") Long object) {
317-
return true;
318-
}
319-
320-
@Specialization
321-
static boolean doBoolean(@SuppressWarnings("unused") Boolean object) {
322-
return true;
323-
}
324-
325-
@Specialization
326-
static boolean doString(@SuppressWarnings("unused") TruffleString object) {
327-
return false;
328-
}
329-
330-
@Specialization
331-
static boolean doPBCT(@SuppressWarnings("unused") PythonBuiltinClassType object) {
332-
return false;
333-
}
334-
335-
@Specialization
336-
static boolean typeCheck(Object obj,
337-
@Cached PyFloatCheckNode floatCheckNode,
338-
@Cached PyLongCheckNode longCheckNode,
339-
@Bind("this") Node inliningTarget,
340-
@Cached InlinedGetClassNode getClassNode,
341-
@CachedLibrary(limit = "3") InteropLibrary interopLibrary) {
342-
if (floatCheckNode.execute(inliningTarget, obj) || longCheckNode.execute(obj)) {
343-
return true;
344-
}
345-
Object type = getClassNode.execute(inliningTarget, obj);
346-
if (type == PythonBuiltinClassType.ForeignObject) {
347-
if (interopLibrary.fitsInDouble(obj) || interopLibrary.fitsInLong(obj) || interopLibrary.isBoolean(obj)) {
348-
return true;
349-
}
350-
}
351-
return false;
352-
}
353-
}
354-
355-
static double convertToDouble(Object obj,
356-
CastToJavaDoubleNode asDoubleNode,
357-
PRaiseNode raiseNode) {
293+
static Object convertToDouble(Object obj,
294+
CastToJavaDoubleNode asDoubleNode) {
358295
try {
359296
return asDoubleNode.execute(obj);
360297
} catch (CannotCastException e) {
361298
// This can only happen to values that are expected to be long.
362-
throw raiseNode.raise(TypeError, ErrorMessages.INTEGER_REQUIRED);
299+
return PNotImplemented.NOT_IMPLEMENTED;
363300
}
364301
}
365302

@@ -412,22 +349,20 @@ static Object doDP(VirtualFrame frame, PythonAbstractNativeObject left, double r
412349

413350
@Fallback
414351
Object doGeneric(Object left, Object right,
415-
@Cached ConvertToDoubleCheckNode convertToDoubleCheckNode,
416352
@Cached CastToJavaDoubleNode castToJavaDoubleNode) {
417-
double leftDouble;
418-
double rightDouble;
419353

420-
if (convertToDoubleCheckNode.execute(left)) {
421-
leftDouble = convertToDouble(left, castToJavaDoubleNode, getRaiseNode());
422-
} else {
354+
Object objLeft = convertToDouble(left, castToJavaDoubleNode);
355+
if (objLeft == PNotImplemented.NOT_IMPLEMENTED) {
423356
return PNotImplemented.NOT_IMPLEMENTED;
424357
}
425358

426-
if (convertToDoubleCheckNode.execute(right)) {
427-
rightDouble = convertToDouble(right, castToJavaDoubleNode, getRaiseNode());
428-
} else {
359+
Object objRight = convertToDouble(right, castToJavaDoubleNode);
360+
if (objRight == PNotImplemented.NOT_IMPLEMENTED) {
429361
return PNotImplemented.NOT_IMPLEMENTED;
430362
}
363+
364+
double leftDouble = (double) objLeft;
365+
double rightDouble = (double) objRight;
431366
return leftDouble + rightDouble;
432367
}
433368
}
@@ -464,22 +399,20 @@ static double doLD(long left, double right) {
464399

465400
@Fallback
466401
Object doGeneric(Object left, Object right,
467-
@Cached ConvertToDoubleCheckNode convertToDoubleCheckNode,
468402
@Cached CastToJavaDoubleNode castToJavaDoubleNode) {
469-
double leftDouble;
470-
double rightDouble;
471403

472-
if (convertToDoubleCheckNode.execute(left)) {
473-
leftDouble = convertToDouble(left, castToJavaDoubleNode, getRaiseNode());
474-
} else {
404+
Object objLeft = convertToDouble(left, castToJavaDoubleNode);
405+
if (objLeft == PNotImplemented.NOT_IMPLEMENTED) {
475406
return PNotImplemented.NOT_IMPLEMENTED;
476407
}
477408

478-
if (convertToDoubleCheckNode.execute(right)) {
479-
rightDouble = convertToDouble(right, castToJavaDoubleNode, getRaiseNode());
480-
} else {
409+
Object objRight = convertToDouble(right, castToJavaDoubleNode);
410+
if (objRight == PNotImplemented.NOT_IMPLEMENTED) {
481411
return PNotImplemented.NOT_IMPLEMENTED;
482412
}
413+
414+
double leftDouble = (double) objLeft;
415+
double rightDouble = (double) objRight;
483416
return leftDouble - rightDouble;
484417
}
485418
}
@@ -544,22 +477,20 @@ Object doDP(VirtualFrame frame, PythonAbstractNativeObject left, PInt right,
544477

545478
@Fallback
546479
Object doGeneric(Object left, Object right,
547-
@Cached ConvertToDoubleCheckNode convertToDoubleCheckNode,
548480
@Cached CastToJavaDoubleNode castToJavaDoubleNode) {
549-
double leftDouble;
550-
double rightDouble;
551481

552-
if (convertToDoubleCheckNode.execute(left)) {
553-
leftDouble = convertToDouble(left, castToJavaDoubleNode, getRaiseNode());
554-
} else {
482+
Object objLeft = convertToDouble(left, castToJavaDoubleNode);
483+
if (objLeft == PNotImplemented.NOT_IMPLEMENTED) {
555484
return PNotImplemented.NOT_IMPLEMENTED;
556485
}
557486

558-
if (convertToDoubleCheckNode.execute(right)) {
559-
rightDouble = convertToDouble(right, castToJavaDoubleNode, getRaiseNode());
560-
} else {
487+
Object objRight = convertToDouble(right, castToJavaDoubleNode);
488+
if (objRight == PNotImplemented.NOT_IMPLEMENTED) {
561489
return PNotImplemented.NOT_IMPLEMENTED;
562490
}
491+
492+
double leftDouble = (double) objLeft;
493+
double rightDouble = (double) objRight;
563494
return leftDouble * rightDouble;
564495
}
565496
}
@@ -673,26 +604,24 @@ Object doDPiToComplex(VirtualFrame frame, PInt left, double right, @SuppressWarn
673604

674605
@Specialization
675606
Object doGeneric(VirtualFrame frame, Object left, Object right, Object mod,
676-
@Cached ConvertToDoubleCheckNode convertToDoubleCheckNode,
677607
@Cached CastToJavaDoubleNode castToJavaDoubleNode,
678608
@Shared("powCall") @Cached("create(Pow)") LookupAndCallTernaryNode callPow) {
679609
if (!(mod instanceof PNone)) {
680610
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.POW_3RD_ARG_NOT_ALLOWED_UNLESS_INTEGERS);
681611
}
682-
double leftDouble;
683-
double rightDouble;
684612

685-
if (convertToDoubleCheckNode.execute(left)) {
686-
leftDouble = convertToDouble(left, castToJavaDoubleNode, getRaiseNode());
687-
} else {
613+
Object objLeft = convertToDouble(left, castToJavaDoubleNode);
614+
if (objLeft == PNotImplemented.NOT_IMPLEMENTED) {
688615
return PNotImplemented.NOT_IMPLEMENTED;
689616
}
690617

691-
if (convertToDoubleCheckNode.execute(right)) {
692-
rightDouble = convertToDouble(right, castToJavaDoubleNode, getRaiseNode());
693-
} else {
618+
Object objRight = convertToDouble(right, castToJavaDoubleNode);
619+
if (objRight == PNotImplemented.NOT_IMPLEMENTED) {
694620
return PNotImplemented.NOT_IMPLEMENTED;
695621
}
622+
623+
double leftDouble = (double) objLeft;
624+
double rightDouble = (double) objRight;
696625
return doDDToComplex(frame, leftDouble, rightDouble, PNone.NONE, callPow);
697626
}
698627

@@ -775,22 +704,20 @@ PTuple doGenericFloat(VirtualFrame frame, Object left, Object right,
775704

776705
@Fallback
777706
Object doGeneric(Object left, Object right,
778-
@Cached ConvertToDoubleCheckNode convertToDoubleCheckNode,
779707
@Cached CastToJavaDoubleNode castToJavaDoubleNode) {
780-
double leftDouble;
781-
double rightDouble;
782708

783-
if (convertToDoubleCheckNode.execute(left)) {
784-
leftDouble = convertToDouble(left, castToJavaDoubleNode, getRaiseNode());
785-
} else {
709+
Object objLeft = convertToDouble(left, castToJavaDoubleNode);
710+
if (objLeft == PNotImplemented.NOT_IMPLEMENTED) {
786711
return PNotImplemented.NOT_IMPLEMENTED;
787712
}
788713

789-
if (convertToDoubleCheckNode.execute(right)) {
790-
rightDouble = convertToDouble(right, castToJavaDoubleNode, getRaiseNode());
791-
} else {
714+
Object objRight = convertToDouble(right, castToJavaDoubleNode);
715+
if (objRight == PNotImplemented.NOT_IMPLEMENTED) {
792716
return PNotImplemented.NOT_IMPLEMENTED;
793717
}
718+
719+
double leftDouble = (double) objLeft;
720+
double rightDouble = (double) objRight;
794721
return doDD(leftDouble, rightDouble);
795722
}
796723

@@ -979,22 +906,20 @@ public abstract static class ModNode extends FloatBinaryBuiltinNode {
979906

980907
@Fallback
981908
Object doGeneric(Object left, Object right,
982-
@Cached ConvertToDoubleCheckNode convertToDoubleCheckNode,
983909
@Cached CastToJavaDoubleNode castToJavaDoubleNode) {
984-
double leftDouble;
985-
double rightDouble;
986910

987-
if (convertToDoubleCheckNode.execute(left)) {
988-
leftDouble = convertToDouble(left, castToJavaDoubleNode, getRaiseNode());
989-
} else {
911+
Object objLeft = convertToDouble(left, castToJavaDoubleNode);
912+
if (objLeft == PNotImplemented.NOT_IMPLEMENTED) {
990913
return PNotImplemented.NOT_IMPLEMENTED;
991914
}
992915

993-
if (convertToDoubleCheckNode.execute(right)) {
994-
rightDouble = convertToDouble(right, castToJavaDoubleNode, getRaiseNode());
995-
} else {
916+
Object objRight = convertToDouble(right, castToJavaDoubleNode);
917+
if (objRight == PNotImplemented.NOT_IMPLEMENTED) {
996918
return PNotImplemented.NOT_IMPLEMENTED;
997919
}
920+
921+
double leftDouble = (double) objLeft;
922+
double rightDouble = (double) objRight;
998923
return doDD(leftDouble, rightDouble);
999924
}
1000925

@@ -1060,22 +985,20 @@ Object doDP(VirtualFrame frame, long left, PythonAbstractNativeObject right,
1060985

1061986
@Fallback
1062987
Object doGeneric(Object left, Object right,
1063-
@Cached ConvertToDoubleCheckNode convertToDoubleCheckNode,
1064988
@Cached CastToJavaDoubleNode castToJavaDoubleNode) {
1065-
double leftDouble;
1066-
double rightDouble;
1067989

1068-
if (convertToDoubleCheckNode.execute(left)) {
1069-
leftDouble = convertToDouble(left, castToJavaDoubleNode, getRaiseNode());
1070-
} else {
990+
Object objLeft = convertToDouble(left, castToJavaDoubleNode);
991+
if (objLeft == PNotImplemented.NOT_IMPLEMENTED) {
1071992
return PNotImplemented.NOT_IMPLEMENTED;
1072993
}
1073994

1074-
if (convertToDoubleCheckNode.execute(right)) {
1075-
rightDouble = convertToDouble(right, castToJavaDoubleNode, getRaiseNode());
1076-
} else {
995+
Object objRight = convertToDouble(right, castToJavaDoubleNode);
996+
if (objRight == PNotImplemented.NOT_IMPLEMENTED) {
1077997
return PNotImplemented.NOT_IMPLEMENTED;
1078998
}
999+
1000+
double leftDouble = (double) objLeft;
1001+
double rightDouble = (double) objRight;
10791002
return doDD(leftDouble, rightDouble);
10801003
}
10811004
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToJavaDoubleNode.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.oracle.truffle.api.interop.UnsupportedMessageException;
6464
import com.oracle.truffle.api.library.CachedLibrary;
6565
import com.oracle.truffle.api.nodes.Node;
66+
import com.oracle.truffle.api.strings.TruffleString;
6667

6768
/**
6869
* Casts a Python "number" to a Java double without coercion. <b>ATTENTION:</b> If the cast fails,
@@ -85,17 +86,22 @@ static double doBoolean(boolean x) {
8586
return x ? 1.0 : 0.0;
8687
}
8788

88-
@Specialization
89-
static double toLong(long x) {
90-
return x;
91-
}
92-
9389
@Specialization
9490
static double toPInt(PInt x,
9591
@Cached PRaiseNode raise) {
9692
return x.doubleValueWithOverflow(raise);
9793
}
9894

95+
@Specialization
96+
static double doString(@SuppressWarnings("unused") TruffleString object) {
97+
throw CannotCastException.INSTANCE;
98+
}
99+
100+
@Specialization
101+
static double doPBCT(@SuppressWarnings("unused") PythonBuiltinClassType object) {
102+
throw CannotCastException.INSTANCE;
103+
}
104+
99105
@Specialization
100106
static double doNativeObject(PythonAbstractNativeObject x,
101107
@Bind("this") Node node,
@@ -111,9 +117,8 @@ static double doNativeObject(PythonAbstractNativeObject x,
111117
throw CannotCastException.INSTANCE;
112118
}
113119

114-
@Specialization(guards = "!isNumber(obj)")
115-
static double doGeneric(Object obj,
116-
@CachedLibrary(limit = "3") InteropLibrary interopLibrary) {
120+
public static Double doInterop(Object obj,
121+
InteropLibrary interopLibrary) {
117122
try {
118123
if (interopLibrary.fitsInDouble(obj)) {
119124
return interopLibrary.asDouble(obj);
@@ -127,6 +132,16 @@ static double doGeneric(Object obj,
127132
} catch (UnsupportedMessageException e) {
128133
throw CompilerDirectives.shouldNotReachHere(e);
129134
}
135+
return null;
136+
}
137+
138+
@Specialization(guards = "!isNumber(obj)")
139+
static double doGeneric(Object obj,
140+
@CachedLibrary(limit = "3") InteropLibrary interopLibrary) {
141+
Double d = doInterop(obj, interopLibrary);
142+
if (d != null) {
143+
return d;
144+
}
130145
throw CannotCastException.INSTANCE;
131146
}
132147

0 commit comments

Comments
 (0)