summaryrefslogtreecommitdiffstats
path: root/test/428-optimizing-arith-rem/src/Main.java
blob: 46bd3c6ac517200324b09fcb8061823fe25c1bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {

  public static void expectEquals(int expected, int result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  public static void expectEquals(long expected, long result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  public static void expectDivisionByZero(int value) {
    try {
      $opt$Rem(value, 0);
      throw new Error("Expected RuntimeException when modulo by 0");
    } catch (java.lang.RuntimeException e) {
    }
    try {
      $opt$RemZero(value);
      throw new Error("Expected RuntimeException when modulo by 0");
    } catch (java.lang.RuntimeException e) {
    }
  }

  public static void expectDivisionByZero(long value) {
    try {
      $opt$Rem(value, 0L);
      throw new Error("Expected RuntimeException when modulo by 0");
    } catch (java.lang.RuntimeException e) {
    }
    try {
      $opt$RemZero(value);
      throw new Error("Expected RuntimeException when modulo by 0");
    } catch (java.lang.RuntimeException e) {
    }
  }

  public static void main(String[] args) {
    rem();
  }

  public static void rem() {
    remInt();
    remLong();
  }

  private static void remInt() {
    expectEquals(2, $opt$RemConst(6));
    expectEquals(2, $opt$Rem(6, 4));
    expectEquals(2, $opt$Rem(6, -4));
    expectEquals(0, $opt$Rem(6, 3));
    expectEquals(0, $opt$Rem(6, -3));
    expectEquals(0, $opt$Rem(6, 1));
    expectEquals(0, $opt$Rem(6, -1));
    expectEquals(-1, $opt$Rem(-7, 3));
    expectEquals(-1, $opt$Rem(-7, -3));
    expectEquals(0, $opt$Rem(6, 6));
    expectEquals(0, $opt$Rem(-6, -6));
    expectEquals(7, $opt$Rem(7, 9));
    expectEquals(7, $opt$Rem(7, -9));
    expectEquals(-7, $opt$Rem(-7, 9));
    expectEquals(-7, $opt$Rem(-7, -9));

    expectEquals(0, $opt$Rem(Integer.MAX_VALUE, 1));
    expectEquals(0, $opt$Rem(Integer.MAX_VALUE, -1));
    expectEquals(0, $opt$Rem(Integer.MIN_VALUE, 1));
    expectEquals(0, $opt$Rem(Integer.MIN_VALUE, -1)); // no overflow
    expectEquals(-1, $opt$Rem(Integer.MIN_VALUE, Integer.MAX_VALUE));
    expectEquals(Integer.MAX_VALUE, $opt$Rem(Integer.MAX_VALUE, Integer.MIN_VALUE));

    expectEquals(0, $opt$Rem(0, 7));
    expectEquals(0, $opt$Rem(0, Integer.MAX_VALUE));
    expectEquals(0, $opt$Rem(0, Integer.MIN_VALUE));

    expectDivisionByZero(0);
    expectDivisionByZero(1);
    expectDivisionByZero(5);
    expectDivisionByZero(Integer.MAX_VALUE);
    expectDivisionByZero(Integer.MIN_VALUE);
  }

  private static void remLong() {
    expectEquals(2L, $opt$RemConst(6L));
    expectEquals(2L, $opt$Rem(6L, 4L));
    expectEquals(2L, $opt$Rem(6L, -4L));
    expectEquals(0L, $opt$Rem(6L, 3L));
    expectEquals(0L, $opt$Rem(6L, -3L));
    expectEquals(0L, $opt$Rem(6L, 1L));
    expectEquals(0L, $opt$Rem(6L, -1L));
    expectEquals(-1L, $opt$Rem(-7L, 3L));
    expectEquals(-1L, $opt$Rem(-7L, -3L));
    expectEquals(0L, $opt$Rem(6L, 6L));
    expectEquals(0L, $opt$Rem(-6L, -6L));
    expectEquals(7L, $opt$Rem(7L, 9L));
    expectEquals(7L, $opt$Rem(7L, -9L));
    expectEquals(-7L, $opt$Rem(-7L, 9L));
    expectEquals(-7L, $opt$Rem(-7L, -9L));

    expectEquals(0L, $opt$Rem(Integer.MAX_VALUE, 1L));
    expectEquals(0L, $opt$Rem(Integer.MAX_VALUE, -1L));
    expectEquals(0L, $opt$Rem(Integer.MIN_VALUE, 1L));
    expectEquals(0L, $opt$Rem(Integer.MIN_VALUE, -1L)); // no overflow
    expectEquals(-1L, $opt$Rem(Integer.MIN_VALUE, Integer.MAX_VALUE));
    expectEquals(Integer.MAX_VALUE, $opt$Rem(Integer.MAX_VALUE, Integer.MIN_VALUE));

    expectEquals(0L, $opt$Rem(0L, 7L));
    expectEquals(0L, $opt$Rem(0L, Integer.MAX_VALUE));
    expectEquals(0L, $opt$Rem(0L, Integer.MIN_VALUE));

    expectDivisionByZero(0L);
    expectDivisionByZero(1L);
    expectDivisionByZero(5L);
    expectDivisionByZero(Integer.MAX_VALUE);
    expectDivisionByZero(Integer.MIN_VALUE);
  }

  static int $opt$Rem(int a, int b) {
    return a % b;
  }

  static int $opt$RemZero(int a) {
    return a % 0;
  }

  // Modulo by literals != 0 should not generate checks.
  static int $opt$RemConst(int a) {
    return a % 4;
  }

  static long $opt$RemConst(long a) {
    return a % 4L;
  }

  static long $opt$Rem(long a, long b) {
    return a % b;
  }

  static long $opt$RemZero(long a) {
    return a % 0L;
  }
}