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