Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Float/BitwiseConversion.java
38812 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* @author Joseph D. Darcy28*/2930import static java.lang.Float.*;31import static sun.misc.FloatConsts.*;3233public class BitwiseConversion {34static int testNanCase(int x) {35int errors = 0;36// Strip out sign and exponent bits37int y = x & SIGNIF_BIT_MASK;3839float values[] = {40intBitsToFloat(EXP_BIT_MASK | y),41intBitsToFloat(SIGN_BIT_MASK | EXP_BIT_MASK | y)42};4344for(float value: values) {45if (!isNaN(value)) {46throw new RuntimeException("Invalid input " + y +47"yielded non-NaN" + value);48}49int converted = floatToIntBits(value);50if (converted != 0x7fc00000) {51errors++;52System.err.format("Non-canoncial NaN bits returned: %x%n",53converted);54}55}56return errors;57}5859public static void main(String... argv) {60int errors = 0;6162for (int i = 0; i < SIGNIFICAND_WIDTH-1; i++) {63errors += testNanCase(1<<i);64}6566if (floatToIntBits(Float.POSITIVE_INFINITY)67!= 0x7F800000) {68errors++;69System.err.println("Bad conversion for +infinity.");70}7172if (floatToIntBits(Float.NEGATIVE_INFINITY)73!= 0xFF800000) {74errors++;75System.err.println("Bad conversion for -infinity.");76}7778if (errors > 0)79throw new RuntimeException();80}81}828384