package som.primitives.arithmetic;

import java.math.BigInteger;

import com.oracle.truffle.api.ExactMath;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.utilities.BranchProfile;

public abstract class ModuloPrim extends ArithmeticPrim {
  private final BranchProfile negativeRightOperand = new BranchProfile();

  @Specialization(order = 1, rewriteOn = ArithmeticException.class)
  public final long doLong(final long left, final long right) {
    long l = left;
    long r = right;
    long result = l % r;

    if (l > 0 && r < 0) {
      negativeRightOperand.enter();
      result = ExactMath.addExact(result, r);
    }
    return result;
  }

  @Specialization(order = 2)
  public final Object doBigInteger(final BigInteger left, final BigInteger right) {
    return reduceToIntIfPossible(left.mod(right));
  }

  @Specialization(order = 3)
  public final double doDouble(final double left, final double right) {
    return left % right;
  }

  @Specialization(order = 10)
  public final Object doBigInteger(final BigInteger left, final long right) {
    return doBigInteger(left, BigInteger.valueOf(right));
  }

  @Specialization(order = 11)
  public final Object doLong(final long left, final BigInteger right) {
    return doBigInteger(BigInteger.valueOf(left), right);
  }

  @Specialization(order = 12)
  public final double doLong(final long left, final double right) {
    return doDouble(left, right);
  }

  @Specialization(order = 13)
  public final double doDouble(final double left, final long right) {
    return doDouble(left, right);
  }
}

@@ -360,58 +360,8 @@
 tion
-(order = 1, rewriteOn = ArithmeticException.class)
 %0A  p
@@ -376,30 +376,36 @@
 nal 
-long doLong
+double doDouble
 (final 
-long
+double
  lef
@@ -405,36 +405,38 @@
 ble left, final 
-long
+double
  right) %7B%0A    lo
@@ -437,199 +437,26 @@
     
-long l = left;%0A    long r = right;%0A    long result = l %25 r;%0A%0A    if (l %3E 0 && r %3C 0) %7B%0A      negativeRightOperand.enter();%0A      result = ExactMath.addExact(result, r);%0A    %7D%0A    return resul
+return left %25 righ
 t;%0A 
@@ -480,19 +480,8 @@
 tion
-(order = 2)
 %0A  p
@@ -496,44 +496,36 @@
 nal 
-Object doBigInteger(final BigInteger
+double doDouble(final double
  lef
@@ -529,34 +529,28 @@
 left, final 
-BigInteger
+long
  right) %7B%0A  
@@ -562,34 +562,21 @@
 urn 
-reduceToIntIfPossi
+doDou
 ble(left
 .mod
@@ -575,20 +575,16 @@
 left
-.mod(
+, 
 right)
-)
 ;%0A  
@@ -607,19 +607,8 @@
 tion
-(order = 3)
 %0A  p
@@ -623,36 +623,44 @@
 nal 
-double doDouble(final double
+Object doBigInteger(final BigInteger
  lef
@@ -660,38 +660,42 @@
 ger left, final 
-double
+BigInteger
  right) %7B%0A    re
@@ -695,36 +695,62 @@
 %0A    return 
-left %25 
+reduceToIntIfPossible(left.mod(
 right
+))
 ;%0A  %7D%0A%0A  @Sp
@@ -765,20 +765,8 @@
 tion
-(order = 10)
 %0A  p
@@ -924,20 +924,8 @@
 tion
-(order = 11)
 %0A  p
@@ -1077,20 +1077,8 @@
 tion
-(order = 12)
 %0A  p
@@ -1203,63 +1203,362 @@
 ion(
-order = 13)%0A  public final double doDouble(final double
+rewriteOn = ArithmeticException.class)%0A  public final long doLong(final long left, final long right) %7B%0A    long l = left;%0A    long r = right;%0A    long result = l %25 r;%0A%0A    if (l %3E 0 && r %3C 0) %7B%0A      negativeRightOperand.enter();%0A      result = ExactMath.addExact(result, r);%0A    %7D%0A    return result;%0A  %7D%0A%0A  public final Object doLongPromotion(final long
  lef
@@ -1589,35 +1589,59 @@
    return do
-Double
+BigInteger(BigInteger.valueOf
 (left
+)
 , right);%0A  

Remove order attribute, and reorder to satisfy DSL