Path: blob/master/test/jdk/java/lang/Float/BitwiseConversion.java
41149 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 503759626* @summary Verify bitwise conversion works for non-canonical NaN values27* @library ../Math28* @build FloatConsts29* @run main BitwiseConversion30* @author Joseph D. Darcy31*/3233import static java.lang.Float.*;3435public class BitwiseConversion {36static int testNanCase(int x) {37int errors = 0;38// Strip out sign and exponent bits39int y = x & FloatConsts.SIGNIF_BIT_MASK;4041float values[] = {42intBitsToFloat(FloatConsts.EXP_BIT_MASK | y),43intBitsToFloat(FloatConsts.SIGN_BIT_MASK | FloatConsts.EXP_BIT_MASK | y)44};4546for(float value: values) {47if (!isNaN(value)) {48throw new RuntimeException("Invalid input " + y +49"yielded non-NaN" + value);50}51int converted = floatToIntBits(value);52if (converted != 0x7fc00000) {53errors++;54System.err.format("Non-canoncial NaN bits returned: %x%n",55converted);56}57}58return errors;59}6061public static void main(String... argv) {62int errors = 0;6364for (int i = 0; i < FloatConsts.SIGNIFICAND_WIDTH-1; i++) {65errors += testNanCase(1<<i);66}6768if (floatToIntBits(Float.POSITIVE_INFINITY)69!= 0x7F800000) {70errors++;71System.err.println("Bad conversion for +infinity.");72}7374if (floatToIntBits(Float.NEGATIVE_INFINITY)75!= 0xFF800000) {76errors++;77System.err.println("Bad conversion for -infinity.");78}7980if (errors > 0)81throw new RuntimeException();82}83}848586