809N/A/*
2362N/A * Copyright (c) 2003, 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 4826774
809N/A * @summary Numerical tests for hexadecimal inputs to parseDouble, parseFloat
809N/A * @author Joseph D. Darcy
809N/A */
809N/A
809N/A
809N/Aimport java.util.regex.*;
809N/Aimport sun.misc.FpUtils;
809N/Aimport sun.misc.DoubleConsts;
809N/A
809N/Apublic class ParseHexFloatingPoint {
809N/A private ParseHexFloatingPoint(){}
809N/A
809N/A public static final double infinityD = Double.POSITIVE_INFINITY;
809N/A public static final double NaND = Double.NaN;
809N/A
809N/A static int test(String testName, String input,
809N/A double result, double expected) {
809N/A int failures =0;
809N/A
809N/A if (Double.compare(result, expected) != 0 ) {
809N/A System.err.println("Failure for " + testName +
809N/A ": For input " + input +
809N/A " expected " + expected +
809N/A " got " + result + ".");
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A static int testCase(String input, double expected) {
809N/A int failures =0;
809N/A
809N/A
809N/A // Try different combination of letter components
809N/A input = input.toLowerCase(java.util.Locale.US);
809N/A
809N/A String [] suffices = {"", "f", "F", "d", "D"};
809N/A String [] signs = {"", "-", "+"};
809N/A
809N/A for(int i = 0; i < 2; i++) {
809N/A String s1 = input;
809N/A if(i == 1)
809N/A s1 = s1.replace('x', 'X');
809N/A
809N/A for(int j = 0; j < 2; j++) {
809N/A String s2 = s1;
809N/A if(j == 1)
809N/A s2 = s2.replace('p', 'P');
809N/A
809N/A for(int k = 0; k < 2; k++) {
809N/A String s3 = s2;
809N/A if(k == 1)
809N/A s3 = upperCaseHex(s3);
809N/A
809N/A
809N/A for(int m = 0; m < suffices.length; m++) {
809N/A String s4 = s3 + suffices[m];
809N/A
809N/A
809N/A for(int n = 0; n < signs.length; n++) {
809N/A String s5 = signs[n] + s4;
809N/A
809N/A double result = Double.parseDouble(s5);
809N/A failures += test("Double.parseDouble",
809N/A s5, result, (signs[n].equals("-") ?
809N/A -expected:
809N/A expected));
809N/A }
809N/A }
809N/A }
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A static String upperCaseHex(String s) {
809N/A return s.replace('a', 'A').replace('b', 'B').replace('c', 'C').
809N/A replace('d', 'D').replace('e','E').replace('f', 'F');
809N/A }
809N/A
809N/A /*
809N/A * Test easy and tricky double rounding cases.
809N/A */
809N/A static int doubleTests() {
809N/A
809N/A /*
809N/A * A String, double pair
809N/A */
809N/A class PairSD {
809N/A public String s;
809N/A public double d;
809N/A PairSD(String s, double d) {
809N/A this.s = s;
809N/A this.d = d;
809N/A }
809N/A }
809N/A int failures = 0;
809N/A
809N/A
809N/A
809N/A // Hex strings that convert to three; test basic functionality
809N/A // of significand and exponent shift adjusts along with the
809N/A // no-op of adding leading zeros. These cases don't exercise
809N/A // the rounding code.
809N/A String leadingZeros = "0x0000000000000000000";
809N/A String [] threeTests = {
809N/A "0x.003p12",
809N/A "0x.006p11",
809N/A "0x.00cp10",
809N/A "0x.018p9",
809N/A
809N/A "0x.3p4",
809N/A "0x.6p3",
809N/A "0x.cp2",
809N/A "0x1.8p1",
809N/A
809N/A "0x3p0",
809N/A "0x6.0p-1",
809N/A "0xc.0p-2",
809N/A "0x18.0p-3",
809N/A
809N/A "0x3000000p-24",
809N/A "0x3.0p0",
809N/A "0x3.000000p0",
809N/A };
809N/A for(int i=0; i < threeTests.length; i++) {
809N/A String input = threeTests[i];
809N/A failures += testCase(input, 3.0);
809N/A
809N/A input.replaceFirst("^0x", leadingZeros);
809N/A failures += testCase(input, 3.0);
809N/A }
809N/A
809N/A long bigExponents [] = {
809N/A 2*DoubleConsts.MAX_EXPONENT,
809N/A 2*DoubleConsts.MIN_EXPONENT,
809N/A
809N/A (long)Integer.MAX_VALUE-1,
809N/A (long)Integer.MAX_VALUE,
809N/A (long)Integer.MAX_VALUE+1,
809N/A
809N/A (long)Integer.MIN_VALUE-1,
809N/A (long)Integer.MIN_VALUE,
809N/A (long)Integer.MIN_VALUE+1,
809N/A
809N/A Long.MAX_VALUE-1,
809N/A Long.MAX_VALUE,
809N/A
809N/A Long.MIN_VALUE+1,
809N/A Long.MIN_VALUE,
809N/A };
809N/A
809N/A // Test zero significand with large exponents.
809N/A for(int i = 0; i < bigExponents.length; i++) {
809N/A failures += testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0);
809N/A }
809N/A
809N/A // Test nonzero significand with large exponents.
809N/A for(int i = 0; i < bigExponents.length; i++) {
809N/A long exponent = bigExponents[i];
809N/A failures += testCase("0x10000.0p"+Long.toString(exponent) ,
809N/A (exponent <0?0.0:infinityD));
809N/A }
809N/A
809N/A // Test significands with different lengths and bit patterns.
809N/A {
809N/A long signif = 0;
809N/A for(int i = 1; i <= 0xe; i++) {
809N/A signif = (signif <<4) | (long)i;
809N/A failures += testCase("0x"+Long.toHexString(signif)+"p0", signif);
809N/A }
809N/A }
809N/A
809N/A PairSD [] testCases = {
809N/A new PairSD("0x0.0p0", 0.0/16.0),
809N/A new PairSD("0x0.1p0", 1.0/16.0),
809N/A new PairSD("0x0.2p0", 2.0/16.0),
809N/A new PairSD("0x0.3p0", 3.0/16.0),
809N/A new PairSD("0x0.4p0", 4.0/16.0),
809N/A new PairSD("0x0.5p0", 5.0/16.0),
809N/A new PairSD("0x0.6p0", 6.0/16.0),
809N/A new PairSD("0x0.7p0", 7.0/16.0),
809N/A new PairSD("0x0.8p0", 8.0/16.0),
809N/A new PairSD("0x0.9p0", 9.0/16.0),
809N/A new PairSD("0x0.ap0", 10.0/16.0),
809N/A new PairSD("0x0.bp0", 11.0/16.0),
809N/A new PairSD("0x0.cp0", 12.0/16.0),
809N/A new PairSD("0x0.dp0", 13.0/16.0),
809N/A new PairSD("0x0.ep0", 14.0/16.0),
809N/A new PairSD("0x0.fp0", 15.0/16.0),
809N/A
809N/A // Half-way case between zero and MIN_VALUE rounds down to
809N/A // zero
809N/A new PairSD("0x1.0p-1075", 0.0),
809N/A
809N/A // Slighly more than half-way case between zero and
809N/A // MIN_VALUES rounds up to zero.
809N/A new PairSD("0x1.1p-1075", Double.MIN_VALUE),
809N/A new PairSD("0x1.000000000001p-1075", Double.MIN_VALUE),
809N/A new PairSD("0x1.000000000000001p-1075", Double.MIN_VALUE),
809N/A
809N/A // More subnormal rounding tests
809N/A new PairSD("0x0.fffffffffffff7fffffp-1022", FpUtils.nextDown(DoubleConsts.MIN_NORMAL)),
809N/A new PairSD("0x0.fffffffffffff8p-1022", DoubleConsts.MIN_NORMAL),
809N/A new PairSD("0x0.fffffffffffff800000001p-1022",DoubleConsts.MIN_NORMAL),
809N/A new PairSD("0x0.fffffffffffff80000000000000001p-1022",DoubleConsts.MIN_NORMAL),
809N/A new PairSD("0x1.0p-1022", DoubleConsts.MIN_NORMAL),
809N/A
809N/A
809N/A // Large value and overflow rounding tests
809N/A new PairSD("0x1.fffffffffffffp1023", Double.MAX_VALUE),
809N/A new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE),
809N/A new PairSD("0x1.fffffffffffff4p1023", Double.MAX_VALUE),
809N/A new PairSD("0x1.fffffffffffff7fffffp1023", Double.MAX_VALUE),
809N/A new PairSD("0x1.fffffffffffff8p1023", infinityD),
809N/A new PairSD("0x1.fffffffffffff8000001p1023", infinityD),
809N/A
809N/A new PairSD("0x1.ffffffffffffep1023", FpUtils.nextDown(Double.MAX_VALUE)),
809N/A new PairSD("0x1.ffffffffffffe0000p1023", FpUtils.nextDown(Double.MAX_VALUE)),
809N/A new PairSD("0x1.ffffffffffffe8p1023", FpUtils.nextDown(Double.MAX_VALUE)),
809N/A new PairSD("0x1.ffffffffffffe7p1023", FpUtils.nextDown(Double.MAX_VALUE)),
809N/A new PairSD("0x1.ffffffffffffeffffffp1023", Double.MAX_VALUE),
809N/A new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE),
809N/A };
809N/A
809N/A for (int i = 0; i < testCases.length; i++) {
809N/A failures += testCase(testCases[i].s,testCases[i].d);
809N/A }
809N/A
809N/A failures += significandAlignmentTests();
809N/A
809N/A {
809N/A java.util.Random rand = new java.util.Random();
809N/A // Consistency check; double => hexadecimal => double
809N/A // preserves the original value.
809N/A for(int i = 0; i < 1000; i++) {
809N/A double d = rand.nextDouble();
809N/A failures += testCase(Double.toHexString(d), d);
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A /*
809N/A * Verify rounding works the same regardless of how the
809N/A * significand is aligned on input. A useful extension could be
809N/A * to have this sort of test for strings near the overflow
809N/A * threshold.
809N/A */
809N/A static int significandAlignmentTests() {
809N/A int failures = 0;
809N/A // baseSignif * 2^baseExp = nextDown(2.0)
809N/A long [] baseSignifs = {
809N/A 0x1ffffffffffffe00L,
809N/A 0x1fffffffffffff00L
809N/A };
809N/A
809N/A double [] answers = {
809N/A FpUtils.nextDown(FpUtils.nextDown(2.0)),
809N/A FpUtils.nextDown(2.0),
809N/A 2.0
809N/A };
809N/A
809N/A int baseExp = -60;
809N/A int count = 0;
809N/A for(int i = 0; i < 2; i++) {
809N/A for(long j = 0; j <= 0xfL; j++) {
809N/A for(long k = 0; k <= 8; k+= 4) { // k = {0, 4, 8}
809N/A long base = baseSignifs[i];
809N/A long testValue = base | (j<<4) | k;
809N/A
809N/A int offset = 0;
809N/A // Calculate when significand should be incremented
809N/A // see table 4.7 in Koren book
809N/A
809N/A if ((base & 0x100L) == 0L ) { // lsb is 0
809N/A if ( (j >= 8L) && // round is 1
809N/A ((j & 0x7L) != 0 || k != 0 ) ) // sticky is 1
809N/A offset = 1;
809N/A }
809N/A else { // lsb is 1
809N/A if (j >= 8L) // round is 1
809N/A offset = 1;
809N/A }
809N/A
809N/A double expected = answers[i+offset];
809N/A
809N/A for(int m = -2; m <= 3; m++) {
809N/A count ++;
809N/A
809N/A // Form equal value string and evaluate it
809N/A String s = "0x" +
809N/A Long.toHexString((m >=0) ?(testValue<<m):(testValue>>(-m))) +
809N/A "p" + (baseExp - m);
809N/A
809N/A failures += testCase(s, expected);
809N/A }
809N/A }
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A
809N/A /*
809N/A * Test tricky float rounding cases. The code which
809N/A * reads in a hex string converts the string to a double value.
809N/A * If a float value is needed, the double value is cast to float.
809N/A * However, the cast be itself not always guaranteed to return the
809N/A * right result since:
809N/A *
809N/A * 1. hex string => double can discard a sticky bit which would
809N/A * influence a direct hex string => float conversion.
809N/A *
809N/A * 2. hex string => double => float can have a rounding to double
809N/A * precision which results in a larger float value while a direct
809N/A * hex string => float conversion would not round up.
809N/A *
809N/A * This method includes tests of the latter two possibilities.
809N/A */
809N/A static int floatTests(){
809N/A int failures = 0;
809N/A
809N/A /*
809N/A * A String, float pair
809N/A */
809N/A class PairSD {
809N/A public String s;
809N/A public float f;
809N/A PairSD(String s, float f) {
809N/A this.s = s;
809N/A this.f = f;
809N/A }
809N/A }
809N/A
809N/A String [][] roundingTestCases = {
809N/A // Target float value hard rouding version
809N/A
809N/A {"0x1.000000p0", "0x1.0000000000001p0"},
809N/A
809N/A // Try some values that should round up to nextUp(1.0f)
809N/A {"0x1.000002p0", "0x1.0000010000001p0"},
809N/A {"0x1.000002p0", "0x1.00000100000008p0"},
809N/A {"0x1.000002p0", "0x1.0000010000000fp0"},
809N/A {"0x1.000002p0", "0x1.00000100000001p0"},
809N/A {"0x1.000002p0", "0x1.00000100000000000000000000000000000000001p0"},
809N/A {"0x1.000002p0", "0x1.0000010000000fp0"},
809N/A
809N/A // Potential double rounding cases
809N/A {"0x1.000002p0", "0x1.000002fffffffp0"},
809N/A {"0x1.000002p0", "0x1.000002fffffff8p0"},
809N/A {"0x1.000002p0", "0x1.000002ffffffffp0"},
809N/A
809N/A {"0x1.000002p0", "0x1.000002ffff0ffp0"},
809N/A {"0x1.000002p0", "0x1.000002ffff0ff8p0"},
809N/A {"0x1.000002p0", "0x1.000002ffff0fffp0"},
809N/A
809N/A
809N/A {"0x1.000000p0", "0x1.000000fffffffp0"},
809N/A {"0x1.000000p0", "0x1.000000fffffff8p0"},
809N/A {"0x1.000000p0", "0x1.000000ffffffffp0"},
809N/A
809N/A {"0x1.000000p0", "0x1.000000ffffffep0"},
809N/A {"0x1.000000p0", "0x1.000000ffffffe8p0"},
809N/A {"0x1.000000p0", "0x1.000000ffffffefp0"},
809N/A
809N/A // Float subnormal cases
809N/A {"0x0.000002p-126", "0x0.0000010000001p-126"},
809N/A {"0x0.000002p-126", "0x0.00000100000000000001p-126"},
809N/A
809N/A {"0x0.000006p-126", "0x0.0000050000001p-126"},
809N/A {"0x0.000006p-126", "0x0.00000500000000000001p-126"},
809N/A
809N/A {"0x0.0p-149", "0x0.7ffffffffffffffp-149"},
809N/A {"0x1.0p-148", "0x1.3ffffffffffffffp-148"},
809N/A {"0x1.cp-147", "0x1.bffffffffffffffp-147"},
809N/A
809N/A {"0x1.fffffcp-127", "0x1.fffffdffffffffp-127"},
809N/A };
809N/A
809N/A String [] signs = {"", "-"};
809N/A
809N/A for(int i = 0; i < roundingTestCases.length; i++) {
809N/A for(int j = 0; j < signs.length; j++) {
809N/A String expectedIn = signs[j]+roundingTestCases[i][0];
809N/A String resultIn = signs[j]+roundingTestCases[i][1];
809N/A
809N/A float expected = Float.parseFloat(expectedIn);
809N/A float result = Float.parseFloat(resultIn);
809N/A
809N/A if( Float.compare(expected, result) != 0) {
809N/A failures += 1;
809N/A System.err.println("" + (i+1));
809N/A System.err.println("Expected = " + Float.toHexString(expected));
809N/A System.err.println("Rounded = " + Float.toHexString(result));
809N/A System.err.println("Double = " + Double.toHexString(Double.parseDouble(resultIn)));
809N/A System.err.println("Input = " + resultIn);
809N/A System.err.println("");
809N/A }
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A public static void main(String argv[]) {
809N/A int failures = 0;
809N/A
809N/A failures += doubleTests();
809N/A failures += floatTests();
809N/A
809N/A if (failures != 0) {
809N/A throw new RuntimeException("" + failures + " failures while " +
809N/A "testing hexadecimal floating-point " +
809N/A "parsing.");
809N/A }
809N/A }
809N/A
809N/A}