https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/212652
>From 71739c11296a27651aacd05c47c9e7858e83d151 Mon Sep 17 00:00:00 2001 From: hulxv <[email protected]> Date: Wed, 29 Jul 2026 01:27:13 +0300 Subject: [PATCH 1/2] [compiler-rt][builtins] libc-backed double-float comparison builtins --- compiler-rt/lib/builtins/CMakeLists.txt | 1 + compiler-rt/lib/builtins/comparedf2.cpp | 54 ++++++++++++++++++++++ libc/shared/builtins.h | 3 ++ libc/shared/builtins/gedf2.h | 29 ++++++++++++ libc/shared/builtins/ledf2.h | 29 ++++++++++++ libc/shared/builtins/unorddf2.h | 29 ++++++++++++ libc/src/__support/builtins/CMakeLists.txt | 27 +++++++++++ libc/src/__support/builtins/gedf2.h | 30 ++++++++++++ libc/src/__support/builtins/ledf2.h | 30 ++++++++++++ libc/src/__support/builtins/unorddf2.h | 30 ++++++++++++ libc/test/shared/CMakeLists.txt | 3 ++ libc/test/shared/shared_builtins_test.cpp | 15 ++++++ 12 files changed, 280 insertions(+) create mode 100644 compiler-rt/lib/builtins/comparedf2.cpp create mode 100644 libc/shared/builtins/gedf2.h create mode 100644 libc/shared/builtins/ledf2.h create mode 100644 libc/shared/builtins/unorddf2.h create mode 100644 libc/src/__support/builtins/gedf2.h create mode 100644 libc/src/__support/builtins/ledf2.h create mode 100644 libc/src/__support/builtins/unorddf2.h diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt index 2dd2935882143..664eea84adb3c 100644 --- a/compiler-rt/lib/builtins/CMakeLists.txt +++ b/compiler-rt/lib/builtins/CMakeLists.txt @@ -288,6 +288,7 @@ if(COMPILER_RT_USE_LIBC_MATH) use_libc_builtin(GENERIC_SOURCES adddf3) use_libc_builtin(GENERIC_SOURCES addsf3) use_libc_builtin(GENERIC_TF_SOURCES addtf3) + use_libc_builtin(GENERIC_SOURCES comparedf2) use_libc_builtin(GENERIC_SOURCES comparesf2) use_libc_builtin(GENERIC_SOURCES divdf3) use_libc_builtin(GENERIC_SOURCES divsf3) 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 592ba3589dc47..dc66b94274ed5 100644 --- a/libc/shared/builtins.h +++ b/libc/shared/builtins.h @@ -51,7 +51,9 @@ #include "builtins/floatunsisf.h" #include "builtins/floatuntidf.h" #include "builtins/floatuntisf.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" @@ -65,6 +67,7 @@ #include "builtins/trunctfdf2.h" #include "builtins/trunctfsf2.h" #include "builtins/trunctfxf2.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 d057f4e03dd1c..09978920d3a41 100644 --- a/libc/src/__support/builtins/CMakeLists.txt +++ b/libc/src/__support/builtins/CMakeLists.txt @@ -515,3 +515,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 681f17de5b9ec..7a5b6453ca8ff 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -862,7 +862,9 @@ add_fp_unittest( libc.src.__support.builtins.floatunsisf libc.src.__support.builtins.floatuntidf libc.src.__support.builtins.floatuntisf + 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 @@ -877,6 +879,7 @@ add_fp_unittest( libc.src.__support.builtins.trunctfsf2 libc.src.__support.builtins.trunctfxf2 libc.src.__support.uint128 + 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 4c3f825187227..9c94786fe52e6 100644 --- a/libc/test/shared/shared_builtins_test.cpp +++ b/libc/test/shared/shared_builtins_test.cpp @@ -76,3 +76,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)); +} >From 47ad5f115840c9397672ea3b4b1387f4a5be2de6 Mon Sep 17 00:00:00 2001 From: hulxv <[email protected]> Date: Wed, 12 Aug 2026 09:34:22 +0300 Subject: [PATCH 2/2] if defined -> ifdef --- compiler-rt/lib/builtins/comparedf2.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler-rt/lib/builtins/comparedf2.cpp b/compiler-rt/lib/builtins/comparedf2.cpp index d794c2dceeee4..9f71eca4eb237 100644 --- a/compiler-rt/lib/builtins/comparedf2.cpp +++ b/compiler-rt/lib/builtins/comparedf2.cpp @@ -27,7 +27,8 @@ extern "C" { COMPILER_RT_ABI CMP_RESULT __ledf2(fp_t a, fp_t b) { return LIBC_NAMESPACE::shared::ledf2(a, b); } -#if defined(__ELF__) + +#ifdef __ELF__ COMPILER_RT_ALIAS(__ledf2, __cmpdf2) #endif COMPILER_RT_ALIAS(__ledf2, __eqdf2) @@ -43,8 +44,8 @@ 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) +#ifdef __ARM_EABI__ +#ifdef 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) _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
