809N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
809N/A * This code is free software; you can redistribute it and/or modify it
809N/A * under the terms of the GNU General Public License version 2 only, as
809N/A * published by the Free Software Foundation.
809N/A *
809N/A * This code is distributed in the hope that it will be useful, but WITHOUT
809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
809N/A * version 2 for more details (a copy is included in the LICENSE file that
809N/A * accompanied this code).
809N/A *
809N/A * You should have received a copy of the GNU General Public License version
809N/A * 2 along with this work; if not, write to the Free Software Foundation,
809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
809N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
809N/A */
809N/A
809N/A/*
809N/A * @test
809N/A * @bug 5037596
809N/A * @summary Verify bitwise conversion works for non-canonical NaN values
809N/A * @author Joseph D. Darcy
809N/A */
809N/A
809N/Aimport static java.lang.Float.*;
809N/Aimport static sun.misc.FloatConsts.*;
809N/A
809N/Apublic class BitwiseConversion {
809N/A static int testNanCase(int x) {
809N/A int errors = 0;
809N/A // Strip out sign and exponent bits
809N/A int y = x & SIGNIF_BIT_MASK;
809N/A
809N/A float values[] = {
809N/A intBitsToFloat(EXP_BIT_MASK | y),
809N/A intBitsToFloat(SIGN_BIT_MASK | EXP_BIT_MASK | y)
809N/A };
809N/A
809N/A for(float value: values) {
809N/A if (!isNaN(value)) {
809N/A throw new RuntimeException("Invalid input " + y +
809N/A "yielded non-NaN" + value);
809N/A }
809N/A int converted = floatToIntBits(value);
809N/A if (converted != 0x7fc00000) {
809N/A errors++;
809N/A System.err.format("Non-canoncial NaN bits returned: %x%n",
809N/A converted);
809N/A }
809N/A }
809N/A return errors;
809N/A }
809N/A
809N/A public static void main(String... argv) {
809N/A int errors = 0;
809N/A
809N/A for (int i = 0; i < SIGNIFICAND_WIDTH-1; i++) {
809N/A errors += testNanCase(1<<i);
809N/A }
809N/A
809N/A if (floatToIntBits(Float.POSITIVE_INFINITY)
809N/A != 0x7F800000) {
809N/A errors++;
809N/A System.err.println("Bad conversion for +infinity.");
809N/A }
809N/A
809N/A if (floatToIntBits(Float.NEGATIVE_INFINITY)
809N/A != 0xFF800000) {
809N/A errors++;
809N/A System.err.println("Bad conversion for -infinity.");
809N/A }
809N/A
809N/A if (errors > 0)
809N/A throw new RuntimeException();
809N/A }
809N/A}