Skip to content

Commit

Permalink
[Truffle] Fix a couple specializations which cannot overflow.
Browse files Browse the repository at this point in the history
* Also rename specializations which deal with the overflow just [op]negWithOverflow
  so we have a compile-time check for this potential issue.
  • Loading branch information
eregon committed Oct 31, 2014
1 parent 62bb382 commit ae9a341
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions core/src/main/java/org/jruby/truffle/nodes/core/FixnumNodes.java
Expand Up @@ -65,14 +65,9 @@ public int neg(int value) {
return ExactMath.subtractExact(0, value);
}

@Specialization(rewriteOn = ArithmeticException.class)
public long negWithLongOverflow(int value) {
return ExactMath.subtractExact(0, (long) value);
}

@Specialization
public BigInteger negWithBigIntegerOverflow(int value) {
return BigInteger.valueOf(value).negate();
public long negWithOverflow(int value) {
return -(long) (value);
}

@Specialization(rewriteOn = ArithmeticException.class)
Expand All @@ -81,7 +76,7 @@ public long neg(long value) {
}

@Specialization
public BigInteger negWithBigIntegerOverflow(long value) {
public BigInteger negWithOverflow(long value) {
return BigInteger.valueOf(value).negate();
}

Expand All @@ -108,7 +103,7 @@ public int add(int a, int b) {
}

@Specialization
public long addWithLongOverflow(int a, int b) {
public long addWithOverflow(int a, int b) {
return (long) a + (long) b;
}

Expand Down Expand Up @@ -138,7 +133,7 @@ public long add(long a, int b) {
}

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

Expand All @@ -148,7 +143,7 @@ public long add(long a, long b) {
}

@Specialization
public Object addWithBigIntegerOverflow(long a, long b) {
public Object addWithOverflow(long a, long b) {
return fixnumOrBignum.fixnumOrBignum(SlowPathBigInteger.add(BigInteger.valueOf(a), BigInteger.valueOf(b)));
}

Expand Down Expand Up @@ -185,7 +180,7 @@ public int sub(int a, int b) {
}

@Specialization
public long subWithLongOverflow(int a, int b) {
public long subWithOverflow(int a, int b) {
return (long) a - (long) b;
}

Expand All @@ -209,13 +204,8 @@ public long sub(long a, int b) {
return ExactMath.subtractExact(a, b);
}

@Specialization(rewriteOn = ArithmeticException.class)
public long subWithLongOverflow(long a, int b) {
return ExactMath.subtractExact(a, (long) b);
}

@Specialization
public Object subWithBigIntegerOverflow(long a, int b) {
public Object subWithOverflow(long a, int b) {
return fixnumOrBignum.fixnumOrBignum(BigInteger.valueOf(a).subtract(BigInteger.valueOf(b)));
}

Expand All @@ -225,7 +215,7 @@ public long sub(long a, long b) {
}

@Specialization
public Object subWithBigIntegerOverflow(long a, long b) {
public Object subWithOverflow(long a, long b) {
return fixnumOrBignum.fixnumOrBignum(BigInteger.valueOf(a).subtract(BigInteger.valueOf(b)));
}

Expand Down Expand Up @@ -262,7 +252,7 @@ public int mul(int a, int b) {
}

@Specialization
public long mulWithLong(int a, int b) {
public long mulWithOverflow(int a, int b) {
return (long) a * (long) b;
}

Expand All @@ -272,7 +262,7 @@ public Object mul(int a, long b) {
}

@Specialization
public Object mulWithBigInteger(int a, long b) {
public Object mulWithOverflow(int a, long b) {
return fixnumOrBignum.fixnumOrBignum(BigInteger.valueOf(a).multiply(BigInteger.valueOf(b)));
}

Expand All @@ -292,7 +282,7 @@ public long mul(long a, int b) {
}

@Specialization
public Object mulWithBigInteger(long a, int b) {
public Object mulWithOverflow(long a, int b) {
return fixnumOrBignum.fixnumOrBignum(SlowPathBigInteger.multiply(BigInteger.valueOf(a), BigInteger.valueOf(b)));
}

Expand All @@ -302,7 +292,7 @@ public long mul(long a, long b) {
}

@Specialization
public Object mulWithBigInteger(long a, long b) {
public Object mulWithOverflow(long a, long b) {
return fixnumOrBignum.fixnumOrBignum(SlowPathBigInteger.multiply(BigInteger.valueOf(a), BigInteger.valueOf(b)));
}

Expand Down

0 comments on commit ae9a341

Please sign in to comment.