[libc-commits] [libc] [libc] Enables MPFR for float80 and adds tests for it (PR #223800)
via libc-commits
libc-commits at lists.llvm.org
Tue Sep 15 12:49:19 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Zorojuro (Sukumarsawant)
<details>
<summary>Changes</summary>
This temporarily only enables them where its natively avaialble i.e where long double is float80 (x86)
---
Full diff: https://github.com/llvm/llvm-project/pull/223800.diff
5 Files Affected:
- (modified) libc/test/src/__support/FPUtil/CMakeLists.txt (+1)
- (modified) libc/test/src/__support/FPUtil/float80_test.cpp (+17)
- (modified) libc/utils/MPFRWrapper/CMakeLists.txt (+1)
- (modified) libc/utils/MPFRWrapper/MPCommon.cpp (+7)
- (modified) libc/utils/MPFRWrapper/MPCommon.h (+14-3)
``````````diff
diff --git a/libc/test/src/__support/FPUtil/CMakeLists.txt b/libc/test/src/__support/FPUtil/CMakeLists.txt
index b5d9ca2331697..994cc1fef4d0b 100644
--- a/libc/test/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/test/src/__support/FPUtil/CMakeLists.txt
@@ -54,6 +54,7 @@ add_fp_unittest(
add_fp_unittest(
float80_test
+ NEED_MPFR
SUITE
libc-fputil-tests
SRCS
diff --git a/libc/test/src/__support/FPUtil/float80_test.cpp b/libc/test/src/__support/FPUtil/float80_test.cpp
index c59be71240b9d..e9850caa219f2 100644
--- a/libc/test/src/__support/FPUtil/float80_test.cpp
+++ b/libc/test/src/__support/FPUtil/float80_test.cpp
@@ -11,6 +11,7 @@
#include "src/__support/FPUtil/float80.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPCommon.h"
using LIBC_NAMESPACE::Sign;
using LIBC_NAMESPACE::fputil::Float80;
@@ -134,6 +135,22 @@ TEST(LlvmLibcFloat80Test, randomTest) {
}
}
+TEST(LlvmLibcFloat80Test, MPFRConversion) {
+ using LIBC_NAMESPACE::testing::mpfr::MPFRNumber;
+
+ Float80 values[] = {
+ Float80(0.0f), Float80(-0.0f), Float80(1.0f),
+ Float80(-1.0f), Float80(2.0f), Float80(0.5f),
+ Float80(100.0), Float80(12345.6789), Float80(1e10f),
+ };
+
+ for (Float80 val : values) {
+ MPFRNumber mpfr_val(val);
+ Float80 result = mpfr_val.as<Float80>();
+ EXPECT_TRUE(val == result);
+ }
+}
+
#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
TEST(LlvmLibcFloat80Test, FromIntegralTypes) {
diff --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index 8efa6fc1f0b12..53ad5ef4aa48c 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -16,6 +16,7 @@ if(LIBC_TESTS_CAN_USE_MPFR OR LIBC_TESTS_CAN_USE_MPC)
libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.float80
libc.src.__support.FPUtil.fp_bits
libc.test.UnitTest.FPTestHelpers
)
diff --git a/libc/utils/MPFRWrapper/MPCommon.cpp b/libc/utils/MPFRWrapper/MPCommon.cpp
index 2422bcf45222f..2901bed391416 100644
--- a/libc/utils/MPFRWrapper/MPCommon.cpp
+++ b/libc/utils/MPFRWrapper/MPCommon.cpp
@@ -11,6 +11,7 @@
#include "src/__support/CPP/string_view.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/float80.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
@@ -646,6 +647,12 @@ template <> float128 MPFRNumber::as<float128>() const {
}
#endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
+#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+template <> float80 MPFRNumber::as<float80>() const {
+ return fputil::cast<float80>(mpfr_get_ld(value, mpfr_rounding));
+}
+#endif
+
template <> bfloat16 MPFRNumber::as<bfloat16>() const {
return fputil::cast<bfloat16>(mpfr_get_flt(value, mpfr_rounding));
}
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index 38fd15fcc956c..07d1228724ccc 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -24,7 +24,7 @@ extern "C" {
int mpfr_set_float128(mpfr_ptr, float128, mpfr_rnd_t);
float128 mpfr_get_float128(mpfr_srcptr, mpfr_rnd_t);
}
-#endif
+#endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
namespace LIBC_NAMESPACE_DECL {
namespace testing {
@@ -41,7 +41,7 @@ template <typename T> struct ExtraPrecision;
template <> struct ExtraPrecision<float16> {
static constexpr unsigned int VALUE = 128;
};
-#endif
+#endif // LIBC_TYPES_HAS_FLOAT16
template <> struct ExtraPrecision<float> {
static constexpr unsigned int VALUE = 128;
@@ -65,6 +65,12 @@ template <> struct ExtraPrecision<float128> {
};
#endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
+#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+template <> struct ExtraPrecision<float80> {
+ static constexpr unsigned int VALUE = 256;
+};
+#endif
+
template <> struct ExtraPrecision<bfloat16> {
static constexpr unsigned int VALUE = 64;
};
@@ -139,7 +145,12 @@ class MPFRNumber {
}
template <typename XType,
- cpp::enable_if_t<cpp::is_same_v<long double, XType>, int> = 0>
+ cpp::enable_if_t<cpp::is_same_v<long double, XType>
+#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+ || cpp::is_same_v<float80, XType>
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+ ,
+ int> = 0>
explicit MPFRNumber(XType x,
unsigned int precision = ExtraPrecision<XType>::VALUE,
RoundingMode rounding = RoundingMode::Nearest)
``````````
</details>
https://github.com/llvm/llvm-project/pull/223800
More information about the libc-commits
mailing list