[libc-commits] [libc] [llvm] [libc] Make str_to_float independent of fenv (PR #102369)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed Aug 7 15:07:50 PDT 2024


https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/102369

>From 4f078c2b5ce060a347424419f194e8e9197dd5e3 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 7 Aug 2024 14:17:53 -0700
Subject: [PATCH 1/2] [libc] Make str_to_float independent of fenv

The str_to_float conversion code doesn't need the features provided by
fenv and the dependency is creating a blocker for hand-in-hand. This
patch uses a workaround to remove this dependency.
---
 libc/src/__support/CMakeLists.txt                 |  2 --
 libc/src/__support/str_to_float.h                 | 15 +++++++++------
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel |  2 --
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index d8a192f1ffa57..9bd1e29081a80 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -190,8 +190,6 @@ add_header_library(
     libc.src.__support.CPP.bit
     libc.src.__support.CPP.limits
     libc.src.__support.CPP.optional
-    libc.src.__support.FPUtil.dyadic_float
-    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.rounding_mode
     libc.src.errno.errno
diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h
index c72bc1f957dc3..ade08f78a11dc 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -13,9 +13,7 @@
 #include "src/__support/CPP/limits.h"
 #include "src/__support/CPP/optional.h"
 #include "src/__support/CPP/string_view.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/dyadic_float.h"
 #include "src/__support/FPUtil/rounding_mode.h"
 #include "src/__support/common.h"
 #include "src/__support/ctype_utils.h"
@@ -27,6 +25,8 @@
 #include "src/__support/uint128.h"
 #include "src/errno/libc_errno.h" // For ERANGE
 
+#include <stdint.h>
+
 namespace LIBC_NAMESPACE_DECL {
 namespace internal {
 
@@ -525,10 +525,13 @@ clinger_fast_path(ExpandedFloat<T> init_num,
   FPBits result;
   T float_mantissa;
   if constexpr (cpp::is_same_v<StorageType, UInt<128>>) {
-    float_mantissa = static_cast<T>(fputil::DyadicFloat<128>(
-        Sign::POS, 0,
-        fputil::DyadicFloat<128>::MantissaType(
-            {uint64_t(mantissa), uint64_t(mantissa >> 64)})));
+    // float_mantissa = static_cast<T>(fputil::DyadicFloat<128>(
+    //     Sign::POS, 0,
+    //     fputil::DyadicFloat<128>::MantissaType(
+    //         {uint64_t(mantissa), uint64_t(mantissa >> 64)})));
+    float_mantissa = (static_cast<T>(uint64_t(mantissa)) *
+                      static_cast<T>(1ull << 32) * static_cast<T>(1ull << 32)) +
+                     static_cast<T>(uint64_t(mantissa >> 64));
   } else {
     float_mantissa = static_cast<T>(mantissa);
   }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 253b89216a88f..0eac44d72cfec 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -658,8 +658,6 @@ libc_support_library(
         ":__support_cpp_optional",
         ":__support_cpp_string_view",
         ":__support_ctype_utils",
-        ":__support_fputil_dyadic_float",
-        ":__support_fputil_fenv_impl",
         ":__support_fputil_fp_bits",
         ":__support_fputil_rounding_mode",
         ":__support_str_to_integer",

>From ea7da5e5a52dac879745c65221b14a5fae801181 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 7 Aug 2024 15:07:21 -0700
Subject: [PATCH 2/2] better 2^64 and remove commented out code

---
 libc/src/__support/str_to_float.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h
index ade08f78a11dc..17cf3dd55b9fb 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -525,13 +525,9 @@ clinger_fast_path(ExpandedFloat<T> init_num,
   FPBits result;
   T float_mantissa;
   if constexpr (cpp::is_same_v<StorageType, UInt<128>>) {
-    // float_mantissa = static_cast<T>(fputil::DyadicFloat<128>(
-    //     Sign::POS, 0,
-    //     fputil::DyadicFloat<128>::MantissaType(
-    //         {uint64_t(mantissa), uint64_t(mantissa >> 64)})));
-    float_mantissa = (static_cast<T>(uint64_t(mantissa)) *
-                      static_cast<T>(1ull << 32) * static_cast<T>(1ull << 32)) +
-                     static_cast<T>(uint64_t(mantissa >> 64));
+    float_mantissa =
+        (static_cast<T>(uint64_t(mantissa)) * static_cast<T>(0x1.0p64)) +
+        static_cast<T>(uint64_t(mantissa >> 64));
   } else {
     float_mantissa = static_cast<T>(mantissa);
   }



More information about the libc-commits mailing list