[libc-commits] [libc] 32568fc - [libc] Fix a bug in MPFRUtils making ULP values off by 2^(-mantissaWidth).

Tue Ly via libc-commits libc-commits at lists.llvm.org
Thu Dec 2 06:10:41 PST 2021


Author: Tue Ly
Date: 2021-12-02T09:07:46-05:00
New Revision: 32568fc95e75b9c6cfaf6964bcf2d8db342f23ad

URL: https://github.com/llvm/llvm-project/commit/32568fc95e75b9c6cfaf6964bcf2d8db342f23ad
DIFF: https://github.com/llvm/llvm-project/commit/32568fc95e75b9c6cfaf6964bcf2d8db342f23ad.diff

LOG: [libc] Fix a bug in MPFRUtils making ULP values off by 2^(-mantissaWidth).

Fix a bug in MPFRUtils making ULP values off by 2^(-mantissaWidth) and incorrect eps for denormal numbers.

Differential Revision: https://reviews.llvm.org/D114878

Added: 
    

Modified: 
    libc/utils/MPFRWrapper/MPFRUtils.cpp

Removed: 
    


################################################################################
diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 9ae0ce6ff8097..0b2b1c0dbd5bd 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -312,11 +312,19 @@ class MPFRNumber {
 
     int thisExponent = fputil::FPBits<T>(thisAsT).getExponent();
     int inputExponent = fputil::FPBits<T>(input).getExponent();
+    // Adjust the exponents for denormal numbers.
+    if (fputil::FPBits<T>(thisAsT).getUnbiasedExponent() == 0)
+      ++thisExponent;
+    if (fputil::FPBits<T>(input).getUnbiasedExponent() == 0)
+      ++inputExponent;
+
     if (thisAsT * input < 0 || thisExponent == inputExponent) {
       MPFRNumber inputMPFR(input);
       mpfr_sub(inputMPFR.value, value, inputMPFR.value, MPFR_RNDN);
       mpfr_abs(inputMPFR.value, inputMPFR.value, MPFR_RNDN);
-      mpfr_mul_2si(inputMPFR.value, inputMPFR.value, -thisExponent, MPFR_RNDN);
+      mpfr_mul_2si(inputMPFR.value, inputMPFR.value,
+                   -thisExponent + int(fputil::MantissaWidth<T>::value),
+                   MPFR_RNDN);
       return inputMPFR.as<double>();
     }
 
@@ -329,6 +337,11 @@ class MPFRNumber {
     T max = thisAsT > input ? thisAsT : input;
     int minExponent = fputil::FPBits<T>(min).getExponent();
     int maxExponent = fputil::FPBits<T>(max).getExponent();
+    // Adjust the exponents for denormal numbers.
+    if (fputil::FPBits<T>(min).getUnbiasedExponent() == 0)
+      ++minExponent;
+    if (fputil::FPBits<T>(max).getUnbiasedExponent() == 0)
+      ++maxExponent;
 
     MPFRNumber minMPFR(min);
     MPFRNumber maxMPFR(max);
@@ -337,10 +350,14 @@ class MPFRNumber {
     mpfr_mul_2si(pivot.value, pivot.value, maxExponent, MPFR_RNDN);
 
     mpfr_sub(minMPFR.value, pivot.value, minMPFR.value, MPFR_RNDN);
-    mpfr_mul_2si(minMPFR.value, minMPFR.value, -minExponent, MPFR_RNDN);
+    mpfr_mul_2si(minMPFR.value, minMPFR.value,
+                 -minExponent + int(fputil::MantissaWidth<T>::value),
+                 MPFR_RNDN);
 
     mpfr_sub(maxMPFR.value, maxMPFR.value, pivot.value, MPFR_RNDN);
-    mpfr_mul_2si(maxMPFR.value, maxMPFR.value, -maxExponent, MPFR_RNDN);
+    mpfr_mul_2si(maxMPFR.value, maxMPFR.value,
+                 -maxExponent + int(fputil::MantissaWidth<T>::value),
+                 MPFR_RNDN);
 
     mpfr_add(minMPFR.value, minMPFR.value, maxMPFR.value, MPFR_RNDN);
     return minMPFR.as<double>();


        


More information about the libc-commits mailing list