[llvm-branch-commits] [compiler-rt] [libc] [compiler-rt][builtins] libc-backed double-float comparison builtins (PR #212652)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jul 28 16:59:42 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: hulxv (hulxv)
<details>
<summary>Changes</summary>
libc-backed double-float comparison builtins
Part of #<!-- -->197824
<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
---
Full diff: https://github.com/llvm/llvm-project/pull/212652.diff
12 Files Affected:
- (modified) compiler-rt/lib/builtins/CMakeLists.txt (+1)
- (added) compiler-rt/lib/builtins/comparedf2.cpp (+54)
- (modified) libc/shared/builtins.h (+3)
- (added) libc/shared/builtins/gedf2.h (+29)
- (added) libc/shared/builtins/ledf2.h (+29)
- (added) libc/shared/builtins/unorddf2.h (+29)
- (modified) libc/src/__support/builtins/CMakeLists.txt (+27)
- (added) libc/src/__support/builtins/gedf2.h (+30)
- (added) libc/src/__support/builtins/ledf2.h (+30)
- (added) libc/src/__support/builtins/unorddf2.h (+30)
- (modified) libc/test/shared/CMakeLists.txt (+3)
- (modified) libc/test/shared/shared_builtins_test.cpp (+15)
``````````diff
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index bc23d511eec0b..ac84ff4baebba 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -265,6 +265,7 @@ if(COMPILER_RT_USE_LIBC_MATH)
add_definitions(-DCOMPILER_RT_USE_LIBC_MATH)
use_libc_builtin(GENERIC_TF_SOURCES addtf3)
+ use_libc_builtin(GENERIC_SOURCES comparedf2)
use_libc_builtin(GENERIC_SOURCES comparesf2)
endif()
diff --git a/compiler-rt/lib/builtins/comparedf2.cpp b/compiler-rt/lib/builtins/comparedf2.cpp
new file mode 100644
index 0000000000000..d794c2dceeee4
--- /dev/null
+++ b/compiler-rt/lib/builtins/comparedf2.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file implements compiler-rt's double comparison routines
+/// (__ledf2/__gedf2/__unorddf2 and their aliases) on top of LLVM-libc's shared
+/// comparison builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#define DOUBLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_compare_impl.inc"
+#include "fp_libc_config.h"
+#include "shared/builtins/gedf2.h"
+#include "shared/builtins/ledf2.h"
+#include "shared/builtins/unorddf2.h"
+
+extern "C" {
+
+COMPILER_RT_ABI CMP_RESULT __ledf2(fp_t a, fp_t b) {
+ return LIBC_NAMESPACE::shared::ledf2(a, b);
+}
+#if defined(__ELF__)
+COMPILER_RT_ALIAS(__ledf2, __cmpdf2)
+#endif
+COMPILER_RT_ALIAS(__ledf2, __eqdf2)
+COMPILER_RT_ALIAS(__ledf2, __ltdf2)
+COMPILER_RT_ALIAS(__ledf2, __nedf2)
+
+COMPILER_RT_ABI CMP_RESULT __gedf2(fp_t a, fp_t b) {
+ return LIBC_NAMESPACE::shared::gedf2(a, b);
+}
+COMPILER_RT_ALIAS(__gedf2, __gtdf2)
+
+COMPILER_RT_ABI CMP_RESULT __unorddf2(fp_t a, fp_t b) {
+ return LIBC_NAMESPACE::shared::unorddf2(a, b);
+}
+
+#if defined(__ARM_EABI__)
+#if defined(COMPILER_RT_ARMHF_TARGET)
+AEABI_RTABI int __aeabi_dcmpun(fp_t a, fp_t b) { return __unorddf2(a, b); }
+#else
+COMPILER_RT_ALIAS(__unorddf2, __aeabi_dcmpun)
+#endif
+#endif
+
+} // extern "C"
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
index c2ad9d5340c96..0ff85b0b24ecb 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -23,7 +23,9 @@
#include "builtins/divdf3.h"
#include "builtins/divsf3.h"
#include "builtins/divtf3.h"
+#include "builtins/gedf2.h"
#include "builtins/gesf2.h"
+#include "builtins/ledf2.h"
#include "builtins/lesf2.h"
#include "builtins/muldf3.h"
#include "builtins/mulsf3.h"
@@ -31,6 +33,7 @@
#include "builtins/subdf3.h"
#include "builtins/subsf3.h"
#include "builtins/subtf3.h"
+#include "builtins/unorddf2.h"
#include "builtins/unordsf2.h"
#endif // LLVM_LIBC_SHARED_BUILTINS_H
diff --git a/libc/shared/builtins/gedf2.h b/libc/shared/builtins/gedf2.h
new file mode 100644
index 0000000000000..ee8714993d8a2
--- /dev/null
+++ b/libc/shared/builtins/gedf2.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This header exposes LLVM-libc's __gedf2 implementation as shared::gedf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_GEDF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_GEDF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/gedf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::gedf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_GEDF2_H
diff --git a/libc/shared/builtins/ledf2.h b/libc/shared/builtins/ledf2.h
new file mode 100644
index 0000000000000..f57f4f2a4d271
--- /dev/null
+++ b/libc/shared/builtins/ledf2.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This header exposes LLVM-libc's __ledf2 implementation as shared::ledf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_LEDF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_LEDF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/ledf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::ledf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_LEDF2_H
diff --git a/libc/shared/builtins/unorddf2.h b/libc/shared/builtins/unorddf2.h
new file mode 100644
index 0000000000000..a8db77c51415a
--- /dev/null
+++ b/libc/shared/builtins/unorddf2.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This header exposes LLVM-libc's __unorddf2 implementation as
+/// shared::unorddf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_UNORDDF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_UNORDDF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/unorddf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::unorddf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_UNORDDF2_H
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index a1978bc7ed292..7760c741e59d5 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -147,3 +147,30 @@ add_header_library(
libc.src.__support.builtins.cmp_helper
libc.src.__support.macros.config
)
+
+add_header_library(
+ ledf2
+ HDRS
+ ledf2.h
+ DEPENDS
+ libc.src.__support.builtins.cmp_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ gedf2
+ HDRS
+ gedf2.h
+ DEPENDS
+ libc.src.__support.builtins.cmp_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ unorddf2
+ HDRS
+ unorddf2.h
+ DEPENDS
+ libc.src.__support.builtins.cmp_helper
+ libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/builtins/gedf2.h b/libc/src/__support/builtins/gedf2.h
new file mode 100644
index 0000000000000..710d6e0e71860
--- /dev/null
+++ b/libc/src/__support/builtins/gedf2.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This header exposes LLVM-libc's __gedf2 implementation as builtins::gedf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_GEDF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_GEDF2_H
+
+#include "src/__support/builtins/cmp_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Greater-equal comparison of double; mirrors compiler-rt's __gedf2.
+LIBC_INLINE int gedf2(double a, double b) { return cmp_ge(a, b); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_GEDF2_H
diff --git a/libc/src/__support/builtins/ledf2.h b/libc/src/__support/builtins/ledf2.h
new file mode 100644
index 0000000000000..d96b02943384e
--- /dev/null
+++ b/libc/src/__support/builtins/ledf2.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This header exposes LLVM-libc's __ledf2 implementation as builtins::ledf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_LEDF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_LEDF2_H
+
+#include "src/__support/builtins/cmp_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Less-equal comparison of double; mirrors compiler-rt's __ledf2.
+LIBC_INLINE int ledf2(double a, double b) { return cmp_le(a, b); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_LEDF2_H
diff --git a/libc/src/__support/builtins/unorddf2.h b/libc/src/__support/builtins/unorddf2.h
new file mode 100644
index 0000000000000..2d4f5f3f1fcc1
--- /dev/null
+++ b/libc/src/__support/builtins/unorddf2.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This header exposes LLVM-libc's __unorddf2 implementation as
+/// builtins::unorddf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDDF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDDF2_H
+
+#include "src/__support/builtins/cmp_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Unordered comparison of double; mirrors compiler-rt's __unorddf2.
+LIBC_INLINE int unorddf2(double a, double b) { return cmp_unord(a, b); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDDF2_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index bfe79a2d912cf..a1a0138e46d7e 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -834,7 +834,9 @@ add_fp_unittest(
libc.src.__support.builtins.divdf3
libc.src.__support.builtins.divsf3
libc.src.__support.builtins.divtf3
+ libc.src.__support.builtins.gedf2
libc.src.__support.builtins.gesf2
+ libc.src.__support.builtins.ledf2
libc.src.__support.builtins.lesf2
libc.src.__support.builtins.muldf3
libc.src.__support.builtins.mulsf3
@@ -842,6 +844,7 @@ add_fp_unittest(
libc.src.__support.builtins.subdf3
libc.src.__support.builtins.subsf3
libc.src.__support.builtins.subtf3
+ libc.src.__support.builtins.unorddf2
libc.src.__support.builtins.unordsf2
)
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index f2b0edaa7751c..8b3f2a490bb4a 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -55,3 +55,18 @@ TEST(LlvmLibcSharedBuiltinsTest, SingleCompare) {
EXPECT_EQ(0, shared::unordsf2(1.0f, 2.0f));
EXPECT_EQ(1, shared::unordsf2(aNaN, 1.0f));
}
+
+TEST_F(LlvmLibcSharedBuiltinsTest, DoubleCompare) {
+ const double aNaN =
+ LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan().get_val();
+ EXPECT_EQ(-1, shared::gedf2(1.0, 2.0));
+ EXPECT_EQ(0, shared::gedf2(1.0, 1.0));
+ EXPECT_EQ(1, shared::gedf2(2.0, 1.0));
+ EXPECT_EQ(-1, shared::gedf2(aNaN, 1.0));
+ EXPECT_EQ(-1, shared::ledf2(1.0, 2.0));
+ EXPECT_EQ(0, shared::ledf2(1.0, 1.0));
+ EXPECT_EQ(1, shared::ledf2(2.0, 1.0));
+ EXPECT_EQ(1, shared::ledf2(aNaN, 1.0));
+ EXPECT_EQ(0, shared::unorddf2(1.0, 2.0));
+ EXPECT_EQ(1, shared::unorddf2(aNaN, 1.0));
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/212652
More information about the llvm-branch-commits
mailing list