LoopHelpers.java revision 3203
5612N/A/*
5612N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5653N/A *
5612N/A * This code is free software; you can redistribute it and/or modify it
6254N/A * under the terms of the GNU General Public License version 2 only, as
5612N/A * published by the Free Software Foundation.
6254N/A *
5612N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5612N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6254N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5612N/A * version 2 for more details (a copy is included in the LICENSE file that
5612N/A * accompanied this code).
5612N/A *
5612N/A * You should have received a copy of the GNU General Public License version
5612N/A * 2 along with this work; if not, write to the Free Software Foundation,
5612N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5612N/A *
5612N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5612N/A * or visit www.oracle.com if you need additional information or have any
5612N/A * questions.
5612N/A */
5612N/A
5612N/A/*
5612N/A * This file is available under and governed by the GNU General Public
5612N/A * License version 2 only, as published by the Free Software Foundation.
5612N/A * However, the following notice accompanied the original version of this
5612N/A * file:
5612N/A *
5612N/A * Written by Doug Lea with assistance from members of JCP JSR-166
5612N/A * Expert Group and released to the public domain, as explained at
5612N/A * http://creativecommons.org/licenses/publicdomain
5612N/A */
5612N/A/**
5612N/A * Misc utilities in JSR166 performance tests
5612N/A */
5612N/A
5612N/Aimport java.util.concurrent.*;
5612N/Aimport java.util.concurrent.atomic.*;
5612N/A
6254N/Aclass LoopHelpers {
5612N/A
5612N/A // Some mindless computation to do between synchronizations...
5612N/A
5612N/A /**
5612N/A * generates 32 bit pseudo-random numbers.
5612N/A * Adapted from http://www.snippets.org
5612N/A */
5612N/A public static int compute1(int x) {
5612N/A int lo = 16807 * (x & 0xFFFF);
5612N/A int hi = 16807 * (x >>> 16);
5612N/A lo += (hi & 0x7FFF) << 16;
5612N/A if ((lo & 0x80000000) != 0) {
5612N/A lo &= 0x7fffffff;
5612N/A ++lo;
5612N/A }
5612N/A lo += hi >>> 15;
5612N/A if (lo == 0 || (lo & 0x80000000) != 0) {
5612N/A lo &= 0x7fffffff;
5612N/A ++lo;
5612N/A }
5612N/A return lo;
5612N/A }
5612N/A
5612N/A /**
5612N/A * Computes a linear congruential random number a random number
5612N/A * of times.
5612N/A */
5612N/A public static int compute2(int x) {
5612N/A int loops = (x >>> 4) & 7;
5612N/A while (loops-- > 0) {
5612N/A x = (x * 2147483647) % 16807;
5612N/A }
5612N/A return x;
5612N/A }
5612N/A
5612N/A /**
5612N/A * An actually useful random number generator, but unsynchronized.
5612N/A * Basically same as java.util.Random.
5612N/A */
5612N/A public static class SimpleRandom {
5612N/A private static final long multiplier = 0x5DEECE66DL;
5612N/A private static final long addend = 0xBL;
5612N/A private static final long mask = (1L << 48) - 1;
5612N/A static final AtomicLong seq = new AtomicLong(1);
5612N/A private long seed = System.nanoTime() + seq.getAndIncrement();
5612N/A
6254N/A public void setSeed(long s) {
5612N/A seed = s;
5612N/A }
5612N/A
5612N/A public int next() {
5612N/A long nextseed = (seed * multiplier + addend) & mask;
5612N/A seed = nextseed;
6254N/A return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
5612N/A }
}
public static class BarrierTimer implements Runnable {
public volatile long startTime;
public volatile long endTime;
public void run() {
long t = System.nanoTime();
if (startTime == 0)
startTime = t;
else
endTime = t;
}
public void clear() {
startTime = 0;
endTime = 0;
}
public long getTime() {
return endTime - startTime;
}
}
public static String rightJustify(long n) {
// There's probably a better way to do this...
String field = " ";
String num = Long.toString(n);
if (num.length() >= field.length())
return num;
StringBuffer b = new StringBuffer(field);
b.replace(b.length()-num.length(), b.length(), num);
return b.toString();
}
}