[libc-commits] [libc] Float128 fpbits integration (PR #186938)

via libc-commits libc-commits at lists.llvm.org
Mon Mar 16 19:09:23 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: None (Emmaliu2006git)

<details>
<summary>Changes</summary>

This patch improves the integration of fputil::Float128 with FPBits.

In the previous patch, support for Float128 in FPBits relied on an ad-hoc check in get_fp_type to recognize the wrapper type. This patch removes that special case and instead relies on the is_floating_point type trait specialization for fputil::Float128.

With this change, Float128 is treated consistently with other floating-point types in libc. FPBits can now correctly recognize fputil::Float128 through the existing floating-point trait mechanism.

Additional unit tests are added to verify:
- FPBits instantiation with fputil::Float128
- correct raw bit storage through UInt128
- FP type detection through get_fp_type
- recognition by the is_floating_point trait

This patch builds on top of PR #<!-- -->186901.

---
Full diff: https://github.com/llvm/llvm-project/pull/186938.diff


5 Files Affected:

- (modified) libc/include/llvm-libc-types/float128.h (+3) 
- (modified) libc/src/__support/CPP/type_traits/is_floating_point.h (+7-1) 
- (modified) libc/src/__support/FPUtil/FPBits.h (+3) 
- (added) libc/src/__support/FPUtil/float128.h (+37) 
- (modified) libc/test/src/__support/FPUtil/fpbits_test.cpp (+36) 


``````````diff
diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
index 82ebb79f1f580..d507be24e9ace 100644
--- a/libc/include/llvm-libc-types/float128.h
+++ b/libc/include/llvm-libc-types/float128.h
@@ -31,6 +31,9 @@ typedef __float128 float128;
 #elif (LDBL_MANT_DIG == 113)
 #define LIBC_TYPES_HAS_FLOAT128
 typedef long double float128;
+#else
+#include "src/__support/FPUtil/float128.h"
+typedef LIBC_NAMESPACE::fputil::Float128 float128;
 #endif
 
 #endif // LLVM_LIBC_TYPES_FLOAT128_H
diff --git a/libc/src/__support/CPP/type_traits/is_floating_point.h b/libc/src/__support/CPP/type_traits/is_floating_point.h
index 9dc77ad7ee0ea..1804742e55943 100644
--- a/libc/src/__support/CPP/type_traits/is_floating_point.h
+++ b/libc/src/__support/CPP/type_traits/is_floating_point.h
@@ -15,6 +15,9 @@
 #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_FLOAT128
 
 namespace LIBC_NAMESPACE_DECL {
+namespace fputil {
+class Float128;
+}
 namespace cpp {
 
 // is_floating_point
@@ -42,7 +45,10 @@ template <typename T> struct is_floating_point {
 template <typename T>
 LIBC_INLINE_VAR constexpr bool is_floating_point_v =
     is_floating_point<T>::value;
-
+template <>
+struct is_floating_point<fputil::Float128> { //Float128 class -> is_floating_point = true
+  LIBC_INLINE_VAR static constexpr bool value = true;
+};
 } // namespace cpp
 } // namespace LIBC_NAMESPACE_DECL
 
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index ce4925bae125a..2b682e745371e 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -26,6 +26,7 @@
 #include "src/__support/math_extras.h"             // mask_trailing_ones
 #include "src/__support/sign.h"                    // Sign
 #include "src/__support/uint128.h"
+#include "src/__support/FPUtil/float128.h"  //include float128.h
 
 namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
@@ -809,6 +810,8 @@ template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
   else if constexpr (cpp::is_same_v<UnqualT, float128>)
     return FPType::IEEE754_Binary128;
 #endif
+  else if constexpr (cpp::is_same_v<UnqualT, LIBC_NAMESPACE::fputil::Float128>)
+    return FPType::IEEE754_Binary128;
   else if constexpr (cpp::is_same_v<UnqualT, bfloat16>)
     return FPType::BFloat16;
   else
diff --git a/libc/src/__support/FPUtil/float128.h b/libc/src/__support/FPUtil/float128.h
new file mode 100644
index 0000000000000..52b78b516b47a
--- /dev/null
+++ b/libc/src/__support/FPUtil/float128.h
@@ -0,0 +1,37 @@
+//===-- Float128 software wrapper ----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines a minimal software-backed Float128 wrapper type used when
+// the host compiler does not provide a native 128-bit floating-point type.
+// The wrapper currently only stores the raw 128-bit representation.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_FLOAT128_H
+#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_FLOAT128_H
+
+#include "src/__support/uint128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace fputil {
+
+class Float128 {
+private:
+  UInt128 bits_ = 0;
+
+public:
+    constexpr Float128() = default;
+
+    constexpr explicit Float128(UInt128 value) : bits_(value) {}
+
+    constexpr UInt128 get_bits() const { return bits_; }
+};
+} // namespace fputil
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_FLOAT128_H
diff --git a/libc/test/src/__support/FPUtil/fpbits_test.cpp b/libc/test/src/__support/FPUtil/fpbits_test.cpp
index 6953d3aace58f..26dbfa1741996 100644
--- a/libc/test/src/__support/FPUtil/fpbits_test.cpp
+++ b/libc/test/src/__support/FPUtil/fpbits_test.cpp
@@ -778,3 +778,39 @@ TEST(LlvmLibcFPBitsTest, Float128Type) {
   EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
 }
 #endif // LIBC_TYPES_HAS_FLOAT128
+
+TEST(LlvmLibcFPBitsTest, Float128WrapperInstantiation) {
+  using LIBC_NAMESPACE::fputil::Float128;
+  using Float128Bits = LIBC_NAMESPACE::fputil::FPBits<Float128>;
+
+  Float128 x;
+  Float128Bits bits(x);
+
+  EXPECT_TRUE(bits.is_zero());
+}
+
+TEST(LlvmLibcFPBitsTest, Float128BitsStorage) {
+  using LIBC_NAMESPACE::fputil::Float128;
+
+  UInt128 raw(123);
+  Float128 x(raw);
+
+  EXPECT_EQ(x.get_bits(), raw);
+}
+
+TEST(LlvmLibcFPBitsTest, Float128GetFPType) {
+  using LIBC_NAMESPACE::fputil::Float128;
+
+  constexpr auto type =
+      LIBC_NAMESPACE::fputil::get_fp_type<Float128>();
+
+  EXPECT_EQ(type,
+            LIBC_NAMESPACE::fputil::FPType::IEEE754_Binary128);
+}
+
+TEST(LlvmLibcFPBitsTest, Float128IsFloatingPoint) {
+  using LIBC_NAMESPACE::fputil::Float128;
+
+  EXPECT_TRUE(
+      LIBC_NAMESPACE::cpp::is_floating_point<Float128>::value);
+}
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/186938


More information about the libc-commits mailing list