Skip to content

Commit cd7499a

Browse files
committed
Reimplement NarrowBigIntegerNode using reflection
1 parent 14ad44b commit cd7499a

File tree

2 files changed

+166
-26
lines changed

2 files changed

+166
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2019, 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+
42+
package com.oracle.graal.python.nodes.util;
43+
44+
import com.oracle.graal.python.builtins.objects.ints.PInt;
45+
import com.oracle.graal.python.test.PythonTests;
46+
import org.junit.Assert;
47+
import org.junit.Before;
48+
import org.junit.Test;
49+
50+
import java.math.BigInteger;
51+
52+
public class NarrowBigIntegerNodeTests {
53+
54+
@Before
55+
public void setUp() {
56+
PythonTests.enterContext();
57+
}
58+
59+
private static NarrowBigIntegerNode narrow = NarrowBigIntegerNodeGen.getUncached();
60+
61+
@Test
62+
public void smallInts() {
63+
expectInt(-2);
64+
expectInt(-1);
65+
expectInt(0);
66+
expectInt(1);
67+
expectInt(2);
68+
}
69+
70+
@Test
71+
public void intLongBoundary() {
72+
expectLong(Integer.MIN_VALUE - 1L);
73+
expectInt(Integer.MIN_VALUE);
74+
expectInt(Integer.MIN_VALUE + 1);
75+
76+
expectInt(Integer.MAX_VALUE - 1);
77+
expectInt(Integer.MAX_VALUE);
78+
expectLong(Integer.MAX_VALUE + 1L);
79+
}
80+
81+
@Test
82+
public void long32bitBoundary() {
83+
expectLong(-0x100000001L);
84+
expectLong(-0x100000000L);
85+
expectLong(-0xFFFFFFFFL);
86+
87+
expectLong(0xFFFFFFFFL);
88+
expectLong(0x100000000L);
89+
expectLong(0x100000001L);
90+
}
91+
92+
@Test
93+
public void longPIntBoundary() {
94+
expectPInt(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE));
95+
expectLong(Long.MIN_VALUE);
96+
expectLong(Long.MIN_VALUE + 1L);
97+
98+
expectLong(Long.MAX_VALUE - 1L);
99+
expectLong(Long.MAX_VALUE);
100+
expectPInt(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE));
101+
}
102+
103+
private static void expectInt(int expected) {
104+
Object actual = narrow.execute(BigInteger.valueOf(expected));
105+
Assert.assertTrue(actual instanceof Integer);
106+
Assert.assertEquals(expected, (int) actual);
107+
}
108+
109+
private static void expectLong(long expected) {
110+
Object actual = narrow.execute(BigInteger.valueOf(expected));
111+
Assert.assertTrue(actual instanceof Long);
112+
Assert.assertEquals(expected, (long) actual);
113+
}
114+
115+
private static void expectPInt(BigInteger expected) {
116+
Object actual = narrow.execute(expected);
117+
Assert.assertTrue(actual instanceof PInt);
118+
Assert.assertEquals(expected, ((PInt) actual).getValue());
119+
}
120+
}

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

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,53 +41,73 @@
4141
package com.oracle.graal.python.nodes.util;
4242

4343
import com.oracle.graal.python.builtins.modules.MathGuards;
44-
import com.oracle.graal.python.builtins.objects.ints.PInt;
4544
import com.oracle.graal.python.nodes.PNodeWithContext;
4645
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
4746
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
47+
import com.oracle.truffle.api.CompilerDirectives;
4848
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4949
import com.oracle.truffle.api.dsl.Cached;
50+
import com.oracle.truffle.api.dsl.GenerateUncached;
5051
import com.oracle.truffle.api.dsl.ImportStatic;
5152
import com.oracle.truffle.api.dsl.Specialization;
5253
import com.oracle.truffle.api.dsl.TypeSystemReference;
54+
import com.oracle.truffle.api.profiles.BranchProfile;
5355

56+
import java.lang.reflect.Field;
5457
import java.math.BigInteger;
5558

5659
@TypeSystemReference(PythonArithmeticTypes.class)
5760
@ImportStatic(MathGuards.class)
61+
@GenerateUncached
5862
public abstract class NarrowBigIntegerNode extends PNodeWithContext {
5963

60-
public abstract Object execute(BigInteger x);
64+
private static final Field MAG_FIELD;
6165

62-
@Specialization(guards = "fitsIntoInt(x)")
63-
int narrowToInt(BigInteger x) {
64-
return PInt.intValue(x);
66+
static {
67+
try {
68+
MAG_FIELD = BigInteger.class.getDeclaredField("mag");
69+
MAG_FIELD.setAccessible(true);
70+
} catch (NoSuchFieldException e) {
71+
throw new RuntimeException("Unable to access BigInteger.mag", e);
72+
}
6573
}
6674

67-
@Specialization(guards = "fitsIntoLongButNotInt(x)")
68-
long narrowToLong(BigInteger x) {
69-
return PInt.longValue(x);
70-
}
75+
public abstract Object execute(BigInteger x);
7176

72-
@Specialization(guards = "!fitsIntoLong(x)")
73-
PInt makePInt(BigInteger x,
74-
@Cached PythonObjectFactory factory) {
77+
@Specialization
78+
Object narrowBigInteger(BigInteger x,
79+
@Cached PythonObjectFactory factory,
80+
@Cached BranchProfile neddsPIntProfile) {
81+
int signum = x.signum();
82+
if (signum == 0) {
83+
return 0;
84+
}
85+
int[] mag = getMag(x);
86+
if (mag.length == 1) {
87+
if (mag[0] > 0 || (mag[0] == 0x80000000 && signum < 0)) {
88+
return signum * mag[0];
89+
} else {
90+
long mag0 = mag[0] & 0xFFFFFFFFL;
91+
return signum * mag0;
92+
}
93+
}
94+
if (mag.length == 2) {
95+
if (mag[0] > 0 || (mag[0] == 0x80000000 && signum < 0 && mag[1] == 0)) {
96+
long mag0 = mag[0] & 0xFFFFFFFFL;
97+
long mag1 = mag[1] & 0xFFFFFFFFL;
98+
return signum * ((mag0 << 32) | mag1);
99+
}
100+
}
101+
neddsPIntProfile.enter();
75102
return factory.createInt(x);
76103
}
77104

78-
@TruffleBoundary
79-
static boolean fitsIntoInt(BigInteger x) {
80-
return x.bitLength() <= 31;
81-
}
82-
83-
@TruffleBoundary
84-
static boolean fitsIntoLongButNotInt(BigInteger x) {
85-
int bitLen = x.bitLength();
86-
return bitLen > 31 && bitLen <= 63;
87-
}
88-
89-
@TruffleBoundary
90-
static boolean fitsIntoLong(BigInteger x) {
91-
return x.bitLength() <= 63;
105+
@TruffleBoundary(allowInlining = true)
106+
static int[] getMag(BigInteger x) {
107+
try {
108+
return (int[]) MAG_FIELD.get(x);
109+
} catch (IllegalAccessException e) {
110+
throw CompilerDirectives.shouldNotReachHere(e);
111+
}
92112
}
93113
}

0 commit comments

Comments
 (0)