author | mgronlun |
Sun, 22 Sep 2019 17:31:57 +0200 (2019-09-22) | |
branch | JEP-349-branch |
changeset 58259 | b6efcf2217f1 |
parent 50211 | 5afedc9e4662 |
permissions | -rw-r--r-- |
1826 | 1 |
/* |
50211
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. |
1826 | 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 |
* |
|
5506 | 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. |
|
1826 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
45288
58be10a068c2
8180805: move RandomFactory to the top level testlibrary
iignatyev
parents:
37877
diff
changeset
|
26 |
* @library /test/lib |
45466
faf1c55d2046
8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents:
45288
diff
changeset
|
27 |
* @build jdk.test.lib.RandomFactory |
30048
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
28 |
* @run main BitTwiddle |
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
29 |
* @bug 4495754 8078672 |
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
30 |
* @summary Basic test for int bit twiddling (use -Dseed=X to set PRNG seed) |
1826 | 31 |
* @author Josh Bloch |
30046 | 32 |
* @key randomness |
1826 | 33 |
*/ |
34 |
||
35 |
import java.util.Random; |
|
45288
58be10a068c2
8180805: move RandomFactory to the top level testlibrary
iignatyev
parents:
37877
diff
changeset
|
36 |
import jdk.test.lib.RandomFactory; |
1826 | 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[]) { |
|
30048
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
43 |
Random rnd = RandomFactory.getRandom(); |
1826 | 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(); |
|
37877
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
61 |
|
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
62 |
String expected = new StringBuilder() |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
63 |
.append(leftpad(toBinaryString(x), 32)) |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
64 |
.reverse().toString(); |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
65 |
|
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
66 |
String actual = leftpad(toBinaryString(reverse(x)), 32); |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
67 |
|
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
68 |
if (!expected.equals(actual)) { |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
69 |
throw new RuntimeException("reverse: \n" + |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
70 |
expected + " \n" + actual); |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
71 |
} |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
72 |
} |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
73 |
|
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
74 |
for (int i = 0; i < N; i++) { |
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
75 |
int x = rnd.nextInt(); |
1826 | 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"); |
|
50211
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
86 |
if (numberOfLeadingZeros(Integer.MAX_VALUE) != 1) |
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
87 |
throw new RuntimeException("lzmax"); |
1826 | 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 |
} |
|
50211
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
101 |
for (int i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) { |
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
102 |
if (numberOfLeadingZeros(i) != r || |
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
103 |
numberOfTrailingZeros(i) != (SIZE - r - 1) || |
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
104 |
numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) { |
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
105 |
throw new RuntimeException("lzx: " + toHexString(i)); |
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
106 |
} |
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
107 |
} |
1826 | 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 |
} |
|
37877
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
163 |
|
50211
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
164 |
private static final String ZEROS = "0".repeat(32); |
37877
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
165 |
private static String leftpad(String s, int width) { |
50211
5afedc9e4662
8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents:
47216
diff
changeset
|
166 |
return ZEROS.substring(0, width - s.length()) + s; |
37877
dae28a12fb38
8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents:
30436
diff
changeset
|
167 |
} |
1826 | 168 |
} |