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