[libc] Add FPBits support for Float128#186901
[libc] Add FPBits support for Float128#186901Emmaliu2006git wants to merge 5 commits intollvm:mainfrom
Conversation
Introduce a minimal fputil::Float128 type used as a fallback when no native float128 type is available.
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-libc Author: None (Emmaliu2006git) ChangesThis patch adds basic FPBits support for the software-backed fputil::Float128 wrapper introduced in the previous patch. Changes include:
This allows Float128 to integrate with the existing FPBits Full diff: https://github.com/llvm/llvm-project/pull/186901.diff 4 Files Affected:
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/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index ce4925bae125a..a64e6bc773658 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
@@ -825,7 +828,8 @@ template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
// It derives its functionality to FPRepImpl above.
template <typename T>
struct FPBits final : public internal::FPRepImpl<get_fp_type<T>(), FPBits<T>> {
- static_assert(cpp::is_floating_point_v<T>,
+ static_assert(cpp::is_floating_point_v<T>
+ ||cpp::is_same_v<T, LIBC_NAMESPACE::fputil::Float128>,
"FPBits instantiated with invalid type.");
using UP = internal::FPRepImpl<get_fp_type<T>(), FPBits<T>>;
using StorageType = typename UP::StorageType;
diff --git a/libc/src/__support/FPUtil/float128.h b/libc/src/__support/FPUtil/float128.h
new file mode 100644
index 0000000000000..a9ad68080b907
--- /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
\ No newline at end of file
diff --git a/libc/test/src/__support/FPUtil/fpbits_test.cpp b/libc/test/src/__support/FPUtil/fpbits_test.cpp
index 6953d3aace58f..99b86ff1a9c77 100644
--- a/libc/test/src/__support/FPUtil/fpbits_test.cpp
+++ b/libc/test/src/__support/FPUtil/fpbits_test.cpp
@@ -778,3 +778,9 @@ TEST(LlvmLibcFPBitsTest, Float128Type) {
EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
}
#endif // LIBC_TYPES_HAS_FLOAT128
+
+TEST(LlvmLibcFPBitsTest, Float128WrapperInstantiation) {
+ LIBC_NAMESPACE::fputil::Float128 x;
+ LIBC_NAMESPACE::fputil::FPBits<LIBC_NAMESPACE::fputil::Float128> bits(x);
+ EXPECT_TRUE(bits.is_zero());
+}
\ No newline at end of file
|
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions cpp,h -- libc/src/__support/FPUtil/float128.h libc/include/llvm-libc-types/float128.h libc/src/__support/FPUtil/FPBits.h libc/test/src/__support/FPUtil/fpbits_test.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index a64e6bc77..8ff63f42b 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -18,6 +18,7 @@
#include "hdr/stdint_proxy.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
+#include "src/__support/FPUtil/float128.h" //include float128.h
#include "src/__support/common.h"
#include "src/__support/libc_assert.h" // LIBC_ASSERT
#include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR
@@ -26,7 +27,6 @@
#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 {
@@ -828,8 +828,8 @@ template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
// It derives its functionality to FPRepImpl above.
template <typename T>
struct FPBits final : public internal::FPRepImpl<get_fp_type<T>(), FPBits<T>> {
- static_assert(cpp::is_floating_point_v<T>
- ||cpp::is_same_v<T, LIBC_NAMESPACE::fputil::Float128>,
+ static_assert(cpp::is_floating_point_v<T> ||
+ cpp::is_same_v<T, LIBC_NAMESPACE::fputil::Float128>,
"FPBits instantiated with invalid type.");
using UP = internal::FPRepImpl<get_fp_type<T>(), FPBits<T>>;
using StorageType = typename UP::StorageType;
diff --git a/libc/src/__support/FPUtil/float128.h b/libc/src/__support/FPUtil/float128.h
index a9ad68080..c5e71f4c9 100644
--- a/libc/src/__support/FPUtil/float128.h
+++ b/libc/src/__support/FPUtil/float128.h
@@ -25,11 +25,11 @@ private:
UInt128 bits_ = 0;
public:
- constexpr Float128() = default;
+ constexpr Float128() = default;
- constexpr explicit Float128(UInt128 value) : bits_(value) {}
+ constexpr explicit Float128(UInt128 value) : bits_(value) {}
- constexpr UInt128 get_bits() const { return bits_; }
+ constexpr UInt128 get_bits() const { return bits_; }
};
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
|
🐧 Linux x64 Test ResultsThe build failed before running any tests. Click on a failure below to see the details. libc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conjf.dir/conjf.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conjf16.dir/conjf16.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conj.__internal__.dir/conj.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conjl.dir/conjl.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.cprojl.dir/cprojl.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conjf16.__internal__.dir/conjf16.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.cproj.__internal__.dir/cproj.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.cprojf16.dir/cprojf16.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.cprojf128.__internal__.dir/cprojf128.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conjf.__internal__.dir/conjf.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.cproj.dir/cproj.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.cprojf128.dir/cprojf128.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conj.dir/conj.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conjl.__internal__.dir/conjl.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.cprojl.__internal__.dir/cprojl.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.cprojf16.__internal__.dir/cprojf16.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conjf128.__internal__.dir/conjf128.cpp.olibc/src/complex/generic/CMakeFiles/libc.src.complex.generic.conjf128.dir/conjf128.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.issignalingl.dir/issignalingl.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.issignaling.__internal__.dir/issignaling.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.issignalingf.__internal__.dir/issignalingf.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.iscanonicall.__internal__.dir/iscanonicall.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.iscanonicall.dir/iscanonicall.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.iscanonical.__internal__.dir/iscanonical.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.iscanonicalf.dir/iscanonicalf.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.issignaling.dir/issignaling.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.iscanonical.dir/iscanonical.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.iscanonicalf.__internal__.dir/iscanonicalf.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.issignalingl.__internal__.dir/issignalingl.cpp.olibc/src/math/generic/CMakeFiles/libc.src.math.generic.issignalingf.dir/issignalingf.cpp.oIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
This patch adds basic FPBits support for the software-backed fputil::Float128 wrapper introduced in the previous patch.
Changes include:
This allows Float128 to integrate with the existing FPBits
infrastructure used throughout libc's floating-point utilities.