Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 16b9a2dbb88c
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 7dba75b0d76e
Choose a head ref
  • 2 commits
  • 13 files changed
  • 1 contributor

Commits on Apr 25, 2016

  1. Revert "[Truffle] Implement Bignum#coerce"

    * This reverts commit d7d6723.
    * It breaks our assumption that Bignum do not have values that can fit a long.
    eregon committed Apr 25, 2016
    Copy the full SHA
    102353e View commit details
  2. [Truffle] Reintroduce assertion that Bignums are never created for va…

    …lues fitting in Fixnums.
    eregon committed Apr 25, 2016
    Copy the full SHA
    7dba75b View commit details
1 change: 1 addition & 0 deletions spec/truffle/tags/core/bignum/coerce_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fails:Bignum#coerce coerces other to a Bignum and returns [other, self] when passed a Fixnum
Original file line number Diff line number Diff line change
@@ -278,12 +278,12 @@ public abstract static class EqualNode extends CoreMethodArrayArgumentsNode {

@Specialization
public boolean equal(DynamicObject a, int b) {
return Layouts.BIGNUM.getValue(a).equals(BigInteger.valueOf(b));
return false;
}

@Specialization
public boolean equal(DynamicObject a, long b) {
return Layouts.BIGNUM.getValue(a).equals(BigInteger.valueOf(b));
return false;
}

@Specialization
@@ -487,26 +487,24 @@ public int bitLength(DynamicObject value) {
@CoreMethod(names = "coerce", required = 1)
public abstract static class CoerceNode extends CoreMethodArrayArgumentsNode {

// NOTE (eregon, 16 Feb. 2015): In other implementations, b is converted to a Bignum here,
// even if it fits in a Fixnum. We avoid it for implementation sanity
// and to keep the representations strictly distinct over the range of values.

@Specialization
public DynamicObject coerce(DynamicObject a, int b) {
CompilerDirectives.transferToInterpreter();

Object[] store = new Object[] { Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), BigInteger.valueOf(b)), a };
Object[] store = new Object[] { b, a };
return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), store, store.length);
}

@Specialization
public DynamicObject coerce(DynamicObject a, long b) {
CompilerDirectives.transferToInterpreter();

Object[] store = new Object[] { Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), BigInteger.valueOf(b)), a };
Object[] store = new Object[] { b, a };
return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), store, store.length);
}

@Specialization(guards = "isRubyBignum(b)")
public DynamicObject coerce(DynamicObject a, DynamicObject b) {
CompilerDirectives.transferToInterpreter();

Object[] store = new Object[] { b, a };
return Layouts.ARRAY.createArray(coreLibrary().getArrayFactory(), store, store.length);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jruby.truffle.core.numeric;

import com.oracle.truffle.api.object.DynamicObject;
import java.math.BigInteger;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;

public class BignumOperations {

private static final BigInteger LONG_MIN_BIGINT = BigInteger.valueOf(Long.MIN_VALUE);
private static final BigInteger LONG_MAX_BIGINT = BigInteger.valueOf(Long.MAX_VALUE);

public static DynamicObject createBignum(RubyContext context, BigInteger value) {
assert value.compareTo(LONG_MIN_BIGINT) < 0 || value.compareTo(LONG_MAX_BIGINT) > 0 : "Bignum in long range : " + value;
return Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), value);
}

}
Original file line number Diff line number Diff line change
@@ -461,7 +461,7 @@ public Object mod(long a, DynamicObject b) {

if (mod < 0 && Layouts.BIGNUM.getValue(b).compareTo(BigInteger.ZERO) > 0 || mod > 0 && Layouts.BIGNUM.getValue(b).compareTo(BigInteger.ZERO) < 0) {
adjustProfile.enter();
return Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), BigInteger.valueOf(mod).add(Layouts.BIGNUM.getValue(b)));
return createBignum(BigInteger.valueOf(mod).add(Layouts.BIGNUM.getValue(b)));
}

return mod;
@@ -1005,7 +1005,7 @@ public long absInBounds(long n) {
@Specialization(contains = "absInBounds")
public Object abs(long n) {
if (n == Long.MIN_VALUE) {
return Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), BigInteger.valueOf(n).abs());
return createBignum(BigInteger.valueOf(n).abs());
}
return (n < 0) ? -n : n;
}
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ public Object fixnumOrBignum(BigInteger value) {
return longValue;
}
} else {
return Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), value);
return createBignum(value);
}
}

Original file line number Diff line number Diff line change
@@ -169,8 +169,4 @@ private DynamicObject divMod(BigInteger a, BigInteger b) {
fixnumOrBignumRemainder.fixnumOrBignum(bigIntegerResults[1])}, 2);
}

