blob: 2683b2548f23e2c3721c1d3f0ea54187c82f82cc [file] [log] [blame]
Aart Bik3f67e692016-01-15 14:35:12 -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
Aart Bik6e74fa92016-01-25 16:51:32 -080019 // TODO: make something like this work when b/26700769 is done.
Aart Bik3f67e692016-01-15 14:35:12 -080020 // CHECK-START-X86_64: int Main.bits32(int) disassembly (after)
21 // CHECK-DAG: popcnt
Aart Bik6e74fa92016-01-25 16:51:32 -080022
23 /// CHECK-START: int Main.bits32(int) intrinsics_recognition (after)
24 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerBitCount
25 /// CHECK-DAG: Return [<<Result>>]
Aart Bik3f67e692016-01-15 14:35:12 -080026 private static int bits32(int x) {
27 return Integer.bitCount(x);
28 }
29
Aart Bik6e74fa92016-01-25 16:51:32 -080030 /// CHECK-START: int Main.bits64(long) intrinsics_recognition (after)
31 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:LongBitCount
32 /// CHECK-DAG: Return [<<Result>>]
Aart Bik3f67e692016-01-15 14:35:12 -080033 private static int bits64(long x) {
34 return Long.bitCount(x);
35 }
36
37 public static void main(String args[]) {
38 expectEquals32(bits32(0x00000000), 0);
39 expectEquals32(bits32(0x00000001), 1);
40 expectEquals32(bits32(0x10000000), 1);
41 expectEquals32(bits32(0x10000001), 2);
42 expectEquals32(bits32(0x00000003), 2);
43 expectEquals32(bits32(0x70000000), 3);
44 expectEquals32(bits32(0x000F0000), 4);
45 expectEquals32(bits32(0x00001111), 4);
46 expectEquals32(bits32(0x11110000), 4);
47 expectEquals32(bits32(0x11111111), 8);
48 expectEquals32(bits32(0x12345678), 13);
49 expectEquals32(bits32(0x9ABCDEF0), 19);
50 expectEquals32(bits32(0xFFFFFFFF), 32);
51
52 for (int i = 0; i < 32; i++) {
53 expectEquals32(bits32(1 << i), 1);
54 }
55
56 expectEquals64(bits64(0x0000000000000000L), 0);
57 expectEquals64(bits64(0x0000000000000001L), 1);
58 expectEquals64(bits64(0x1000000000000000L), 1);
59 expectEquals64(bits64(0x1000000000000001L), 2);
60 expectEquals64(bits64(0x0000000000000003L), 2);
61 expectEquals64(bits64(0x7000000000000000L), 3);
62 expectEquals64(bits64(0x000F000000000000L), 4);
63 expectEquals64(bits64(0x0000000011111111L), 8);
64 expectEquals64(bits64(0x1111111100000000L), 8);
65 expectEquals64(bits64(0x1111111111111111L), 16);
66 expectEquals64(bits64(0x123456789ABCDEF1L), 33);
67 expectEquals64(bits64(0xFFFFFFFFFFFFFFFFL), 64);
68
69 for (int i = 0; i < 64; i++) {
70 expectEquals64(bits64(1L << i), 1);
71 }
72
73 System.out.println("passed");
74 }
75
76 private static void expectEquals32(int expected, int result) {
77 if (expected != result) {
78 throw new Error("Expected: " + expected + ", found: " + result);
79 }
80 }
81 private static void expectEquals64(long expected, long result) {
82 if (expected != result) {
83 throw new Error("Expected: " + expected + ", found: " + result);
84 }
85 }
86}