|
1 /* |
|
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. |
|
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 /** |
|
25 * @test |
|
26 * @bug 7177917 |
|
27 * @summary Micro-benchmark for Math.pow() and Math.exp() |
|
28 * @modules java.base/jdk.internal.misc |
|
29 * @library /test/lib |
|
30 * |
|
31 * @run main compiler.c2.Test7177917 |
|
32 */ |
|
33 |
|
34 package compiler.c2; |
|
35 |
|
36 import jdk.test.lib.Utils; |
|
37 |
|
38 import java.util.Random; |
|
39 |
|
40 public class Test7177917 { |
|
41 |
|
42 static double d; |
|
43 |
|
44 static final Random R = Utils.getRandomInstance(); |
|
45 |
|
46 static long m_pow(double[][] values) { |
|
47 double res = 0; |
|
48 long start = System.nanoTime(); |
|
49 for (int i = 0; i < values.length; i++) { |
|
50 res += Math.pow(values[i][0], values[i][1]); |
|
51 } |
|
52 long stop = System.nanoTime(); |
|
53 d = res; |
|
54 return (stop - start) / 1000; |
|
55 } |
|
56 |
|
57 static long m_exp(double[] values) { |
|
58 double res = 0; |
|
59 long start = System.nanoTime(); |
|
60 for (int i = 0; i < values.length; i++) { |
|
61 res += Math.exp(values[i]); |
|
62 } |
|
63 long stop = System.nanoTime(); |
|
64 d = res; |
|
65 return (stop - start) / 1000; |
|
66 } |
|
67 |
|
68 static double[][] pow_values(int nb) { |
|
69 double[][] res = new double[nb][2]; |
|
70 for (int i = 0; i < nb; i++) { |
|
71 double ylogx = (1 + (R.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin |
|
72 double x = Math.abs(Double.longBitsToDouble(R.nextLong())); |
|
73 while (x != x) { |
|
74 x = Math.abs(Double.longBitsToDouble(R.nextLong())); |
|
75 } |
|
76 double logx = Math.log(x) / Math.log(2); |
|
77 double y = ylogx / logx; |
|
78 |
|
79 res[i][0] = x; |
|
80 res[i][1] = y; |
|
81 } |
|
82 return res; |
|
83 } |
|
84 |
|
85 static double[] exp_values(int nb) { |
|
86 double[] res = new double[nb]; |
|
87 for (int i = 0; i < nb; i++) { |
|
88 double ylogx = (1 + (R.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin |
|
89 double x = Math.E; |
|
90 double logx = Math.log(x) / Math.log(2); |
|
91 double y = ylogx / logx; |
|
92 res[i] = y; |
|
93 } |
|
94 return res; |
|
95 } |
|
96 |
|
97 static public void main(String[] args) { |
|
98 { |
|
99 // warmup |
|
100 double[][] warmup_values = pow_values(10); |
|
101 m_pow(warmup_values); |
|
102 |
|
103 for (int i = 0; i < 20000; i++) { |
|
104 m_pow(warmup_values); |
|
105 } |
|
106 // test pow perf |
|
107 double[][] values = pow_values(1000000); |
|
108 System.out.println("==> POW " + m_pow(values)); |
|
109 |
|
110 // force uncommon trap |
|
111 double[][] nan_values = new double[1][2]; |
|
112 nan_values[0][0] = Double.NaN; |
|
113 nan_values[0][1] = Double.NaN; |
|
114 m_pow(nan_values); |
|
115 |
|
116 // force recompilation |
|
117 for (int i = 0; i < 20000; i++) { |
|
118 m_pow(warmup_values); |
|
119 } |
|
120 |
|
121 // test pow perf again |
|
122 System.out.println("==> POW " + m_pow(values)); |
|
123 } |
|
124 { |
|
125 // warmup |
|
126 double[] warmup_values = exp_values(10); |
|
127 m_exp(warmup_values); |
|
128 |
|
129 for (int i = 0; i < 20000; i++) { |
|
130 m_exp(warmup_values); |
|
131 } |
|
132 |
|
133 // test pow perf |
|
134 double[] values = exp_values(1000000); |
|
135 System.out.println("==> EXP " + m_exp(values)); |
|
136 |
|
137 // force uncommon trap |
|
138 double[] nan_values = new double[1]; |
|
139 nan_values[0] = Double.NaN; |
|
140 m_exp(nan_values); |
|
141 |
|
142 // force recompilation |
|
143 for (int i = 0; i < 20000; i++) { |
|
144 m_exp(warmup_values); |
|
145 } |
|
146 |
|
147 // test pow perf again |
|
148 System.out.println("==> EXP " + m_exp(values)); |
|
149 } |
|
150 } |
|
151 } |