https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/212653
>From 43d6d04caaa17b87151dc99def2d1f798a4bea3c Mon Sep 17 00:00:00 2001 From: hulxv <[email protected]> Date: Wed, 29 Jul 2026 01:29:11 +0300 Subject: [PATCH 1/3] [compiler-rt][builtins] libc-backed quad-float comparison builtins --- compiler-rt/lib/builtins/CMakeLists.txt | 1 + compiler-rt/lib/builtins/comparetf2.cpp | 49 ++++++++++++++++++++++ libc/shared/builtins.h | 3 ++ libc/shared/builtins/getf2.h | 35 ++++++++++++++++ libc/shared/builtins/letf2.h | 35 ++++++++++++++++ libc/shared/builtins/unordtf2.h | 35 ++++++++++++++++ libc/src/__support/builtins/CMakeLists.txt | 30 +++++++++++++ libc/src/__support/builtins/getf2.h | 36 ++++++++++++++++ libc/src/__support/builtins/letf2.h | 36 ++++++++++++++++ libc/src/__support/builtins/unordtf2.h | 36 ++++++++++++++++ libc/test/shared/CMakeLists.txt | 3 ++ libc/test/shared/shared_builtins_test.cpp | 22 ++++++++++ 12 files changed, 321 insertions(+) create mode 100644 compiler-rt/lib/builtins/comparetf2.cpp create mode 100644 libc/shared/builtins/getf2.h create mode 100644 libc/shared/builtins/letf2.h create mode 100644 libc/shared/builtins/unordtf2.h create mode 100644 libc/src/__support/builtins/getf2.h create mode 100644 libc/src/__support/builtins/letf2.h create mode 100644 libc/src/__support/builtins/unordtf2.h diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt index 11b56d8c388c6..ef3de95f9e982 100644 --- a/compiler-rt/lib/builtins/CMakeLists.txt +++ b/compiler-rt/lib/builtins/CMakeLists.txt @@ -290,6 +290,7 @@ if(COMPILER_RT_USE_LIBC_MATH) use_libc_builtin(GENERIC_TF_SOURCES addtf3) use_libc_builtin(GENERIC_SOURCES comparedf2) use_libc_builtin(GENERIC_SOURCES comparesf2) + use_libc_builtin(GENERIC_TF_SOURCES comparetf2) use_libc_builtin(GENERIC_SOURCES divdf3) use_libc_builtin(GENERIC_SOURCES divsf3) use_libc_builtin(GENERIC_TF_SOURCES divtf3) diff --git a/compiler-rt/lib/builtins/comparetf2.cpp b/compiler-rt/lib/builtins/comparetf2.cpp new file mode 100644 index 0000000000000..8abd8f08dab93 --- /dev/null +++ b/compiler-rt/lib/builtins/comparetf2.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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 float128 comparison routines +/// (__letf2/__getf2/__unordtf2 and their aliases) on top of LLVM-libc's shared +/// comparison builtins. +/// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#if defined(CRT_HAS_TF_MODE) +#include "fp_compare_impl.inc" +#include "fp_libc_config.h" +#include "shared/builtins/getf2.h" +#include "shared/builtins/letf2.h" +#include "shared/builtins/unordtf2.h" + +extern "C" { + +COMPILER_RT_ABI CMP_RESULT __letf2(fp_t a, fp_t b) { + return LIBC_NAMESPACE::shared::letf2(a, b); +} +#if defined(__ELF__) +COMPILER_RT_ALIAS(__letf2, __cmptf2) +#endif +COMPILER_RT_ALIAS(__letf2, __eqtf2) +COMPILER_RT_ALIAS(__letf2, __lttf2) +COMPILER_RT_ALIAS(__letf2, __netf2) + +COMPILER_RT_ABI CMP_RESULT __getf2(fp_t a, fp_t b) { + return LIBC_NAMESPACE::shared::getf2(a, b); +} +COMPILER_RT_ALIAS(__getf2, __gttf2) + +COMPILER_RT_ABI CMP_RESULT __unordtf2(fp_t a, fp_t b) { + return LIBC_NAMESPACE::shared::unordtf2(a, b); +} + +} // extern "C" + +#endif diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h index dc66b94274ed5..29380688849e5 100644 --- a/libc/shared/builtins.h +++ b/libc/shared/builtins.h @@ -53,8 +53,10 @@ #include "builtins/floatuntisf.h" #include "builtins/gedf2.h" #include "builtins/gesf2.h" +#include "builtins/getf2.h" #include "builtins/ledf2.h" #include "builtins/lesf2.h" +#include "builtins/letf2.h" #include "builtins/muldf3.h" #include "builtins/mulsf3.h" #include "builtins/multf3.h" @@ -69,5 +71,6 @@ #include "builtins/trunctfxf2.h" #include "builtins/unorddf2.h" #include "builtins/unordsf2.h" +#include "builtins/unordtf2.h" #endif // LLVM_LIBC_SHARED_BUILTINS_H diff --git a/libc/shared/builtins/getf2.h b/libc/shared/builtins/getf2.h new file mode 100644 index 0000000000000..c91227edf391f --- /dev/null +++ b/libc/shared/builtins/getf2.h @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 __getf2 implementation as shared::getf2 so +/// that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_GETF2_H +#define LLVM_LIBC_SHARED_BUILTINS_GETF2_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/builtins/getf2.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::getf2; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_GETF2_H diff --git a/libc/shared/builtins/letf2.h b/libc/shared/builtins/letf2.h new file mode 100644 index 0000000000000..55251ab0c7a08 --- /dev/null +++ b/libc/shared/builtins/letf2.h @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 __letf2 implementation as shared::letf2 so +/// that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_LETF2_H +#define LLVM_LIBC_SHARED_BUILTINS_LETF2_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/builtins/letf2.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::letf2; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_LETF2_H diff --git a/libc/shared/builtins/unordtf2.h b/libc/shared/builtins/unordtf2.h new file mode 100644 index 0000000000000..d2ceeea8944fc --- /dev/null +++ b/libc/shared/builtins/unordtf2.h @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 __unordtf2 implementation as +/// shared::unordtf2 so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SHARED_BUILTINS_UNORDTF2_H +#define LLVM_LIBC_SHARED_BUILTINS_UNORDTF2_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/builtins/unordtf2.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using builtins::unordtf2; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_BUILTINS_UNORDTF2_H diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt index e11571db2b3ee..591c5e042c28c 100644 --- a/libc/src/__support/builtins/CMakeLists.txt +++ b/libc/src/__support/builtins/CMakeLists.txt @@ -550,3 +550,33 @@ add_header_library( libc.src.__support.builtins.cmp_helper libc.src.__support.macros.config ) + +add_header_library( + letf2 + HDRS + letf2.h + DEPENDS + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.cmp_helper + libc.src.__support.macros.config +) + +add_header_library( + getf2 + HDRS + getf2.h + DEPENDS + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.cmp_helper + libc.src.__support.macros.config +) + +add_header_library( + unordtf2 + HDRS + unordtf2.h + DEPENDS + libc.include.llvm-libc-types.float128 + libc.src.__support.builtins.cmp_helper + libc.src.__support.macros.config +) diff --git a/libc/src/__support/builtins/getf2.h b/libc/src/__support/builtins/getf2.h new file mode 100644 index 0000000000000..d24d210e3426a --- /dev/null +++ b/libc/src/__support/builtins/getf2.h @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// 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 __getf2 implementation as builtins::getf2 so +/// that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_GETF2_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_GETF2_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/builtins/cmp_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Greater-equal comparison of float128; mirrors compiler-rt's __getf2. +LIBC_INLINE int getf2(float128 a, float128 b) { return cmp_ge(a, b); } + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_GETF2_H diff --git a/libc/src/__support/builtins/letf2.h b/libc/src/__support/builtins/letf2.h new file mode 100644 index 0000000000000..ba901e2ade58a --- /dev/null +++ b/libc/src/__support/builtins/letf2.h @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// 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 __letf2 implementation as builtins::letf2 so +/// that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_LETF2_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_LETF2_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/builtins/cmp_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Less-equal comparison of float128; mirrors compiler-rt's __letf2. +LIBC_INLINE int letf2(float128 a, float128 b) { return cmp_le(a, b); } + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_LETF2_H diff --git a/libc/src/__support/builtins/unordtf2.h b/libc/src/__support/builtins/unordtf2.h new file mode 100644 index 0000000000000..d8e3ebf9644b3 --- /dev/null +++ b/libc/src/__support/builtins/unordtf2.h @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// 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 __unordtf2 implementation as +/// builtins::unordtf2 so that it can be reused by compiler-rt's builtins. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDTF2_H +#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDTF2_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/builtins/cmp_helper.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace builtins { + +// Unordered comparison of float128; mirrors compiler-rt's __unordtf2. +LIBC_INLINE int unordtf2(float128 a, float128 b) { return cmp_unord(a, b); } + +} // namespace builtins +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_UNORDTF2_H diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index e2481c789cc2e..c7ac8d0f3f2b7 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -866,8 +866,10 @@ add_fp_unittest( libc.src.__support.builtins.floatuntisf libc.src.__support.builtins.gedf2 libc.src.__support.builtins.gesf2 + libc.src.__support.builtins.getf2 libc.src.__support.builtins.ledf2 libc.src.__support.builtins.lesf2 + libc.src.__support.builtins.letf2 libc.src.__support.builtins.muldf3 libc.src.__support.builtins.mulsf3 libc.src.__support.builtins.multf3 @@ -883,6 +885,7 @@ add_fp_unittest( libc.src.__support.uint128 libc.src.__support.builtins.unorddf2 libc.src.__support.builtins.unordsf2 + libc.src.__support.builtins.unordtf2 ) add_fp_unittest( diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp index 9c94786fe52e6..ad4610078ff64 100644 --- a/libc/test/shared/shared_builtins_test.cpp +++ b/libc/test/shared/shared_builtins_test.cpp @@ -91,3 +91,25 @@ TEST_F(LlvmLibcSharedBuiltinsTest, DoubleCompare) { EXPECT_EQ(0, shared::unorddf2(1.0, 2.0)); EXPECT_EQ(1, shared::unorddf2(aNaN, 1.0)); } + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +using LlvmLibcSharedBuiltinsQuadTest = + LIBC_NAMESPACE::testing::FPTest<float128>; + +TEST_F(LlvmLibcSharedBuiltinsQuadTest, Comparison) { + const float128 aNaN = + LIBC_NAMESPACE::fputil::FPBits<float128>::quiet_nan().get_val(); + EXPECT_EQ(-1, shared::getf2(float128(1.0), float128(2.0))); + EXPECT_EQ(0, shared::getf2(float128(1.0), float128(1.0))); + EXPECT_EQ(1, shared::getf2(float128(2.0), float128(1.0))); + EXPECT_EQ(-1, shared::getf2(aNaN, float128(1.0))); + EXPECT_EQ(-1, shared::letf2(float128(1.0), float128(2.0))); + EXPECT_EQ(0, shared::letf2(float128(1.0), float128(1.0))); + EXPECT_EQ(1, shared::letf2(float128(2.0), float128(1.0))); + EXPECT_EQ(1, shared::letf2(aNaN, float128(1.0))); + EXPECT_EQ(0, shared::unordtf2(float128(1.0), float128(2.0))); + EXPECT_EQ(1, shared::unordtf2(aNaN, float128(1.0))); +} + +#endif // LIBC_TYPES_HAS_FLOAT128 >From 98dc9d333205e12d71a129ca838584c603745b5b Mon Sep 17 00:00:00 2001 From: hulxv <[email protected]> Date: Sat, 1 Aug 2026 18:55:00 +0300 Subject: [PATCH 2/3] fix conflicts --- libc/test/shared/shared_builtins_test.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp index ad4610078ff64..f4409d8909508 100644 --- a/libc/test/shared/shared_builtins_test.cpp +++ b/libc/test/shared/shared_builtins_test.cpp @@ -77,7 +77,7 @@ TEST(LlvmLibcSharedBuiltinsTest, SingleCompare) { EXPECT_EQ(1, shared::unordsf2(aNaN, 1.0f)); } -TEST_F(LlvmLibcSharedBuiltinsTest, DoubleCompare) { +TEST(LlvmLibcSharedBuiltinsTest, DoubleCompare) { const double aNaN = LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan().get_val(); EXPECT_EQ(-1, shared::gedf2(1.0, 2.0)); @@ -94,10 +94,7 @@ TEST_F(LlvmLibcSharedBuiltinsTest, DoubleCompare) { #ifdef LIBC_TYPES_HAS_FLOAT128 -using LlvmLibcSharedBuiltinsQuadTest = - LIBC_NAMESPACE::testing::FPTest<float128>; - -TEST_F(LlvmLibcSharedBuiltinsQuadTest, Comparison) { +TEST(LlvmLibcSharedBuiltinsTest, Comparison) { const float128 aNaN = LIBC_NAMESPACE::fputil::FPBits<float128>::quiet_nan().get_val(); EXPECT_EQ(-1, shared::getf2(float128(1.0), float128(2.0))); >From 2e0acb681c9eb25db7a311951d8e27ac34baec28 Mon Sep 17 00:00:00 2001 From: hulxv <[email protected]> Date: Wed, 12 Aug 2026 11:03:35 +0300 Subject: [PATCH 3/3] ifdef --- compiler-rt/lib/builtins/comparetf2.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler-rt/lib/builtins/comparetf2.cpp b/compiler-rt/lib/builtins/comparetf2.cpp index 8abd8f08dab93..0d71b773be4e4 100644 --- a/compiler-rt/lib/builtins/comparetf2.cpp +++ b/compiler-rt/lib/builtins/comparetf2.cpp @@ -16,7 +16,7 @@ #define QUAD_PRECISION #include "fp_lib.h" -#if defined(CRT_HAS_TF_MODE) +#ifdef CRT_HAS_TF_MODE #include "fp_compare_impl.inc" #include "fp_libc_config.h" #include "shared/builtins/getf2.h" @@ -28,7 +28,8 @@ extern "C" { COMPILER_RT_ABI CMP_RESULT __letf2(fp_t a, fp_t b) { return LIBC_NAMESPACE::shared::letf2(a, b); } -#if defined(__ELF__) + +#ifdef __ELF__ COMPILER_RT_ALIAS(__letf2, __cmptf2) #endif COMPILER_RT_ALIAS(__letf2, __eqtf2) _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
