Skip to content

Commit bd330ab

Browse files
committed
Fix: '__pow__' are ternary methods.
1 parent 5bc9a99 commit bd330ab

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import com.oracle.graal.python.nodes.call.special.LookupAndCallVarargsNode;
7171
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7272
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
73+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
7374
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7475
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
7576
import com.oracle.graal.python.runtime.JavaTypeConversions;
@@ -295,28 +296,73 @@ Object doGeneric(Object left, Object right) {
295296
abstract static class RMulNode extends MulNode {
296297
}
297298

298-
@Builtin(name = __POW__, fixedNumOfArguments = 2)
299+
@Builtin(name = __POW__, minNumOfArguments = 2, maxNumOfArguments = 3)
299300
@TypeSystemReference(PythonArithmeticTypes.class)
300301
@GenerateNodeFactory
301-
abstract static class PowerNode extends PythonBinaryBuiltinNode {
302+
abstract static class PowerNode extends PythonTernaryBuiltinNode {
302303
@Specialization
303-
double doDL(double left, long right) {
304+
double doDL(double left, long right, @SuppressWarnings("unused") PNone none) {
304305
return Math.pow(left, right);
305306
}
306307

307308
@Specialization
308-
double doDPi(double left, PInt right) {
309+
double doDPi(double left, PInt right, @SuppressWarnings("unused") PNone none) {
309310
return Math.pow(left, right.doubleValue());
310311
}
311312

312313
@Specialization
313-
double doDD(double left, double right) {
314+
double doDD(double left, double right, @SuppressWarnings("unused") PNone none) {
314315
return Math.pow(left, right);
315316
}
316317

318+
@Specialization(guards = "!isNone(mod)")
319+
double doDL(double left, long right, long mod) {
320+
return Math.pow(left, right) % mod;
321+
}
322+
323+
@Specialization(guards = "!isNone(mod)")
324+
double doDPi(double left, PInt right, long mod) {
325+
return Math.pow(left, right.doubleValue()) % mod;
326+
}
327+
328+
@Specialization(guards = "!isNone(mod)")
329+
double doDD(double left, double right, long mod) {
330+
return Math.pow(left, right) % mod;
331+
}
332+
333+
@Specialization(guards = "!isNone(mod)")
334+
double doDL(double left, long right, PInt mod) {
335+
return Math.pow(left, right) % mod.doubleValue();
336+
}
337+
338+
@Specialization(guards = "!isNone(mod)")
339+
double doDPi(double left, PInt right, PInt mod) {
340+
return Math.pow(left, right.doubleValue()) % mod.doubleValue();
341+
}
342+
343+
@Specialization(guards = "!isNone(mod)")
344+
double doDD(double left, double right, PInt mod) {
345+
return Math.pow(left, right) % mod.doubleValue();
346+
}
347+
348+
@Specialization(guards = "!isNone(mod)")
349+
double doDL(double left, long right, double mod) {
350+
return Math.pow(left, right) % mod;
351+
}
352+
353+
@Specialization(guards = "!isNone(mod)")
354+
double doDPi(double left, PInt right, double mod) {
355+
return Math.pow(left, right.doubleValue()) % mod;
356+
}
357+
358+
@Specialization(guards = "!isNone(mod)")
359+
double doDD(double left, double right, double mod) {
360+
return Math.pow(left, right) % mod;
361+
}
362+
317363
@SuppressWarnings("unused")
318364
@Fallback
319-
Object doGeneric(Object left, Object right) {
365+
Object doGeneric(Object left, Object right, Object none) {
320366
return PNotImplemented.NOT_IMPLEMENTED;
321367
}
322368
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.oracle.graal.python.nodes.SpecialMethodNames;
5151
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5252
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
53+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
5354
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5455
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
5556
import com.oracle.graal.python.runtime.ArithmeticUtil;
@@ -805,11 +806,11 @@ Object doGeneric(Object left, Object right) {
805806
abstract static class RMulNode extends MulNode {
806807
}
807808

808-
@Builtin(name = SpecialMethodNames.__POW__, fixedNumOfArguments = 2)
809+
@Builtin(name = SpecialMethodNames.__POW__, minNumOfArguments = 2, maxNumOfArguments = 3)
809810
@GenerateNodeFactory
810-
abstract static class PowNode extends PythonBinaryBuiltinNode {
811+
abstract static class PowNode extends PythonTernaryBuiltinNode {
811812
@Specialization(guards = "right >= 0", rewriteOn = ArithmeticException.class)
812-
int doIntegerFast(int left, int right) {
813+
int doIntegerFast(int left, int right, @SuppressWarnings("unused") PNone none) {
813814
int result = 1;
814815
int exponent = right;
815816
int base = left;
@@ -824,32 +825,32 @@ int doIntegerFast(int left, int right) {
824825
}
825826

826827
@Specialization(guards = "right >= 0")
827-
PInt doInteger(int left, int right) {
828+
PInt doInteger(int left, int right, @SuppressWarnings("unused") PNone none) {
828829
return factory().createInt(op(BigInteger.valueOf(left), right));
829830
}
830831

831832
@Specialization(guards = "right >= 0", rewriteOn = ArithmeticException.class)
832-
long doLongFast(long left, int right) {
833-
return doLongFast(left, (long) right);
833+
long doLongFast(long left, int right, PNone none) {
834+
return doLongFast(left, (long) right, none);
834835
}
835836

836837
@Specialization(guards = "right >= 0")
837-
PInt doLong(long left, int right) {
838-
return doLong(left, (long) right);
838+
PInt doLong(long left, int right, PNone none) {
839+
return doLong(left, (long) right, none);
839840
}
840841

841842
@Specialization(guards = "right >= 0", rewriteOn = ArithmeticException.class)
842-
long doLongFast(int left, long right) {
843-
return doLongFast((long) left, right);
843+
long doLongFast(int left, long right, PNone none) {
844+
return doLongFast((long) left, right, none);
844845
}
845846

846847
@Specialization(guards = "right >= 0")
847-
PInt doLong(int left, long right) {
848-
return doLong((long) left, right);
848+
PInt doLong(int left, long right, PNone none) {
849+
return doLong((long) left, right, none);
849850
}
850851

851852
@Specialization(guards = "right >= 0", rewriteOn = ArithmeticException.class)
852-
long doLongFast(long left, long right) {
853+
long doLongFast(long left, long right, @SuppressWarnings("unused") PNone none) {
853854
long result = 1;
854855
long exponent = right;
855856
long base = left;
@@ -864,22 +865,22 @@ long doLongFast(long left, long right) {
864865
}
865866

866867
@Specialization(guards = "right >= 0")
867-
PInt doLong(long left, long right) {
868+
PInt doLong(long left, long right, @SuppressWarnings("unused") PNone none) {
868869
return factory().createInt(op(BigInteger.valueOf(left), right));
869870
}
870871

871872
@Specialization
872-
double doInt(long left, long right) {
873+
double doInt(long left, long right, @SuppressWarnings("unused") PNone none) {
873874
return Math.pow(left, right);
874875
}
875876

876877
@Specialization
877-
double doInt(long left, double right) {
878+
double doInt(long left, double right, @SuppressWarnings("unused") PNone none) {
878879
return Math.pow(left, right);
879880
}
880881

881882
@Specialization
882-
PInt doPInt(PInt left, PInt right) {
883+
PInt doPInt(PInt left, PInt right, @SuppressWarnings("unused") PNone none) {
883884
try {
884885
return factory().createInt(op(left.getValue(), right.getValue().longValueExact()));
885886
} catch (ArithmeticException e) {

0 commit comments

Comments
 (0)