Skip to content

Commit c79d250

Browse files
committed
[GR-26036] Remove the reflective access to BigInteger#mag.
PullRequest: graalpython/1268
2 parents f909a7c + 1c97a91 commit c79d250

File tree

4 files changed

+43
-151
lines changed

4 files changed

+43
-151
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/nodes/util/BigIntegerUtilsTests.java renamed to graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/objects/ints/PIntBigIntUtilsTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@
3939
* SOFTWARE.
4040
*/
4141

42-
package com.oracle.graal.python.nodes.util;
42+
package com.oracle.graal.python.test.objects.ints;
4343

4444
import java.math.BigInteger;
4545

46+
import com.oracle.graal.python.builtins.objects.ints.PInt;
4647
import org.junit.Assert;
4748
import org.junit.Test;
4849
import org.junit.experimental.theories.Theory;
4950

5051
import com.oracle.graal.python.util.OverflowException;
5152

52-
public class BigIntegerUtilsTests {
53+
public class PIntBigIntUtilsTests {
5354
@Test
5455
public void smallInts() {
5556
checkInt(-2);
@@ -107,15 +108,15 @@ public void longBoundary() {
107108

108109
private static void checkInt(int value) {
109110
try {
110-
Assert.assertEquals(value, BigIntegerUtils.intValueExact(BigInteger.valueOf(value)));
111+
Assert.assertEquals(value, PInt.intValueExact(BigInteger.valueOf(value)));
111112
} catch (OverflowException e) {
112113
Assert.fail("intValueExact: unexpected overflow");
113114
}
114115
}
115116

116117
private static void checkIntOverflow(long value) {
117118
try {
118-
BigIntegerUtils.intValueExact(BigInteger.valueOf(value));
119+
PInt.intValueExact(BigInteger.valueOf(value));
119120
Assert.fail("intValueExact should overflow for " + value);
120121
} catch (OverflowException e) {
121122
// nop
@@ -124,7 +125,7 @@ private static void checkIntOverflow(long value) {
124125

125126
private static void checkLong(long value) {
126127
try {
127-
Assert.assertEquals(value, BigIntegerUtils.longValueExact(BigInteger.valueOf(value)));
128+
Assert.assertEquals(value, PInt.longValueExact(BigInteger.valueOf(value)));
128129
} catch (OverflowException e) {
129130
Assert.fail("intValueExact: unexpected overflow");
130131
}
@@ -133,7 +134,7 @@ private static void checkLong(long value) {
133134
private static void checkLongOverflow(long value1, long value2) {
134135
try {
135136
BigInteger value = BigInteger.valueOf(value1).add(BigInteger.valueOf(value2));
136-
BigIntegerUtils.longValueExact(value);
137+
PInt.longValueExact(value);
137138
Assert.fail("intValueExact should overflow for " + value);
138139
} catch (OverflowException e) {
139140
// nop

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3939
import com.oracle.graal.python.nodes.ErrorMessages;
4040
import com.oracle.graal.python.nodes.PRaiseNode;
41-
import com.oracle.graal.python.nodes.util.BigIntegerUtils;
4241
import com.oracle.graal.python.nodes.util.CastToJavaDoubleNode;
4342
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
4443
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
@@ -361,20 +360,20 @@ public int intValue() {
361360
return intValue(value);
362361
}
363362

364-
@TruffleBoundary
363+
@TruffleBoundary(allowInlining = true)
365364
public static int intValue(BigInteger value) {
366365
return value.intValue();
367366
}
368367

369368
public int intValueExact() throws OverflowException {
370-
return BigIntegerUtils.intValueExact(value);
369+
return intValueExact(value);
371370
}
372371

373372
public long longValue() {
374373
return longValue(value);
375374
}
376375

377-
@TruffleBoundary
376+
@TruffleBoundary(allowInlining = true)
378377
public static long longValue(BigInteger integer) {
379378
return integer.longValue();
380379
}
@@ -383,8 +382,11 @@ public long longValueExact() throws OverflowException {
383382
return longValueExact(value);
384383
}
385384

386-
public static long longValueExact(BigInteger value) throws OverflowException {
387-
return BigIntegerUtils.longValueExact(value);
385+
public static long longValueExact(BigInteger x) throws OverflowException {
386+
if (!fitsIn(x, MIN_LONG, MAX_LONG)) {
387+
throw OverflowException.INSTANCE;
388+
}
389+
return longValue(x);
388390
}
389391

390392
public PInt max(PInt val) {
@@ -445,6 +447,13 @@ public static int intValueExact(long val) throws OverflowException {
445447
return (int) val;
446448
}
447449

450+
public static int intValueExact(BigInteger x) throws OverflowException {
451+
if (!fitsIn(x, MIN_INT, MAX_INT)) {
452+
throw OverflowException.INSTANCE;
453+
}
454+
return intValue(x);
455+
}
456+
448457
public static char charValueExact(int val) throws OverflowException {
449458
char t = (char) val;
450459
if (t != val) {
@@ -560,8 +569,12 @@ public static long hashBigInteger(BigInteger i) {
560569
return h == -1 ? -2 : h;
561570
}
562571

563-
@TruffleBoundary
564-
private boolean fitsIn(BigInteger left, BigInteger right) {
572+
@TruffleBoundary(allowInlining = true)
573+
public static boolean fitsIn(BigInteger value, BigInteger left, BigInteger right) {
565574
return value.compareTo(left) >= 0 && value.compareTo(right) <= 0;
566575
}
576+
577+
private boolean fitsIn(BigInteger left, BigInteger right) {
578+
return fitsIn(value, left, right);
579+
}
567580
}

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

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

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

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,37 @@
4040
*/
4141
package com.oracle.graal.python.nodes.util;
4242

43-
import static com.oracle.graal.python.nodes.util.BigIntegerUtils.getMag;
44-
4543
import java.math.BigInteger;
4644

45+
import com.oracle.graal.python.builtins.objects.ints.PInt;
4746
import com.oracle.graal.python.nodes.PNodeWithContext;
4847
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4948
import com.oracle.truffle.api.dsl.Cached;
5049
import com.oracle.truffle.api.dsl.GenerateUncached;
5150
import com.oracle.truffle.api.dsl.Specialization;
52-
import com.oracle.truffle.api.profiles.BranchProfile;
51+
import com.oracle.truffle.api.profiles.ConditionProfile;
5352

5453
@GenerateUncached
5554
public abstract class NarrowBigIntegerNode extends PNodeWithContext {
5655

5756
public abstract Object execute(BigInteger x);
5857

59-
@Specialization
58+
@Specialization(guards = "x.signum() == 0")
59+
Object narrowBigInteger0(@SuppressWarnings("unused") BigInteger x) {
60+
return 0;
61+
}
62+
63+
@Specialization(guards = "x.signum() != 0")
6064
Object narrowBigInteger(BigInteger x,
61-
@Cached PythonObjectFactory factory,
62-
@Cached BranchProfile neddsPIntProfile) {
63-
int signum = x.signum();
64-
if (signum == 0) {
65-
return 0;
66-
}
67-
int[] mag = getMag(x);
68-
if (mag.length == 1) {
69-
if (mag[0] > 0 || (mag[0] == 0x80000000 && signum < 0)) {
70-
return signum * mag[0];
71-
} else {
72-
long mag0 = mag[0] & 0xFFFFFFFFL;
73-
return signum * mag0;
74-
}
65+
@Cached ConditionProfile fitsIntProfile,
66+
@Cached ConditionProfile fitsLongProfile,
67+
@Cached PythonObjectFactory factory) {
68+
if (fitsIntProfile.profile(PInt.fitsIn(x, PInt.MIN_INT, PInt.MAX_INT))) {
69+
return PInt.intValue(x);
7570
}
76-
if (mag.length == 2) {
77-
if (mag[0] > 0 || (mag[0] == 0x80000000 && signum < 0 && mag[1] == 0)) {
78-
long mag0 = mag[0] & 0xFFFFFFFFL;
79-
long mag1 = mag[1] & 0xFFFFFFFFL;
80-
return signum * ((mag0 << 32) | mag1);
81-
}
71+
if (fitsLongProfile.profile(PInt.fitsIn(x, PInt.MIN_LONG, PInt.MAX_LONG))) {
72+
return PInt.longValue(x);
8273
}
83-
neddsPIntProfile.enter();
8474
return factory.createInt(x);
8575
}
8676
}

0 commit comments

Comments
 (0)