Skip to content

Commit 5f7156a

Browse files
committed
consolidate util methods in PythonUtils
1 parent 9e6b1b4 commit 5f7156a

File tree

4 files changed

+50
-99
lines changed

4 files changed

+50
-99
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@
124124
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode.ComputeIndices;
125125
import com.oracle.graal.python.nodes.util.CastToByteNode;
126126
import com.oracle.graal.python.nodes.util.CastToJavaByteNode;
127-
import com.oracle.graal.python.nodes.util.ExactMath;
128127
import com.oracle.graal.python.runtime.PythonContext;
129128
import com.oracle.graal.python.runtime.PythonOptions;
130129
import com.oracle.graal.python.runtime.exception.PException;
@@ -2408,7 +2407,7 @@ public ExtendNode(GenNodeSupplier genNodeProvider) {
24082407

24092408
private static int lengthResult(int current, int ext) {
24102409
try {
2411-
return ExactMath.addExact(current, ext);
2410+
return PythonUtils.addExact(current, ext);
24122411
} catch (OverflowException e) {
24132412
// (mq) There is no need to ensure capacity as we either
24142413
// run out of memory or dealing with a fake length.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/range/RangeNodes.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
import com.oracle.graal.python.nodes.PRaiseNode;
5151
import com.oracle.graal.python.nodes.SpecialMethodNames;
5252
import com.oracle.graal.python.nodes.util.CastToJavaBigIntegerNode;
53-
import com.oracle.graal.python.nodes.util.ExactMath;
5453
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5554
import com.oracle.graal.python.util.OverflowException;
55+
import com.oracle.graal.python.util.PythonUtils;
5656
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5757
import com.oracle.truffle.api.dsl.Cached;
5858
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -189,14 +189,14 @@ static Object doBigInt(BigInteger lo, BigInteger hi, BigInteger step) {
189189
public abstract static class LenOfIntRangeNodeExact extends LenOfIntRangeBaseNode {
190190
@Specialization(guards = {"step > 0", "lo < hi"})
191191
static int mightBeBig1(int lo, int hi, int step) throws OverflowException {
192-
long diff = ExactMath.subtractExact(ExactMath.subtractExact(hi, (long) lo), 1);
193-
return ExactMath.toIntExact(ExactMath.addExact(diff / step, 1));
192+
long diff = PythonUtils.subtractExact(PythonUtils.subtractExact(hi, (long) lo), 1);
193+
return PythonUtils.toIntExact(PythonUtils.addExact(diff / step, 1));
194194
}
195195

196196
@Specialization(guards = {"step < 0", "lo > hi"})
197197
static int mightBeBig2(int lo, int hi, int step) throws OverflowException {
198-
long diff = ExactMath.subtractExact(ExactMath.subtractExact(lo, (long) hi), 1);
199-
return ExactMath.toIntExact(ExactMath.addExact(diff / -(long) step, 1));
198+
long diff = PythonUtils.subtractExact(PythonUtils.subtractExact(lo, (long) hi), 1);
199+
return PythonUtils.toIntExact(PythonUtils.addExact(diff / -(long) step, 1));
200200
}
201201
}
202202

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

Lines changed: 0 additions & 92 deletions
This file was deleted.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/PythonUtils.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,50 @@ public static void getChars(String str, int srcBegin, int srcEnd, char[] dst, in
9494
str.getChars(srcBegin, srcEnd, dst, dstBegin);
9595
}
9696

97+
/*
98+
* Replacements for JDK's exact math methods that throw the checked singleton
99+
* {@link OverflowException}. The implementation is taken from JDK.
100+
*/
101+
public static int addExact(int x, int y) throws OverflowException {
102+
int r = x + y;
103+
if (((x ^ r) & (y ^ r)) < 0) {
104+
throw OverflowException.INSTANCE;
105+
}
106+
return r;
107+
}
108+
109+
public static long addExact(long x, long y) throws OverflowException {
110+
long r = x + y;
111+
if (((x ^ r) & (y ^ r)) < 0) {
112+
throw OverflowException.INSTANCE;
113+
}
114+
return r;
115+
}
116+
117+
public static int subtractExact(int x, int y) throws OverflowException {
118+
int r = x - y;
119+
if (((x ^ y) & (x ^ r)) < 0) {
120+
throw OverflowException.INSTANCE;
121+
}
122+
return r;
123+
}
124+
125+
public static long subtractExact(long x, long y) throws OverflowException {
126+
long r = x - y;
127+
if (((x ^ y) & (x ^ r)) < 0) {
128+
throw OverflowException.INSTANCE;
129+
}
130+
return r;
131+
}
132+
133+
public static int toIntExact(long x) throws OverflowException {
134+
int r = (int) x;
135+
if (r != x) {
136+
throw OverflowException.INSTANCE;
137+
}
138+
return r;
139+
}
140+
97141
public static int multiplyExact(int x, int y) throws OverflowException {
98142
// copy&paste from Math.multiplyExact
99143
long r = (long) x * (long) y;

0 commit comments

Comments
 (0)