|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 THL A29 Limited, a Tencent company. 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 8265325 |
| 27 | + * @summary test the optimization of pow(x, 0.5) |
| 28 | + * @requires os.arch=="amd64" | os.arch=="x86_64" |
| 29 | + * @run main/othervm TestPow0Dot5Opt |
| 30 | + * @run main/othervm -Xint TestPow0Dot5Opt |
| 31 | + * @run main/othervm -Xbatch -XX:TieredStopAtLevel=1 TestPow0Dot5Opt |
| 32 | + * @run main/othervm -Xbatch -XX:-TieredCompilation TestPow0Dot5Opt |
| 33 | + */ |
| 34 | + |
| 35 | +public class TestPow0Dot5Opt { |
| 36 | + |
| 37 | + static void test(double a) throws Exception { |
| 38 | + // pow(x, 0.5) isn't replaced with sqrt(x) for x < 0.0 |
| 39 | + if (a < 0.0) return; |
| 40 | + |
| 41 | + double r1 = Math.sqrt(a); |
| 42 | + double r2 = Math.pow(a, 0.5); |
| 43 | + if (r1 != r2) { |
| 44 | + throw new RuntimeException("pow(" + a + ", 0.5), expected: " + r1 + ", actual: " + r2); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + for (int i = 0; i < 10; i++) { |
| 50 | + for (int j = 1; j < 100000; j++) { |
| 51 | + test(j * 1.0); |
| 52 | + test(1.0 / j); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + test(0.0); |
| 57 | + |
| 58 | + // Special case: pow(+0.0, 0.5) = 0.0 |
| 59 | + double r = Math.pow(+0.0, 0.5); |
| 60 | + if (Double.doubleToRawLongBits(r) != Double.doubleToRawLongBits(0.0)) { |
| 61 | + throw new RuntimeException("pow(+0.0, 0.5), expected: 0.0, actual: " + r); |
| 62 | + } |
| 63 | + |
| 64 | + // Special case: pow(-0.0, 0.5) = 0.0 |
| 65 | + r = Math.pow(-0.0, 0.5); |
| 66 | + if (Double.doubleToRawLongBits(r) != Double.doubleToRawLongBits(0.0)) { |
| 67 | + throw new RuntimeException("pow(-0.0, 0.5), expected: 0.0, actual: " + r); |
| 68 | + } |
| 69 | + |
| 70 | + // Special case: pow(Double.POSITIVE_INFINITY, 0.5) = Infinity |
| 71 | + r = Math.pow(Double.POSITIVE_INFINITY, 0.5); |
| 72 | + if (!(r > 0 && Double.isInfinite(r))) { |
| 73 | + throw new RuntimeException("pow(+Infinity, 0.5), expected: Infinity, actual: " + r); |
| 74 | + } |
| 75 | + |
| 76 | + // Special case: pow(Double.NEGATIVE_INFINITY, 0.5) = Infinity |
| 77 | + r = Math.pow(Double.NEGATIVE_INFINITY, 0.5); |
| 78 | + if (!(r > 0 && Double.isInfinite(r))) { |
| 79 | + throw new RuntimeException("pow(-Infinity, 0.5), expected: Infinity, actual: " + r); |
| 80 | + } |
| 81 | + |
| 82 | + // Special case: pow(Double.NaN, 0.5) = NaN |
| 83 | + r = Math.pow(Double.NaN, 0.5); |
| 84 | + if (!Double.isNaN(r)) { |
| 85 | + throw new RuntimeException("pow(NaN, 0.5), expected: NaN, actual: " + r); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments