• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package test.java.lang.Long;
36 
37 import java.util.Random;
38 import jdk.test.lib.RandomFactory;
39 import static java.lang.Long.*;
40 
41 public class BitTwiddle {
42     private static final int N = 1000; // # of repetitions per test
43 
main(String args[])44     public static void main(String args[]) {
45         Random rnd = RandomFactory.getRandom();
46 
47         if (highestOneBit(0) != 0)
48             throw new RuntimeException("a");
49         if (highestOneBit(-1) != MIN_VALUE)
50             throw new RuntimeException("b");
51         if (highestOneBit(1) != 1)
52             throw new RuntimeException("c");
53 
54         if (lowestOneBit(0) != 0)
55             throw new RuntimeException("d");
56         if (lowestOneBit(-1) != 1)
57             throw new RuntimeException("e");
58         if (lowestOneBit(MIN_VALUE) != MIN_VALUE)
59             throw new RuntimeException("f");
60 
61         for (int i = 0; i < N; i++) {
62             long x = rnd.nextLong();
63 
64             String expected = new StringBuilder()
65                 .append(leftpad(toBinaryString(x), 64))
66                 .reverse().toString();
67 
68             String actual = leftpad(toBinaryString(reverse(x)), 64);
69 
70             if (!expected.equals(actual)) {
71                 throw new RuntimeException("reverse: \n" +
72                         expected + " \n" + actual);
73             }
74         }
75 
76         for (int i = 0; i < N; i++) {
77             long x = rnd.nextLong();
78             if (highestOneBit(x) != reverse(lowestOneBit(reverse(x))))
79                 throw new RuntimeException("g: " + toHexString(x));
80         }
81 
82         if (numberOfLeadingZeros(0) != SIZE)
83             throw new RuntimeException("h");
84         if (numberOfLeadingZeros(-1) != 0)
85             throw new RuntimeException("i");
86         if (numberOfLeadingZeros(1) != (SIZE - 1))
87             throw new RuntimeException("j");
88         if (numberOfLeadingZeros(Long.MAX_VALUE) != 1)
89             throw new RuntimeException("lzmax");
90         if (numberOfLeadingZeros(Integer.MAX_VALUE + 1L) != (SIZE - 32))
91             throw new RuntimeException("lz32");
92 
93         if (numberOfTrailingZeros(0) != SIZE)
94             throw new RuntimeException("k");
95         if (numberOfTrailingZeros(1) != 0)
96             throw new RuntimeException("l");
97         if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
98             throw new RuntimeException("m");
99 
100         for (int i = 0; i < N; i++) {
101             long x = rnd.nextLong();
102             if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
103                 throw new RuntimeException("n: " + toHexString(x));
104         }
105         for (long i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) {
106             if (numberOfLeadingZeros(i) != (int)r ||
107                 numberOfTrailingZeros(i) != (SIZE - (int)r - 1) ||
108                 numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) {
109                 throw new RuntimeException("lzx: " + toHexString(i));
110             }
111         }
112 
113         if (bitCount(0) != 0)
114                 throw new RuntimeException("o");
115 
116         for (int i = 0; i < SIZE; i++) {
117             long pow2 = 1L << i;
118             if (bitCount(pow2) != 1)
119                 throw new RuntimeException("p: " + i);
120             if (bitCount(pow2 -1) != i)
121                 throw new RuntimeException("q: " + i);
122         }
123 
124         for (int i = 0; i < N; i++) {
125             long x = rnd.nextLong();
126             if (bitCount(x) != bitCount(reverse(x)))
127                 throw new RuntimeException("r: " + toHexString(x));
128         }
129 
130         for (int i = 0; i < N; i++) {
131             long x = rnd.nextLong();
132             int dist = rnd.nextInt();
133             if (bitCount(x) != bitCount(rotateRight(x, dist)))
134                 throw new RuntimeException("s: " + toHexString(x) +
135                                            toHexString(dist));
136             if (bitCount(x) != bitCount(rotateLeft(x, dist)))
137                 throw new RuntimeException("t: " + toHexString(x) +
138                                            toHexString(dist));
139             if (rotateRight(x, dist) != rotateLeft(x, -dist))
140                 throw new RuntimeException("u: " + toHexString(x) +
141                                            toHexString(dist));
142             if (rotateRight(x, -dist) != rotateLeft(x, dist))
143                 throw new RuntimeException("v: " + toHexString(x) +
144                                            toHexString(dist));
145         }
146 
147         if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
148             || signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
149             throw new RuntimeException("w");
150 
151         for (int i = 0; i < N; i++) {
152             long x = rnd.nextLong();
153             int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
154             if (signum(x) != sign)
155                 throw new RuntimeException("x: " + toHexString(x));
156         }
157 
158         if(reverseBytes(0xaabbccdd11223344L) != 0x44332211ddccbbaaL)
159             throw new RuntimeException("y");
160 
161         for (int i = 0; i < N; i++) {
162             long x = rnd.nextLong();
163             if (bitCount(x) != bitCount(reverseBytes(x)))
164                 throw new RuntimeException("z: " + toHexString(x));
165         }
166     }
167 
168     private static final String ZEROS = "0".repeat(64);
leftpad(String s, int width)169     private static String leftpad(String s, int width) {
170         return ZEROS.substring(0, width - s.length()) + s;
171     }
172 }
173