ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LoopHelpers.java
Revision: 1.14
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +0 -2 lines
Log Message:
delete extraneous blank lines

File Contents

# User Rev Content
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 jsr166 1.11 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6 dl 1.1
7     import java.util.concurrent.*;
8     import java.util.concurrent.atomic.*;
9    
10 jsr166 1.13 /**
11     * Misc utilities in JSR166 performance tests
12     */
13 dl 1.1 class LoopHelpers {
14    
15     static final SimpleRandom staticRNG = new SimpleRandom();
16    
17     // Some mindless computation to do between synchronizations...
18    
19     /**
20 jsr166 1.7 * generates 32 bit pseudo-random numbers.
21 dl 1.1 * Adapted from http://www.snippets.org
22     */
23 jsr166 1.7 public static int compute1(int x) {
24 dl 1.1 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 jsr166 1.7 public static int compute2(int x) {
44 dl 1.1 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 jsr166 1.7 public static int compute3(int x) {
55 dl 1.1 int t = (x % 127773) * 16807 - (x / 127773) * 2836;
56 jsr166 1.8 return (t > 0) ? t : t + 0x7fffffff;
57 dl 1.1 }
58    
59     /**
60     * Yet another random number generator
61     */
62 jsr166 1.7 public static int compute4(int x) {
63 dl 1.1 return x * 134775813 + 1;
64     }
65    
66     /**
67     * Yet another random number generator
68     */
69 jsr166 1.7 public static int compute5(int x) {
70 dl 1.1 return 36969 * (x & 65535) + (x >> 16);
71     }
72    
73 dl 1.3 /**
74     * Marsaglia xorshift (1, 3, 10)
75     */
76 jsr166 1.7 public static int compute6(int seed) {
77     seed ^= seed << 1;
78     seed ^= seed >>> 3;
79 dl 1.4 seed ^= (seed << 10);
80     return seed;
81 dl 1.3 }
82    
83     /**
84     * Marsaglia xorshift (6, 21, 7)
85     */
86     public static int compute7(int y) {
87 jsr166 1.7 y ^= y << 6;
88     y ^= y >>> 21;
89 dl 1.3 y ^= (y << 7);
90     return y;
91     }
92    
93 dl 1.6 // FNV: (x ^ 0x811c9dc5) * 0x01000193; 15485863;
94 dl 1.3
95     /**
96     * Marsaglia xorshift for longs
97     */
98 jsr166 1.7 public static long compute8(long x) {
99     x ^= x << 13;
100     x ^= x >>> 7;
101 dl 1.3 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 jsr166 1.12 public XorShift32Random(int seed) { x = seed; }
109 jsr166 1.7 public XorShift32Random() {
110 jsr166 1.9 this((int) System.nanoTime() + seq.getAndAdd(129));
111 dl 1.4 }
112     public int next() {
113 jsr166 1.7 x ^= x << 6;
114     x ^= x >>> 21;
115 dl 1.4 x ^= (x << 7);
116     return x;
117     }
118     }
119    
120 dl 1.3 /** Multiplication-free RNG from Marsaglia "Xorshift RNGs" paper */
121 dl 1.4 public static final class MarsagliaRandom {
122     static final AtomicInteger seq = new AtomicInteger(3122688);
123 dl 1.3 int x;
124     int y = 842502087;
125 dl 1.4 int z = -715159705;
126 dl 1.3 int w = 273326509;
127 dl 1.4 public MarsagliaRandom(int seed) { x = seed; }
128 jsr166 1.7 public MarsagliaRandom() {
129 jsr166 1.9 this((int) System.nanoTime() + seq.getAndAdd(129));
130 dl 1.4 }
131 dl 1.3 public int next() {
132     int t = x ^ (x << 11);
133 jsr166 1.7 x = y;
134     y = z;
135 dl 1.3 z = w;
136 dl 1.4 return w = (w ^ (w >>> 19) ^ (t ^ (t >>> 8)));
137 dl 1.3 }
138     }
139 dl 1.1
140     /**
141 dl 1.4 * Unsynchronized version of java.util.Random algorithm.
142 dl 1.1 */
143 dl 1.4 public static final class SimpleRandom {
144 jsr166 1.10 private static final long multiplier = 0x5DEECE66DL;
145     private static final long addend = 0xBL;
146     private static final long mask = (1L << 48) - 1;
147 dl 1.4 static final AtomicLong seq = new AtomicLong( -715159705);
148     private long seed;
149    
150     SimpleRandom(long s) {
151     seed = s;
152     }
153    
154     SimpleRandom() {
155     seed = System.nanoTime() + seq.getAndAdd(129);
156     }
157 dl 1.1
158     public void setSeed(long s) {
159     seed = s;
160     }
161    
162     public int next() {
163     long nextseed = (seed * multiplier + addend) & mask;
164     seed = nextseed;
165 jsr166 1.9 return ((int) (nextseed >>> 17)) & 0x7FFFFFFF;
166 dl 1.1 }
167     }
168    
169     public static class BarrierTimer implements Runnable {
170 dl 1.5 volatile boolean started;
171     volatile long startTime;
172     volatile long endTime;
173 dl 1.1 public void run() {
174     long t = System.nanoTime();
175 dl 1.5 if (!started) {
176     started = true;
177 dl 1.1 startTime = t;
178 dl 1.5 } else
179 dl 1.1 endTime = t;
180     }
181     public void clear() {
182 dl 1.5 started = false;
183 dl 1.1 }
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 jsr166 1.7
200 dl 1.1 }