1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 * version 2 for more details (a copy is included in the LICENSE file that
12 * accompanied this code).
13 *
14 * You should have received a copy of the GNU General Public License version
15 * 2 along with this work; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 * or visit www.oracle.com if you need additional information or have any
20 * questions.
21 */
22
23/*
24 * This file is available under and governed by the GNU General Public
25 * License version 2 only, as published by the Free Software Foundation.
26 * However, the following notice accompanied the original version of this
27 * file:
28 *
29 * Written by Doug Lea with assistance from members of JCP JSR-166
30 * Expert Group and released to the public domain, as explained at
31 * http://creativecommons.org/publicdomain/zero/1.0/
32 */
33
34/**
35 * Misc utilities in JSR166 performance tests
36 */
37class LoopHelpers {
38
39    // Some mindless computation to do between synchronizations...
40
41    /**
42     * generates 32 bit pseudo-random numbers.
43     * Adapted from http://www.snippets.org
44     */
45    public static int compute1(int x) {
46        int lo = 16807 * (x & 0xFFFF);
47        int hi = 16807 * (x >>> 16);
48        lo += (hi & 0x7FFF) << 16;
49        if ((lo & 0x80000000) != 0) {
50            lo &= 0x7fffffff;
51            ++lo;
52        }
53        lo += hi >>> 15;
54        if (lo == 0 || (lo & 0x80000000) != 0) {
55            lo &= 0x7fffffff;
56            ++lo;
57        }
58        return lo;
59    }
60
61    /**
62     *  Computes a linear congruential random number a random number
63     *  of times.
64     */
65    public static int compute2(int x) {
66        int loops = (x >>> 4) & 7;
67        while (loops-- > 0) {
68            x = (x * 2147483647) % 16807;
69        }
70        return x;
71    }
72
73    public static class BarrierTimer implements Runnable {
74        public volatile long startTime;
75        public volatile long endTime;
76        public void run() {
77            long t = System.nanoTime();
78            if (startTime == 0)
79                startTime = t;
80            else
81                endTime = t;
82        }
83        public void clear() {
84            startTime = 0;
85            endTime = 0;
86        }
87        public long getTime() {
88            return endTime - startTime;
89        }
90    }
91
92    public static String rightJustify(long n) {
93        // There's probably a better way to do this...
94        String field = "         ";
95        String num = Long.toString(n);
96        if (num.length() >= field.length())
97            return num;
98        StringBuffer b = new StringBuffer(field);
99        b.replace(b.length()-num.length(), b.length(), num);
100        return b.toString();
101    }
102
103}
104