Path: blob/master/test/jdk/java/lang/Double/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 DoubleConsts29* @run main BitwiseConversion30* @author Joseph D. Darcy31*/3233import static java.lang.Double.*;3435public class BitwiseConversion {36static int testNanCase(long x) {37int errors = 0;38// Strip out sign and exponent bits39long y = x & DoubleConsts.SIGNIF_BIT_MASK;4041double values[] = {42longBitsToDouble(DoubleConsts.EXP_BIT_MASK | y),43longBitsToDouble(DoubleConsts.SIGN_BIT_MASK | DoubleConsts.EXP_BIT_MASK | y)44};4546for(double value: values) {47if (!isNaN(value)) {48throw new RuntimeException("Invalid input " + y +49"yielded non-NaN" + value);50}51long converted = doubleToLongBits(value);52if (converted != 0x7ff8000000000000L) {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 < DoubleConsts.SIGNIFICAND_WIDTH-1; i++) {65errors += testNanCase(1L<<i);66}6768if (doubleToLongBits(Double.POSITIVE_INFINITY)69!= 0x7ff0000000000000L) {70errors++;71System.err.println("Bad conversion for +infinity.");72}7374if (doubleToLongBits(Double.NEGATIVE_INFINITY)75!= 0xfff0000000000000L) {76errors++;77System.err.println("Bad conversion for -infinity.");78}7980if (errors > 0)81throw new RuntimeException();82}83}848586