Skip to content

Commit

Permalink
[Truffle] Add ImplicitCast from int to long.
Browse files Browse the repository at this point in the history
* Allows to remove all the (int,long) and (long,int) useless overrides.
  This works for most cases, either because they were converting to long anyway
  or they were using BigInteger.valueOf(long) which has no int equivalent.
* We can also remove (BigInteger,int) and (int,BigInteger) if
  there is a corresponding (BigInteger,long) and (long,BigInteger)
  since these operations will need to cast the primitive to a BigInteger.
* We keep the (int,double) and (double,int) specializations since
  often they convert the int to a double and that is likely faster
  than a long to a double (plus the possible int->long).
* We also have some additional specializations for binary ops on int
  that cannot exceed the range of long (+,-,*) and therefore need
  no overflow check.
  • Loading branch information
eregon committed Oct 28, 2014
1 parent 67371e1 commit 2e4b534
Showing 6 changed files with 29 additions and 423 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/RubyTypes.java
Original file line number Diff line number Diff line change
@@ -73,6 +73,11 @@

public class RubyTypes {

@ImplicitCast
public long int2long(int value) {
return value;
}

@ImplicitCast
public boolean unboxBoolean(RubyTrueClass value) {
return true;
80 changes: 0 additions & 80 deletions core/src/main/java/org/jruby/truffle/nodes/core/BignumNodes.java
Original file line number Diff line number Diff line change
@@ -76,11 +76,6 @@ public AddNode(AddNode prev) {
fixnumOrBignum = prev.fixnumOrBignum;
}

@Specialization
public Object add(BigInteger a, int b) {
return SlowPathBigInteger.add(a, BigInteger.valueOf(b));
}

@Specialization
public Object add(BigInteger a, long b) {
return SlowPathBigInteger.add(a, BigInteger.valueOf(b));
@@ -113,11 +108,6 @@ public SubNode(SubNode prev) {
fixnumOrBignum = prev.fixnumOrBignum;
}

@Specialization
public Object sub(BigInteger a, int b) {
return SlowPathBigInteger.subtract(a, BigInteger.valueOf(b));
}

@Specialization
public Object sub(BigInteger a, long b) {
return SlowPathBigInteger.subtract(a, BigInteger.valueOf(b));
@@ -146,11 +136,6 @@ public MulNode(MulNode prev) {
super(prev);
}

@Specialization
public Object mul(BigInteger a, int b) {
return SlowPathBigInteger.multiply(a, BigInteger.valueOf(b));
}

@Specialization
public Object mul(BigInteger a, long b) {
return SlowPathBigInteger.multiply(a, BigInteger.valueOf(b));
@@ -214,11 +199,6 @@ public DivNode(DivNode prev) {
super(prev);
}

@Specialization
public Object div(BigInteger a, int b) {
return SlowPathBigInteger.divide(a, BigInteger.valueOf(b));
}

@Specialization
public Object div(BigInteger a, long b) {
return SlowPathBigInteger.divide(a, BigInteger.valueOf(b));
@@ -247,11 +227,6 @@ public ModNode(ModNode prev) {
super(prev);
}

@Specialization
public Object mod(BigInteger a, int b) {
return RubyFixnum.fixnumOrBignum(SlowPathBigInteger.mod(a, BigInteger.valueOf(b)));
}

@Specialization
public Object mod(BigInteger a, long b) {
return RubyFixnum.fixnumOrBignum(SlowPathBigInteger.mod(a, BigInteger.valueOf(b)));
@@ -279,11 +254,6 @@ public DivModNode(DivModNode prev) {
divModNode = new GeneralDivModNode(getContext());
}

@Specialization
public RubyArray divMod(BigInteger a, int b) {
return divModNode.execute(a, b);
}

@Specialization
public RubyArray divMod(BigInteger a, long b) {
return divModNode.execute(a, b);
@@ -307,11 +277,6 @@ public LessNode(LessNode prev) {
super(prev);
}

@Specialization
public boolean less(BigInteger a, int b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) < 0;
}

@Specialization
public boolean less(BigInteger a, long b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) < 0;
@@ -339,11 +304,6 @@ public LessEqualNode(LessEqualNode prev) {
super(prev);
}

@Specialization
public boolean lessEqual(BigInteger a, int b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) <= 0;
}

@Specialization
public boolean lessEqual(BigInteger a, long b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) <= 0;
@@ -371,11 +331,6 @@ public EqualNode(EqualNode prev) {
super(prev);
}

@Specialization
public boolean equal(BigInteger a, int b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) == 0;
}

@Specialization
public boolean equal(BigInteger a, long b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) == 0;
@@ -403,11 +358,6 @@ public CompareNode(CompareNode prev) {
super(prev);
}

@Specialization
public int compare(BigInteger a, int b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b));
}

@Specialization
public int compare(BigInteger a, long b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b));
@@ -435,11 +385,6 @@ public NotEqualNode(NotEqualNode prev) {
super(prev);
}

@Specialization
public boolean notEqual(BigInteger a, int b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) != 0;
}

@Specialization
public boolean notEqual(BigInteger a, long b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) != 0;
@@ -467,11 +412,6 @@ public GreaterEqualNode(GreaterEqualNode prev) {
super(prev);
}

@Specialization
public boolean greaterEqual(BigInteger a, int b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) >= 0;
}

@Specialization
public boolean greaterEqual(BigInteger a, long b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) >= 0;
@@ -499,11 +439,6 @@ public GreaterNode(GreaterNode prev) {
super(prev);
}

@Specialization
public boolean greater(BigInteger a, int b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) > 0;
}

@Specialization
public boolean greater(BigInteger a, long b) {
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) > 0;
@@ -535,11 +470,6 @@ public BitAndNode(BitAndNode prev) {
fixnumOrBignumNode = prev.fixnumOrBignumNode;
}

@Specialization
public Object bitAnd(BigInteger a, int b) {
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.and(a, BigInteger.valueOf(b)));
}

@Specialization
public Object bitAnd(BigInteger a, long b) {
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.and(a, BigInteger.valueOf(b)));
@@ -566,11 +496,6 @@ public BitOrNode(BitOrNode prev) {
fixnumOrBignumNode = prev.fixnumOrBignumNode;
}

@Specialization
public Object bitOr(BigInteger a, int b) {
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.or(a, BigInteger.valueOf(b)));
}

@Specialization
public Object bitOr(BigInteger a, long b) {
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.or(a, BigInteger.valueOf(b)));
@@ -597,11 +522,6 @@ public BitXOrNode(BitXOrNode prev) {
fixnumOrBignumNode = prev.fixnumOrBignumNode;
}

@Specialization
public Object bitXOr(BigInteger a, int b) {
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.xor(a, BigInteger.valueOf(b)));
}

@Specialization
public Object bitXOr(BigInteger a, long b) {
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.xor(a, BigInteger.valueOf(b)));
Loading

0 comments on commit 2e4b534

Please sign in to comment.