From 67ec331a7201091c979eca18abe94ceeec9c290f Mon Sep 17 00:00:00 2001 From: xtonik Date: Sat, 3 Jun 2023 01:14:12 +0200 Subject: [PATCH] Mantissa cannot be greater than significand's highest bit and if it is equal, it can be set to zero as mask used afterwards clears the most significant bit --- .../randelshofer/fastdoubleparser/FastDoubleMath.java | 8 ++++---- .../randelshofer/fastdoubleparser/FastFloatMath.java | 11 +++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/FastDoubleMath.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/FastDoubleMath.java index cf02d901..70f7641c 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/FastDoubleMath.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/FastDoubleMath.java @@ -932,14 +932,14 @@ static double tryDecToDoubleWithFastAlgorithm(boolean isNegative, long significa mantissa >>>= 1; // Here we have mantissa < (1<<53), unless there was an overflow - if (mantissa >= (1L << DOUBLE_SIGNIFICAND_WIDTH)) { + if (mantissa == (1L << DOUBLE_SIGNIFICAND_WIDTH)) { // This will happen when parsing values such as 7.2057594037927933e+16 - mantissa = (1L << (DOUBLE_SIGNIFICAND_WIDTH - 1)); + mantissa = 0; lz--; // undo previous addition + } else { + mantissa &= ~(1L << (DOUBLE_SIGNIFICAND_WIDTH - 1)); } - mantissa &= ~(1L << (DOUBLE_SIGNIFICAND_WIDTH - 1)); - long realExponent = exponent - lz; // we have to check that realExponent is in range, otherwise we bail out if ((realExponent < 1) || (realExponent > DOUBLE_MAX_EXPONENT_POWER_OF_TWO + DOUBLE_EXPONENT_BIAS)) { diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/FastFloatMath.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/FastFloatMath.java index 10bdca84..f9168e19 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/FastFloatMath.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/FastFloatMath.java @@ -232,15 +232,14 @@ static float tryDecToFloatWithFastAlgorithm(boolean isNegative, long significand mantissa >>>= 1; // Here we have mantissa < (1<<24), unless there was an overflow - if (mantissa >= (1L << FLOAT_SIGNIFICAND_WIDTH)) { - // This will happen when parsing values such as 7.2057594037927933e+16 ?? - mantissa = (1L << (FLOAT_SIGNIFICAND_WIDTH - 1)); + if (mantissa == (1L << FLOAT_SIGNIFICAND_WIDTH)) { + // This will happen when parsing values such as 1.4757395E20 + mantissa = 0; lz--; // undo previous addition + } else { + mantissa &= ~(1L << (FLOAT_SIGNIFICAND_WIDTH - 1)); } - mantissa &= ~(1L << (FLOAT_SIGNIFICAND_WIDTH - 1)); - - long real_exponent = exponent - lz; // we have to check that real_exponent is in range, otherwise we bail out if ((real_exponent < 1) || (real_exponent > FLOAT_MAX_EXPONENT_POWER_OF_TWO + FLOAT_EXPONENT_BIAS)) {