• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 public class Main {
main(String[] args)18   public static void main(String[] args) {
19     test_Integer_compareUnsigned_no_fold();
20     test_Long_compareUnsigned_no_fold();
21     test_Integer_compareUnsigned();
22     test_Long_compareUnsigned();
23     test_Integer_divideUnsigned_no_fold();
24     test_Long_divideUnsigned_no_fold();
25     test_Integer_divideUnsigned();
26     test_Long_divideUnsigned();
27     test_Integer_remainderUnsigned_no_fold();
28     test_Long_remainderUnsigned_no_fold();
29     test_Integer_remainderUnsigned();
30     test_Long_remainderUnsigned();
31   }
32 
$noinline$cmpUnsignedInt(int a, int b)33   public static int $noinline$cmpUnsignedInt(int a, int b) {
34     return Integer.compareUnsigned(a, b);
35   }
36 
test_Integer_compareUnsigned_no_fold()37   public static void test_Integer_compareUnsigned_no_fold() {
38     assertEquals($noinline$cmpUnsignedInt(100, 100), 0);
39     assertEquals($noinline$cmpUnsignedInt(100, 1), 1);
40     assertEquals($noinline$cmpUnsignedInt(1, 100), -1);
41     assertEquals($noinline$cmpUnsignedInt(-2, 2), 1);
42     assertEquals($noinline$cmpUnsignedInt(2, -2), -1);
43     assertEquals($noinline$cmpUnsignedInt(Integer.MAX_VALUE, Integer.MIN_VALUE), -1);
44     assertEquals($noinline$cmpUnsignedInt(Integer.MIN_VALUE, Integer.MAX_VALUE), 1);
45     assertEquals($noinline$cmpUnsignedInt(Integer.MAX_VALUE, -1), -1);
46     assertEquals($noinline$cmpUnsignedInt(-1, Integer.MAX_VALUE), 1);
47     assertEquals($noinline$cmpUnsignedInt(0, 0), 0);
48   }
49 
$noinline$cmpUnsignedLong(long a, long b)50   public static int $noinline$cmpUnsignedLong(long a, long b) {
51     return Long.compareUnsigned(a, b);
52   }
53 
test_Long_compareUnsigned_no_fold()54   public static void test_Long_compareUnsigned_no_fold() {
55     assertEquals($noinline$cmpUnsignedLong(100L, 100L), 0);
56     assertEquals($noinline$cmpUnsignedLong(100L, 1L), 1);
57     assertEquals($noinline$cmpUnsignedLong(1L, 100L), -1);
58     assertEquals($noinline$cmpUnsignedLong(-2L, 2L), 1);
59     assertEquals($noinline$cmpUnsignedLong(2L, -2L), -1);
60     assertEquals($noinline$cmpUnsignedLong(Long.MAX_VALUE, Long.MIN_VALUE), -1);
61     assertEquals($noinline$cmpUnsignedLong(Long.MIN_VALUE, Long.MAX_VALUE), 1);
62     assertEquals($noinline$cmpUnsignedLong(Long.MAX_VALUE, -1L), -1);
63     assertEquals($noinline$cmpUnsignedLong(-1L, Long.MAX_VALUE), 1);
64     assertEquals($noinline$cmpUnsignedLong(0L, 0L), 0);
65   }
66 
test_Integer_compareUnsigned()67   public static void test_Integer_compareUnsigned() {
68     assertEquals(Integer.compareUnsigned(100, 100), 0);
69     assertEquals(Integer.compareUnsigned(100, 1), 1);
70     assertEquals(Integer.compareUnsigned(1, 100), -1);
71     assertEquals(Integer.compareUnsigned(-2, 2), 1);
72     assertEquals(Integer.compareUnsigned(2, -2), -1);
73     assertEquals(Integer.compareUnsigned(Integer.MAX_VALUE, Integer.MIN_VALUE), -1);
74     assertEquals(Integer.compareUnsigned(Integer.MIN_VALUE, Integer.MAX_VALUE), 1);
75     assertEquals(Integer.compareUnsigned(Integer.MAX_VALUE, -1), -1);
76     assertEquals(Integer.compareUnsigned(-1, Integer.MAX_VALUE), 1);
77     assertEquals(Integer.compareUnsigned(0, 0), 0);
78   }
79 
test_Long_compareUnsigned()80   public static void test_Long_compareUnsigned() {
81     assertEquals(Long.compareUnsigned(100L, 100L), 0);
82     assertEquals(Long.compareUnsigned(100L, 1L), 1);
83     assertEquals(Long.compareUnsigned(1L, 100L), -1);
84     assertEquals(Long.compareUnsigned(-2L, 2L), 1);
85     assertEquals(Long.compareUnsigned(2L, -2L), -1);
86     assertEquals(Long.compareUnsigned(Long.MAX_VALUE, Long.MIN_VALUE), -1);
87     assertEquals(Long.compareUnsigned(Long.MIN_VALUE, Long.MAX_VALUE), 1);
88     assertEquals(Long.compareUnsigned(Long.MAX_VALUE, -1L), -1);
89     assertEquals(Long.compareUnsigned(-1L, Long.MAX_VALUE), 1);
90     assertEquals(Long.compareUnsigned(0L, 0L), 0);
91   }
92 
$noinline$divideUnsignedInt(int a, int b)93   public static int $noinline$divideUnsignedInt(int a, int b) {
94     return Integer.divideUnsigned(a, b);
95   }
96 
test_Integer_divideUnsigned_no_fold()97   public static void test_Integer_divideUnsigned_no_fold() {
98     assertEquals($noinline$divideUnsignedInt(100, 10), 10);
99     assertEquals($noinline$divideUnsignedInt(100, 1), 100);
100     assertEquals($noinline$divideUnsignedInt(1024, 128), 8);
101     assertEquals($noinline$divideUnsignedInt(12345678, 264), 46763);
102     assertEquals($noinline$divideUnsignedInt(13, 5), 2);
103     assertEquals($noinline$divideUnsignedInt(-2, 2), Integer.MAX_VALUE);
104     assertEquals($noinline$divideUnsignedInt(-1, 2), Integer.MAX_VALUE);
105     assertEquals($noinline$divideUnsignedInt(100000, -1), 0);
106     assertEquals($noinline$divideUnsignedInt(Integer.MAX_VALUE, -1), 0);
107     assertEquals($noinline$divideUnsignedInt(-2, -1), 0);
108     assertEquals($noinline$divideUnsignedInt(-1, -2), 1);
109     assertEquals($noinline$divideUnsignedInt(-173448, 13), 330368757);
110     assertEquals($noinline$divideUnsignedInt(Integer.MIN_VALUE, 2), (1 << 30));
111     assertEquals($noinline$divideUnsignedInt(-1, Integer.MIN_VALUE), 1);
112     assertEquals($noinline$divideUnsignedInt(Integer.MAX_VALUE, Integer.MIN_VALUE), 0);
113     assertEquals($noinline$divideUnsignedInt(Integer.MIN_VALUE, Integer.MAX_VALUE), 1);
114 
115     try {
116       $noinline$divideUnsignedInt(1, 0);
117       throw new Error("Unreachable");
118     } catch (ArithmeticException expected) {
119     }
120   }
121 
$noinline$divideUnsignedLong(long a, long b)122   public static long $noinline$divideUnsignedLong(long a, long b) {
123     return Long.divideUnsigned(a, b);
124   }
125 
126   private static final long BIG_LONG_VALUE = 739287620162442240L;
127 
test_Long_divideUnsigned_no_fold()128   public static void test_Long_divideUnsigned_no_fold() {
129     assertEquals($noinline$divideUnsignedLong(100L, 10L), 10L);
130     assertEquals($noinline$divideUnsignedLong(100L, 1L), 100L);
131     assertEquals($noinline$divideUnsignedLong(1024L, 128L), 8L);
132     assertEquals($noinline$divideUnsignedLong(12345678L, 264L), 46763L);
133     assertEquals($noinline$divideUnsignedLong(13L, 5L), 2L);
134     assertEquals($noinline$divideUnsignedLong(-2L, 2L), Long.MAX_VALUE);
135     assertEquals($noinline$divideUnsignedLong(-1L, 2L), Long.MAX_VALUE);
136     assertEquals($noinline$divideUnsignedLong(100000L, -1L), 0L);
137     assertEquals($noinline$divideUnsignedLong(Long.MAX_VALUE, -1L), 0L);
138     assertEquals($noinline$divideUnsignedLong(-2L, -1L), 0L);
139     assertEquals($noinline$divideUnsignedLong(-1L, -2L), 1L);
140     assertEquals($noinline$divideUnsignedLong(-173448L, 13L), 1418980313362259859L);
141     assertEquals($noinline$divideUnsignedLong(Long.MIN_VALUE, 2L), (1L << 62));
142     assertEquals($noinline$divideUnsignedLong(-1L, Long.MIN_VALUE), 1L);
143     assertEquals($noinline$divideUnsignedLong(Long.MAX_VALUE, Long.MIN_VALUE), 0L);
144     assertEquals($noinline$divideUnsignedLong(Long.MIN_VALUE, Long.MAX_VALUE), 1L);
145     assertEquals($noinline$divideUnsignedLong(Long.MAX_VALUE, 1L), Long.MAX_VALUE);
146     assertEquals($noinline$divideUnsignedLong(Long.MIN_VALUE, 1L), Long.MIN_VALUE);
147     assertEquals($noinline$divideUnsignedLong(BIG_LONG_VALUE, BIG_LONG_VALUE), 1L);
148     assertEquals($noinline$divideUnsignedLong(BIG_LONG_VALUE, 1L), BIG_LONG_VALUE);
149     assertEquals($noinline$divideUnsignedLong(BIG_LONG_VALUE, 1024L), 721960566564885L);
150     assertEquals($noinline$divideUnsignedLong(BIG_LONG_VALUE, 0x1FFFFFFFFL), 86064406L);
151 
152     try {
153       $noinline$divideUnsignedLong(1L, 0L);
154       throw new Error("Unreachable");
155     } catch (ArithmeticException expected) {
156     }
157   }
158 
test_Integer_divideUnsigned()159   public static void test_Integer_divideUnsigned() {
160     assertEquals(Integer.divideUnsigned(100, 10), 10);
161     assertEquals(Integer.divideUnsigned(100, 1), 100);
162     assertEquals(Integer.divideUnsigned(1024, 128), 8);
163     assertEquals(Integer.divideUnsigned(12345678, 264), 46763);
164     assertEquals(Integer.divideUnsigned(13, 5), 2);
165     assertEquals(Integer.divideUnsigned(-2, 2), Integer.MAX_VALUE);
166     assertEquals(Integer.divideUnsigned(-1, 2), Integer.MAX_VALUE);
167     assertEquals(Integer.divideUnsigned(100000, -1), 0);
168     assertEquals(Integer.divideUnsigned(Integer.MAX_VALUE, -1), 0);
169     assertEquals(Integer.divideUnsigned(-2, -1), 0);
170     assertEquals(Integer.divideUnsigned(-1, -2), 1);
171     assertEquals(Integer.divideUnsigned(-173448, 13), 330368757);
172     assertEquals(Integer.divideUnsigned(Integer.MIN_VALUE, 2), (1 << 30));
173     assertEquals(Integer.divideUnsigned(-1, Integer.MIN_VALUE), 1);
174     assertEquals(Integer.divideUnsigned(Integer.MAX_VALUE, Integer.MIN_VALUE), 0);
175     assertEquals(Integer.divideUnsigned(Integer.MIN_VALUE, Integer.MAX_VALUE), 1);
176 
177     try {
178       Integer.divideUnsigned(1, 0);
179       throw new Error("Unreachable");
180     } catch (ArithmeticException expected) {
181     }
182   }
183 
test_Long_divideUnsigned()184   public static void test_Long_divideUnsigned() {
185     assertEquals(Long.divideUnsigned(100L, 10L), 10L);
186     assertEquals(Long.divideUnsigned(100L, 1L), 100L);
187     assertEquals(Long.divideUnsigned(1024L, 128L), 8L);
188     assertEquals(Long.divideUnsigned(12345678L, 264L), 46763L);
189     assertEquals(Long.divideUnsigned(13L, 5L), 2L);
190     assertEquals(Long.divideUnsigned(-2L, 2L), Long.MAX_VALUE);
191     assertEquals(Long.divideUnsigned(-1L, 2L), Long.MAX_VALUE);
192     assertEquals(Long.divideUnsigned(100000L, -1L), 0L);
193     assertEquals(Long.divideUnsigned(Long.MAX_VALUE, -1L), 0L);
194     assertEquals(Long.divideUnsigned(-2L, -1L), 0L);
195     assertEquals(Long.divideUnsigned(-1L, -2L), 1L);
196     assertEquals(Long.divideUnsigned(-173448L, 13L), 1418980313362259859L);
197     assertEquals(Long.divideUnsigned(Long.MIN_VALUE, 2L), (1L << 62));
198     assertEquals(Long.divideUnsigned(-1L, Long.MIN_VALUE), 1L);
199     assertEquals(Long.divideUnsigned(Long.MAX_VALUE, Long.MIN_VALUE), 0L);
200     assertEquals(Long.divideUnsigned(Long.MIN_VALUE, Long.MAX_VALUE), 1L);
201     assertEquals(Long.divideUnsigned(Long.MAX_VALUE, 1L), Long.MAX_VALUE);
202     assertEquals(Long.divideUnsigned(Long.MIN_VALUE, 1L), Long.MIN_VALUE);
203     assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, BIG_LONG_VALUE), 1L);
204     assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 1L), BIG_LONG_VALUE);
205     assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 1024L), 721960566564885L);
206     assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 0x1FFFFFFFFL), 86064406L);
207 
208     try {
209       Long.divideUnsigned(1L, 0L);
210       throw new Error("Unreachable");
211     } catch (ArithmeticException expected) {
212     }
213   }
214 
$noinline$remainderUnsignedInt(int a, int b)215   public static int $noinline$remainderUnsignedInt(int a, int b) {
216     return Integer.remainderUnsigned(a, b);
217   }
218 
test_Integer_remainderUnsigned_no_fold()219   public static void test_Integer_remainderUnsigned_no_fold() {
220     assertEquals($noinline$remainderUnsignedInt(100, 10), 0);
221     assertEquals($noinline$remainderUnsignedInt(100, 1), 0);
222     assertEquals($noinline$remainderUnsignedInt(1024, 127), 8);
223     assertEquals($noinline$remainderUnsignedInt(12345678, 264), 246);
224     assertEquals($noinline$remainderUnsignedInt(13, 5), 3);
225     assertEquals($noinline$remainderUnsignedInt(-2, 2), 0);
226     assertEquals($noinline$remainderUnsignedInt(-1, 2), 1);
227     assertEquals($noinline$remainderUnsignedInt(100000, -1), 100000);
228     assertEquals($noinline$remainderUnsignedInt(Integer.MAX_VALUE, -1), Integer.MAX_VALUE);
229     assertEquals($noinline$remainderUnsignedInt(-2, -1), -2);
230     assertEquals($noinline$remainderUnsignedInt(-1, -2), 1);
231     assertEquals($noinline$remainderUnsignedInt(-173448, 13), 7);
232     assertEquals($noinline$remainderUnsignedInt(Integer.MIN_VALUE, 2), 0);
233     assertEquals($noinline$remainderUnsignedInt(-1, Integer.MIN_VALUE), Integer.MAX_VALUE);
234     assertEquals(
235         $noinline$remainderUnsignedInt(Integer.MAX_VALUE, Integer.MIN_VALUE), Integer.MAX_VALUE);
236     assertEquals($noinline$remainderUnsignedInt(Integer.MIN_VALUE, Integer.MAX_VALUE), 1);
237 
238     try {
239       $noinline$remainderUnsignedInt(1, 0);
240       throw new Error("Unreachable");
241     } catch (ArithmeticException expected) {
242     }
243   }
244 
$noinline$remainderUnsignedLong(long a, long b)245   public static long $noinline$remainderUnsignedLong(long a, long b) {
246     return Long.remainderUnsigned(a, b);
247   }
248 
test_Long_remainderUnsigned_no_fold()249   public static void test_Long_remainderUnsigned_no_fold() {
250     assertEquals($noinline$remainderUnsignedLong(100L, 10L), 0L);
251     assertEquals($noinline$remainderUnsignedLong(100L, 1L), 0L);
252     assertEquals($noinline$remainderUnsignedLong(1024L, 127L), 8L);
253     assertEquals($noinline$remainderUnsignedLong(12345678L, 264L), 246L);
254     assertEquals($noinline$remainderUnsignedLong(13L, 5L), 3L);
255     assertEquals($noinline$remainderUnsignedLong(-2L, 2L), 0L);
256     assertEquals($noinline$remainderUnsignedLong(-1L, 2L), 1L);
257     assertEquals($noinline$remainderUnsignedLong(100000L, -1L), 100000L);
258     assertEquals($noinline$remainderUnsignedLong(Long.MAX_VALUE, -1L), Long.MAX_VALUE);
259     assertEquals($noinline$remainderUnsignedLong(-2L, -1L), -2L);
260     assertEquals($noinline$remainderUnsignedLong(-1L, -2L), 1L);
261     assertEquals($noinline$remainderUnsignedLong(-173448L, 13L), 1L);
262     assertEquals($noinline$remainderUnsignedLong(Long.MIN_VALUE, 2L), 0L);
263     assertEquals($noinline$remainderUnsignedLong(-1L, Long.MIN_VALUE), Long.MAX_VALUE);
264     assertEquals($noinline$remainderUnsignedLong(Long.MAX_VALUE, Long.MIN_VALUE), Long.MAX_VALUE);
265     assertEquals($noinline$remainderUnsignedLong(Long.MIN_VALUE, Long.MAX_VALUE), 1L);
266     assertEquals($noinline$remainderUnsignedLong(Long.MAX_VALUE, 1L), 0L);
267     assertEquals($noinline$remainderUnsignedLong(Long.MIN_VALUE, 1L), 0L);
268     assertEquals($noinline$remainderUnsignedLong(BIG_LONG_VALUE, BIG_LONG_VALUE), 0L);
269     assertEquals($noinline$remainderUnsignedLong(BIG_LONG_VALUE, 1L), 0L);
270     assertEquals($noinline$remainderUnsignedLong(BIG_LONG_VALUE, 1024L), 0L);
271     assertEquals($noinline$remainderUnsignedLong(BIG_LONG_VALUE, 0x1FFFFFFFFL), 2009174294L);
272     assertEquals(
273         $noinline$remainderUnsignedLong(BIG_LONG_VALUE, -38766615688777L), 739287620162442240L);
274 
275     try {
276       $noinline$remainderUnsignedLong(1L, 0L);
277       throw new Error("Unreachable");
278     } catch (ArithmeticException expected) {
279     }
280   }
281 
test_Integer_remainderUnsigned()282   public static void test_Integer_remainderUnsigned() {
283     assertEquals(Integer.remainderUnsigned(100, 10), 0);
284     assertEquals(Integer.remainderUnsigned(100, 1), 0);
285     assertEquals(Integer.remainderUnsigned(1024, 127), 8);
286     assertEquals(Integer.remainderUnsigned(12345678, 264), 246);
287     assertEquals(Integer.remainderUnsigned(13, 5), 3);
288     assertEquals(Integer.remainderUnsigned(-2, 2), 0);
289     assertEquals(Integer.remainderUnsigned(-1, 2), 1);
290     assertEquals(Integer.remainderUnsigned(100000, -1), 100000);
291     assertEquals(Integer.remainderUnsigned(Integer.MAX_VALUE, -1), Integer.MAX_VALUE);
292     assertEquals(Integer.remainderUnsigned(-2, -1), -2);
293     assertEquals(Integer.remainderUnsigned(-1, -2), 1);
294     assertEquals(Integer.remainderUnsigned(-173448, 13), 7);
295     assertEquals(Integer.remainderUnsigned(Integer.MIN_VALUE, 2), 0);
296     assertEquals(Integer.remainderUnsigned(-1, Integer.MIN_VALUE), Integer.MAX_VALUE);
297     assertEquals(
298         Integer.remainderUnsigned(Integer.MAX_VALUE, Integer.MIN_VALUE), Integer.MAX_VALUE);
299     assertEquals(Integer.remainderUnsigned(Integer.MIN_VALUE, Integer.MAX_VALUE), 1);
300 
301     try {
302       Integer.remainderUnsigned(1, 0);
303       throw new Error("Unreachable");
304     } catch (ArithmeticException expected) {
305     }
306   }
307 
test_Long_remainderUnsigned()308   public static void test_Long_remainderUnsigned() {
309     assertEquals(Long.remainderUnsigned(100L, 10L), 0L);
310     assertEquals(Long.remainderUnsigned(100L, 1L), 0L);
311     assertEquals(Long.remainderUnsigned(1024L, 127L), 8L);
312     assertEquals(Long.remainderUnsigned(12345678L, 264L), 246L);
313     assertEquals(Long.remainderUnsigned(13L, 5L), 3L);
314     assertEquals(Long.remainderUnsigned(-2L, 2L), 0L);
315     assertEquals(Long.remainderUnsigned(-1L, 2L), 1L);
316     assertEquals(Long.remainderUnsigned(100000L, -1L), 100000L);
317     assertEquals(Long.remainderUnsigned(Long.MAX_VALUE, -1L), Long.MAX_VALUE);
318     assertEquals(Long.remainderUnsigned(-2L, -1L), -2L);
319     assertEquals(Long.remainderUnsigned(-1L, -2L), 1L);
320     assertEquals(Long.remainderUnsigned(-173448L, 13L), 1L);
321     assertEquals(Long.remainderUnsigned(Long.MIN_VALUE, 2L), 0L);
322     assertEquals(Long.remainderUnsigned(-1L, Long.MIN_VALUE), Long.MAX_VALUE);
323     assertEquals(Long.remainderUnsigned(Long.MAX_VALUE, Long.MIN_VALUE), Long.MAX_VALUE);
324     assertEquals(Long.remainderUnsigned(Long.MIN_VALUE, Long.MAX_VALUE), 1L);
325     assertEquals(Long.remainderUnsigned(Long.MAX_VALUE, 1L), 0L);
326     assertEquals(Long.remainderUnsigned(Long.MIN_VALUE, 1L), 0L);
327     assertEquals(Long.remainderUnsigned(BIG_LONG_VALUE, BIG_LONG_VALUE), 0L);
328     assertEquals(Long.remainderUnsigned(BIG_LONG_VALUE, 1L), 0L);
329     assertEquals(Long.remainderUnsigned(BIG_LONG_VALUE, 1024L), 0L);
330     assertEquals(Long.remainderUnsigned(BIG_LONG_VALUE, 0x1FFFFFFFFL), 2009174294L);
331     assertEquals(Long.remainderUnsigned(BIG_LONG_VALUE, -38766615688777L), 739287620162442240L);
332 
333     try {
334       Long.remainderUnsigned(1L, 0L);
335       throw new Error("Unreachable");
336     } catch (ArithmeticException expected) {
337     }
338   }
339 
assertEquals(int expected, int actual)340   public static void assertEquals(int expected, int actual) {
341       if (expected != actual) {
342           throw new Error("Expected: " + expected + ", found: " + actual);
343       }
344   }
345 
assertEquals(long expected, long actual)346   public static void assertEquals(long expected, long actual) {
347       if (expected != actual) {
348           throw new Error("Expected: " + expected + ", found: " + actual);
349       }
350   }
351 }
352