Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Alternative int overflow logic

  • Loading branch information...
commit 83a39fafbf6bb96f5ac0dbb5f7c6b8e7633f1d27 1 parent 367b52e
@krestenkrab krestenkrab authored
Showing with 13 additions and 3 deletions.
  1. +13 −3 src/main/java/erjang/ESmall.java
View
16 src/main/java/erjang/ESmall.java
@@ -211,13 +211,23 @@ public ENumber add(EObject rhs) {
return rhs.add(value, false);
}
- public EInteger add(int rhs, boolean guard) {
- return ERT.box((long) value + (long) rhs);
+ public EInteger add(int b, boolean guard) {
+ int a = value;
+ int r=a+b;
+ if (( (a^r) & (b^r) ) < 0)
+ return ERT.box((long)a + (long)b);
+ return ERT.box(r);
}
+ /** overflow check due to http://www.drdobbs.com/jvm/signalling-integer-overflows-in-java/210500001 */
@BIF(name="+")
public EInteger add(ESmall rhs) {
- return ERT.box((long)value + (long)rhs.value);
+ int a = value;
+ int b = rhs.value;
+ int r=a+b;
+ if (( (a^r) & (b^r) ) < 0)
+ return ERT.box((long)a + (long)b);
+ return ERT.box(r);
}
public ENumber add(double lhs, boolean guard) {
Please sign in to comment.
Something went wrong with that request. Please try again.