public DynamicObject create(BigInteger value) {
return Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), value);
}

}
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ public DynamicObject pow(DynamicObject a, long b) {
return null; // Primitive failure
} else {
// TODO CS 15-Feb-15 what about this cast?
return Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), Layouts.BIGNUM.getValue(a).pow((int) b));
return createBignum(Layouts.BIGNUM.getValue(a).pow((int) b));
}
}

Original file line number Diff line number Diff line change
@@ -102,7 +102,7 @@ public static abstract class RandomizerGenSeedPrimitiveNode extends RubiniusPrim
@Specialization
public DynamicObject randomizerGenSeed(DynamicObject randomizerClass) {
final BigInteger seed = RubyRandom.randomSeedBigInteger(getContext().getJRubyRuntime().getRandom());
return Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), seed);
return createBignum(seed);
}
}

Original file line number Diff line number Diff line change
@@ -1576,7 +1576,7 @@ private Object toTruffle(IRubyObject object) {
return (int) value;
} else if (object instanceof org.jruby.RubyBignum) {
final BigInteger value = ((org.jruby.RubyBignum) object).getBigIntegerValue();
return Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), value);
return createBignum(value);
} else {
throw new UnsupportedOperationException();
}
Original file line number Diff line number Diff line change
@@ -18,13 +18,15 @@
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.DynamicObjectFactory;
import com.oracle.truffle.api.source.SourceSection;
import java.math.BigInteger;
import jnr.ffi.provider.MemoryManager;
import org.jcodings.Encoding;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.CoreLibrary;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.core.exception.CoreExceptions;
import org.jruby.truffle.core.kernel.TraceManager;
import org.jruby.truffle.core.numeric.BignumOperations;
import org.jruby.truffle.core.rope.CodeRange;
import org.jruby.truffle.core.rope.Rope;
import org.jruby.truffle.core.string.CoreStrings;
@@ -95,6 +97,10 @@ protected DynamicObject createString(Rope rope) {
return StringOperations.createString(getContext(), rope);
}

protected DynamicObject createBignum(BigInteger value) {
return BignumOperations.createBignum(getContext(), value);
}

protected CoreStrings coreStrings() {
return getContext().getCoreStrings();
}
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
import com.oracle.truffle.api.object.Property;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;

import org.jruby.truffle.core.numeric.BignumOperations;
import java.math.BigInteger;

/**
@@ -70,13 +70,13 @@ public static long smallFixnumToID(long fixnum) {
public static DynamicObject largeFixnumToID(RubyContext context, long fixnum) {
assert !isSmallFixnum(fixnum);
BigInteger big = unsignedBigInteger(fixnum);
return Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), big.or(LARGE_FIXNUM_FLAG));
return BignumOperations.createBignum(context, big.or(LARGE_FIXNUM_FLAG));
}

public static DynamicObject floatToID(RubyContext context, double value) {
long bits = Double.doubleToRawLongBits(value);
BigInteger big = unsignedBigInteger(bits);
return Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), big.or(FLOAT_FLAG));
return BignumOperations.createBignum(context, big.or(FLOAT_FLAG));
}

// ID => primitive
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@
import org.jruby.truffle.core.kernel.KernelNodesFactory;
import org.jruby.truffle.core.kernel.TraceManager;
import org.jruby.truffle.core.module.ModuleNodesFactory;
import org.jruby.truffle.core.numeric.BignumOperations;
import org.jruby.truffle.core.proc.ProcType;
import org.jruby.truffle.core.range.RangeNodesFactory;
import org.jruby.truffle.core.regexp.InterpolatedRegexpNode;
@@ -168,7 +169,6 @@
import org.jruby.truffle.stdlib.CoverageManager;
import org.jruby.util.ByteList;
import org.jruby.util.KeyValuePair;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
@@ -405,7 +405,7 @@ public RubyNode visitBignumNode(org.jruby.ast.BignumNode node) {
final RubyNode ret;

if (value.bitLength() >= 64) {
ret = new ObjectLiteralNode(context, sourceSection, Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), node.getValue()));
ret = new ObjectLiteralNode(context, sourceSection, BignumOperations.createBignum(context, node.getValue()));
} else {
ret = new LongFixnumLiteralNode(context, sourceSection, value.longValue());
}
Original file line number Diff line number Diff line change
@@ -44,9 +44,9 @@
import org.jcodings.specific.UTF8Encoding;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.core.numeric.BignumOperations;
import org.jruby.truffle.core.rope.CodeRange;
import org.jruby.truffle.core.string.StringOperations;

import java.math.BigInteger;
import java.util.Arrays;

@@ -163,7 +163,7 @@ public static void load(RubiniusConfiguration configuration, RubyContext context
}

protected static DynamicObject newBignum(RubyContext context, String value) {
return Layouts.BIGNUM.createBignum(context.getCoreLibrary().getBignumFactory(), new BigInteger(value));
return BignumOperations.createBignum(context, new BigInteger(value));
}

protected static DynamicObject string(RubyContext context, String value) {