|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | +* @summary Test x86_64 intrinsics for Double methods isNaN, isFinite, isInfinite. |
| 27 | +* @requires vm.cpu.features ~= ".*avx512dq.*" |
| 28 | +* @library /test/lib / |
| 29 | +* @run driver compiler.intrinsics.TestDoubleClassCheck |
| 30 | +*/ |
| 31 | + |
| 32 | +package compiler.intrinsics; |
| 33 | +import compiler.lib.ir_framework.*; |
| 34 | +import java.util.random.RandomGenerator; |
| 35 | +import java.util.random.RandomGeneratorFactory; |
| 36 | + |
| 37 | +public class TestDoubleClassCheck { |
| 38 | + RandomGenerator rng; |
| 39 | + int BUFFER_SIZE = 1024; |
| 40 | + double[] inputs; |
| 41 | + boolean[] outputs; |
| 42 | + |
| 43 | + public static void main(String args[]) { |
| 44 | + TestFramework.run(TestDoubleClassCheck.class); |
| 45 | + } |
| 46 | + |
| 47 | + public TestDoubleClassCheck() { |
| 48 | + outputs = new boolean[BUFFER_SIZE]; |
| 49 | + inputs = new double[BUFFER_SIZE]; |
| 50 | + RandomGenerator rng = RandomGeneratorFactory.getDefault().create(0); |
| 51 | + double input; |
| 52 | + for (int i = 0; i < BUFFER_SIZE; i++) { |
| 53 | + if (i % 5 == 0) { |
| 54 | + input = (i%2 == 0) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; |
| 55 | + } |
| 56 | + else if (i % 3 == 0) input = Double.NaN; |
| 57 | + else input = rng.nextDouble(); |
| 58 | + inputs[i] = input; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test // needs to be run in (fast) debug mode |
| 63 | + @Warmup(10000) |
| 64 | + @IR(counts = {"IsInfiniteD", ">= 1"}) // Atleast one IsInfiniteD node is generated if intrinsic is used |
| 65 | + public void testIsInfinite() { |
| 66 | + for (int i = 0; i < BUFFER_SIZE; i++) { |
| 67 | + outputs[i] = Double.isInfinite(inputs[i]); |
| 68 | + } |
| 69 | + checkResult("isInfinite"); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public void checkResult(String method) { |
| 74 | + for (int i=0; i < BUFFER_SIZE; i++) { |
| 75 | + boolean expected = doubleClassCheck(inputs[i], method); |
| 76 | + if (expected != outputs[i]) { |
| 77 | + String errorMsg = "Correctness check failed for Double." + method + |
| 78 | + "() for input = " + inputs[i]; |
| 79 | + throw new RuntimeException(errorMsg); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public boolean doubleClassCheck(double f, String method) { |
| 85 | + long infBits = Double.doubleToRawLongBits(Double.POSITIVE_INFINITY); |
| 86 | + long bits = Double.doubleToRawLongBits(f); |
| 87 | + bits = bits & Long.MAX_VALUE; |
| 88 | + switch (method) { |
| 89 | + case "isFinite": return (bits < infBits); |
| 90 | + case "isInfinite": return (bits == infBits); |
| 91 | + case "isNaN": return (bits > infBits); |
| 92 | + default: throw new IllegalArgumentException("incorrect method for Double"); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments