blob: 7007c6a5076a8376e11835986dc0757ae111d359 [file] [log] [blame]
Aart Bik6e74fa92016-01-25 16:51:32 -08001/*
2 * Copyright (C) 2016 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
17public class Main {
18
19 /// CHECK-START: int Main.hi32(int) intrinsics_recognition (after)
20 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerHighestOneBit
21 /// CHECK-DAG: Return [<<Result>>]
22 private static int hi32(int x) {
23 return Integer.highestOneBit(x);
24 }
25
26 /// CHECK-START: int Main.lo32(int) intrinsics_recognition (after)
27 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerLowestOneBit
28 /// CHECK-DAG: Return [<<Result>>]
29 private static int lo32(int x) {
30 return Integer.lowestOneBit(x);
31 }
32
33 /// CHECK-START: long Main.hi64(long) intrinsics_recognition (after)
34 /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:LongHighestOneBit
35 /// CHECK-DAG: Return [<<Result>>]
36 private static long hi64(long x) {
37 return Long.highestOneBit(x);
38 }
39
40 /// CHECK-START: long Main.lo64(long) intrinsics_recognition (after)
41 /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:LongLowestOneBit
42 /// CHECK-DAG: Return [<<Result>>]
43 private static long lo64(long x) {
44 return Long.lowestOneBit(x);
45 }
46
47 public static void main(String args[]) {
48 expectEquals32(0x00000000, hi32(0x00000000));
49 expectEquals32(0x00000000, lo32(0x00000000));
50 expectEquals32(0x00010000, hi32(0x00010000));
51 expectEquals32(0x00010000, lo32(0x00010000));
52 expectEquals32(0x00800000, hi32(0x00FF0000));
53 expectEquals32(0x00010000, lo32(0x00FF0000));
54 expectEquals32(0x80000000, hi32(0xFFFFFFFF));
55 expectEquals32(0x00000001, lo32(0xFFFFFFFF));
56
57 for (int i = 0; i < 32; i++) {
58 expectEquals32(1 << i, hi32(1 << i));
59 expectEquals32(1 << i, lo32(1 << i));
60 int expected = i < 29 ? 0x8 << i : 0x80000000;
61 expectEquals32(expected, hi32(0xF << i));
62 expectEquals32(0x1 << i, lo32(0xF << i));
63 }
64
65 expectEquals64(0x0000000000000000L, hi64(0x0000000000000000L));
66 expectEquals64(0x0000000000000000L, lo64(0x0000000000000000L));
67 expectEquals64(0x0000000100000000L, hi64(0x0000000100000000L));
68 expectEquals64(0x0000000100000000L, lo64(0x0000000100000000L));
69 expectEquals64(0x0000008000000000L, hi64(0x000000FF00000000L));
70 expectEquals64(0x0000000100000000L, lo64(0x000000FF00000000L));
71 expectEquals64(0x8000000000000000L, hi64(0xFFFFFFFFFFFFFFFFL));
72 expectEquals64(0x0000000000000001L, lo64(0xFFFFFFFFFFFFFFFFL));
73
74 for (int i = 0; i < 64; i++) {
75 expectEquals64(1L << i, hi64(1L << i));
76 expectEquals64(1L << i, lo64(1L << i));
77 long expected = i < 61 ? 0x8L << i : 0x8000000000000000L;
78 expectEquals64(expected, hi64(0xFL << i));
79 expectEquals64(0x1L << i, lo64(0xFL << i));
80 }
81
82 System.out.println("passed");
83 }
84
85 private static void expectEquals32(int expected, int result) {
86 if (expected != result) {
87 throw new Error("Expected: " + expected + ", found: " + result);
88 }
89 }
90 private static void expectEquals64(long expected, long result) {
91 if (expected != result) {
92 throw new Error("Expected: " + expected + ", found: " + result);
93 }
94 }
95}