[libc-commits] [compiler-rt] [libc] [compiler-rt][builtins] libc-backed floating-point extend/trunct builtins (PR #209984)

via libc-commits libc-commits at lists.llvm.org
Fri Jul 24 10:47:16 PDT 2026


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

>From 63358bcfcc0c966b4c1a209bf2ba149e33c51ac0 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 16 Jul 2026 09:43:25 +0300
Subject: [PATCH 1/4] [compiler-rt][builtins] libc-backed floating-point
 extend/truncate builtins

---
 compiler-rt/lib/builtins/CMakeLists.txt       | 50 ++++++----
 compiler-rt/lib/builtins/extenddftf2.cpp      | 25 +++++
 compiler-rt/lib/builtins/extendsfdf2.cpp      | 21 ++++
 compiler-rt/lib/builtins/extendsftf2.cpp      | 25 +++++
 compiler-rt/lib/builtins/extendxftf2.cpp      | 25 +++++
 compiler-rt/lib/builtins/truncdfsf2.cpp       | 21 ++++
 compiler-rt/lib/builtins/trunctfdf2.cpp       | 25 +++++
 compiler-rt/lib/builtins/trunctfsf2.cpp       | 25 +++++
 compiler-rt/lib/builtins/trunctfxf2.cpp       | 25 +++++
 libc/shared/builtins.h                        |  8 ++
 libc/shared/builtins/extenddftf2.h            | 35 +++++++
 libc/shared/builtins/extendsfdf2.h            | 29 ++++++
 libc/shared/builtins/extendsftf2.h            | 35 +++++++
 libc/shared/builtins/extendxftf2.h            | 37 +++++++
 libc/shared/builtins/truncdfsf2.h             | 29 ++++++
 libc/shared/builtins/trunctfdf2.h             | 35 +++++++
 libc/shared/builtins/trunctfsf2.h             | 35 +++++++
 libc/shared/builtins/trunctfxf2.h             | 37 +++++++
 libc/src/__support/builtins/CMakeLists.txt    | 96 +++++++++++++++++++
 libc/src/__support/builtins/extenddftf2.h     | 36 +++++++
 libc/src/__support/builtins/extendsfdf2.h     | 30 ++++++
 libc/src/__support/builtins/extendsftf2.h     | 36 +++++++
 libc/src/__support/builtins/extendxftf2.h     | 40 ++++++++
 .../src/__support/builtins/fpconvert_helper.h | 79 +++++++++++++++
 libc/src/__support/builtins/truncdfsf2.h      | 30 ++++++
 libc/src/__support/builtins/trunctfdf2.h      | 36 +++++++
 libc/src/__support/builtins/trunctfsf2.h      | 36 +++++++
 libc/src/__support/builtins/trunctfxf2.h      | 40 ++++++++
 libc/test/shared/CMakeLists.txt               |  8 ++
 libc/test/shared/shared_builtins_test.cpp     | 24 +++++
 30 files changed, 992 insertions(+), 21 deletions(-)
 create mode 100644 compiler-rt/lib/builtins/extenddftf2.cpp
 create mode 100644 compiler-rt/lib/builtins/extendsfdf2.cpp
 create mode 100644 compiler-rt/lib/builtins/extendsftf2.cpp
 create mode 100644 compiler-rt/lib/builtins/extendxftf2.cpp
 create mode 100644 compiler-rt/lib/builtins/truncdfsf2.cpp
 create mode 100644 compiler-rt/lib/builtins/trunctfdf2.cpp
 create mode 100644 compiler-rt/lib/builtins/trunctfsf2.cpp
 create mode 100644 compiler-rt/lib/builtins/trunctfxf2.cpp
 create mode 100644 libc/shared/builtins/extenddftf2.h
 create mode 100644 libc/shared/builtins/extendsfdf2.h
 create mode 100644 libc/shared/builtins/extendsftf2.h
 create mode 100644 libc/shared/builtins/extendxftf2.h
 create mode 100644 libc/shared/builtins/truncdfsf2.h
 create mode 100644 libc/shared/builtins/trunctfdf2.h
 create mode 100644 libc/shared/builtins/trunctfsf2.h
 create mode 100644 libc/shared/builtins/trunctfxf2.h
 create mode 100644 libc/src/__support/builtins/extenddftf2.h
 create mode 100644 libc/src/__support/builtins/extendsfdf2.h
 create mode 100644 libc/src/__support/builtins/extendsftf2.h
 create mode 100644 libc/src/__support/builtins/extendxftf2.h
 create mode 100644 libc/src/__support/builtins/fpconvert_helper.h
 create mode 100644 libc/src/__support/builtins/truncdfsf2.h
 create mode 100644 libc/src/__support/builtins/trunctfdf2.h
 create mode 100644 libc/src/__support/builtins/trunctfsf2.h
 create mode 100644 libc/src/__support/builtins/trunctfxf2.h

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index ea21c36d3d31f..acf7e7d278f6c 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -243,6 +243,27 @@ set(GENERIC_TF_SOURCES
   trunctfsf2.c
 )
 
+# Implement extended-precision builtins, assuming long double is 80 bits.
+# long double is not 80 bits on Android or MSVC.
+set(x86_80_BIT_SOURCES
+  divxc3.c
+  extendhfxf2.c
+  extendxftf2.c
+  fixxfdi.c
+  fixxfti.c
+  fixunsxfdi.c
+  fixunsxfsi.c
+  fixunsxfti.c
+  floatdixf.c
+  floattixf.c
+  floatundixf.c
+  floatuntixf.c
+  mulxc3.c
+  powixf2.c
+  trunctfxf2.c
+  truncxfhf2.c
+)
+
 option(COMPILER_RT_USE_LIBC_MATH
       "Use LLVM libc math routines for floating-point builtins" OFF)
 
@@ -265,6 +286,14 @@ if(COMPILER_RT_USE_LIBC_MATH)
   add_definitions(-DCOMPILER_RT_USE_LIBC_MATH)
 
   use_libc_builtin(GENERIC_TF_SOURCES addtf3)
+  use_libc_builtin(GENERIC_TF_SOURCES extenddftf2)
+  use_libc_builtin(GENERIC_SOURCES    extendsfdf2)
+  use_libc_builtin(GENERIC_TF_SOURCES extendsftf2)
+  use_libc_builtin(x86_80_BIT_SOURCES extendxftf2)
+  use_libc_builtin(GENERIC_SOURCES    truncdfsf2)  
+  use_libc_builtin(GENERIC_TF_SOURCES trunctfdf2)
+  use_libc_builtin(GENERIC_TF_SOURCES trunctfsf2)
+  use_libc_builtin(x86_80_BIT_SOURCES trunctfxf2)
 endif()
 
 option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
@@ -336,27 +365,6 @@ if (NOT MSVC)
   )
 endif ()
 
