Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/Integer/BitTwiddle.java
41149 views
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 int 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.Integer.*;
38
39
public class BitTwiddle {
40
private static final int N = 1000; // # of repetitions per test
41
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
int x = rnd.nextInt();
61
62
String expected = new StringBuilder()
63
.append(leftpad(toBinaryString(x), 32))
64
.reverse().toString();
65
66
String actual = leftpad(toBinaryString(reverse(x)), 32);
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
int x = rnd.nextInt();
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(Integer.MAX_VALUE) != 1)
87
throw new RuntimeException("lzmax");
88
89
if (numberOfTrailingZeros(0) != SIZE)
90
throw new RuntimeException("k");
91
if (numberOfTrailingZeros(1) != 0)
92
throw new RuntimeException("l");
93
if (numberOfTrailingZeros(MIN_VALUE) != (SIZE - 1))
94
throw new RuntimeException("m");
95
96
for (int i = 0; i < N; i++) {
97
int x = rnd.nextInt();
98
if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
99
throw new RuntimeException("n: " + toHexString(x));
100
}
101
for (int i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) {
102
if (numberOfLeadingZeros(i) != r ||
103
numberOfTrailingZeros(i) != (SIZE - r - 1) ||
104
numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) {
105
throw new RuntimeException("lzx: " + toHexString(i));
106
}
107
}
108
109
if (bitCount(0) != 0)
110
throw new RuntimeException("o");
111
112
for (int i = 0; i < SIZE; i++) {
113
int pow2 = 1 << i;
114
if (bitCount(pow2) != 1)
115
throw new RuntimeException("p: " + i);
116
if (bitCount(pow2 -1) != i)
117
throw new RuntimeException("q: " + i);
118
}
119
120
for (int i = 0; i < N; i++) {
121
int x = rnd.nextInt();
122
if (bitCount(x) != bitCount(reverse(x)))
123
throw new RuntimeException("r: " + toHexString(x));
124
}
125
126
for (int i = 0; i < N; i++) {
127
int x = rnd.nextInt();
128
int dist = rnd.nextInt();
129
if (bitCount(x) != bitCount(rotateRight(x, dist)))
130
throw new RuntimeException("s: " + toHexString(x) +
131
toHexString(dist));
132
if (bitCount(x) != bitCount(rotateLeft(x, dist)))
133
throw new RuntimeException("t: " + toHexString(x) +
134
toHexString(dist));
135
if (rotateRight(x, dist) != rotateLeft(x, -dist))
136
throw new RuntimeException("u: " + toHexString(x) +
137
toHexString(dist));
138
if (rotateRight(x, -dist) != rotateLeft(x, dist))
139
throw new RuntimeException("v: " + toHexString(x) +
140
toHexString(dist));
141
}
142
143
if (signum(0) != 0 || signum(1) != 1 || signum(-1) != -1
144
|| signum(MIN_VALUE) != -1 || signum(MAX_VALUE) != 1)
145
throw new RuntimeException("w");
146
147
for (int i = 0; i < N; i++) {
148
int x = rnd.nextInt();
149
int sign = (x < 0 ? -1 : (x == 0 ? 0 : 1));
150
if (signum(x) != sign)
151
throw new RuntimeException("x: " + toHexString(x));
152
}
153
154
if(reverseBytes(0xaabbccdd) != 0xddccbbaa)
155
throw new RuntimeException("y");
156
157
for (int i = 0; i < N; i++) {
158
int x = rnd.nextInt();
159
if (bitCount(x) != bitCount(reverseBytes(x)))
160
throw new RuntimeException("z: " + toHexString(x));
161
}
162
}
163
164
private static final String ZEROS = "0".repeat(32);
165
private static String leftpad(String s, int width) {
166
return ZEROS.substring(0, width - s.length()) + s;
167
}
168
}
169
170