author | tpivovarova |
Tue, 12 Jul 2016 18:24:48 +0300 | |
changeset 40059 | c2304140ed64 |
parent 30604 | hotspot/test/compiler/c2/7177917/Test7177917.java@b8d532cb6420 |
child 40631 | ed82623d7831 |
permissions | -rw-r--r-- |
13203 | 1 |
/* |
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
27699
diff
changeset
|
2 |
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. |
13203 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
40059 | 25 |
/** |
26 |
* @test |
|
27 |
* @bug 7177917 |
|
28 |
* @summary Micro-benchmark for Math.pow() and Math.exp() |
|
29 |
* @modules java.base/jdk.internal.misc |
|
30 |
* @library /testlibrary |
|
31 |
* |
|
32 |
* @run main compiler.c2.Test7177917 |
|
13203 | 33 |
*/ |
34 |
||
40059 | 35 |
package compiler.c2; |
36 |
||
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
27699
diff
changeset
|
37 |
import jdk.test.lib.Utils; |
40059 | 38 |
|
27453
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
13203
diff
changeset
|
39 |
import java.util.Random; |
13203 | 40 |
|
41 |
public class Test7177917 { |
|
42 |
||
43 |
static double d; |
|
44 |
||
27453
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
13203
diff
changeset
|
45 |
static final Random R = Utils.getRandomInstance(); |
13203 | 46 |
|
47 |
static long m_pow(double[][] values) { |
|
48 |
double res = 0; |
|
49 |
long start = System.nanoTime(); |
|
50 |
for (int i = 0; i < values.length; i++) { |
|
51 |
res += Math.pow(values[i][0], values[i][1]); |
|
52 |
} |
|
53 |
long stop = System.nanoTime(); |
|
54 |
d = res; |
|
55 |
return (stop - start) / 1000; |
|
56 |
} |
|
57 |
||
58 |
static long m_exp(double[] values) { |
|
59 |
double res = 0; |
|
60 |
long start = System.nanoTime(); |
|
61 |
for (int i = 0; i < values.length; i++) { |
|
62 |
res += Math.exp(values[i]); |
|
63 |
} |
|
64 |
long stop = System.nanoTime(); |
|
65 |
d = res; |
|
66 |
return (stop - start) / 1000; |
|
67 |
} |
|
68 |
||
69 |
static double[][] pow_values(int nb) { |
|
70 |
double[][] res = new double[nb][2]; |
|
71 |
for (int i = 0; i < nb; i++) { |
|
27453
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
13203
diff
changeset
|
72 |
double ylogx = (1 + (R.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin |
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
13203
diff
changeset
|
73 |
double x = Math.abs(Double.longBitsToDouble(R.nextLong())); |
13203 | 74 |
while (x != x) { |
27453
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
13203
diff
changeset
|
75 |
x = Math.abs(Double.longBitsToDouble(R.nextLong())); |
13203 | 76 |
} |
77 |
double logx = Math.log(x) / Math.log(2); |
|
78 |
double y = ylogx / logx; |
|
79 |
||
80 |
res[i][0] = x; |
|
81 |
res[i][1] = y; |
|
82 |
} |
|
83 |
return res; |
|
84 |
} |
|
85 |
||
86 |
static double[] exp_values(int nb) { |
|
87 |
double[] res = new double[nb]; |
|
88 |
for (int i = 0; i < nb; i++) { |
|
27453
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
13203
diff
changeset
|
89 |
double ylogx = (1 + (R.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin |
13203 | 90 |
double x = Math.E; |
91 |
double logx = Math.log(x) / Math.log(2); |
|
92 |
double y = ylogx / logx; |
|
93 |
res[i] = y; |
|
94 |
} |
|
95 |
return res; |
|
96 |
} |
|
97 |
||
98 |
static public void main(String[] args) { |
|
99 |
{ |
|
100 |
// warmup |
|
101 |
double[][] warmup_values = pow_values(10); |
|
102 |
m_pow(warmup_values); |
|
103 |
||
104 |
for (int i = 0; i < 20000; i++) { |
|
105 |
m_pow(warmup_values); |
|
106 |
} |
|
107 |
// test pow perf |
|
108 |
double[][] values = pow_values(1000000); |
|
109 |
System.out.println("==> POW " + m_pow(values)); |
|
110 |
||
111 |
// force uncommon trap |
|
112 |
double[][] nan_values = new double[1][2]; |
|
113 |
nan_values[0][0] = Double.NaN; |
|
114 |
nan_values[0][1] = Double.NaN; |
|
115 |
m_pow(nan_values); |
|
116 |
||
117 |
// force recompilation |
|
118 |
for (int i = 0; i < 20000; i++) { |
|
119 |
m_pow(warmup_values); |
|
120 |
} |
|
121 |
||
122 |
// test pow perf again |
|
123 |
System.out.println("==> POW " + m_pow(values)); |
|
124 |
} |
|
125 |
{ |
|
126 |
// warmup |
|
127 |
double[] warmup_values = exp_values(10); |
|
128 |
m_exp(warmup_values); |
|
129 |
||
130 |
for (int i = 0; i < 20000; i++) { |
|
131 |
m_exp(warmup_values); |
|
132 |
} |
|
133 |
||
134 |
// test pow perf |
|
135 |
double[] values = exp_values(1000000); |
|
136 |
System.out.println("==> EXP " + m_exp(values)); |
|
137 |
||
138 |
// force uncommon trap |
|
139 |
double[] nan_values = new double[1]; |
|
140 |
nan_values[0] = Double.NaN; |
|
141 |
m_exp(nan_values); |
|
142 |
||
143 |
// force recompilation |
|
144 |
for (int i = 0; i < 20000; i++) { |
|
145 |
m_exp(warmup_values); |
|
146 |
} |
|
147 |
||
148 |
// test pow perf again |
|
149 |
System.out.println("==> EXP " + m_exp(values)); |
|
150 |
} |
|
151 |
} |
|
152 |
} |