ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LoopHelpers.java
Revision: 1.3
Committed: Fri Jun 10 15:45:19 2005 UTC (18 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.2: +49 -0 lines
Log Message:
Add CAS timing test; use cheaper RNGs in others

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     * 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     public static int compute6(int y) {
78     y ^= y << 1;
79     y ^= y >>> 3;
80     y ^= (y << 10);
81     return y;
82     }
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     public static final long compute8InitialValue = 88172645463325252L;
95    
96     /**
97     * Marsaglia xorshift for longs
98     */
99     public static long compute8(long x) {
100     x ^= x << 13;
101     x ^= x >>> 7;
102     x ^= (x << 17);
103     return x;
104     }
105    
106    
107     /** Multiplication-free RNG from Marsaglia "Xorshift RNGs" paper */
108     public static class MarsagliaRandom {
109     int x;
110     int y = 842502087;
111     int z = (int)(3579807591L & 0xffff);
112     int w = 273326509;
113     public MarsagliaRandom(int seed) { x = seed; }
114     public MarsagliaRandom() { this((int)System.nanoTime()); }
115     public int next() {
116     int t = x ^ (x << 11);
117     x = y;
118     y = z;
119     z = w;
120     return w = (w ^ (w >>> 19)) ^ (t ^ (t >>> 8));
121     }
122     }
123 dl 1.1
124     /**
125     * An actually useful random number generator, but unsynchronized.
126     * Basically same as java.util.Random.
127     */
128     public static class SimpleRandom {
129     private final static long multiplier = 0x5DEECE66DL;
130     private final static long addend = 0xBL;
131     private final static long mask = (1L << 48) - 1;
132     static final AtomicLong seq = new AtomicLong(1);
133     private long seed = System.nanoTime() + seq.getAndIncrement();
134    
135     public void setSeed(long s) {
136     seed = s;
137     }
138    
139     public int next() {
140     long nextseed = (seed * multiplier + addend) & mask;
141     seed = nextseed;
142     return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
143     }
144     }
145    
146     public static class BarrierTimer implements Runnable {
147     public volatile long startTime;
148     public volatile long endTime;
149     public void run() {
150     long t = System.nanoTime();
151     if (startTime == 0)
152     startTime = t;
153     else
154     endTime = t;
155     }
156     public void clear() {
157     startTime = 0;
158     endTime = 0;
159     }
160     public long getTime() {
161     return endTime - startTime;
162     }
163     }
164    
165     public static String rightJustify(long n) {
166     // There's probably a better way to do this...
167     String field = " ";
168     String num = Long.toString(n);
169     if (num.length() >= field.length())
170     return num;
171     StringBuffer b = new StringBuffer(field);
172     b.replace(b.length()-num.length(), b.length(), num);
173     return b.toString();
174     }
175    
176     }