Skip to content

Commit 59315b6

Browse files
committed
avoid the fallback guard and implement a generic specialization for float.__pow__
1 parent 1e5f351 commit 59315b6

File tree

1 file changed

+24
-4
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats

1 file changed

+24
-4
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
106106
import com.oracle.truffle.api.dsl.ImportStatic;
107107
import com.oracle.truffle.api.dsl.NodeFactory;
108+
import com.oracle.truffle.api.dsl.ReportPolymorphism;
108109
import com.oracle.truffle.api.dsl.Specialization;
109110
import com.oracle.truffle.api.dsl.TypeSystemReference;
110111
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -456,6 +457,7 @@ PNotImplemented doGeneric(Object left, Object right) {
456457
@Builtin(name = __POW__, minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
457458
@TypeSystemReference(PythonArithmeticTypes.class)
458459
@GenerateNodeFactory
460+
@ReportPolymorphism
459461
abstract static class PowerNode extends PythonTernaryBuiltinNode {
460462
@Specialization
461463
double doDL(double left, long right, @SuppressWarnings("unused") PNone none,
@@ -555,13 +557,31 @@ Object doDPiToComplex(VirtualFrame frame, PInt left, double right, @SuppressWarn
555557
return doDDToComplex(frame, left.doubleValue(), right, none, callPow, negativeRaise);
556558
}
557559

558-
@Fallback
559-
Object doGeneric(@SuppressWarnings("unused") Object left, @SuppressWarnings("unused") Object right, Object mod) {
560-
if (mod instanceof PNone) {
560+
@Specialization
561+
Object doGeneric(VirtualFrame frame, Object left, Object right, Object mod,
562+
@CachedLibrary(limit = "5") PythonObjectLibrary lib,
563+
@Shared("powCall") @Cached("create(__POW__)") LookupAndCallTernaryNode callPow,
564+
@Shared("negativeRaise") @Cached BranchProfile negativeRaise) {
565+
if (!(mod instanceof PNone)) {
566+
throw raise(PythonBuiltinClassType.TypeError, "pow() 3rd argument not allowed unless all arguments are integers");
567+
}
568+
double leftDouble;
569+
double rightDouble;
570+
if (lib.canBeJavaDouble(left)) {
571+
leftDouble = lib.asJavaDouble(left);
572+
} else if (left instanceof PInt) {
573+
leftDouble = ((PInt) left).doubleValue();
574+
} else {
561575
return PNotImplemented.NOT_IMPLEMENTED;
576+
}
577+
if (lib.canBeJavaDouble(right)) {
578+
rightDouble = lib.asJavaDouble(right);
579+
} else if (right instanceof PInt) {
580+
rightDouble = ((PInt) right).doubleValue();
562581
} else {
563-
throw raise(PythonBuiltinClassType.TypeError, "pow() 3rd argument not allowed unless all arguments are integers");
582+
return PNotImplemented.NOT_IMPLEMENTED;
564583
}
584+
return doDDToComplex(frame, leftDouble, rightDouble, PNone.NONE, callPow, negativeRaise);
565585
}
566586
}
567587

0 commit comments

Comments
 (0)