[libc-commits] [libc] [libc] Add float128 fallback support and integrate with FPBits (PR #187299)

via libc-commits libc-commits at lists.llvm.org
Wed Mar 18 08:22:15 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: None (Emmaliu2006git)

<details>
<summary>Changes</summary>

This patch introduces a fallback implementation of float128 and integrates it with libc's FPBits and type traits infrastructure.

Motivation (GSoC math support for float128) : 
Not all platforms or compilers provide native support for float128. This prevents building and testing float128-based
math functionality in a portable way. This patch provides a minimal fallback representation to enable compilation and basic functionality.

Changes:
- Add a fallback float128 type in FPUtil as a trivially copyable struct backed by UInt128.
- Enable float128 in llvm-libc-types/float128.h for all configurations by defining LIBC_TYPES_HAS_FLOAT128 and aliasing to the fallback type when necessary.
- Add basic unit tests to validate:
  * is_floating_point behavior
  * FPBits construction and classification
  * special values (inf, NaN)

Notes:
- This patch focuses on type plumbing and basic integration only.
- Arithmetic support for the fallback type is not included and will be handled in future patches.

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


3 Files Affected:

- (modified) libc/include/llvm-libc-types/float128.h (+4) 
- (added) libc/src/__support/FPUtil/float128.h (+35) 
- (modified) libc/test/src/__support/FPUtil/fpbits_test.cpp (+54) 


``````````diff
diff --git a/libc/include/llvm-libc-types/float128.h b/libc/include/llvm-libc-types/float128.h
index 82ebb79f1f580..a7ae7d80f9d92 100644
--- a/libc/include/llvm-libc-types/float128.h
+++ b/libc/include/llvm-libc-types/float128.h
@@ -31,6 +31,10 @@ typedef __float128 float128;
 #elif (LDBL_MANT_DIG == 113)
 #define LIBC_TYPES_HAS_FLOAT128
 typedef long double float128;
+#else
+#define LIBC_TYPES_HAS_FLOAT128
+#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/FPUtil/float128.h b/libc/src/__support/FPUtil/float128.h
new file mode 100644
index 0000000000000..a0420679f439b
--- /dev/null
+++ b/libc/src/__support/FPUtil/float128.h
@@ -0,0 +1,35 @@
+//===-- 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 {
+
+struct Float128 {
+  UInt128 bits = 0;
+
+  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..a2bb1cdca9ca0 100644
--- a/libc/test/src/__support/FPUtil/fpbits_test.cpp
+++ b/libc/test/src/__support/FPUtil/fpbits_test.cpp
@@ -778,3 +778,57 @@ TEST(LlvmLibcFPBitsTest, Float128Type) {
   EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
 }
 #endif // LIBC_TYPES_HAS_FLOAT128
+
+//===----------------------------------------------------------------------===//
+// Float128 Tests
+//
+// These tests validate the basic integration of the float128 type with:
+//   - type traits (is_floating_point)
+//   - FPBits functionality
+//
+// The goal is to ensure that both the type alias (float128) and the fallback
+// implementation behave consistently with other floating-point types.
+//===----------------------------------------------------------------------===//
+
+// Test: float128 is recognized as a floating-point type.
+TEST(LlvmLibcTypeTraitsTest, Float128IsFloatingPoint) {
+  using LIBC_NAMESPACE::cpp::is_floating_point_v;
+
+  EXPECT_TRUE(is_floating_point_v<float128>);
+}
+
+// Test: Basic FPBits usage with float128 default initialization.
+// Verifies zero initialization and basic classification APIs.
+TEST(LlvmLibcFPBitsTest, Float128BasicUsage) {
+  using LIBC_NAMESPACE::fputil::FPBits;
+
+  float128 x{}; // Default-initialized to zero
+  FPBits<float128> bits(x);
+
+  EXPECT_TRUE(bits.is_zero());
+  EXPECT_TRUE(bits.is_finite());
+  EXPECT_FALSE(bits.is_nan());
+  EXPECT_FALSE(bits.is_inf());
+}
+
+// Test: Construct FPBits<float128> directly from raw bits.
+// Verifies that zero bit pattern is interpreted correctly.
+TEST(LlvmLibcFPBitsTest, Float128FromBits) {
+  using LIBC_NAMESPACE::fputil::FPBits;
+  UInt128 raw = 0;
+  FPBits<float128> bits(raw);
+
+  EXPECT_TRUE(bits.is_zero());
+}
+
+// Test: Special values (infinity and NaN) for float128.
+// Ensures FPBits builders work correctly for float128.
+TEST(LlvmLibcFPBitsTest, Float128SpecialValues) {
+  using LIBC_NAMESPACE::fputil::FPBits;
+
+  auto inf = FPBits<float128>::inf();
+  EXPECT_TRUE(inf.is_inf());
+
+  auto nan = FPBits<float128>::quiet_nan();
+  EXPECT_TRUE(nan.is_nan());
+}
\ No newline at end of file

``````````

</details>


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


More information about the libc-commits mailing list