• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 public class Main {
18 
19   // TODO: make something like this work when b/26700769 is done.
20   // CHECK-START-X86_64: int Main.bits32(int) disassembly (after)
21   // CHECK-DAG: popcnt
22 
23 
24   /// CHECK-START: int Main.$noinline$BitCountBoolean(boolean) builder (after)
25   /// CHECK-DAG:     <<Result:i\d+>>  InvokeStaticOrDirect intrinsic:IntegerBitCount
26   /// CHECK-DAG:                      Return [<<Result>>]
$noinline$BitCountBoolean(boolean x)27   private static int $noinline$BitCountBoolean(boolean x) {
28     return Integer.bitCount(x ? 1 : 0);
29   }
30 
31   /// CHECK-START: int Main.$noinline$BitCountByte(byte) builder (after)
32   /// CHECK-DAG:     <<Result:i\d+>>  InvokeStaticOrDirect intrinsic:IntegerBitCount
33   /// CHECK-DAG:                      Return [<<Result>>]
$noinline$BitCountByte(byte x)34   private static int $noinline$BitCountByte(byte x) {
35     return Integer.bitCount(x);
36   }
37 
38   /// CHECK-START: int Main.$noinline$BitCountShort(short) builder (after)
39   /// CHECK-DAG:     <<Result:i\d+>>  InvokeStaticOrDirect intrinsic:IntegerBitCount
40   /// CHECK-DAG:                      Return [<<Result>>]
$noinline$BitCountShort(short x)41   private static int $noinline$BitCountShort(short x) {
42     return Integer.bitCount(x);
43   }
44 
45   /// CHECK-START: int Main.$noinline$BitCountChar(char) builder (after)
46   /// CHECK-DAG:     <<Result:i\d+>>  InvokeStaticOrDirect intrinsic:IntegerBitCount
47   /// CHECK-DAG:                      Return [<<Result>>]
$noinline$BitCountChar(char x)48   private static int $noinline$BitCountChar(char x) {
49     return Integer.bitCount(x);
50   }
51 
52   /// CHECK-START: int Main.$noinline$BitCountInt(int) builder (after)
53   /// CHECK-DAG:     <<Result:i\d+>>  InvokeStaticOrDirect intrinsic:IntegerBitCount
54   /// CHECK-DAG:                      Return [<<Result>>]
$noinline$BitCountInt(int x)55   private static int $noinline$BitCountInt(int x) {
56     return Integer.bitCount(x);
57   }
58 
59   /// CHECK-START: int Main.$noinline$BitCountLong(long) builder (after)
60   /// CHECK-DAG:     <<Result:i\d+>>  InvokeStaticOrDirect intrinsic:LongBitCount
61   /// CHECK-DAG:                      Return [<<Result>>]
$noinline$BitCountLong(long x)62   private static int $noinline$BitCountLong(long x) {
63     return Long.bitCount(x);
64   }
65 
testBitCountBoolean()66   public static void testBitCountBoolean() {
67     expectEqualsInt($noinline$BitCountBoolean(false), 0);
68     expectEqualsInt($noinline$BitCountBoolean(true), 1);
69   }
70 
testBitCountByte()71   public static void testBitCountByte() {
72     // Number of bits in an 32-bit integer representing the sign
73     // extension of a byte value widened to an int.
74     int signExtensionSize = Integer.SIZE - Byte.SIZE;
75     // Sign bit position in a byte.
76     int signBit = Byte.SIZE - 1;
77 
78     expectEqualsInt($noinline$BitCountByte((byte) 0x00), 0);
79     expectEqualsInt($noinline$BitCountByte((byte) 0x01), 1);
80     expectEqualsInt($noinline$BitCountByte((byte) 0x10), 1);
81     expectEqualsInt($noinline$BitCountByte((byte) 0x11), 2);
82     expectEqualsInt($noinline$BitCountByte((byte) 0x03), 2);
83     expectEqualsInt($noinline$BitCountByte((byte) 0x70), 3);
84     expectEqualsInt($noinline$BitCountByte((byte) 0xF0), 4 + signExtensionSize);
85     expectEqualsInt($noinline$BitCountByte((byte) 0x0F), 4);
86     expectEqualsInt($noinline$BitCountByte((byte) 0x12), 2);
87     expectEqualsInt($noinline$BitCountByte((byte) 0x9A), 4 + signExtensionSize);
88     expectEqualsInt($noinline$BitCountByte((byte) 0xFF), 8 + signExtensionSize);
89 
90     for (int i = 0; i < Byte.SIZE; i++) {
91       expectEqualsInt($noinline$BitCountByte((byte) (1 << i)),
92                       (i < signBit) ? 1 : 1 + signExtensionSize);
93     }
94   }
95 
96   public static void testBitCountShort() {
97     // Number of bits in an 32-bit integer representing the sign
98     // extension of a short value widened to an int.
99     int signExtensionSize = Integer.SIZE - Short.SIZE;
100     // Sign bit position in a short.
101     int signBit = Short.SIZE - 1;
102 
103     expectEqualsInt($noinline$BitCountShort((short) 0x0000), 0);
104     expectEqualsInt($noinline$BitCountShort((short) 0x0001), 1);
105     expectEqualsInt($noinline$BitCountShort((short) 0x1000), 1);
106     expectEqualsInt($noinline$BitCountShort((short) 0x1001), 2);
107     expectEqualsInt($noinline$BitCountShort((short) 0x0003), 2);
108     expectEqualsInt($noinline$BitCountShort((short) 0x7000), 3);
109     expectEqualsInt($noinline$BitCountShort((short) 0x0F00), 4);
110     expectEqualsInt($noinline$BitCountShort((short) 0x0011), 2);
111     expectEqualsInt($noinline$BitCountShort((short) 0x1100), 2);
112     expectEqualsInt($noinline$BitCountShort((short) 0x1111), 4);
113     expectEqualsInt($noinline$BitCountShort((short) 0x1234), 5);
114     expectEqualsInt($noinline$BitCountShort((short) 0x9ABC), 9 + signExtensionSize);
115     expectEqualsInt($noinline$BitCountShort((short) 0xFFFF), 16 + signExtensionSize);
116 
117     for (int i = 0; i < Short.SIZE; i++) {
118       expectEqualsInt($noinline$BitCountShort((short) (1 << i)),
119                       (i < signBit) ? 1 : 1 + signExtensionSize);
120     }
121   }
122 
123   public static void testBitCountChar() {
124     expectEqualsInt($noinline$BitCountChar((char) 0x0000), 0);
125     expectEqualsInt($noinline$BitCountChar((char) 0x0001), 1);
126     expectEqualsInt($noinline$BitCountChar((char) 0x1000), 1);
127     expectEqualsInt($noinline$BitCountChar((char) 0x1001), 2);
128     expectEqualsInt($noinline$BitCountChar((char) 0x0003), 2);
129     expectEqualsInt($noinline$BitCountChar((char) 0x7000), 3);
130     expectEqualsInt($noinline$BitCountChar((char) 0x0F00), 4);
131     expectEqualsInt($noinline$BitCountChar((char) 0x0011), 2);
132     expectEqualsInt($noinline$BitCountChar((char) 0x1100), 2);
133     expectEqualsInt($noinline$BitCountChar((char) 0x1111), 4);
134     expectEqualsInt($noinline$BitCountChar((char) 0x1234), 5);
135     expectEqualsInt($noinline$BitCountChar((char) 0x9ABC), 9);
136     expectEqualsInt($noinline$BitCountChar((char) 0xFFFF), 16);
137 
138     for (int i = 0; i < Character.SIZE; i++) {
139       expectEqualsInt($noinline$BitCountChar((char) (1 << i)), 1);
140     }
141   }
142 
143   public static void testBitCountInt() {
144     expectEqualsInt($noinline$BitCountInt(0x00000000), 0);
145     expectEqualsInt($noinline$BitCountInt(0x00000001), 1);
146     expectEqualsInt($noinline$BitCountInt(0x10000000), 1);
147     expectEqualsInt($noinline$BitCountInt(0x10000001), 2);
148     expectEqualsInt($noinline$BitCountInt(0x00000003), 2);
149     expectEqualsInt($noinline$BitCountInt(0x70000000), 3);
150     expectEqualsInt($noinline$BitCountInt(0x000F0000), 4);
151     expectEqualsInt($noinline$BitCountInt(0x00001111), 4);
152     expectEqualsInt($noinline$BitCountInt(0x11110000), 4);
153     expectEqualsInt($noinline$BitCountInt(0x11111111), 8);
154     expectEqualsInt($noinline$BitCountInt(0x12345678), 13);
155     expectEqualsInt($noinline$BitCountInt(0x9ABCDEF0), 19);
156     expectEqualsInt($noinline$BitCountInt(0xFFFFFFFF), 32);
157 
158     for (int i = 0; i < Integer.SIZE; i++) {
159       expectEqualsInt($noinline$BitCountInt(1 << i), 1);
160     }
161   }
162 
163   public static void testBitCountLong() {
164     expectEqualsInt($noinline$BitCountLong(0x0000000000000000L), 0);
165     expectEqualsInt($noinline$BitCountLong(0x0000000000000001L), 1);
166     expectEqualsInt($noinline$BitCountLong(0x1000000000000000L), 1);
167     expectEqualsInt($noinline$BitCountLong(0x1000000000000001L), 2);
168     expectEqualsInt($noinline$BitCountLong(0x0000000000000003L), 2);
169     expectEqualsInt($noinline$BitCountLong(0x7000000000000000L), 3);
170     expectEqualsInt($noinline$BitCountLong(0x000F000000000000L), 4);
171     expectEqualsInt($noinline$BitCountLong(0x0000000011111111L), 8);
172     expectEqualsInt($noinline$BitCountLong(0x1111111100000000L), 8);
173     expectEqualsInt($noinline$BitCountLong(0x1111111111111111L), 16);
174     expectEqualsInt($noinline$BitCountLong(0x123456789ABCDEF1L), 33);
175     expectEqualsInt($noinline$BitCountLong(0xFFFFFFFFFFFFFFFFL), 64);
176 
177     for (int i = 0; i < Long.SIZE; i++) {
178       expectEqualsInt($noinline$BitCountLong(1L << i), 1);
179     }
180   }
181 
182   public static void main(String args[]) {
183     testBitCountBoolean();
184     testBitCountByte();
185     testBitCountShort();
186     testBitCountChar();
187     testBitCountInt();
188     testBitCountLong();
189 
190     System.out.println("passed");
191   }
192 
193   private static void expectEqualsInt(int expected, int result) {
194     if (expected != result) {
195       throw new Error("Expected: " + expected + ", found: " + result);
196     }
197   }
198 }
199