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