Skip to content

Commit

Permalink
[Truffle] Use normal Math rather than Truffle's ExactMath.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Oct 13, 2016
1 parent fbb433f commit 3b2f4ff
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
Expand Up @@ -11,7 +11,6 @@

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.ExactMath;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
Expand Down Expand Up @@ -52,7 +51,7 @@ public abstract static class NegNode extends CoreMethodArrayArgumentsNode {

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

@Specialization(contains = "neg")
Expand All @@ -65,7 +64,7 @@ public Object negWithOverflow(int value) {

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

@Specialization
Expand All @@ -87,7 +86,7 @@ public abstract static class AddNode extends BignumNodes.BignumCoreMethodNode im

@Specialization(rewriteOn = ArithmeticException.class)
public int add(int a, int b) {
return ExactMath.addExact(a, b);
return Math.addExact(a, b);
}

@Specialization
Expand All @@ -106,7 +105,7 @@ public Object addCoerced(

@Specialization(rewriteOn = ArithmeticException.class)
public long add(long a, long b) {
return ExactMath.addExact(a, b);
return Math.addExact(a, b);
}

@Specialization
Expand Down Expand Up @@ -142,7 +141,7 @@ public abstract static class SubNode extends BignumNodes.BignumCoreMethodNode im

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

@Specialization
Expand All @@ -161,7 +160,7 @@ public Object subCoerced(

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

@Specialization
Expand Down Expand Up @@ -197,7 +196,7 @@ public abstract static class MulNode extends BignumNodes.BignumCoreMethodNode im

@Specialization(rewriteOn = ArithmeticException.class)
public int mul(int a, int b) {
return ExactMath.multiplyExact(a, b);
return Math.multiplyExact(a, b);
}

@Specialization
Expand All @@ -216,7 +215,7 @@ public Object mulCoerced(

@Specialization(rewriteOn = ArithmeticException.class)
public long mul(long a, long b) {
return ExactMath.multiplyExact(a, b);
return Math.multiplyExact(a, b);
}

@TruffleBoundary
Expand Down Expand Up @@ -1005,7 +1004,7 @@ public abstract static class AbsNode extends CoreMethodArrayArgumentsNode {

@Specialization(rewriteOn = ArithmeticException.class)
public int absIntInBounds(int n) {
return (n < 0) ? ExactMath.subtractExact(0, n) : n;
return (n < 0) ? Math.subtractExact(0, n) : n;
}

@Specialization(contains = "absIntInBounds")
Expand All @@ -1018,7 +1017,7 @@ public Object abs(int n) {

@Specialization(rewriteOn = ArithmeticException.class)
public long absInBounds(long n) {
return (n < 0) ? ExactMath.subtractExact(0, n) : n;
return (n < 0) ? Math.subtractExact(0, n) : n;
}

@Specialization(contains = "absInBounds")
Expand Down
Expand Up @@ -16,7 +16,6 @@

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.ExactMath;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.NodeChild;
Expand Down Expand Up @@ -241,7 +240,7 @@ public abstract static class MakeConcatNode extends RubyNode {
public Rope concatMutableRope(RopeBuffer left, Rope right, Encoding encoding,
@Cached("createBinaryProfile()") ConditionProfile differentEncodingProfile) {
try {
ExactMath.addExact(left.byteLength(), right.byteLength());
Math.addExact(left.byteLength(), right.byteLength());
} catch (ArithmeticException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw new RaiseException(getContext().getCoreExceptions().argumentError("Result of string concatenation exceeds the system maximum string length", this));
Expand All @@ -264,7 +263,7 @@ public Rope concat(Rope left, Rope right, Encoding encoding,
@Cached("createBinaryProfile()") ConditionProfile brokenCodeRangeProfile,
@Cached("createBinaryProfile()") ConditionProfile isLeftSingleByteOptimizableProfile) {
try {
ExactMath.addExact(left.byteLength(), right.byteLength());
Math.addExact(left.byteLength(), right.byteLength());
} catch (ArithmeticException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw new RaiseException(getContext().getCoreExceptions().argumentError("Result of string concatenation exceeds the system maximum string length", this));
Expand Down Expand Up @@ -560,7 +559,7 @@ public Rope multiplySingleByteString(Rope base, int times,
@Specialization(guards = { "!isRopeBuffer(base)", "!isSingleByteString(base)", "times > 1" })
public Rope repeat(Rope base, int times) {
try {
ExactMath.multiplyExact(base.byteLength(), times);
Math.multiplyExact(base.byteLength(), times);
} catch (ArithmeticException e) {
throw new RaiseException(getContext().getCoreExceptions().argumentError("Result of repeating string exceeds the system maximum string length", this));
}
Expand Down
Expand Up @@ -10,7 +10,6 @@
package org.jruby.truffle.core.time;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.ExactMath;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
Expand Down Expand Up @@ -150,7 +149,7 @@ public abstract static class AddInternalNode extends CoreMethodArrayArgumentsNod
@Specialization
public DynamicObject addInternal(DynamicObject time, long seconds, long nanoSeconds) {
final DateTime dateTime = Layouts.TIME.getDateTime(time);
final long addMilis = ExactMath.addExact(ExactMath.multiplyExact(seconds, 1000L), (nanoSeconds / 1_000_000));
final long addMilis = Math.addExact(Math.multiplyExact(seconds, 1000L), (nanoSeconds / 1_000_000));
Layouts.TIME.setDateTime(time, dateTime.plus(addMilis));
Layouts.TIME.setNSec(time, (1_000_000 + Layouts.TIME.getNSec(time) + nanoSeconds % 1_000_000) % 1_000_000);
return time;
Expand Down Expand Up @@ -313,7 +312,7 @@ public DynamicObject timeSSpecific(VirtualFrame frame, DynamicObject timeClass,

private long getMillis(long seconds, int nanoseconds) {
try {
return ExactMath.addExact(ExactMath.multiplyExact(seconds, 1000L), (nanoseconds / 1_000_000));
return Math.addExact(Math.multiplyExact(seconds, 1000L), (nanoseconds / 1_000_000));
} catch (ArithmeticException e) {
String message = StringUtils.format("UNIX epoch + %d seconds out of range for Time (Joda-Time limitation)", seconds);
throw new RaiseException(coreExceptions().rangeError(message, this));
Expand Down
Expand Up @@ -10,7 +10,6 @@
package org.jruby.truffle.language.objects;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.ExactMath;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.Property;
import org.jruby.truffle.Layouts;
Expand Down Expand Up @@ -60,7 +59,7 @@ public static boolean isSmallFixnum(long fixnum) {
}

public static long smallFixnumToIDOverflow(long fixnum) throws ArithmeticException {
return ExactMath.addExact(ExactMath.multiplyExact(fixnum, 2), 1);
return Math.addExact(Math.multiplyExact(fixnum, 2), 1);
}

public static long smallFixnumToID(long fixnum) {
Expand Down

0 comments on commit 3b2f4ff

Please sign in to comment.