1 /*
2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @library /test/lib
27  * @build jdk.test.lib.RandomFactory
28  * @run main BitTwiddle
29  * @bug     4495754 8078672
30  * @summary Basic test for long bit twiddling (use -Dseed=X to set PRNG seed)
31  * @author  Josh Bloch
32  * @key randomness
33  */
34 
35 import java.util.Random;
36 import jdk.test.lib.RandomFactory;
37 import static java.lang.Long.*;
38 
39 public class BitTwiddle {
40     private static final int N = 1000; // # of repetitions per test
41 
main(String args[])42     public static void main(String args[]) {
43         Random rnd = RandomFactory.getRandom();
44 
45         if (highestOneBit(0) != 0)
46             throw new RuntimeException("a");
47         if (highestOneBit(-1) != MIN_VALUE)
48             throw new RuntimeException("b");
49         if (highestOneBit(1) != 1)
50             throw new RuntimeException("c");
51 
52         if (lowestOneBit(0) != 0)
53             throw new RuntimeException("d");
54         if (lowestOneBit(-1) != 1)
55             throw new RuntimeException("e");
56         if (lowestOneBit(MIN_VALUE) != MIN_VALUE)
57             throw new RuntimeException("f");
58 
59         for (int i = 0; i < N; i++) {
60             long x = rnd.nextLong();
61 
62             String expected = new StringBuilder()
63                 .append(leftpad(toBinaryString(x), 64))
64                 .reverse().toString();
65 
66             String actual = leftpad(toBinaryString(reverse(x)), 64);
67 
68             if (!expected.equals(actual)) {
69                 throw new RuntimeException("reverse: \n" +
70                         expected + " \n" + actual);
71             }
72         }
73 
74         for (int i = 0; i < N; i++) {
75             long x = rnd.nextLong();
76             if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
77                 throw new RuntimeException("g: " + toHexString(x));
78         }
79 
80         if (numberOfLeadingZeros(0) != SIZE)
81             throw new RuntimeException("h");
82         if (numberOfLeadingZeros(-1) != 0)
83             throw new RuntimeException("i");
84         if (numberOfLeadingZeros(1) != (SIZE - 1))
85             throw new RuntimeException("j");
86         if (numberOfLeadingZeros(Long.MAX_VALUE) != 1)
87             throw new RuntimeException("lzmax");
88         if (numberOfLeadingZeros(Integer.MAX_VALUE + 1L) != (SIZE - 32))
89             throw new RuntimeException("lz32");
90 
91         if (numberOfTrailingZeros(0) != SIZE)
92             throw new RuntimeException("k");
93         if (numberOfTrailingZeros(1) != 0)
94             throw new RuntimeException("l");
95         if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
96             throw new RuntimeException("m");
97 
98         for (int i = 0; i < N; i++) {
99             long x = rnd.nextLong();
100             if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
101                 throw new RuntimeException("n: " + toHexString(x));
102         }
103         for (long i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) {
104             if (numberOfLeadingZeros(i) != (int)r ||
105                 numberOfTrailingZeros(i) != (SIZE - (int)r - 1) ||
106                 numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) {
107                 throw new RuntimeException("lzx: " + toHexString(i));
108             }
109         }
110 
111         if (bitCount(0) != 0)
112                 throw new RuntimeException("o");
113 
114         for (int i = 0; i < SIZE; i++) {
115             long pow2 = 1L << i;
116             if (bitCount(pow2) != 1)
117                 throw new RuntimeException("p: " + i);
118             if (bitCount(pow2 -1) != i)
119                 throw new RuntimeException("q: " + i);
120         }
121 
122         for (int i = 0; i < N; i++) {
123             long x = rnd.nextLong();
124             if (bitCount(x) != bitCount(reverse(x)))
125                 throw new RuntimeException("r: " + toHexString(x));
126         }
127 
128         for (int i = 0; i < N; i++) {
129             long x = rnd.nextLong();
130             int dist = rnd.nextInt();
131             if (bitCount(x) != bitCount(rotateRight(x, dist)))
132                 throw new RuntimeException("s: " + toHexString(x) +
133                                            toHexString(dist));
134             if (bitCount(x) != bitCount(rotateLeft(x, dist)))
135                 throw new RuntimeException("t: " + toHexString(x) +
136                                            toHexString(dist));
137             if (rotateRight(x, dist) != rotateLeft(x, -dist))
138                 throw new RuntimeException("u: " + toHexString(x) +
139                                            toHexString(dist));
140             if (rotateRight(x, -dist) != rotateLeft(x, dist))
141                 throw new RuntimeException("v: " + toHexString(x) +
142                                            toHexString(dist));
143         }
144 
145         if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
146             || signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
147             throw new RuntimeException("w");
148 
149         for (int i = 0; i < N; i++) {
150             long x = rnd.nextLong();
151             int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
152             if (signum(x) != sign)
153                 throw new RuntimeException("x: " + toHexString(x));
154         }
155 
156         if(reverseBytes(0xaabbccdd11223344L) != 0x44332211ddccbbaaL)
157             throw new RuntimeException("y");
158 
159         for (int i = 0; i < N; i++) {
160             long x = rnd.nextLong();
161             if (bitCount(x) != bitCount(reverseBytes(x)))
162                 throw new RuntimeException("z: " + toHexString(x));
163         }
164     }
165 
166     private static final String ZEROS = "0".repeat(64);
leftpad(String s, int width)167     private static String leftpad(String s, int width) {
168         return ZEROS.substring(0, width - s.length()) + s;
169     }
170 }
171