Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopting GraalVM's support for BigInteger #7420

Merged
merged 29 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
eb89979
Request BigInteger if exact precision is requested
JaroslavTulach Jul 28, 2023
681d7b2
Removing assert mangling. Use power of 3 to disalign with binary enco…
JaroslavTulach Jul 28, 2023
f20b569
EnsoBigInteger no longer extends Number
JaroslavTulach Jul 28, 2023
ad2bae5
No longer need to supress abstract export warning
JaroslavTulach Jul 28, 2023
5c358a5
Note in changelog
JaroslavTulach Jul 28, 2023
a435d7c
Spec for working with BigInteger in various polyglot form
JaroslavTulach Jul 28, 2023
6d77f54
Merge with develop
JaroslavTulach Jul 28, 2023
ca73109
Simple BigInteger interop with Java test
JaroslavTulach Jul 29, 2023
be6e3f6
Cached and uncached version of ToEnsoNumberNode
JaroslavTulach Jul 29, 2023
21ef3b3
Can multiply big integer with long
JaroslavTulach Jul 29, 2023
4f9d8ca
Convert to EnsoBigInteger on boundary
JaroslavTulach Jul 29, 2023
59f3d36
Check expected_value is the same for all implementations
JaroslavTulach Jul 31, 2023
a667393
CONVERT_TO_BIG_INT not a number
JaroslavTulach Jul 31, 2023
a164a1b
Returning back deleted doByte conversion
JaroslavTulach Aug 1, 2023
335f469
Avoid conversion of values with warnings
JaroslavTulach Aug 1, 2023
986d7a2
Nicer toString() for Warning
JaroslavTulach Aug 1, 2023
ec5ee2a
Avoid conversion of valid protocol values
JaroslavTulach Aug 2, 2023
881f93b
Only convert TruffleObject instances that fit into big integer
JaroslavTulach Aug 2, 2023
28ab2de
Merge remote-tracking branch 'origin/develop' into wip/jtulach/BigInt…
JaroslavTulach Aug 2, 2023
90a2c7e
Removing duplicataed 7176 line
JaroslavTulach Aug 2, 2023
8730f48
Prefer intrinsified shouldNotReachHere over regular IllegalStateExcep…
JaroslavTulach Aug 2, 2023
b61860f
Verify big integer is_a Integer
JaroslavTulach Aug 2, 2023
4e9dfda
Adding Radek's test
JaroslavTulach Aug 2, 2023
9439797
Use WarningsLibrary as a guard
JaroslavTulach Aug 2, 2023
79cc212
Verify behavior of _:Integer
JaroslavTulach Aug 2, 2023
be82551
Removing debris
JaroslavTulach Aug 3, 2023
20a4d5a
Merge branch 'develop' into wip/jtulach/BigInt_7213
mergify[bot] Aug 3, 2023
45b66d8
Merge branch 'develop' into wip/jtulach/BigInt_7213
mergify[bot] Aug 3, 2023
99c7293
Merge branch 'develop' into wip/jtulach/BigInt_7213
mergify[bot] Aug 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ boolean equalsDoubleBool(double self, boolean other) {
@Specialization
@TruffleBoundary
boolean equalsDoubleBigInt(double self, EnsoBigInteger other) {
return self == other.doubleValue();
return self == other.asDouble();
}

@Specialization
Expand All @@ -130,7 +130,7 @@ boolean equalsBigIntBigInt(EnsoBigInteger self, EnsoBigInteger otherBigInt) {
@Specialization
@TruffleBoundary
boolean equalsBitIntDouble(EnsoBigInteger self, double other) {
return self.doubleValue() == other;
return self.asDouble() == other;
}

@Specialization
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package org.enso.interpreter.runtime.number;

import java.math.BigInteger;

import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;

import java.math.BigInteger;

/** Internal wrapper for a {@link BigInteger}. */
@ExportLibrary(InteropLibrary.class)
@ExportLibrary(TypesLibrary.class)
@SuppressWarnings("truffle-abstract-export")
public final class EnsoBigInteger extends Number implements TruffleObject {
public final class EnsoBigInteger implements TruffleObject {
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
private final BigInteger value;

/**
Expand Down Expand Up @@ -82,40 +83,50 @@ final boolean fitsInDouble() {
return false;
}

@ExportMessage
final boolean fitsInBigInteger() {
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final byte asByte() throws UnsupportedMessageException {
return byteValue();
return value.byteValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final short asShort() throws UnsupportedMessageException {
return shortValue();
return value.shortValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final int asInt() throws UnsupportedMessageException {
return intValue();
return value.intValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final long asLong() throws UnsupportedMessageException {
return longValue();
return value.longValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final float asFloat() throws UnsupportedMessageException {
return floatValue();
final float asFloat() {
return value.floatValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final double asDouble() throws UnsupportedMessageException {
return doubleValue();
public final double asDouble() {
return value.doubleValue();
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
}

@ExportMessage
public final BigInteger asBigInteger() {
return value;
}

@ExportMessage
Expand All @@ -138,26 +149,6 @@ Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
return EnsoContext.get(thisLib).getBuiltins().number().getBigInteger();
}

@Override
public int intValue() {
return value.intValue();
}

@Override
public long longValue() {
return value.longValue();
}

@Override
public float floatValue() {
return value.floatValue();
}

@Override
public double doubleValue() {
return value.doubleValue();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof EnsoBigInteger otherBigInt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ public static double numberArrayAverage(Number[] arr) {
return sum / arr.length;
}

public static String exactArrayAverage(Number[] arr) {
public static String exactArrayAverage(BigInteger[] arr) {
var sum = BigInteger.ZERO;
for (int i = 0; i < arr.length; i++) {
var n = arr[i] instanceof Long l ? BigInteger.valueOf(l) :
new BigInteger(arr[i].toString());
sum = sum.add(n);
sum = sum.add(arr[i]);
}
return sum.divide(BigInteger.valueOf(arr.length)).toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -38,7 +38,7 @@ public void evaluation() throws Exception {
powers n =
go x v b = if x > n then b.to_vector else
b.append v
@Tail_Call go x+1 v*2 b
@Tail_Call go x+1 v*3 b
go 1 1 Vector.new_builder
""";
var powers = evalCode(code, "powers");
Expand All @@ -59,33 +59,20 @@ public void evaluation() throws Exception {
if (e.fitsInDouble()) {
doubles++;
}
boolean assertsOn = false;
// Explanation at
// https://github.com/enso-org/enso/pull/4074#discussion_r1086222800
// rewrite when proper support for BigInteger is available
// https://github.com/oracle/graal/pull/5490
assert assertsOn = true;
String s;
if (!assertsOn) {
var n = e.as(Number.class);
assertNotNull("All numbers can be seen as java.lang.Number", n);
s = n.toString();
} else {
s = e.toString();
}
var b = new BigInteger(s);
var s = e.toString();
var b = e.asBigInteger();
assertNotNull("Each Enso number can be parsed as big integer", b);
assertEquals("Textual values are the same", s, b.toString());
values.add(b);
}
assertEquals("There are few long values and rest of doubles", 63, longs);
assertEquals("There are few double values and rest of Numbers", 63, doubles);
assertEquals("There are few long values and rest of doubles", 40, longs);
assertEquals("There are few double values and rest of Numbers", 34, doubles);
assertEquals("Two hundred numbers collected", 200, values.size());
for (int i = 1; i < values.size(); i++) {
var prev = values.get(i - 1);
var next = values.get(i);

assertEquals("Each value is accurate", prev.multiply(BigInteger.valueOf(2)), next);
assertEquals("Each value is accurate", prev.multiply(BigInteger.valueOf(3)), next);
}
}

Expand Down