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