[libc-commits] [libc] [libc] introduce shared compiler-rt builtins (PR #200094)

via libc-commits libc-commits at lists.llvm.org
Sat May 30 08:42:10 PDT 2026


https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/200094

>From 9a0bb04b42e141693e7b5c6fbfe5623f1beb08b0 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 28 May 2026 03:01:10 +0300
Subject: [PATCH] [libc] introduce shared compiler-rt builtins

---
 libc/include/llvm-libc-macros/float-macros.h |  6 ++++
 libc/shared/builtins.h                       | 16 +++++++++
 libc/shared/builtins/addtf3.h                | 35 ++++++++++++++++++++
 libc/src/__support/CMakeLists.txt            |  1 +
 libc/src/__support/FPUtil/dyadic_float.h     |  3 ++
 libc/src/__support/builtins/CMakeLists.txt   |  9 +++++
 libc/src/__support/builtins/addtf3.h         | 32 ++++++++++++++++++
 libc/test/shared/CMakeLists.txt              | 10 ++++++
 libc/test/shared/shared_builtins_test.cpp    | 29 ++++++++++++++++
 9 files changed, 141 insertions(+)
 create mode 100644 libc/shared/builtins.h
 create mode 100644 libc/shared/builtins/addtf3.h
 create mode 100644 libc/src/__support/builtins/CMakeLists.txt
 create mode 100644 libc/src/__support/builtins/addtf3.h
 create mode 100644 libc/test/shared/shared_builtins_test.cpp

diff --git a/libc/include/llvm-libc-macros/float-macros.h b/libc/include/llvm-libc-macros/float-macros.h
index a25ef60a293d3..38a3a771cca54 100644
--- a/libc/include/llvm-libc-macros/float-macros.h
+++ b/libc/include/llvm-libc-macros/float-macros.h
@@ -9,6 +9,12 @@
 #ifndef LLVM_LIBC_MACROS_FLOAT_MACROS_H
 #define LLVM_LIBC_MACROS_FLOAT_MACROS_H
 
+// __has_builtin is a Clang extension; GCC < 10 doesn't define it, which
+// turns a bare `#if __has_builtin(...)` into a preprocessor syntax error.
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
+
 #ifndef FLT_RADIX
 #define FLT_RADIX __FLT_RADIX__
 #endif // FLT_RADIX
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
new file mode 100644
index 0000000000000..157b6622cbcff
--- /dev/null
+++ b/libc/shared/builtins.h
@@ -0,0 +1,16 @@
+//===-- Compiler-runtime builtin primitives ---------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_H
+#define LLVM_LIBC_SHARED_BUILTINS_H
+
+#include "libc_common.h"
+
+#include "builtins/addtf3.h"
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_H
diff --git a/libc/shared/builtins/addtf3.h b/libc/shared/builtins/addtf3.h
new file mode 100644
index 0000000000000..9c39e6e9a8406
--- /dev/null
+++ b/libc/shared/builtins/addtf3.h
@@ -0,0 +1,35 @@
+//===-- Shared __addtf3 function -------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This header exposes LLVM-libc's __addtf3 implementation as shared::addtf3
+// so that it can be reused by other LLVM projects, such as compiler-rt's
+// builtins library.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_ADDTF3_H
+#define LLVM_LIBC_SHARED_BUILTINS_ADDTF3_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/addtf3.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::addtf3;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_ADDTF3_H
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 9d08b4bcf7303..c09ca012c3594 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -472,6 +472,7 @@ add_subdirectory(wchar)
 add_subdirectory(wctype)
 
 add_subdirectory(math)
+add_subdirectory(builtins)
 if(LIBC_COMPILER_HAS_EXT_VECTOR_TYPE)
   add_subdirectory(mathvec)
 endif()
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index 7e4ea2f1a81be..d99a14f35f38e 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -415,6 +415,9 @@ template <size_t Bits> struct DyadicFloat {
     if constexpr (cpp::is_same_v<T, bfloat16>
 #if defined(LIBC_TYPES_HAS_FLOAT16) && !defined(__LIBC_USE_FLOAT16_CONVERSION)
                   || cpp::is_same_v<T, float16>
+#endif
+#if defined(LIBC_TYPES_HAS_FLOAT128)
+                  || cpp::is_same_v<T, float128>
 #endif
     )
       return generic_as<T, ShouldSignalExceptions>();
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
new file mode 100644
index 0000000000000..c4982887d0a7c
--- /dev/null
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -0,0 +1,9 @@
+add_header_library(
+  addtf3
+  HDRS
+    addtf3.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/builtins/addtf3.h b/libc/src/__support/builtins/addtf3.h
new file mode 100644
index 0000000000000..129ad2a81e827
--- /dev/null
+++ b/libc/src/__support/builtins/addtf3.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for __addtf3 ----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_ADDTF3_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_ADDTF3_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Mirrors the compiler-rt __addtf3 ABI: a + b at float128 precision.
+LIBC_INLINE float128 addtf3(float128 x, float128 y) {
+  return fputil::generic::add<float128>(x, y);
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_ADDTF3_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7a5c3a369c2fc..0f8027e2e0057 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -814,6 +814,16 @@ add_fp_unittest(
 )
 endif() # LIBC_TEST_SKIP_SHARED_MATH_TESTS
 
+add_fp_unittest(
+  shared_builtins_test
+  SUITE
+    libc-shared-tests
+  SRCS
+    shared_builtins_test.cpp
+  DEPENDS
+    libc.src.__support.builtins.addtf3
+)
+
 add_fp_unittest(
   shared_str_to_num_test
   SUITE
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
new file mode 100644
index 0000000000000..67d40b349cef3
--- /dev/null
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -0,0 +1,29 @@
+//===-- Unittests for shared builtins -------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "shared/builtins.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcSharedBuiltinsTest, AllFloat) {
+  // TODO: assertions for shared::*sf3 builtins.
+}
+
+TEST(LlvmLibcSharedBuiltinsTest, AllDouble) {
+  // TODO: assertions for shared::*df3 builtins.
+}
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+TEST(LlvmLibcSharedBuiltinsTest, AllFloat128) {
+  namespace shared = LIBC_NAMESPACE::shared;
+
+  EXPECT_FP_EQ(float128(3.0), shared::addtf3(float128(1.0), float128(2.0)));
+}
+
+#endif // LIBC_TYPES_HAS_FLOAT128



More information about the libc-commits mailing list