jdk
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
24package compiler.intrinsics;25import java.util.random.RandomGenerator;26import java.util.random.RandomGeneratorFactory;27
28public class TestFloatClassCheck {29RandomGenerator rng;30int BUFFER_SIZE = 1024;31float[] inputs;32boolean[] outputs;33
34public TestFloatClassCheck() {35outputs = new boolean[BUFFER_SIZE];36inputs = new float[BUFFER_SIZE];37RandomGenerator rng = RandomGeneratorFactory.getDefault().create(0);38float input;39for (int i = 0; i < BUFFER_SIZE; i++) {40if (i % 5 == 0) {41input = (i%2 == 0) ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;42}43else if (i % 3 == 0) input = Float.NaN;44else input = rng.nextFloat();45inputs[i] = input;46}47}48
49public void checkResult(String method) {50for (int i=0; i < BUFFER_SIZE; i++) {51boolean expected = floatClassCheck(inputs[i], method);52if (expected != outputs[i]) {53String errorMsg = "Correctness check failed for Float." + method +54"() for input = " + inputs[i];55throw new RuntimeException(errorMsg);56}57}58}59
60public boolean floatClassCheck(float f, String method) {61int infBits = Float.floatToRawIntBits(Float.POSITIVE_INFINITY);62int bits = Float.floatToRawIntBits(f);63bits = bits & Integer.MAX_VALUE;64switch (method) {65case "isFinite": return (bits < infBits);66case "isInfinite": return (bits == infBits);67case "isNaN": return (bits > infBits);68default: throw new IllegalArgumentException("incorrect method for Float");69}70}71
72}
73