[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
Tue Jul 28 17:02:26 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: hulxv (hulxv)

<details>
<summary>Changes</summary>

libc-backed quad-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/212653.diff


12 Files Affected:

- (modified) compiler-rt/lib/builtins/CMakeLists.txt (+1) 
- (added) compiler-rt/lib/builtins/comparetf2.cpp (+49) 
- (modified) libc/shared/builtins.h (+3) 
- (added) libc/shared/builtins/getf2.h (+35) 
- (added) libc/shared/builtins/letf2.h (+35) 
- (added) libc/shared/builtins/unordtf2.h (+35) 
- (modified) libc/src/__support/builtins/CMakeLists.txt (+30) 
- (added) libc/src/__support/builtins/getf2.h (+36) 
- (added) libc/src/__support/builtins/letf2.h (+36) 
- (added) libc/src/__support/builtins/unordtf2.h (+36) 
- (modified) libc/test/shared/CMakeLists.txt (+3) 
- (modified) libc/test/shared/shared_builtins_test.cpp (+24) 


``````````diff
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index ac84ff4baebba..0fb3d667cbac4 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -267,6 +267,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)
 endif()
 
 option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
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 0ff85b0b24ecb..136ca3de4b06a 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -25,8 +25,10 @@
 #include "builtins/divtf3.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"
@@ -35,5 +37,6 @@
 #include "builtins/subtf3.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 7760c741e59d5..e6340dd157460 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -174,3 +174,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 a1a0138e46d7e..27f3179a16bcc 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -836,8 +836,10 @@ add_fp_unittest(
     libc.src.__support.builtins.divtf3
     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
@@ -846,6 +848,7 @@ add_fp_unittest(
     libc.src.__support.builtins.subtf3
     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 8b3f2a490bb4a..023b29f69eed4 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -59,6 +59,8 @@ TEST(LlvmLibcSharedBuiltinsTest, SingleCompare) {
 TEST_F(LlvmLibcSharedBuiltinsTest, DoubleCompare) {
   const double aNaN =
       LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan().get_val();
+  const float aNaN =
+      LIBC_NAMESPACE::fputil::FPBits<float>::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));
@@ -70,3 +72,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

``````````

</details>


https://github.com/llvm/llvm-project/pull/212653


More information about the llvm-branch-commits mailing list