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

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


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG32568fc95e75: [libc] Fix a bug in MPFRUtils making ULP values off by 2^(-mantissaWidth). (authored by lntue).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114878/new/

https://reviews.llvm.org/D114878

Files:
  libc/utils/MPFRWrapper/MPFRUtils.cpp


Index: libc/utils/MPFRWrapper/MPFRUtils.cpp
===================================================================
--- libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -312,11 +312,19 @@
 
     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 @@
     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 @@
     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>();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114878.391300.patch
Type: text/x-patch
Size: 2349 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20211202/91cb2725/attachment.bin>


More information about the libc-commits mailing list