Skip to content

Commit 88f8b0e

Browse files
committed
Added specializations for (double, long) and (long, double) for binary operations
1 parent d61a1f0 commit 88f8b0e

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ abstract static class AddNode extends PythonBinaryBuiltinNode {
295295
return left + right;
296296
}
297297

298+
@Specialization
299+
double doLD(long left, double right) {
300+
return left + right;
301+
}
302+
298303
@Specialization
299304
double doDPi(double left, PInt right) {
300305
return left + right.doubleValueWithOverflow(getRaiseNode());
@@ -376,6 +381,11 @@ abstract static class MulNode extends PythonBinaryBuiltinNode {
376381
return left * right;
377382
}
378383

384+
@Specialization
385+
double doLD(long left, double right) {
386+
return left * right;
387+
}
388+
379389
@Specialization
380390
double doDD(double left, double right) {
381391
return left * right;
@@ -1011,6 +1021,11 @@ boolean eqDbLn(double a, long b) {
10111021
return compareDoubleToLong(a, b) == 0;
10121022
}
10131023

1024+
@Specialization
1025+
boolean eqLnDb(long a, double b) {
1026+
return compareDoubleToLong(b, a) == 0;
1027+
}
1028+
10141029
@Specialization
10151030
boolean eqDbPI(double a, PInt b) {
10161031
return compareDoubleToLargeInt(a, b) == 0;
@@ -1041,6 +1056,7 @@ PNotImplemented eq(Object a, Object b) {
10411056
}
10421057

10431058
// adapted from CPython's float_richcompare in floatobject.c
1059+
@TruffleBoundary(allowInlining = true)
10441060
public static double compareDoubleToLong(double v, long w) {
10451061
if (!Double.isFinite(v)) {
10461062
return v;
@@ -1094,6 +1110,11 @@ boolean neDbLn(double a, long b) {
10941110
return EqNode.compareDoubleToLong(a, b) != 0;
10951111
}
10961112

1113+
@Specialization
1114+
boolean neLnDb(long a, double b) {
1115+
return EqNode.compareDoubleToLong(b, a) != 0;
1116+
}
1117+
10971118
@Specialization
10981119
boolean neDbPI(double a, PInt b) {
10991120
return EqNode.compareDoubleToLargeInt(a, b) != 0;
@@ -1138,6 +1159,11 @@ boolean doDL(double x, long y) {
11381159
return EqNode.compareDoubleToLong(x, y) < 0;
11391160
}
11401161

1162+
@Specialization
1163+
boolean doLD(long x, double y) {
1164+
return EqNode.compareDoubleToLong(y, x) > 0;
1165+
}
1166+
11411167
@Specialization
11421168
boolean doPI(double x, PInt y) {
11431169
return EqNode.compareDoubleToLargeInt(x, y) < 0;
@@ -1203,6 +1229,11 @@ boolean doDL(double x, long y) {
12031229
return EqNode.compareDoubleToLong(x, y) <= 0;
12041230
}
12051231

1232+
@Specialization
1233+
boolean doLD(long x, double y) {
1234+
return EqNode.compareDoubleToLong(y, x) >= 0;
1235+
}
1236+
12061237
@Specialization
12071238
boolean doPI(double x, PInt y) {
12081239
return EqNode.compareDoubleToLargeInt(x, y) <= 0;
@@ -1268,6 +1299,11 @@ boolean doDL(double x, long y) {
12681299
return EqNode.compareDoubleToLong(x, y) > 0;
12691300
}
12701301

1302+
@Specialization
1303+
boolean doLD(long x, double y) {
1304+
return EqNode.compareDoubleToLong(y, x) < 0;
1305+
}
1306+
12711307
@Specialization
12721308
boolean doPI(double x, PInt y) {
12731309
return EqNode.compareDoubleToLargeInt(x, y) > 0;
@@ -1333,6 +1369,11 @@ boolean doDL(double x, long y) {
13331369
return EqNode.compareDoubleToLong(x, y) >= 0;
13341370
}
13351371

1372+
@Specialization
1373+
boolean doLD(long x, double y) {
1374+
return EqNode.compareDoubleToLong(y, x) <= 0;
1375+
}
1376+
13361377
@Specialization
13371378
boolean doPI(double x, PInt y) {
13381379
return EqNode.compareDoubleToLargeInt(x, y) >= 0;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallBinaryNode.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,20 @@ public abstract static class NotImplementedHandler extends PNodeWithContext {
9292

9393
public abstract long executeLong(VirtualFrame frame, Object arg, Object arg2) throws UnexpectedResultException;
9494

95+
public abstract double executeDouble(VirtualFrame frame, long arg, double arg2) throws UnexpectedResultException;
96+
97+
public abstract double executeDouble(VirtualFrame frame, double arg, long arg2) throws UnexpectedResultException;
98+
9599
public abstract double executeDouble(VirtualFrame frame, double arg, double arg2) throws UnexpectedResultException;
96100

97101
public abstract boolean executeBool(VirtualFrame frame, int arg, int arg2) throws UnexpectedResultException;
98102

99103
public abstract boolean executeBool(VirtualFrame frame, long arg, long arg2) throws UnexpectedResultException;
100104

105+
public abstract boolean executeBool(VirtualFrame frame, long arg, double arg2) throws UnexpectedResultException;
106+
107+
public abstract boolean executeBool(VirtualFrame frame, double arg, long arg2) throws UnexpectedResultException;
108+
101109
public abstract boolean executeBool(VirtualFrame frame, double arg, double arg2) throws UnexpectedResultException;
102110

103111
public abstract boolean executeBool(VirtualFrame frame, Object arg, Object arg2) throws UnexpectedResultException;
@@ -255,6 +263,32 @@ boolean callBoolean(VirtualFrame frame, long left, long right,
255263
}
256264
}
257265

266+
// long, double
267+
268+
@Specialization(guards = "function != null", rewriteOn = UnexpectedResultException.class)
269+
boolean callBoolean(VirtualFrame frame, long left, double right,
270+
@Cached("getBuiltin(right)") PythonBinaryBuiltinNode function) throws UnexpectedResultException {
271+
return function.executeBool(frame, left, right);
272+
}
273+
274+
@Specialization(guards = "function != null", rewriteOn = UnexpectedResultException.class)
275+
boolean callBoolean(VirtualFrame frame, double left, long right,
276+
@Cached("getBuiltin(left)") PythonBinaryBuiltinNode function) throws UnexpectedResultException {
277+
return function.executeBool(frame, left, right);
278+
}
279+
280+
@Specialization(guards = "function != null", rewriteOn = UnexpectedResultException.class)
281+
double callDouble(VirtualFrame frame, long left, double right,
282+
@Cached("getBuiltin(right)") PythonBinaryBuiltinNode function) throws UnexpectedResultException {
283+
return function.executeDouble(frame, left, right);
284+
}
285+
286+
@Specialization(guards = "function != null", rewriteOn = UnexpectedResultException.class)
287+
double callDouble(VirtualFrame frame, double left, long right,
288+
@Cached("getBuiltin(left)") PythonBinaryBuiltinNode function) throws UnexpectedResultException {
289+
return function.executeDouble(frame, left, right);
290+
}
291+
258292
// double, double
259293

260294
@Specialization(guards = "function != null", rewriteOn = UnexpectedResultException.class)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/BinaryComparisonNode.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ boolean doLI(VirtualFrame frame, long left, long right) {
208208
}
209209
}
210210

211+
@Specialization
212+
boolean doLI(VirtualFrame frame, long left, double right) {
213+
try {
214+
return profileCondition(callNode.executeBool(frame, left, right));
215+
} catch (UnexpectedResultException e) {
216+
CompilerDirectives.transferToInterpreterAndInvalidate();
217+
throw new IllegalStateException("Comparison on primitive values didn't return a boolean");
218+
}
219+
}
220+
211221
@Specialization
212222
boolean doDB(VirtualFrame frame, double left, boolean right) {
213223
try {
@@ -228,6 +238,16 @@ boolean doDI(VirtualFrame frame, double left, int right) {
228238
}
229239
}
230240

241+
@Specialization
242+
boolean doDL(VirtualFrame frame, double left, long right) {
243+
try {
244+
return profileCondition(callNode.executeBool(frame, left, right));
245+
} catch (UnexpectedResultException e) {
246+
CompilerDirectives.transferToInterpreterAndInvalidate();
247+
throw new IllegalStateException("Comparison on primitive values didn't return a boolean");
248+
}
249+
}
250+
231251
@Specialization
232252
boolean doDD(VirtualFrame frame, double left, double right) {
233253
try {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/PythonBinaryBuiltinNode.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -64,6 +64,14 @@ public long executeLong(VirtualFrame frame, long arg, long arg2) throws Unexpect
6464
return PGuards.expectLong(execute(frame, arg, arg2));
6565
}
6666

67+
public double executeDouble(VirtualFrame frame, long arg, double arg2) throws UnexpectedResultException {
68+
return PGuards.expectDouble(execute(frame, arg, arg2));
69+
}
70+
71+
public double executeDouble(VirtualFrame frame, double arg, long arg2) throws UnexpectedResultException {
72+
return PGuards.expectDouble(execute(frame, arg, arg2));
73+
}
74+
6775
public double executeDouble(VirtualFrame frame, double arg, double arg2) throws UnexpectedResultException {
6876
return PGuards.expectDouble(execute(frame, arg, arg2));
6977
}
@@ -76,6 +84,14 @@ public boolean executeBool(VirtualFrame frame, long arg, long arg2) throws Unexp
7684
return PGuards.expectBoolean(execute(frame, arg, arg2));
7785
}
7886

87+
public boolean executeBool(VirtualFrame frame, long arg, double arg2) throws UnexpectedResultException {
88+
return PGuards.expectBoolean(execute(frame, arg, arg2));
89+
}
90+
91+
public boolean executeBool(VirtualFrame frame, double arg, long arg2) throws UnexpectedResultException {
92+
return PGuards.expectBoolean(execute(frame, arg, arg2));
93+
}
94+
7995
public boolean executeBool(VirtualFrame frame, double arg, double arg2) throws UnexpectedResultException {
8096
return PGuards.expectBoolean(execute(frame, arg, arg2));
8197
}

0 commit comments

Comments
 (0)