[llvm-branch-commits] [compiler-rt] [libc] [compiler-rt][builtins] libc-backed double-float comparison builtins (PR #212652)

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/212652

>From 01d882a741cc1b190c6e25e1c507c63156b9231b Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 29 Jul 2026 01:27:13 +0300
Subject: [PATCH] [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 59259d8d88c30..40da13610b0a3 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 9ea5cf9b2d18d..488083a92f23d 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -27,7 +27,9 @@
 #include "builtins/extendsfdf2.h"
 #include "builtins/extendsftf2.h"
 #include "builtins/extendxftf2.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"
@@ -41,6 +43,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 7f608d377b392..9c3463c85d801 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -257,3 +257,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 dccccd3fbcc18..938e3ca6b0504 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -838,7 +838,9 @@ add_fp_unittest(
     libc.src.__support.builtins.extendsfdf2
     libc.src.__support.builtins.extendsftf2
     libc.src.__support.builtins.extendxftf2
+    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
@@ -852,6 +854,7 @@ add_fp_unittest(
     libc.src.__support.builtins.trunctfdf2
     libc.src.__support.builtins.trunctfsf2
     libc.src.__support.builtins.trunctfxf2
+    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 ba17e83fdef8a..8582258cfd688 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -75,3 +75,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));
+}



More information about the llvm-branch-commits mailing list