1 public class IntArithTests {
   2 
   3     private static final int IIMM12_0 = 0x1;   // first imm value
   4     private static final int IIMM12_1 = 0xfff; // last 12bit imm value
   5     private static final int IIMM12_2 = 0x1001; // Should not encode as imm
   6     private static final int IIMM24_3 = 0x1000; // first 12 bit shifted imm
   7     private static final int IIMM24_4 = 0xfff000; // Last 12 bit shifted imm
   8     private static final int IIMM24_5 = 0x1001000; // Should not encode as imm
   9 
  10     private static int test_neg(int a) {
  11         return -a;
  12     }
  13 
  14     private static int test_addi(int a, int b) {
  15         return a + b;
  16     }
  17 
  18     private static int test_addic0(int a) {
  19         return a + IIMM12_0;
  20     }
  21 
  22     private static int test_addic1(int a) {
  23         return a + IIMM12_1;
  24     }
  25 
  26     private static int test_addic2(int a) {
  27         return a + IIMM12_2;
  28     }
  29 
  30     private static int test_addic3(int a) {
  31         return a + IIMM24_3;
  32     }
  33 
  34     private static int test_addic4(int a) {
  35         return a + IIMM24_4;
  36     }
  37 
  38     private static int test_addic5(int a) {
  39         return a + IIMM24_5;
  40     }
  41 
  42     private static int test_subi(int a, int b) {
  43         return a - b;
  44     }
  45 
  46     private static int test_subc1(int a) {
  47         return a - 11;
  48     }
  49 
  50     private static int test_mulic1(int a) {
  51         // Generates shl.
  52         return a * 8;
  53     }
  54 
  55     private static int test_mulic2(int a) {
  56         // Generates shl followed by add.
  57         return a * 9;
  58     }
  59 
  60     private static int test_mulic3(int a) {
  61         // Generates shl followed by sub.
  62         return a * 7;
  63     }
  64 
  65     private static int test_mulic4(int a) {
  66         // Generates normal mul.
  67         return a * 10;
  68     }
  69 
  70     private static int test_muli(int a, int b) {
  71         // Generates normal mul.
  72         return a * b;
  73     }
  74 
  75     private static int test_divi(int a, int b) {
  76         return a / b;
  77     }
  78 
  79     private static int test_remi(int a, int b) {
  80         return a % b;
  81     }
  82 
  83     private static void assertThat(boolean assertion) {
  84         if (! assertion) {
  85             throw new AssertionError();
  86         }
  87     }
  88 
  89     public static void main(String[] args) {
  90         assertThat(test_neg(10) == -10);
  91         assertThat(test_addi(3, 2) == 5);
  92         assertThat(test_addi(Integer.MAX_VALUE, 1) == Integer.MIN_VALUE);
  93         assertThat(test_addic0(3) == 4);
  94         assertThat(test_addic1(3) == 0x1002);
  95         assertThat(test_addic2(3) == 0x1004);
  96         assertThat(test_addic3(3) == 0x1003);
  97         assertThat(test_addic4(3) == 0xfff003);
  98         assertThat(test_addic5(3) == 0x1001003);
  99 
 100         assertThat(test_subi(40, 13) == 27);
 101         assertThat(test_subi(Integer.MIN_VALUE, 1) == Integer.MAX_VALUE);
 102         assertThat(test_subc1(40) == 29);
 103 
 104         assertThat(test_mulic1(5) == 40);
 105         assertThat(test_mulic2(5) == 45);
 106         assertThat(test_mulic3(5) == 35);
 107         assertThat(test_mulic4(5) == 50);
 108         assertThat(test_muli(5, 200) == 1000);
 109 
 110         assertThat(test_divi(30, 3) == 10);
 111         assertThat(test_divi(29, 3) == 9);
 112         assertThat(test_divi(Integer.MIN_VALUE, -1) == Integer.MIN_VALUE);
 113         try {
 114             test_divi(30, 0);
 115             throw new AssertionError();
 116         } catch (ArithmeticException ex) {
 117             // Pass.
 118         }
 119 
 120         assertThat(test_remi(30, 3) == 0);
 121         assertThat(test_remi(29, 3) == 2);
 122         assertThat(test_remi(Integer.MIN_VALUE, -1) == 0);
 123         try {
 124             test_remi(30, 0);
 125             throw new AssertionError();
 126         } catch (ArithmeticException ex) {
 127             // Pass.
 128         }
 129     }
 130 }
 131