1 /*
2  * Copyright (c) 2012, 2020, 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  * @key randomness
27  * @bug 7177917
28  * @summary Micro-benchmark for Math.pow() and Math.exp()
29  * @modules java.base/jdk.internal.misc
30  * @library /test/lib
31  *
32  * @run main compiler.c2.Test7177917
33  */
34 
35 package compiler.c2;
36 
37 import jdk.test.lib.Utils;
38 
39 import java.util.Random;
40 
41 public class Test7177917 {
42 
43   static double d;
44 
45   static final Random R = Utils.getRandomInstance();
46 
m_pow(double[][] values)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 
m_exp(double[] values)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 
pow_values(int nb)69   static double[][] pow_values(int nb) {
70     double[][] res = new double[nb][2];
71     for (int i = 0; i < nb; i++) {
72       double ylogx = (1 + (R.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin
73       double x = Math.abs(Double.longBitsToDouble(R.nextLong()));
74       while (x != x) {
75         x = Math.abs(Double.longBitsToDouble(R.nextLong()));
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 
exp_values(int nb)86   static double[] exp_values(int nb) {
87     double[] res = new double[nb];
88     for (int i = 0; i < nb; i++) {
89       double ylogx = (1 + (R.nextDouble() * 2045)) - 1023; // 2045 rather than 2046 as a safety margin
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 
main(String[] args)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 }
153