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: ba51ba030b63
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ab39d3f940b6
Choose a head ref
  • 6 commits
  • 5 files changed
  • 1 contributor

Commits on Dec 12, 2016

  1. Copy the full SHA
    35af266 View commit details
  2. Revert "Correct Random#rand(Bignum) spec - it always returns a Bignum"

    * This reverts commit 4de52ed.
    * This is incorrect in 2.4 and an implementation detail on older rubies.
    eregon committed Dec 12, 2016
    Copy the full SHA
    36ab32a View commit details
  3. Fix Random#rand spec with a Bignum

    * Make sure the results are in Bignum-range:
      it was generating numbers between 0 and 2**63-1.
    eregon committed Dec 12, 2016
    Copy the full SHA
    1d204fa View commit details
  4. Revert "[Truffle] Add a standard compliant Bignum#coerce"

    * This reverts commit 0b25c82.
    * Avoid breaking important assumptions and ease the transition to 2.4.
    eregon committed Dec 12, 2016
    Copy the full SHA
    5008f6b View commit details
  5. Revert "[Truffle] Random#rand with a Bignum limit always returns a Bi…

    …gnum."
    
    * This reverts commit 4ffc2a0.
    * Not needed hack and counter-productive for 2.4.
    eregon committed Dec 12, 2016
    Copy the full SHA
    8995fa8 View commit details
  6. Copy the full SHA
    ab39d3f View commit details
4 changes: 2 additions & 2 deletions spec/ruby/core/random/rand_spec.rb
Original file line number Diff line number Diff line change
@@ -96,9 +96,9 @@
end

describe "Random#rand with Bignum" do
it "always returns a Bignum" do
it "typically returns a Bignum" do
rnd = Random.new(1)
10.times.map{ rnd.rand(bignum_value) }.min.should be_an_instance_of(Bignum)
10.times.map{ rnd.rand(bignum_value*2) }.max.should be_an_instance_of(Bignum)
end

it "returns a Bignum greater than or equal to 0" do
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@
import org.jruby.truffle.builtins.CoreClass;
import org.jruby.truffle.builtins.CoreMethod;
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.builtins.NonStandard;
import org.jruby.truffle.builtins.Primitive;
import org.jruby.truffle.builtins.PrimitiveArrayArgumentsNode;
import org.jruby.truffle.builtins.UnaryCoreMethodNode;
@@ -584,29 +583,6 @@ public int bitLength(DynamicObject value) {

}

@NonStandard
@CoreMethod(names = "force_coerce", required = 1)
public abstract static class ForceCoerceNode extends CoreMethodArrayArgumentsNode {

@Specialization
public DynamicObject coerce(DynamicObject a, long b) {
Object[] store = new Object[] { Layouts.BIGNUM.createBignum(coreLibrary().getBignumFactory(), toBigInteger(b)), a };
return createArray(store, store.length);
}

@TruffleBoundary
private static BigInteger toBigInteger(long value) {
return BigInteger.valueOf(value);
}

@Specialization(guards = "isRubyBignum(b)")
public DynamicObject coerce(DynamicObject a, DynamicObject b) {
Object[] store = new Object[] { b, a };
return createArray(store, store.length);
}

}

@CoreMethod(names = "divmod", required = 1)
public abstract static class DivModNode extends CoreMethodArrayArgumentsNode {

Original file line number Diff line number Diff line change
@@ -34,13 +34,15 @@
package org.jruby.truffle.core.rubinius;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.DynamicObject;
import org.jcodings.specific.ASCIIEncoding;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.algorithms.Randomizer;
import org.jruby.truffle.builtins.Primitive;
import org.jruby.truffle.builtins.PrimitiveArrayArgumentsNode;
import org.jruby.truffle.core.numeric.FixnumOrBignumNode;
import org.jruby.truffle.core.rope.CodeRange;
import org.jruby.truffle.core.rope.RopeOperations;

@@ -159,9 +161,10 @@ public long randomizerRandInt(DynamicObject randomizer, long limit) {
}

@Specialization(guards = "isRubyBignum(limit)")
public DynamicObject randomizerRandInt(DynamicObject randomizer, DynamicObject limit) {
public Object randomizerRandInt(DynamicObject randomizer, DynamicObject limit,
@Cached("new()") FixnumOrBignumNode fixnumOrBignum) {
final Randomizer r = Layouts.RANDOMIZER.getRandomizer(randomizer);
return createBignum(randLimitedBignum(r, Layouts.BIGNUM.getValue(limit)));
return fixnumOrBignum.fixnumOrBignum(randLimitedBignum(r, Layouts.BIGNUM.getValue(limit)));
}

@TruffleBoundary
2 changes: 2 additions & 0 deletions truffle/src/main/ruby/core/bignum.rb
Original file line number Diff line number Diff line change
@@ -30,6 +30,8 @@ def coerce(other)
# NOTE (eregon, 16 Feb. 2015): In other implementations, other 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.
# This is assumed for instance in Fixnum#/ and other places.
# Note 2.4 solves this problem with Integer unification.
raise TypeError unless other.is_a?(Integer)
[other, self]
end
10 changes: 1 addition & 9 deletions truffle/src/main/ruby/core/random.rb
Original file line number Diff line number Diff line change
@@ -68,25 +68,17 @@ def random(limit)
limit_int = Rubinius::Type.coerce_to limit, Integer, :to_int
raise ArgumentError, "invalid argument - #{limit}" if limit_int <= 0

if limit.is_a?(Bignum)
as_bignum(random_integer(limit - 1))
elsif limit.is_a?(Integer)
if limit.is_a?(Integer)
random_integer(limit - 1)
elsif limit.respond_to?(:to_f)
random_float * limit
elsif limit_int.is_a?(Bignum)
as_bignum(random_integer(limit_int - 1))
else
random_integer(limit_int - 1)
end
end
end
end

def as_bignum(integer)
0xffff_ffff_ffff_ffff_ffff.force_coerce(integer)[0]
end

# Generate a random Float, in the range 0...1.0
def random_float
Truffle.primitive :randomizer_rand_float