1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
*/ |
6 |
dl |
1.1 |
/** |
7 |
|
|
* Misc utilities in JSR166 performance tests |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
import java.util.concurrent.*; |
11 |
|
|
import java.util.concurrent.atomic.*; |
12 |
|
|
|
13 |
|
|
class LoopHelpers { |
14 |
|
|
|
15 |
|
|
static final SimpleRandom staticRNG = new SimpleRandom(); |
16 |
|
|
|
17 |
|
|
// Some mindless computation to do between synchronizations... |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* generates 32 bit pseudo-random numbers. |
21 |
|
|
* Adapted from http://www.snippets.org |
22 |
|
|
*/ |
23 |
|
|
public static int compute1(int x) { |
24 |
|
|
int lo = 16807 * (x & 0xFFFF); |
25 |
|
|
int hi = 16807 * (x >>> 16); |
26 |
|
|
lo += (hi & 0x7FFF) << 16; |
27 |
|
|
if ((lo & 0x80000000) != 0) { |
28 |
|
|
lo &= 0x7fffffff; |
29 |
|
|
++lo; |
30 |
|
|
} |
31 |
|
|
lo += hi >>> 15; |
32 |
|
|
if (lo == 0 || (lo & 0x80000000) != 0) { |
33 |
|
|
lo &= 0x7fffffff; |
34 |
|
|
++lo; |
35 |
|
|
} |
36 |
|
|
return lo; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* Computes a linear congruential random number a random number |
41 |
|
|
* of times. |
42 |
|
|
*/ |
43 |
|
|
public static int compute2(int x) { |
44 |
|
|
int loops = (x >>> 4) & 7; |
45 |
|
|
while (loops-- > 0) { |
46 |
|
|
x = (x * 2147483647) % 16807; |
47 |
|
|
} |
48 |
|
|
return x; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* Yet another random number generator |
53 |
|
|
*/ |
54 |
|
|
public static int compute3(int x) { |
55 |
|
|
int t = (x % 127773) * 16807 - (x / 127773) * 2836; |
56 |
|
|
return (t > 0)? t : t + 0x7fffffff; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
/** |
60 |
|
|
* Yet another random number generator |
61 |
|
|
*/ |
62 |
|
|
public static int compute4(int x) { |
63 |
|
|
return x * 134775813 + 1; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Yet another random number generator |
69 |
|
|
*/ |
70 |
|
|
public static int compute5(int x) { |
71 |
|
|
return 36969 * (x & 65535) + (x >> 16); |
72 |
|
|
} |
73 |
|
|
|
74 |
dl |
1.3 |
/** |
75 |
|
|
* Marsaglia xorshift (1, 3, 10) |
76 |
|
|
*/ |
77 |
dl |
1.4 |
public static int compute6(int seed) { |
78 |
|
|
seed ^= seed << 1; |
79 |
|
|
seed ^= seed >>> 3; |
80 |
|
|
seed ^= (seed << 10); |
81 |
|
|
return seed; |
82 |
dl |
1.3 |
} |
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* Marsaglia xorshift (6, 21, 7) |
86 |
|
|
*/ |
87 |
|
|
public static int compute7(int y) { |
88 |
|
|
y ^= y << 6; |
89 |
|
|
y ^= y >>> 21; |
90 |
|
|
y ^= (y << 7); |
91 |
|
|
return y; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
|
95 |
|
|
/** |
96 |
|
|
* Marsaglia xorshift for longs |
97 |
|
|
*/ |
98 |
|
|
public static long compute8(long x) { |
99 |
|
|
x ^= x << 13; |
100 |
|
|
x ^= x >>> 7; |
101 |
|
|
x ^= (x << 17); |
102 |
|
|
return x; |
103 |
|
|
} |
104 |
|
|
|
105 |
dl |
1.4 |
public static final class XorShift32Random { |
106 |
|
|
static final AtomicInteger seq = new AtomicInteger(8862213); |
107 |
|
|
int x = -1831433054; |
108 |
|
|
public XorShift32Random(int seed) { x = seed; } |
109 |
|
|
public XorShift32Random() { |
110 |
|
|
this((int)System.nanoTime() + seq.getAndAdd(129)); |
111 |
|
|
} |
112 |
|
|
public int next() { |
113 |
|
|
x ^= x << 6; |
114 |
|
|
x ^= x >>> 21; |
115 |
|
|
x ^= (x << 7); |
116 |
|
|
return x; |
117 |
|
|
} |
118 |
|
|
} |
119 |
|
|
|
120 |
dl |
1.3 |
|
121 |
|
|
/** Multiplication-free RNG from Marsaglia "Xorshift RNGs" paper */ |
122 |
dl |
1.4 |
public static final class MarsagliaRandom { |
123 |
|
|
static final AtomicInteger seq = new AtomicInteger(3122688); |
124 |
dl |
1.3 |
int x; |
125 |
|
|
int y = 842502087; |
126 |
dl |
1.4 |
int z = -715159705; |
127 |
dl |
1.3 |
int w = 273326509; |
128 |
dl |
1.4 |
public MarsagliaRandom(int seed) { x = seed; } |
129 |
|
|
public MarsagliaRandom() { |
130 |
|
|
this((int)System.nanoTime() + seq.getAndAdd(129)); |
131 |
|
|
} |
132 |
dl |
1.3 |
public int next() { |
133 |
|
|
int t = x ^ (x << 11); |
134 |
|
|
x = y; |
135 |
|
|
y = z; |
136 |
|
|
z = w; |
137 |
dl |
1.4 |
return w = (w ^ (w >>> 19) ^ (t ^ (t >>> 8))); |
138 |
dl |
1.3 |
} |
139 |
|
|
} |
140 |
dl |
1.1 |
|
141 |
|
|
/** |
142 |
dl |
1.4 |
* Unsynchronized version of java.util.Random algorithm. |
143 |
dl |
1.1 |
*/ |
144 |
dl |
1.4 |
public static final class SimpleRandom { |
145 |
dl |
1.1 |
private final static long multiplier = 0x5DEECE66DL; |
146 |
|
|
private final static long addend = 0xBL; |
147 |
|
|
private final static long mask = (1L << 48) - 1; |
148 |
dl |
1.4 |
static final AtomicLong seq = new AtomicLong( -715159705); |
149 |
|
|
private long seed; |
150 |
|
|
|
151 |
|
|
SimpleRandom(long s) { |
152 |
|
|
seed = s; |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
SimpleRandom() { |
156 |
|
|
seed = System.nanoTime() + seq.getAndAdd(129); |
157 |
|
|
} |
158 |
dl |
1.1 |
|
159 |
|
|
public void setSeed(long s) { |
160 |
|
|
seed = s; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
public int next() { |
164 |
|
|
long nextseed = (seed * multiplier + addend) & mask; |
165 |
|
|
seed = nextseed; |
166 |
|
|
return ((int)(nextseed >>> 17)) & 0x7FFFFFFF; |
167 |
|
|
} |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
public static class BarrierTimer implements Runnable { |
171 |
|
|
public volatile long startTime; |
172 |
|
|
public volatile long endTime; |
173 |
|
|
public void run() { |
174 |
|
|
long t = System.nanoTime(); |
175 |
|
|
if (startTime == 0) |
176 |
|
|
startTime = t; |
177 |
|
|
else |
178 |
|
|
endTime = t; |
179 |
|
|
} |
180 |
|
|
public void clear() { |
181 |
|
|
startTime = 0; |
182 |
|
|
endTime = 0; |
183 |
|
|
} |
184 |
|
|
public long getTime() { |
185 |
|
|
return endTime - startTime; |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
public static String rightJustify(long n) { |
190 |
|
|
// There's probably a better way to do this... |
191 |
|
|
String field = " "; |
192 |
|
|
String num = Long.toString(n); |
193 |
|
|
if (num.length() >= field.length()) |
194 |
|
|
return num; |
195 |
|
|
StringBuffer b = new StringBuffer(field); |
196 |
|
|
b.replace(b.length()-num.length(), b.length(), num); |
197 |
|
|
return b.toString(); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
} |