[llvm-branch-commits] [compiler-rt] [libc] [compiler-rt][builtins] libc-backed single-float comparison builtins (PR #212651)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jul 28 17:01:55 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: hulxv (hulxv)
<details>
<summary>Changes</summary>
libc-backed single-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/212651.diff
12 Files Affected:
- (modified) compiler-rt/lib/builtins/CMakeLists.txt (+1)
- (added) compiler-rt/lib/builtins/comparesf2.cpp (+54)
- (modified) libc/shared/builtins.h (+3)
- (added) libc/shared/builtins/gesf2.h (+29)
- (added) libc/shared/builtins/lesf2.h (+29)
- (added) libc/shared/builtins/unordsf2.h (+29)
- (modified) libc/src/__support/builtins/CMakeLists.txt (+27)
- (added) libc/src/__support/builtins/gesf2.h (+30)
- (added) libc/src/__support/builtins/lesf2.h (+30)
- (added) libc/src/__support/builtins/unordsf2.h (+30)
- (modified) libc/test/shared/CMakeLists.txt (+3)
- (modified) libc/test/shared/shared_builtins_test.cpp (+17)
``````````diff
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index ea21c36d3d31f..bc23d511eec0b 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 comparesf2)
endif()
option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
diff --git a/compiler-rt/lib/builtins/comparesf2.cpp b/compiler-rt/lib/builtins/comparesf2.cpp
new file mode 100644
index 0000000000000..4db28588f4afc
--- /dev/null
+++ b/compiler-rt/lib/builtins/comparesf2.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 float comparison routines
+/// (__lesf2/__gesf2/__unordsf2 and their aliases) on top of LLVM-libc's shared
+/// comparison builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#define SINGLE_PRECISION
+#include "fp_lib.h"
+
+#include "fp_compare_impl.inc"
+#include "fp_libc_config.h"
+#include "shared/builtins/gesf2.h"
+#include "shared/builtins/lesf2.h"
+#include "shared/builtins/unordsf2.h"
+
+extern "C" {
+
+COMPILER_RT_ABI CMP_RESULT __lesf2(fp_t a, fp_t b) {
+ return LIBC_NAMESPACE::shared::lesf2(a, b);
+}
+#if defined(__ELF__)
+COMPILER_RT_ALIAS(__lesf2, __cmpsf2)
+#endif
+COMPILER_RT_ALIAS(__lesf2, __eqsf2)
+COMPILER_RT_ALIAS(__lesf2, __ltsf2)
+COMPILER_RT_ALIAS(__lesf2, __nesf2)
+
+COMPILER_RT_ABI CMP_RESULT __gesf2(fp_t a, fp_t b) {
+ return LIBC_NAMESPACE::shared::gesf2(a, b);
+}
+COMPILER_RT_ALIAS(__gesf2, __gtsf2)
+
+COMPILER_RT_ABI CMP_RESULT __unordsf2(fp_t a, fp_t b) {
+ return LIBC_NAMESPACE::shared::unordsf2(a, b);
+}
+
+#if defined(__ARM_EABI__)
+#if defined(COMPILER_RT_ARMHF_TARGET)
+AEABI_RTABI int __aeabi_fcmpun(fp_t a, fp_t b) { return __unordsf2(a, b); }
+#else
+COMPILER_RT_ALIAS(__unordsf2, __aeabi_fcmpun)
+#endif
+#endif
+
+} // extern "C"
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
index ced054cb02582..c2ad9d5340c96 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -23,11 +23,14 @@
#include "builtins/divdf3.h"
#include "builtins/divsf3.h"
#include "builtins/divtf3.h"
+#include "builtins/gesf2.h"
+#include "builtins/lesf2.h"
#include "builtins/muldf3.h"
#include "builtins/mulsf3.h"
#include "builtins/multf3.h"
#include "builtins/subdf3.h"
#include "builtins/subsf3.h"
#include "builtins/subtf3.h"
+#include "builtins/unordsf2.h"
#endif // LLVM_LIBC_SHARED_BUILTINS_H
diff --git a/libc/shared/builtins/gesf2.h b/libc/shared/builtins/gesf2.h
new file mode 100644
index 0000000000000..81b0d3972d280
--- /dev/null
+++ b/libc/shared/builtins/gesf2.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 __gesf2 implementation as shared::gesf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_GESF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_GESF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/gesf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::gesf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_GESF2_H
diff --git a/libc/shared/builtins/lesf2.h b/libc/shared/builtins/lesf2.h
new file mode 100644
index 0000000000000..3fec6d3f0366f
--- /dev/null
+++ b/libc/shared/builtins/lesf2.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 __lesf2 implementation as shared::lesf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_LESF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_LESF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/lesf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::lesf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_LESF2_H
diff --git a/libc/shared/builtins/unordsf2.h b/libc/shared/builtins/unordsf2.h
new file mode 100644
index 0000000000000..eb2c4d4c0483e
--- /dev/null
+++ b/libc/shared/builtins/unordsf2.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 __unordsf2 implementation as
+/// shared::unordsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_UNORDSF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_UNORDSF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/unordsf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::unordsf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_UNORDSF2_H
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index b54c332f2ed9c..a1978bc7ed292 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -120,3 +120,30 @@ add_header_library(
libc.src.__support.macros.attributes
libc.src.__support.macros.config
)
+
+add_header_library(
+ lesf2
+ HDRS
+ lesf2.h
+ DEPENDS
+ libc.src.__support.builtins.cmp_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ gesf2
+ HDRS
+ gesf2.h
+ DEPENDS
+ libc.src.__support.builtins.cmp_helper
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ unordsf2
+ HDRS
+ unordsf2.h
+ DEPENDS
+ libc.src.__support.builtins.cmp_helper
+ libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/builtins/gesf2.h b/libc/src/__support/builtins/gesf2.h
new file mode 100644
index 0000000000000..14523acce5be7
--- /dev/null
+++ b/libc/src/__support/builtins/gesf2.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 __gesf2 implementation as builtins::gesf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_GESF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_GESF2_H
+
+#include "src/__support/builtins/cmp_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Greater-equal comparison of float; mirrors compiler-rt's __gesf2.
+LIBC_INLINE int gesf2(float a, float b) { return cmp_ge(a, b); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_GESF2_H
diff --git a/libc/src/__support/builtins/lesf2.h b/libc/src/__support/builtins/lesf2.h
new file mode 100644
index 0000000000000..8358198d90df1
--- /dev/null
+++ b/libc/src/__support/builtins/lesf2.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 __lesf2 implementation as builtins::lesf2 so
+/// that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_LESF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_LESF2_H
+
+#include "src/__support/builtins/cmp_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Less-equal comparison of float; mirrors compiler-rt's __lesf2.
+LIBC_INLINE int lesf2(float a, float b) { return cmp_le(a, b); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_LESF2_H
diff --git a/libc/src/__support/builtins/unordsf2.h b/libc/src/__support/builtins/unordsf2.h
new file mode 100644
index 0000000000000..23268413863e5
--- /dev/null
+++ b/libc/src/__support/builtins/unordsf2.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 __unordsf2 implementation as
+/// builtins::unordsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDSF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDSF2_H
+
+#include "src/__support/builtins/cmp_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Unordered comparison of float; mirrors compiler-rt's __unordsf2.
+LIBC_INLINE int unordsf2(float a, float b) { return cmp_unord(a, b); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDSF2_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9208dbcc5ade6..bfe79a2d912cf 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -834,12 +834,15 @@ add_fp_unittest(
libc.src.__support.builtins.divdf3
libc.src.__support.builtins.divsf3
libc.src.__support.builtins.divtf3
+ libc.src.__support.builtins.gesf2
+ libc.src.__support.builtins.lesf2
libc.src.__support.builtins.muldf3
libc.src.__support.builtins.mulsf3
libc.src.__support.builtins.multf3
libc.src.__support.builtins.subdf3
libc.src.__support.builtins.subsf3
libc.src.__support.builtins.subtf3
+ libc.src.__support.builtins.unordsf2
)
add_fp_unittest(
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index dac84345eade1..f2b0edaa7751c 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -10,6 +10,8 @@
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
+namespace shared = LIBC_NAMESPACE::shared;
+
TEST(LlvmLibcSharedBuiltinsTest, AllFloat) {
EXPECT_FP_EQ(3.0f, LIBC_NAMESPACE::shared::addsf3(1.0f, 2.0f));
EXPECT_FP_EQ(3.0f, LIBC_NAMESPACE::shared::divsf3(6.0f, 2.0f));
@@ -38,3 +40,18 @@ TEST(LlvmLibcSharedBuiltinsTest, AllFloat128) {
}
#endif // LIBC_TYPES_HAS_FLOAT128
+
+TEST(LlvmLibcSharedBuiltinsTest, SingleCompare) {
+ const float aNaN =
+ LIBC_NAMESPACE::fputil::FPBits<float>::quiet_nan().get_val();
+ EXPECT_EQ(-1, shared::gesf2(1.0f, 2.0f));
+ EXPECT_EQ(0, shared::gesf2(1.0f, 1.0f));
+ EXPECT_EQ(1, shared::gesf2(2.0f, 1.0f));
+ EXPECT_EQ(-1, shared::gesf2(aNaN, 1.0f));
+ EXPECT_EQ(-1, shared::lesf2(1.0f, 2.0f));
+ EXPECT_EQ(0, shared::lesf2(1.0f, 1.0f));
+ EXPECT_EQ(1, shared::lesf2(2.0f, 1.0f));
+ EXPECT_EQ(1, shared::lesf2(aNaN, 1.0f));
+ EXPECT_EQ(0, shared::unordsf2(1.0f, 2.0f));
+ EXPECT_EQ(1, shared::unordsf2(aNaN, 1.0f));
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/212651
More information about the llvm-branch-commits
mailing list