Skip to content

Commit bed63f5

Browse files
committed
Replace exception-based int narrowing with a helper node
1 parent 41bbdb0 commit bed63f5

File tree

3 files changed

+95
-19
lines changed

3 files changed

+95
-19
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
6666
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6767
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
68+
import com.oracle.graal.python.nodes.util.NarrowBigIntegerNode;
6869
import com.oracle.graal.python.runtime.exception.PException;
6970
import com.oracle.graal.python.runtime.exception.PythonErrorType;
7071
import com.oracle.truffle.api.CompilerDirectives;
@@ -2599,15 +2600,17 @@ public double count(double x) {
25992600
public abstract static class IsqrtNode extends PythonUnaryBuiltinNode {
26002601

26012602
@Specialization
2602-
Object isqrtLong(long x) {
2603+
Object isqrtLong(long x,
2604+
@Cached NarrowBigIntegerNode makeInt) {
26032605
raiseIfNegative(x < 0);
2604-
return makeInt(op(PInt.longToBigInteger(x)));
2606+
return makeInt.execute(op(PInt.longToBigInteger(x)));
26052607
}
26062608

26072609
@Specialization
2608-
Object isqrtPInt(PInt x) {
2610+
Object isqrtPInt(PInt x,
2611+
@Cached NarrowBigIntegerNode makeInt) {
26092612
raiseIfNegative(x.isNegative());
2610-
return makeInt(op(x.getValue()));
2613+
return makeInt.execute(op(x.getValue()));
26112614
}
26122615

26132616
@Specialization(guards = "!isInteger(x)")
@@ -2618,20 +2621,6 @@ Object doGeneral(VirtualFrame frame, Object x,
26182621
return recursiveNode.execute(frame, lib.asIndexWithFrame(x, hasFrame, frame));
26192622
}
26202623

2621-
private Object makeInt(BigInteger i) {
2622-
try {
2623-
return PInt.intValueExact(i);
2624-
} catch (ArithmeticException e) {
2625-
// does not fit int, so try long
2626-
}
2627-
try {
2628-
return PInt.longValueExact(i);
2629-
} catch (ArithmeticException e) {
2630-
// does not fit long either, create PInt
2631-
}
2632-
return factory().createInt(i);
2633-
}
2634-
26352624
@TruffleBoundary
26362625
private static BigInteger op(BigInteger x) {
26372626
// assumes x >= 0

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public int intValue() {
348348
}
349349

350350
@TruffleBoundary
351-
private static int intValue(BigInteger value) {
351+
public static int intValue(BigInteger value) {
352352
return value.intValue();
353353
}
354354

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.nodes.util;
42+
43+
import com.oracle.graal.python.builtins.modules.MathGuards;
44+
import com.oracle.graal.python.builtins.objects.ints.PInt;
45+
import com.oracle.graal.python.nodes.PNodeWithContext;
46+
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
47+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
48+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
49+
import com.oracle.truffle.api.dsl.Cached;
50+
import com.oracle.truffle.api.dsl.ImportStatic;
51+
import com.oracle.truffle.api.dsl.Specialization;
52+
import com.oracle.truffle.api.dsl.TypeSystemReference;
53+
54+
import java.math.BigInteger;
55+
56+
@TypeSystemReference(PythonArithmeticTypes.class)
57+
@ImportStatic(MathGuards.class)
58+
public abstract class NarrowBigIntegerNode extends PNodeWithContext {
59+
60+
public abstract Object execute(BigInteger x);
61+
62+
@Specialization(guards = "fitsIntoInt(x)")
63+
int narrowToInt(BigInteger x) {
64+
return PInt.intValue(x);
65+
}
66+
67+
@Specialization(guards = {"fitsIntoLong(x)", "!fitsIntoInt(x)"})
68+
long narrowToLong(BigInteger x) {
69+
return PInt.longValue(x);
70+
}
71+
72+
@Specialization(guards = "!fitsIntoLong(x)")
73+
PInt makePInt(BigInteger x,
74+
@Cached PythonObjectFactory factory) {
75+
return factory.createInt(x);
76+
}
77+
78+
@TruffleBoundary
79+
static boolean fitsIntoInt(BigInteger x) {
80+
return x.bitLength() <= 31;
81+
}
82+
83+
@TruffleBoundary
84+
static boolean fitsIntoLong(BigInteger x) {
85+
return x.bitLength() <= 63;
86+
}
87+
}

0 commit comments

Comments
 (0)