-# Implement extended-precision builtins, assuming long double is 80 bits.
-# long double is not 80 bits on Android or MSVC.
-set(x86_80_BIT_SOURCES
-  divxc3.c
-  extendhfxf2.c
-  extendxftf2.c
-  fixxfdi.c
-  fixxfti.c
-  fixunsxfdi.c
-  fixunsxfsi.c
-  fixunsxfti.c
-  floatdixf.c
-  floattixf.c
-  floatundixf.c
-  floatuntixf.c
-  mulxc3.c
-  powixf2.c
-  trunctfxf2.c
-  truncxfhf2.c
-)
-
 if (NOT MSVC)
   set(x86_64_SOURCES
     ${GENERIC_SOURCES}
diff --git a/compiler-rt/lib/builtins/extenddftf2.cpp b/compiler-rt/lib/builtins/extenddftf2.cpp
new file mode 100644
index 0000000000000..268ca6cdde6b8
--- /dev/null
+++ b/compiler-rt/lib/builtins/extenddftf2.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extenddftf2, extend double to float128,
+/// on top of LLVM-libc's shared::extenddftf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/extenddftf2.h"
+
+#if defined(CRT_HAS_TF_MODE)
+extern "C" COMPILER_RT_ABI tf_float __extenddftf2(double a) {
+  return LIBC_NAMESPACE::shared::extenddftf2(a);
+}
+#endif
diff --git a/compiler-rt/lib/builtins/extendsfdf2.cpp b/compiler-rt/lib/builtins/extendsfdf2.cpp
new file mode 100644
index 0000000000000..e422d55c6f1fe
--- /dev/null
+++ b/compiler-rt/lib/builtins/extendsfdf2.cpp
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendsfdf2, extend float to double, on
+/// top of LLVM-libc's shared::extendsfdf2.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/extendsfdf2.h"
+
+extern "C" COMPILER_RT_ABI double __extendsfdf2(float a) {
+  return LIBC_NAMESPACE::shared::extendsfdf2(a);
+}
diff --git a/compiler-rt/lib/builtins/extendsftf2.cpp b/compiler-rt/lib/builtins/extendsftf2.cpp
new file mode 100644
index 0000000000000..8f90aa6282009
--- /dev/null
+++ b/compiler-rt/lib/builtins/extendsftf2.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendsftf2, extend float to float128,
+/// on top of LLVM-libc's shared::extendsftf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/extendsftf2.h"
+
+#if defined(CRT_HAS_TF_MODE)
+extern "C" COMPILER_RT_ABI tf_float __extendsftf2(float a) {
+  return LIBC_NAMESPACE::shared::extendsftf2(a);
+}
+#endif
diff --git a/compiler-rt/lib/builtins/extendxftf2.cpp b/compiler-rt/lib/builtins/extendxftf2.cpp
new file mode 100644
index 0000000000000..5c0c91118ecbe
--- /dev/null
+++ b/compiler-rt/lib/builtins/extendxftf2.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendxftf2, extend long double to
+/// float128, on top of LLVM-libc's shared::extendxftf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/extendxftf2.h"
+
+#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
+extern "C" COMPILER_RT_ABI tf_float __extendxftf2(xf_float a) {
+  return LIBC_NAMESPACE::shared::extendxftf2(a);
+}
+#endif
diff --git a/compiler-rt/lib/builtins/truncdfsf2.cpp b/compiler-rt/lib/builtins/truncdfsf2.cpp
new file mode 100644
index 0000000000000..9d2281cb1c59d
--- /dev/null
+++ b/compiler-rt/lib/builtins/truncdfsf2.cpp
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __truncdfsf2, truncate double to float,
+/// on top of LLVM-libc's shared::truncdfsf2.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fp_libc_config.h"
+#include "int_lib.h"
+#include "shared/builtins/truncdfsf2.h"
+
+extern "C" COMPILER_RT_ABI float __truncdfsf2(double a) {
+  return LIBC_NAMESPACE::shared::truncdfsf2(a);
+}
diff --git a/compiler-rt/lib/builtins/trunctfdf2.cpp b/compiler-rt/lib/builtins/trunctfdf2.cpp
new file mode 100644
index 0000000000000..f381d2768d6b1
--- /dev/null
+++ b/compiler-rt/lib/builtins/trunctfdf2.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __trunctfdf2, truncate float128 to
+/// double, on top of LLVM-libc's shared::trunctfdf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/trunctfdf2.h"
+
+#if defined(CRT_HAS_TF_MODE)
+extern "C" COMPILER_RT_ABI double __trunctfdf2(tf_float a) {
+  return LIBC_NAMESPACE::shared::trunctfdf2(a);
+}
+#endif
diff --git a/compiler-rt/lib/builtins/trunctfsf2.cpp b/compiler-rt/lib/builtins/trunctfsf2.cpp
new file mode 100644
index 0000000000000..aac13c25b4b28
--- /dev/null
+++ b/compiler-rt/lib/builtins/trunctfsf2.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __trunctfsf2, truncate float128 to float,
+/// on top of LLVM-libc's shared::trunctfsf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/trunctfsf2.h"
+
+#if defined(CRT_HAS_TF_MODE)
+extern "C" COMPILER_RT_ABI float __trunctfsf2(tf_float a) {
+  return LIBC_NAMESPACE::shared::trunctfsf2(a);
+}
+#endif
diff --git a/compiler-rt/lib/builtins/trunctfxf2.cpp b/compiler-rt/lib/builtins/trunctfxf2.cpp
new file mode 100644
index 0000000000000..88dae578025bc
--- /dev/null
+++ b/compiler-rt/lib/builtins/trunctfxf2.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __trunctfxf2, truncate float128 to long
+/// double, on top of LLVM-libc's shared::trunctfxf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/trunctfxf2.h"
+
+#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
+extern "C" COMPILER_RT_ABI xf_float __trunctfxf2(tf_float a) {
+  return LIBC_NAMESPACE::shared::trunctfxf2(a);
+}
+#endif
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
index ced054cb02582..0b1cc7974d284 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -23,11 +23,19 @@
 #include "builtins/divdf3.h"
 #include "builtins/divsf3.h"
 #include "builtins/divtf3.h"
+#include "builtins/extenddftf2.h"
+#include "builtins/extendsfdf2.h"
+#include "builtins/extendsftf2.h"
+#include "builtins/extendxftf2.h"
 #include "builtins/muldf3.h"
 #include "builtins/mulsf3.h"
 #include "builtins/multf3.h"
 #include "builtins/subdf3.h"
 #include "builtins/subsf3.h"
 #include "builtins/subtf3.h"
+#include "builtins/truncdfsf2.h"
+#include "builtins/trunctfdf2.h"
+#include "builtins/trunctfsf2.h"
+#include "builtins/trunctfxf2.h"
 
 #endif // LLVM_LIBC_SHARED_BUILTINS_H
diff --git a/libc/shared/builtins/extenddftf2.h b/libc/shared/builtins/extenddftf2.h
new file mode 100644
index 0000000000000..4790d48371eef
--- /dev/null
+++ b/libc/shared/builtins/extenddftf2.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 __extenddftf2 implementation as
+/// shared::extenddftf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDDFTF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_EXTENDDFTF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/extenddftf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::extenddftf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDDFTF2_H
diff --git a/libc/shared/builtins/extendsfdf2.h b/libc/shared/builtins/extendsfdf2.h
new file mode 100644
index 0000000000000..4f361ff9b4b9e
--- /dev/null
+++ b/libc/shared/builtins/extendsfdf2.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 __extendsfdf2 implementation as
+/// shared::extendsfdf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDSFDF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_EXTENDSFDF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/extendsfdf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::extendsfdf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDSFDF2_H
diff --git a/libc/shared/builtins/extendsftf2.h b/libc/shared/builtins/extendsftf2.h
new file mode 100644
index 0000000000000..93395df407981
--- /dev/null
+++ b/libc/shared/builtins/extendsftf2.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 __extendsftf2 implementation as
+/// shared::extendsftf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDSFTF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_EXTENDSFTF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/extendsftf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::extendsftf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDSFTF2_H
diff --git a/libc/shared/builtins/extendxftf2.h b/libc/shared/builtins/extendxftf2.h
new file mode 100644
index 0000000000000..d1631c8c46f1e
--- /dev/null
+++ b/libc/shared/builtins/extendxftf2.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendxftf2 implementation as
+/// shared::extendxftf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDXFTF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_EXTENDXFTF2_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT128) &&                                        \
+    defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/extendxftf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::extendxftf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDXFTF2_H
diff --git a/libc/shared/builtins/truncdfsf2.h b/libc/shared/builtins/truncdfsf2.h
new file mode 100644
index 0000000000000..7145b45c5ed0c
--- /dev/null
+++ b/libc/shared/builtins/truncdfsf2.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 __truncdfsf2 implementation as
+/// shared::truncdfsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCDFSF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCDFSF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/truncdfsf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::truncdfsf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCDFSF2_H
diff --git a/libc/shared/builtins/trunctfdf2.h b/libc/shared/builtins/trunctfdf2.h
new file mode 100644
index 0000000000000..5e8b34f3cecd6
--- /dev/null
+++ b/libc/shared/builtins/trunctfdf2.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 __trunctfdf2 implementation as
+/// shared::trunctfdf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCTFDF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCTFDF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/trunctfdf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::trunctfdf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCTFDF2_H
diff --git a/libc/shared/builtins/trunctfsf2.h b/libc/shared/builtins/trunctfsf2.h
new file mode 100644
index 0000000000000..4d0f64b399279
--- /dev/null
+++ b/libc/shared/builtins/trunctfsf2.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 __trunctfsf2 implementation as
+/// shared::trunctfsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCTFSF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCTFSF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/trunctfsf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::trunctfsf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCTFSF2_H
diff --git a/libc/shared/builtins/trunctfxf2.h b/libc/shared/builtins/trunctfxf2.h
new file mode 100644
index 0000000000000..eb275d4463be1
--- /dev/null
+++ b/libc/shared/builtins/trunctfxf2.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __trunctfxf2 implementation as
+/// shared::trunctfxf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCTFXF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCTFXF2_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT128) &&                                        \
+    defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/trunctfxf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::trunctfxf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCTFXF2_H
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index f5b5ffe3e07b4..91ace33d43014 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -1,3 +1,19 @@
+add_header_library(
+  fpconvert_helper
+  HDRS
+    fpconvert_helper.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.CPP.bit
+    libc.src.__support.CPP.type_traits
+    libc.src.__support.FPUtil.dyadic_float
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.macros.attributes
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   addtf3
   HDRS
@@ -109,3 +125,83 @@ add_header_library(
     libc.src.__support.FPUtil.generic.div
     libc.src.__support.macros.config
 )
+
+add_header_library(
+  extendsftf2
+  HDRS
+    extendsftf2.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  extenddftf2
+  HDRS
+    extenddftf2.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  extendxftf2
+  HDRS
+    extendxftf2.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  trunctfdf2
+  HDRS
+    trunctfdf2.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  trunctfsf2
+  HDRS
+    trunctfsf2.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  trunctfxf2
+  HDRS
+    trunctfxf2.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  extendsfdf2
+  HDRS
+    extendsfdf2.h
+  DEPENDS
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  truncdfsf2
+  HDRS
+    truncdfsf2.h
+  DEPENDS
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/builtins/extenddftf2.h b/libc/src/__support/builtins/extenddftf2.h
new file mode 100644
index 0000000000000..5a1b8bfc0dd29
--- /dev/null
+++ b/libc/src/__support/builtins/extenddftf2.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 __extenddftf2 implementation as
+/// builtins::extenddftf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDDFTF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDDFTF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Extend double to float128; mirrors compiler-rt's __extenddftf2.
+LIBC_INLINE float128 extenddftf2(double x) { return fpconvert<float128>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDDFTF2_H
diff --git a/libc/src/__support/builtins/extendsfdf2.h b/libc/src/__support/builtins/extendsfdf2.h
new file mode 100644
index 0000000000000..bcd57d10a4a7f
--- /dev/null
+++ b/libc/src/__support/builtins/extendsfdf2.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 __extendsfdf2 implementation as
+/// builtins::extendsfdf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDSFDF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDSFDF2_H
+
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Extend float to double; mirrors compiler-rt's __extendsfdf2.
+LIBC_INLINE double extendsfdf2(float x) { return fpconvert<double>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDSFDF2_H
diff --git a/libc/src/__support/builtins/extendsftf2.h b/libc/src/__support/builtins/extendsftf2.h
new file mode 100644
index 0000000000000..8450d93090014
--- /dev/null
+++ b/libc/src/__support/builtins/extendsftf2.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 __extendsftf2 implementation as
+/// builtins::extendsftf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDSFTF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDSFTF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Extend float to float128; mirrors compiler-rt's __extendsftf2.
+LIBC_INLINE float128 extendsftf2(float x) { return fpconvert<float128>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDSFTF2_H
diff --git a/libc/src/__support/builtins/extendxftf2.h b/libc/src/__support/builtins/extendxftf2.h
new file mode 100644
index 0000000000000..a1382a8c6de4b
--- /dev/null
+++ b/libc/src/__support/builtins/extendxftf2.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendxftf2 implementation as
+/// builtins::extendxftf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDXFTF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDXFTF2_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT128) &&                                        \
+    defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Extend long double to float128; mirrors compiler-rt's __extendxftf2.
+LIBC_INLINE float128 extendxftf2(long double x) {
+  return fpconvert<float128>(x);
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDXFTF2_H
diff --git a/libc/src/__support/builtins/fpconvert_helper.h b/libc/src/__support/builtins/fpconvert_helper.h
new file mode 100644
index 0000000000000..2b7b331b44b5c
--- /dev/null
+++ b/libc/src/__support/builtins/fpconvert_helper.h
@@ -0,0 +1,79 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Shared float-to-float extend/truncate conversions, rounding to nearest
+/// (ties to even) when narrowing.  These mirror compiler-rt's __extend<a><b>2 /
+/// __trunc<a><b>2 builtins, so they can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_FPCONVERT_HELPER_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FPCONVERT_HELPER_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/CPP/algorithm.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/type_traits.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// TODO: use fputil::cast after adding Float128/64/32/16 classes currently, we
+// are using this helper to avoid an infinity loop that happens because
+// fputil::cast is calling the builtins if the target doesn't have FPU.
+//
+// assembly (each body below compiles to a self-call):
+//   // double f(float128 x) { return cast<double>(x); }
+//   trunc_tf_df:  callq __trunctfdf2 at PLT
+//   // float128 g(double x) { return cast<float128>(x); }
+//   ext_df_tf:    jmp   __extenddftf2 at PLT   // TAILCALL
+
+// Convert the floating-point value x from From to To (extend or truncate).
+// Narrowing rounds to nearest, ties to even; mirrors compiler-rt __extend* /
+// __trunc*.
+template <typename To, typename From>
+LIBC_INLINE constexpr To fpconvert(From x) {
+  using FromBits = fputil::FPBits<From>;
+  using ToBits = fputil::FPBits<To>;
+  using ToStorageType = typename ToBits::StorageType;
+
+  FromBits x_bits(x);
+
+  if (x_bits.is_nan()) {
+    if (x_bits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return ToBits::quiet_nan().get_val();
+    }
+    typename FromBits::StorageType x_mant = x_bits.get_mantissa();
+    if (FromBits::FRACTION_LEN > ToBits::FRACTION_LEN)
+      x_mant >>= FromBits::FRACTION_LEN - ToBits::FRACTION_LEN;
+    return ToBits::quiet_nan(x_bits.sign(), static_cast<ToStorageType>(x_mant))
+        .get_val();
+  }
+
+  if (x_bits.is_inf())
+    return ToBits::inf(x_bits.sign()).get_val();
+
+  // Zero and subnormals fall through: DyadicFloat(x) gives a zero mantissa for
+  // zero, which as<To>() maps back to a correctly-signed zero.
+  constexpr size_t MAX_FRACTION_LEN =
+      cpp::max(ToBits::FRACTION_LEN, FromBits::FRACTION_LEN);
+  fputil::DyadicFloat<cpp::bit_ceil(MAX_FRACTION_LEN)> xd(x);
+  return xd.template as<To, /*ShouldSignalExceptions=*/true>();
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_FPCONVERT_HELPER_H
diff --git a/libc/src/__support/builtins/truncdfsf2.h b/libc/src/__support/builtins/truncdfsf2.h
new file mode 100644
index 0000000000000..bb59a22e585d2
--- /dev/null
+++ b/libc/src/__support/builtins/truncdfsf2.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 __truncdfsf2 implementation as
+/// builtins::truncdfsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFSF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFSF2_H
+
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate double to float; mirrors compiler-rt's __truncdfsf2.
+LIBC_INLINE float truncdfsf2(double x) { return fpconvert<float>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFSF2_H
diff --git a/libc/src/__support/builtins/trunctfdf2.h b/libc/src/__support/builtins/trunctfdf2.h
new file mode 100644
index 0000000000000..fb461e77cb093
--- /dev/null
+++ b/libc/src/__support/builtins/trunctfdf2.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 __trunctfdf2 implementation as
+/// builtins::trunctfdf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFDF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFDF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate float128 to double; mirrors compiler-rt's __trunctfdf2.
+LIBC_INLINE double trunctfdf2(float128 x) { return fpconvert<double>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFDF2_H
diff --git a/libc/src/__support/builtins/trunctfsf2.h b/libc/src/__support/builtins/trunctfsf2.h
new file mode 100644
index 0000000000000..8db7b8885e0bd
--- /dev/null
+++ b/libc/src/__support/builtins/trunctfsf2.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 __trunctfsf2 implementation as
+/// builtins::trunctfsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFSF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFSF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate float128 to float; mirrors compiler-rt's __trunctfsf2.
+LIBC_INLINE float trunctfsf2(float128 x) { return fpconvert<float>(x); }
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFSF2_H
diff --git a/libc/src/__support/builtins/trunctfxf2.h b/libc/src/__support/builtins/trunctfxf2.h
new file mode 100644
index 0000000000000..e53ce7677dea0
--- /dev/null
+++ b/libc/src/__support/builtins/trunctfxf2.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __trunctfxf2 implementation as
+/// builtins::trunctfxf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFXF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFXF2_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT128) &&                                        \
+    defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
+
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate float128 to long double; mirrors compiler-rt's __trunctfxf2.
+LIBC_INLINE long double trunctfxf2(float128 x) {
+  return fpconvert<long double>(x);
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFXF2_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 6efe8365b1557..395adfed7b32c 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -833,12 +833,20 @@ add_fp_unittest(
     libc.src.__support.builtins.divdf3
     libc.src.__support.builtins.divsf3
     libc.src.__support.builtins.divtf3
+    libc.src.__support.builtins.extenddftf2
+    libc.src.__support.builtins.extendsfdf2
+    libc.src.__support.builtins.extendsftf2
+    libc.src.__support.builtins.extendxftf2
     libc.src.__support.builtins.muldf3
     libc.src.__support.builtins.mulsf3
     libc.src.__support.builtins.multf3
     libc.src.__support.builtins.subdf3
     libc.src.__support.builtins.subsf3
     libc.src.__support.builtins.subtf3
+    libc.src.__support.builtins.truncdfsf2
+    libc.src.__support.builtins.trunctfdf2
+    libc.src.__support.builtins.trunctfsf2
+    libc.src.__support.builtins.trunctfxf2
 )
 
 add_fp_unittest(
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index dac84345eade1..44d9989cbf1e1 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -10,6 +10,8 @@
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
+namespace shared = LIBC_NAMESPACE::shared;
+
 TEST(LlvmLibcSharedBuiltinsTest, AllFloat) {
   EXPECT_FP_EQ(3.0f, LIBC_NAMESPACE::shared::addsf3(1.0f, 2.0f));
   EXPECT_FP_EQ(3.0f, LIBC_NAMESPACE::shared::divsf3(6.0f, 2.0f));
@@ -38,3 +40,25 @@ TEST(LlvmLibcSharedBuiltinsTest, AllFloat128) {
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
+
+TEST(LlvmLibcSharedBuiltinsTest, ExtendConversion) {
+  EXPECT_FP_EQ(1.5, shared::extendsfdf2(1.5f));
+#ifdef LIBC_TYPES_HAS_FLOAT128
+  EXPECT_FP_EQ(float128(1.5), shared::extenddftf2(1.5));
+  EXPECT_FP_EQ(float128(1.5), shared::extendsftf2(1.5f));
+#if LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+  EXPECT_FP_EQ(float128(1.5), shared::extendxftf2(1.5L));
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+#endif // LIBC_TYPES_HAS_FLOAT128
+}
+
+TEST(LlvmLibcSharedBuiltinsTest, TruncateConversion) {
+  EXPECT_FP_EQ(1.5f, shared::truncdfsf2(1.5));
+#ifdef LIBC_TYPES_HAS_FLOAT128
+  EXPECT_FP_EQ(1.5, shared::trunctfdf2(float128(1.5)));
+  EXPECT_FP_EQ(1.5f, shared::trunctfsf2(float128(1.5)));
+#if LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+  EXPECT_FP_EQ(1.5L, shared::trunctfxf2(float128(1.5)));
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+#endif // LIBC_TYPES_HAS_FLOAT128
+}

>From 0aa026107cc3c023d2ba4cf8292d5f11f989d5ff Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 16 Jul 2026 09:52:57 +0300
Subject: [PATCH 2/4] fix format

---
 compiler-rt/lib/builtins/extendsfdf2.cpp | 2 +-
 compiler-rt/lib/builtins/truncdfsf2.cpp  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/builtins/extendsfdf2.cpp b/compiler-rt/lib/builtins/extendsfdf2.cpp
index e422d55c6f1fe..e7c48dbac0cd8 100644
--- a/compiler-rt/lib/builtins/extendsfdf2.cpp
+++ b/compiler-rt/lib/builtins/extendsfdf2.cpp
@@ -12,9 +12,9 @@
 ///
 //===----------------------------------------------------------------------===//
 
+#include "shared/builtins/extendsfdf2.h"
 #include "fp_libc_config.h"
 #include "int_lib.h"
-#include "shared/builtins/extendsfdf2.h"
 
 extern "C" COMPILER_RT_ABI double __extendsfdf2(float a) {
   return LIBC_NAMESPACE::shared::extendsfdf2(a);
diff --git a/compiler-rt/lib/builtins/truncdfsf2.cpp b/compiler-rt/lib/builtins/truncdfsf2.cpp
index 9d2281cb1c59d..775526dbff96a 100644
--- a/compiler-rt/lib/builtins/truncdfsf2.cpp
+++ b/compiler-rt/lib/builtins/truncdfsf2.cpp
@@ -12,9 +12,9 @@
 ///
 //===----------------------------------------------------------------------===//
 
+#include "shared/builtins/truncdfsf2.h"
 #include "fp_libc_config.h"
 #include "int_lib.h"
-#include "shared/builtins/truncdfsf2.h"
 
 extern "C" COMPILER_RT_ABI float __truncdfsf2(double a) {
   return LIBC_NAMESPACE::shared::truncdfsf2(a);

>From 8cc460eac240cafb7e7130961ea02b00bc37eb41 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 17 Jul 2026 10:07:15 +0300
Subject: [PATCH 3/4] fix syntax error

---
 libc/test/shared/shared_builtins_test.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index 44d9989cbf1e1..d2454ed4e5a91 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -46,7 +46,7 @@ TEST(LlvmLibcSharedBuiltinsTest, ExtendConversion) {
 #ifdef LIBC_TYPES_HAS_FLOAT128
   EXPECT_FP_EQ(float128(1.5), shared::extenddftf2(1.5));
   EXPECT_FP_EQ(float128(1.5), shared::extendsftf2(1.5f));
-#if LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
   EXPECT_FP_EQ(float128(1.5), shared::extendxftf2(1.5L));
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
 #endif // LIBC_TYPES_HAS_FLOAT128
@@ -57,7 +57,7 @@ TEST(LlvmLibcSharedBuiltinsTest, TruncateConversion) {
 #ifdef LIBC_TYPES_HAS_FLOAT128
   EXPECT_FP_EQ(1.5, shared::trunctfdf2(float128(1.5)));
   EXPECT_FP_EQ(1.5f, shared::trunctfsf2(float128(1.5)));
-#if LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
   EXPECT_FP_EQ(1.5L, shared::trunctfxf2(float128(1.5)));
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
 #endif // LIBC_TYPES_HAS_FLOAT128

>From a4c7c2f665ec7ebdf9b5613bc5fc2c40ac31985a Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 24 Jul 2026 19:13:43 +0300
Subject: [PATCH 4/4] `if defined()` to `ifdef`

---
 compiler-rt/lib/builtins/extenddftf2.cpp | 2 +-
 compiler-rt/lib/builtins/extendsftf2.cpp | 2 +-
 compiler-rt/lib/builtins/trunctfdf2.cpp  | 2 +-
 compiler-rt/lib/builtins/trunctfsf2.cpp  | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/lib/builtins/extenddftf2.cpp b/compiler-rt/lib/builtins/extenddftf2.cpp
index 268ca6cdde6b8..38f201316a1cd 100644
--- a/compiler-rt/lib/builtins/extenddftf2.cpp
+++ b/compiler-rt/lib/builtins/extenddftf2.cpp
@@ -18,7 +18,7 @@
 #include "fp_libc_config.h"
 #include "shared/builtins/extenddftf2.h"
 
-#if defined(CRT_HAS_TF_MODE)
+#ifdef CRT_HAS_TF_MODE
 extern "C" COMPILER_RT_ABI tf_float __extenddftf2(double a) {
   return LIBC_NAMESPACE::shared::extenddftf2(a);
 }
diff --git a/compiler-rt/lib/builtins/extendsftf2.cpp b/compiler-rt/lib/builtins/extendsftf2.cpp
index 8f90aa6282009..0c6cc83b9d142 100644
--- a/compiler-rt/lib/builtins/extendsftf2.cpp
+++ b/compiler-rt/lib/builtins/extendsftf2.cpp
@@ -18,7 +18,7 @@
 #include "fp_libc_config.h"
 #include "shared/builtins/extendsftf2.h"
 
-#if defined(CRT_HAS_TF_MODE)
+#ifdef CRT_HAS_TF_MODE
 extern "C" COMPILER_RT_ABI tf_float __extendsftf2(float a) {
   return LIBC_NAMESPACE::shared::extendsftf2(a);
 }
diff --git a/compiler-rt/lib/builtins/trunctfdf2.cpp b/compiler-rt/lib/builtins/trunctfdf2.cpp
index f381d2768d6b1..487a09bba0071 100644
--- a/compiler-rt/lib/builtins/trunctfdf2.cpp
+++ b/compiler-rt/lib/builtins/trunctfdf2.cpp
@@ -18,7 +18,7 @@
 #include "fp_libc_config.h"
 #include "shared/builtins/trunctfdf2.h"
 
-#if defined(CRT_HAS_TF_MODE)
+#ifdef CRT_HAS_TF_MODE
 extern "C" COMPILER_RT_ABI double __trunctfdf2(tf_float a) {
   return LIBC_NAMESPACE::shared::trunctfdf2(a);
 }
diff --git a/compiler-rt/lib/builtins/trunctfsf2.cpp b/compiler-rt/lib/builtins/trunctfsf2.cpp
index aac13c25b4b28..2486cb3a39a7f 100644
--- a/compiler-rt/lib/builtins/trunctfsf2.cpp
+++ b/compiler-rt/lib/builtins/trunctfsf2.cpp
@@ -18,7 +18,7 @@
 #include "fp_libc_config.h"
 #include "shared/builtins/trunctfsf2.h"
 
-#if defined(CRT_HAS_TF_MODE)
+#ifdef CRT_HAS_TF_MODE
 extern "C" COMPILER_RT_ABI float __trunctfsf2(tf_float a) {
   return LIBC_NAMESPACE::shared::trunctfsf2(a);
 }



More information about the libc-commits mailing list