[llvm-branch-commits] [compiler-rt] [libc] [compiler-rt][builtins] libc-backed quad-float comparison builtins (PR #212653)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Aug 1 11:31:37 PDT 2026
https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/212653
>From 1984097bcfe9f508058923011734eb1f4707226a Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 29 Jul 2026 01:29:11 +0300
Subject: [PATCH 1/2] [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 40da13610b0a3..45ac3bbf0dc24 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 488083a92f23d..0c86f56fbc805 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -29,8 +29,10 @@
#include "builtins/extendxftf2.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"
@@ -45,5 +47,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 9c3463c85d801..a990a74fcde81 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -284,3 +284,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 938e3ca6b0504..3e362be0b5124 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -840,8 +840,10 @@ add_fp_unittest(
libc.src.__support.builtins.extendxftf2
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
@@ -856,6 +858,7 @@ add_fp_unittest(
libc.src.__support.builtins.trunctfxf2
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 8582258cfd688..95bb1b3f27cfe 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -90,3 +90,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 cfd7f8bbeb76379e2a1c97c70fdef255e98e1e30 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 1 Aug 2026 18:55:00 +0300
Subject: [PATCH 2/2] 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 95bb1b3f27cfe..6f0a50bb47592 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -76,7 +76,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));
@@ -93,10 +93,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)));
More information about the llvm-branch-commits
mailing list