[libc-commits] [compiler-rt] [libc] [compiler-rt][builtins] libc-backed float16 extend/truncate builtins (PR #211882)

via libc-commits libc-commits at lists.llvm.org
Sat Aug 1 12:32:09 PDT 2026


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

>From 15bf717fc55cbd24e875a407b4791402e1979786 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/7] [compiler-rt][builtins] libc-backed floating-point
 extend/truncate builtins

---
 compiler-rt/lib/builtins/CMakeLists.txt       |  74 +++++---
 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                        |  10 ++
 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    | 159 ++++++++++++++++++
 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               |  10 ++
 libc/test/shared/shared_builtins_test.cpp     |  24 +++
 30 files changed, 1083 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 0d52b582a8f26..e3c449a4f109b 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -243,6 +243,59 @@ 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)
+
+# Swap the legacy soft-float <name>.c builtin in <list> for the libc-backed
+# <name>.cpp implementation (which delegates to LIBC_NAMESPACE::shared::*).
+macro(use_libc_builtin list_var name)
+  list(REMOVE_ITEM ${list_var} ${name}.c)
+  list(APPEND ${list_var} ${name}.cpp)
+endmacro()
+
+if(COMPILER_RT_USE_LIBC_MATH)
+  include(FindLibcCommonUtils)
+  if(NOT TARGET llvm-libc-common-utilities)
+    message(FATAL_ERROR "LLVM libc is not found at ${libc_path}.")
+  endif()
+
+  get_target_property(_libc_include_dirs llvm-libc-common-utilities
+                      INTERFACE_INCLUDE_DIRECTORIES)
+  include_directories(SYSTEM ${_libc_include_dirs})
+  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
   "Skip the atomic builtin (these should normally be provided by a shared library)"
   On)
@@ -312,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 dca0171c02d18..72f54b3bf69c6 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -20,7 +20,17 @@
 #include "builtins/adddf3.h"
 #include "builtins/addtf3.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/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 dee5a830b8857..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
@@ -46,3 +62,146 @@ add_header_library(
     libc.src.__support.FPUtil.generic.add_sub
     libc.src.__support.macros.config
 )
+
+add_header_library(
+  subdf3
+  HDRS
+    subdf3.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  muldf3
+  HDRS
+    muldf3.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.mul
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  divdf3
+  HDRS
+    divdf3.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.div
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  addsf3
+  HDRS
+    addsf3.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  subsf3
+  HDRS
+    subsf3.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  mulsf3
+  HDRS
+    mulsf3.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.mul
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  divsf3
+  HDRS
+    divsf3.h
+  DEPENDS
+    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 120524bed5ead..69c46247dfcd9 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -830,8 +830,18 @@ add_fp_unittest(
     libc.src.__support.builtins.adddf3
     libc.src.__support.builtins.addtf3
     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.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 4960c7abe6b04..5f84fa58a2333 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) {
   // TODO: assertions for shared::*sf3 builtins.
 }
@@ -32,3 +34,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 12cf13875be4de80c51a6e1e26fcdb460186d7b5 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/7] 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 c3b8edcaa9d42601d293ab9d5c4313afe1f8b44d 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/7] 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 5f84fa58a2333..c378482c0bf19 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -40,7 +40,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
@@ -51,7 +51,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 21823f90fa660afdb330996c86f642b9f3a150fe 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/7] `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);
 }

>From 9d8b0766706704ac483ee81f5c6d8e0a783f1c45 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 24 Jul 2026 20:33:18 +0300
Subject: [PATCH 5/7] [compiler-rt][builtins] libc-backed bfloat16
 extend/truncate builtins

---
 compiler-rt/lib/builtins/CMakeLists.txt    |  4 +++
 compiler-rt/lib/builtins/extendbfsf2.cpp   | 24 +++++++++++++
 compiler-rt/lib/builtins/truncdfbf2.cpp    | 24 +++++++++++++
 compiler-rt/lib/builtins/truncsfbf2.cpp    | 24 +++++++++++++
 compiler-rt/lib/builtins/trunctfbf2.cpp    | 29 +++++++++++++++
 libc/shared/builtins.h                     |  4 +++
 libc/shared/builtins/extendbfsf2.h         | 29 +++++++++++++++
 libc/shared/builtins/truncdfbf2.h          | 29 +++++++++++++++
 libc/shared/builtins/truncsfbf2.h          | 29 +++++++++++++++
 libc/shared/builtins/trunctfbf2.h          | 35 ++++++++++++++++++
 libc/src/__support/builtins/CMakeLists.txt | 42 ++++++++++++++++++++++
 libc/src/__support/builtins/extendbfsf2.h  | 35 ++++++++++++++++++
 libc/src/__support/builtins/truncdfbf2.h   | 33 +++++++++++++++++
 libc/src/__support/builtins/truncsfbf2.h   | 33 +++++++++++++++++
 libc/src/__support/builtins/trunctfbf2.h   | 39 ++++++++++++++++++++
 libc/test/shared/CMakeLists.txt            |  4 +++
 libc/test/shared/shared_builtins_test.cpp  |  6 ++++
 17 files changed, 423 insertions(+)
 create mode 100644 compiler-rt/lib/builtins/extendbfsf2.cpp
 create mode 100644 compiler-rt/lib/builtins/truncdfbf2.cpp
 create mode 100644 compiler-rt/lib/builtins/truncsfbf2.cpp
 create mode 100644 compiler-rt/lib/builtins/trunctfbf2.cpp
 create mode 100644 libc/shared/builtins/extendbfsf2.h
 create mode 100644 libc/shared/builtins/truncdfbf2.h
 create mode 100644 libc/shared/builtins/truncsfbf2.h
 create mode 100644 libc/shared/builtins/trunctfbf2.h
 create mode 100644 libc/src/__support/builtins/extendbfsf2.h
 create mode 100644 libc/src/__support/builtins/truncdfbf2.h
 create mode 100644 libc/src/__support/builtins/truncsfbf2.h
 create mode 100644 libc/src/__support/builtins/trunctfbf2.h

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index e3c449a4f109b..122449f13ddbd 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -286,11 +286,15 @@ if(COMPILER_RT_USE_LIBC_MATH)
   add_definitions(-DCOMPILER_RT_USE_LIBC_MATH)
 
   use_libc_builtin(GENERIC_TF_SOURCES addtf3)
+  use_libc_builtin(BF16_SOURCES       extendbfsf2)
   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(BF16_SOURCES       truncdfbf2)
+  use_libc_builtin(BF16_SOURCES       truncsfbf2)
+  use_libc_builtin(BF16_SOURCES       trunctfbf2)
   use_libc_builtin(GENERIC_TF_SOURCES trunctfdf2)
   use_libc_builtin(GENERIC_TF_SOURCES trunctfsf2)
   use_libc_builtin(x86_80_BIT_SOURCES trunctfxf2)
diff --git a/compiler-rt/lib/builtins/extendbfsf2.cpp b/compiler-rt/lib/builtins/extendbfsf2.cpp
new file mode 100644
index 0000000000000..74c56803d5c65
--- /dev/null
+++ b/compiler-rt/lib/builtins/extendbfsf2.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendbfsf2, extend bfloat16 to float,
+/// on top of LLVM-libc's shared::extendbfsf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define SRC_BFLOAT16
+#define DST_SINGLE
+#include "fp_extend.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/extendbfsf2.h"
+
+extern "C" COMPILER_RT_ABI dst_t __extendbfsf2(src_t a) {
+  return LIBC_NAMESPACE::shared::extendbfsf2(__builtin_bit_cast(uint16_t, a));
+}
diff --git a/compiler-rt/lib/builtins/truncdfbf2.cpp b/compiler-rt/lib/builtins/truncdfbf2.cpp
new file mode 100644
index 0000000000000..a5ae505b03553
--- /dev/null
+++ b/compiler-rt/lib/builtins/truncdfbf2.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __truncdfbf2, truncate double to
+/// bfloat16, on top of LLVM-libc's shared::truncdfbf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define SRC_DOUBLE
+#define DST_BFLOAT
+#include "fp_trunc.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/truncdfbf2.h"
+
+extern "C" COMPILER_RT_ABI dst_t __truncdfbf2(src_t a) {
+  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::truncdfbf2(a));
+}
diff --git a/compiler-rt/lib/builtins/truncsfbf2.cpp b/compiler-rt/lib/builtins/truncsfbf2.cpp
new file mode 100644
index 0000000000000..91011f086ad85
--- /dev/null
+++ b/compiler-rt/lib/builtins/truncsfbf2.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __truncsfbf2, truncate float to bfloat16,
+/// on top of LLVM-libc's shared::truncsfbf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define SRC_SINGLE
+#define DST_BFLOAT
+#include "fp_trunc.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/truncsfbf2.h"
+
+extern "C" COMPILER_RT_ABI dst_t __truncsfbf2(src_t a) {
+  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::truncsfbf2(a));
+}
diff --git a/compiler-rt/lib/builtins/trunctfbf2.cpp b/compiler-rt/lib/builtins/trunctfbf2.cpp
new file mode 100644
index 0000000000000..d789111907fd3
--- /dev/null
+++ b/compiler-rt/lib/builtins/trunctfbf2.cpp
@@ -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 file implements compiler-rt's __trunctfbf2, truncate float128 to
+/// bfloat16, on top of LLVM-libc's shared::trunctfbf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/trunctfbf2.h"
+
+#if defined(CRT_HAS_TF_MODE) && defined(__x86_64__)
+#define SRC_QUAD
+#define DST_BFLOAT
+#include "fp_trunc.h"
+
+extern "C" COMPILER_RT_ABI dst_t __trunctfbf2(src_t a) {
+  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::trunctfbf2(a));
+}
+#endif
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
index 72f54b3bf69c6..1248bf3ff2cb1 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -20,6 +20,7 @@
 #include "builtins/adddf3.h"
 #include "builtins/addtf3.h"
 #include "builtins/divtf3.h"
+#include "builtins/extendbfsf2.h"
 #include "builtins/extenddftf2.h"
 #include "builtins/extendsfdf2.h"
 #include "builtins/extendsftf2.h"
@@ -28,7 +29,10 @@
 #include "builtins/mulsf3.h"
 #include "builtins/multf3.h"
 #include "builtins/subtf3.h"
+#include "builtins/truncdfbf2.h"
 #include "builtins/truncdfsf2.h"
+#include "builtins/truncsfbf2.h"
+#include "builtins/trunctfbf2.h"
 #include "builtins/trunctfdf2.h"
 #include "builtins/trunctfsf2.h"
 #include "builtins/trunctfxf2.h"
diff --git a/libc/shared/builtins/extendbfsf2.h b/libc/shared/builtins/extendbfsf2.h
new file mode 100644
index 0000000000000..f660301a5e31c
--- /dev/null
+++ b/libc/shared/builtins/extendbfsf2.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 __extendbfsf2 implementation as
+/// shared::extendbfsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDBFSF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_EXTENDBFSF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/extendbfsf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::extendbfsf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDBFSF2_H
diff --git a/libc/shared/builtins/truncdfbf2.h b/libc/shared/builtins/truncdfbf2.h
new file mode 100644
index 0000000000000..2846c484fa813
--- /dev/null
+++ b/libc/shared/builtins/truncdfbf2.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 __truncdfbf2 implementation as
+/// shared::truncdfbf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCDFBF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCDFBF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/truncdfbf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::truncdfbf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCDFBF2_H
diff --git a/libc/shared/builtins/truncsfbf2.h b/libc/shared/builtins/truncsfbf2.h
new file mode 100644
index 0000000000000..93f4bfb256f84
--- /dev/null
+++ b/libc/shared/builtins/truncsfbf2.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 __truncsfbf2 implementation as
+/// shared::truncsfbf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCSFBF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCSFBF2_H
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/truncsfbf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::truncsfbf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCSFBF2_H
diff --git a/libc/shared/builtins/trunctfbf2.h b/libc/shared/builtins/trunctfbf2.h
new file mode 100644
index 0000000000000..76aca0aa13565
--- /dev/null
+++ b/libc/shared/builtins/trunctfbf2.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 __trunctfbf2 implementation as
+/// shared::trunctfbf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCTFBF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCTFBF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/trunctfbf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::trunctfbf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCTFBF2_H
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index 91ace33d43014..2113fe26f2cef 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -205,3 +205,45 @@ add_header_library(
     libc.src.__support.builtins.fpconvert_helper
     libc.src.__support.macros.config
 )
+
+add_header_library(
+  extendbfsf2
+  HDRS
+    extendbfsf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  truncsfbf2
+  HDRS
+    truncsfbf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  truncdfbf2
+  HDRS
+    truncdfbf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  trunctfbf2
+  HDRS
+    trunctfbf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+)
+
diff --git a/libc/src/__support/builtins/extendbfsf2.h b/libc/src/__support/builtins/extendbfsf2.h
new file mode 100644
index 0000000000000..1ce0009a71055
--- /dev/null
+++ b/libc/src/__support/builtins/extendbfsf2.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 __extendbfsf2 implementation as
+/// builtins::extendbfsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDBFSF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDBFSF2_H
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Extend bfloat16 to float; mirrors compiler-rt's __extendbfsf2.
+LIBC_INLINE float extendbfsf2(uint16_t bits) {
+  bfloat16 x;
+  x.bits = bits;
+  return fputil::cast<float>(x);
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDBFSF2_H
diff --git a/libc/src/__support/builtins/truncdfbf2.h b/libc/src/__support/builtins/truncdfbf2.h
new file mode 100644
index 0000000000000..4bccb2db9de6d
--- /dev/null
+++ b/libc/src/__support/builtins/truncdfbf2.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __truncdfbf2 implementation as
+/// builtins::truncdfbf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFBF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFBF2_H
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate double to bfloat16; mirrors compiler-rt's __truncdfbf2.
+LIBC_INLINE uint16_t truncdfbf2(double x) {
+  return fputil::cast<bfloat16>(x).bits;
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFBF2_H
diff --git a/libc/src/__support/builtins/truncsfbf2.h b/libc/src/__support/builtins/truncsfbf2.h
new file mode 100644
index 0000000000000..08611f20fb2a2
--- /dev/null
+++ b/libc/src/__support/builtins/truncsfbf2.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __truncsfbf2 implementation as
+/// builtins::truncsfbf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFBF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFBF2_H
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate float to bfloat16; mirrors compiler-rt's __truncsfbf2.
+LIBC_INLINE uint16_t truncsfbf2(float x) {
+  return fputil::cast<bfloat16>(x).bits;
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFBF2_H
diff --git a/libc/src/__support/builtins/trunctfbf2.h b/libc/src/__support/builtins/trunctfbf2.h
new file mode 100644
index 0000000000000..99e2833c39058
--- /dev/null
+++ b/libc/src/__support/builtins/trunctfbf2.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __trunctfbf2 implementation as
+/// builtins::trunctfbf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFBF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFBF2_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate float128 to bfloat16; mirrors compiler-rt's __trunctfbf2.
+LIBC_INLINE uint16_t trunctfbf2(float128 x) {
+  return fputil::cast<bfloat16>(x).bits;
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFBF2_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 69c46247dfcd9..96ec6eff5e324 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -830,6 +830,7 @@ add_fp_unittest(
     libc.src.__support.builtins.adddf3
     libc.src.__support.builtins.addtf3
     libc.src.__support.builtins.divtf3
+    libc.src.__support.builtins.extendbfsf2
     libc.src.__support.builtins.extenddftf2
     libc.src.__support.builtins.extendsfdf2
     libc.src.__support.builtins.extendsftf2
@@ -838,7 +839,10 @@ add_fp_unittest(
     libc.src.__support.builtins.mulsf3
     libc.src.__support.builtins.multf3
     libc.src.__support.builtins.subtf3
+    libc.src.__support.builtins.truncdfbf2
     libc.src.__support.builtins.truncdfsf2
+    libc.src.__support.builtins.truncsfbf2
+    libc.src.__support.builtins.trunctfbf2
     libc.src.__support.builtins.trunctfdf2
     libc.src.__support.builtins.trunctfsf2
     libc.src.__support.builtins.trunctfxf2
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index c378482c0bf19..77d4a315d8eac 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -37,6 +37,7 @@ TEST(LlvmLibcSharedBuiltinsTest, AllFloat128) {
 
 TEST(LlvmLibcSharedBuiltinsTest, ExtendConversion) {
   EXPECT_FP_EQ(1.5, shared::extendsfdf2(1.5f));
+  EXPECT_FP_EQ(1.5f, shared::extendbfsf2(static_cast<uint16_t>(0x3FC0)));
 #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));
@@ -47,7 +48,12 @@ TEST(LlvmLibcSharedBuiltinsTest, ExtendConversion) {
 }
 
 TEST(LlvmLibcSharedBuiltinsTest, TruncateConversion) {
+  EXPECT_EQ(static_cast<uint16_t>(0x3FC0), shared::truncdfbf2(1.5));
   EXPECT_FP_EQ(1.5f, shared::truncdfsf2(1.5));
+  EXPECT_EQ(static_cast<uint16_t>(0x3FC0), shared::truncsfbf2(1.5f));
+#ifdef LIBC_TYPES_HAS_FLOAT128
+  EXPECT_EQ(static_cast<uint16_t>(0x3FC0), shared::trunctfbf2(float128(1.5)));
+#endif // LIBC_TYPES_HAS_FLOAT128
 #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)));

>From 8ceac3a511f7ed706443830495260f143eff3281 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 24 Jul 2026 20:53:10 +0300
Subject: [PATCH 6/7] [compiler-rt][builtins] libc-backed float16
 extend/truncate builtins

---
 compiler-rt/lib/builtins/CMakeLists.txt       |  6 ++
 compiler-rt/lib/builtins/extendhfdf2.cpp      | 30 ++++++++
 compiler-rt/lib/builtins/extendhfsf2.cpp      | 44 ++++++++++++
 compiler-rt/lib/builtins/extendhftf2.cpp      | 35 ++++++++++
 compiler-rt/lib/builtins/truncdfhf2.cpp       | 30 ++++++++
 compiler-rt/lib/builtins/truncsfhf2.cpp       | 44 ++++++++++++
 compiler-rt/lib/builtins/trunctfhf2.cpp       | 35 ++++++++++
 libc/shared/builtins.h                        |  6 ++
 libc/shared/builtins/extendhfdf2.h            | 35 ++++++++++
 libc/shared/builtins/extendhfsf2.h            | 35 ++++++++++
 libc/shared/builtins/extendhftf2.h            | 36 ++++++++++
 libc/shared/builtins/truncdfhf2.h             | 35 ++++++++++
 libc/shared/builtins/truncsfhf2.h             | 35 ++++++++++
 libc/shared/builtins/trunctfhf2.h             | 36 ++++++++++
 libc/src/__support/builtins/CMakeLists.txt    | 70 +++++++++++++++++++
 libc/src/__support/builtins/extendhfdf2.h     | 39 +++++++++++
 libc/src/__support/builtins/extendhfsf2.h     | 39 +++++++++++
 libc/src/__support/builtins/extendhftf2.h     | 40 +++++++++++
 .../src/__support/builtins/fpconvert_helper.h | 35 ++++++++--
 libc/src/__support/builtins/truncdfhf2.h      | 40 +++++++++++
 libc/src/__support/builtins/truncsfhf2.h      | 40 +++++++++++
 libc/src/__support/builtins/trunctfhf2.h      | 41 +++++++++++
 libc/test/shared/CMakeLists.txt               |  6 ++
 23 files changed, 785 insertions(+), 7 deletions(-)
 create mode 100644 compiler-rt/lib/builtins/extendhfdf2.cpp
 create mode 100644 compiler-rt/lib/builtins/extendhfsf2.cpp
 create mode 100644 compiler-rt/lib/builtins/extendhftf2.cpp
 create mode 100644 compiler-rt/lib/builtins/truncdfhf2.cpp
 create mode 100644 compiler-rt/lib/builtins/truncsfhf2.cpp
 create mode 100644 compiler-rt/lib/builtins/trunctfhf2.cpp
 create mode 100644 libc/shared/builtins/extendhfdf2.h
 create mode 100644 libc/shared/builtins/extendhfsf2.h
 create mode 100644 libc/shared/builtins/extendhftf2.h
 create mode 100644 libc/shared/builtins/truncdfhf2.h
 create mode 100644 libc/shared/builtins/truncsfhf2.h
 create mode 100644 libc/shared/builtins/trunctfhf2.h
 create mode 100644 libc/src/__support/builtins/extendhfdf2.h
 create mode 100644 libc/src/__support/builtins/extendhfsf2.h
 create mode 100644 libc/src/__support/builtins/extendhftf2.h
 create mode 100644 libc/src/__support/builtins/truncdfhf2.h
 create mode 100644 libc/src/__support/builtins/truncsfhf2.h
 create mode 100644 libc/src/__support/builtins/trunctfhf2.h

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 122449f13ddbd..6a9e600e8a1e0 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -288,6 +288,9 @@ if(COMPILER_RT_USE_LIBC_MATH)
   use_libc_builtin(GENERIC_TF_SOURCES addtf3)
   use_libc_builtin(BF16_SOURCES       extendbfsf2)
   use_libc_builtin(GENERIC_TF_SOURCES extenddftf2)
+  use_libc_builtin(GENERIC_SOURCES    extendhfsf2)
+  use_libc_builtin(GENERIC_SOURCES    extendhfdf2)
+  use_libc_builtin(GENERIC_TF_SOURCES extendhftf2)
   use_libc_builtin(GENERIC_SOURCES    extendsfdf2)
   use_libc_builtin(GENERIC_TF_SOURCES extendsftf2)
   use_libc_builtin(x86_80_BIT_SOURCES extendxftf2)
@@ -296,6 +299,9 @@ if(COMPILER_RT_USE_LIBC_MATH)
   use_libc_builtin(BF16_SOURCES       truncsfbf2)
   use_libc_builtin(BF16_SOURCES       trunctfbf2)
   use_libc_builtin(GENERIC_TF_SOURCES trunctfdf2)
+  use_libc_builtin(GENERIC_SOURCES    truncsfhf2)
+  use_libc_builtin(GENERIC_SOURCES    truncdfhf2)
+  use_libc_builtin(GENERIC_TF_SOURCES trunctfhf2)
   use_libc_builtin(GENERIC_TF_SOURCES trunctfsf2)
   use_libc_builtin(x86_80_BIT_SOURCES trunctfxf2)
 endif()
diff --git a/compiler-rt/lib/builtins/extendhfdf2.cpp b/compiler-rt/lib/builtins/extendhfdf2.cpp
new file mode 100644
index 0000000000000..f1d40971b7f45
--- /dev/null
+++ b/compiler-rt/lib/builtins/extendhfdf2.cpp
@@ -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 file implements compiler-rt's __extendhfdf2, extend float16 to double,
+/// on top of LLVM-libc's shared::extendhfdf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define SRC_HALF
+#define DST_DOUBLE
+#include "fp_extend_impl.inc"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/extendhfdf2.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT16)
+extern "C" COMPILER_RT_ABI NOINLINE dst_t __extendhfdf2(src_t a) {
+  return LIBC_NAMESPACE::shared::extendhfdf2(__builtin_bit_cast(uint16_t, a));
+}
+#else
+extern "C" COMPILER_RT_ABI NOINLINE dst_t __extendhfdf2(src_t a) {
+  return __extendXfYf2__(a);
+}
+#endif
diff --git a/compiler-rt/lib/builtins/extendhfsf2.cpp b/compiler-rt/lib/builtins/extendhfsf2.cpp
new file mode 100644
index 0000000000000..6dc3ef17c22d5
--- /dev/null
+++ b/compiler-rt/lib/builtins/extendhfsf2.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendhfsf2, extend float16 to float,
+/// on top of LLVM-libc's shared::extendhfsf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define SRC_HALF
+#define DST_SINGLE
+#include "fp_extend_impl.inc"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/extendhfsf2.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT16)
+extern "C" COMPILER_RT_ABI NOINLINE dst_t __extendhfsf2(src_t a) {
+  return LIBC_NAMESPACE::shared::extendhfsf2(__builtin_bit_cast(uint16_t, a));
+}
+#else
+extern "C" COMPILER_RT_ABI NOINLINE dst_t __extendhfsf2(src_t a) {
+  return __extendXfYf2__(a);
+}
+#endif
+
+extern "C" {
+#if defined(__ARM_EABI__)
+#if defined(COMPILER_RT_ARMHF_TARGET)
+AEABI_RTABI dst_t __gnu_h2f_ieee(src_t a) { return __extendhfsf2(a); }
+AEABI_RTABI dst_t __aeabi_h2f(src_t a) { return __extendhfsf2(a); }
+#else
+COMPILER_RT_ALIAS(__extendhfsf2, __gnu_h2f_ieee)
+COMPILER_RT_ALIAS(__extendhfsf2, __aeabi_h2f)
+#endif
+#else
+COMPILER_RT_ABI dst_t __gnu_h2f_ieee(src_t a) { return __extendhfsf2(a); }
+#endif
+}
diff --git a/compiler-rt/lib/builtins/extendhftf2.cpp b/compiler-rt/lib/builtins/extendhftf2.cpp
new file mode 100644
index 0000000000000..2b035ee639b22
--- /dev/null
+++ b/compiler-rt/lib/builtins/extendhftf2.cpp
@@ -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 file implements compiler-rt's __extendhftf2, extend float16 to
+/// float128, on top of LLVM-libc's shared::extendhftf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/extendhftf2.h"
+
+#if defined(CRT_HAS_TF_MODE) && defined(COMPILER_RT_HAS_FLOAT16)
+#define SRC_HALF
+#define DST_QUAD
+#include "fp_extend_impl.inc"
+
+#if defined(LIBC_TYPES_HAS_FLOAT16)
+extern "C" COMPILER_RT_ABI dst_t __extendhftf2(src_t a) {
+  return LIBC_NAMESPACE::shared::extendhftf2(__builtin_bit_cast(uint16_t, a));
+}
+#else
+extern "C" COMPILER_RT_ABI dst_t __extendhftf2(src_t a) {
+  return __extendXfYf2__(a);
+}
+#endif
+#endif
diff --git a/compiler-rt/lib/builtins/truncdfhf2.cpp b/compiler-rt/lib/builtins/truncdfhf2.cpp
new file mode 100644
index 0000000000000..ab08483cdabfa
--- /dev/null
+++ b/compiler-rt/lib/builtins/truncdfhf2.cpp
@@ -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 file implements compiler-rt's __truncdfhf2, truncate double to float16,
+/// on top of LLVM-libc's shared::truncdfhf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define SRC_DOUBLE
+#define DST_HALF
+#include "fp_trunc_impl.inc"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/truncdfhf2.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT16)
+extern "C" COMPILER_RT_ABI dst_t __truncdfhf2(src_t a) {
+  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::truncdfhf2(a));
+}
+#else
+extern "C" COMPILER_RT_ABI dst_t __truncdfhf2(src_t a) {
+  return __truncXfYf2__(a);
+}
+#endif
diff --git a/compiler-rt/lib/builtins/truncsfhf2.cpp b/compiler-rt/lib/builtins/truncsfhf2.cpp
new file mode 100644
index 0000000000000..3fa614d2970fe
--- /dev/null
+++ b/compiler-rt/lib/builtins/truncsfhf2.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __truncsfhf2, truncate float to float16,
+/// on top of LLVM-libc's shared::truncsfhf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define SRC_SINGLE
+#define DST_HALF
+#include "fp_trunc_impl.inc"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/truncsfhf2.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT16)
+extern "C" COMPILER_RT_ABI NOINLINE dst_t __truncsfhf2(src_t a) {
+  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::truncsfhf2(a));
+}
+#else
+extern "C" COMPILER_RT_ABI NOINLINE dst_t __truncsfhf2(src_t a) {
+  return __truncXfYf2__(a);
+}
+#endif
+
+extern "C" {
+#if defined(__ARM_EABI__)
+#if defined(COMPILER_RT_ARMHF_TARGET)
+AEABI_RTABI dst_t __gnu_f2h_ieee(float a) { return __truncsfhf2(a); }
+AEABI_RTABI dst_t __aeabi_f2h(float a) { return __truncsfhf2(a); }
+#else
+COMPILER_RT_ALIAS(__truncsfhf2, __gnu_f2h_ieee)
+COMPILER_RT_ALIAS(__truncsfhf2, __aeabi_f2h)
+#endif
+#else
+COMPILER_RT_ABI dst_t __gnu_f2h_ieee(float a) { return __truncsfhf2(a); }
+#endif
+}
diff --git a/compiler-rt/lib/builtins/trunctfhf2.cpp b/compiler-rt/lib/builtins/trunctfhf2.cpp
new file mode 100644
index 0000000000000..a1c06a9415229
--- /dev/null
+++ b/compiler-rt/lib/builtins/trunctfhf2.cpp
@@ -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 file implements compiler-rt's __trunctfhf2, truncate float128 to
+/// float16, on top of LLVM-libc's shared::trunctfhf2.
+///
+//===----------------------------------------------------------------------===//
+
+#define QUAD_PRECISION
+#include "fp_lib.h"
+
+#include "fp_libc_config.h"
+#include "shared/builtins/trunctfhf2.h"
+
+#if defined(CRT_HAS_TF_MODE) && defined(COMPILER_RT_HAS_FLOAT16)
+#define SRC_QUAD
+#define DST_HALF
+#include "fp_trunc_impl.inc"
+
+#if defined(LIBC_TYPES_HAS_FLOAT16)
+extern "C" COMPILER_RT_ABI dst_t __trunctfhf2(src_t a) {
+  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::trunctfhf2(a));
+}
+#else
+extern "C" COMPILER_RT_ABI dst_t __trunctfhf2(src_t a) {
+  return __truncXfYf2__(a);
+}
+#endif
+#endif
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
index 1248bf3ff2cb1..6d28476c511f3 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -22,6 +22,9 @@
 #include "builtins/divtf3.h"
 #include "builtins/extendbfsf2.h"
 #include "builtins/extenddftf2.h"
+#include "builtins/extendhfdf2.h"
+#include "builtins/extendhfsf2.h"
+#include "builtins/extendhftf2.h"
 #include "builtins/extendsfdf2.h"
 #include "builtins/extendsftf2.h"
 #include "builtins/extendxftf2.h"
@@ -30,10 +33,13 @@
 #include "builtins/multf3.h"
 #include "builtins/subtf3.h"
 #include "builtins/truncdfbf2.h"
+#include "builtins/truncdfhf2.h"
 #include "builtins/truncdfsf2.h"
 #include "builtins/truncsfbf2.h"
+#include "builtins/truncsfhf2.h"
 #include "builtins/trunctfbf2.h"
 #include "builtins/trunctfdf2.h"
+#include "builtins/trunctfhf2.h"
 #include "builtins/trunctfsf2.h"
 #include "builtins/trunctfxf2.h"
 
diff --git a/libc/shared/builtins/extendhfdf2.h b/libc/shared/builtins/extendhfdf2.h
new file mode 100644
index 0000000000000..ebae43abfbf3b
--- /dev/null
+++ b/libc/shared/builtins/extendhfdf2.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 __extendhfdf2 implementation as
+/// shared::extendhfdf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDHFDF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_EXTENDHFDF2_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/extendhfdf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::extendhfdf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDHFDF2_H
diff --git a/libc/shared/builtins/extendhfsf2.h b/libc/shared/builtins/extendhfsf2.h
new file mode 100644
index 0000000000000..346961746bd52
--- /dev/null
+++ b/libc/shared/builtins/extendhfsf2.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 __extendhfsf2 implementation as
+/// shared::extendhfsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDHFSF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_EXTENDHFSF2_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/extendhfsf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::extendhfsf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDHFSF2_H
diff --git a/libc/shared/builtins/extendhftf2.h b/libc/shared/builtins/extendhftf2.h
new file mode 100644
index 0000000000000..28a3331fcfc5d
--- /dev/null
+++ b/libc/shared/builtins/extendhftf2.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 __extendhftf2 implementation as
+/// shared::extendhftf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDHFTF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_EXTENDHFTF2_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT128) && defined(LIBC_TYPES_HAS_FLOAT16)
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/extendhftf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::extendhftf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDHFTF2_H
diff --git a/libc/shared/builtins/truncdfhf2.h b/libc/shared/builtins/truncdfhf2.h
new file mode 100644
index 0000000000000..697261cc261e1
--- /dev/null
+++ b/libc/shared/builtins/truncdfhf2.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 __truncdfhf2 implementation as
+/// shared::truncdfhf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCDFHF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCDFHF2_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/truncdfhf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::truncdfhf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCDFHF2_H
diff --git a/libc/shared/builtins/truncsfhf2.h b/libc/shared/builtins/truncsfhf2.h
new file mode 100644
index 0000000000000..de011638e7c12
--- /dev/null
+++ b/libc/shared/builtins/truncsfhf2.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 __truncsfhf2 implementation as
+/// shared::truncsfhf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCSFHF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCSFHF2_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/truncsfhf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::truncsfhf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCSFHF2_H
diff --git a/libc/shared/builtins/trunctfhf2.h b/libc/shared/builtins/trunctfhf2.h
new file mode 100644
index 0000000000000..00134f87c9978
--- /dev/null
+++ b/libc/shared/builtins/trunctfhf2.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 __trunctfhf2 implementation as
+/// shared::trunctfhf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCTFHF2_H
+#define LLVM_LIBC_SHARED_BUILTINS_TRUNCTFHF2_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT128) && defined(LIBC_TYPES_HAS_FLOAT16)
+
+#include "shared/libc_common.h"
+#include "src/__support/builtins/trunctfhf2.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using builtins::trunctfhf2;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCTFHF2_H
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index 2113fe26f2cef..87fce9f38e3d4 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -247,3 +247,73 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  extendhfsf2
+  HDRS
+    extendhfsf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  extendhfdf2
+  HDRS
+    extendhfdf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.builtins.fpconvert_helper
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  extendhftf2
+  HDRS
+    extendhftf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    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(
+  truncsfhf2
+  HDRS
+    truncsfhf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.CPP.bit
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  truncdfhf2
+  HDRS
+    truncdfhf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.CPP.bit
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  trunctfhf2
+  HDRS
+    trunctfhf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.CPP.bit
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
diff --git a/libc/src/__support/builtins/extendhfdf2.h b/libc/src/__support/builtins/extendhfdf2.h
new file mode 100644
index 0000000000000..36e747dc8112c
--- /dev/null
+++ b/libc/src/__support/builtins/extendhfdf2.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendhfdf2 implementation as
+/// builtins::extendhfdf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFDF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFDF2_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Extend float16 to double; mirrors compiler-rt's __extendhfdf2.
+LIBC_INLINE double extendhfdf2(uint16_t bits) {
+  return fpconvert_from_bits<double, float16>(bits);
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFDF2_H
diff --git a/libc/src/__support/builtins/extendhfsf2.h b/libc/src/__support/builtins/extendhfsf2.h
new file mode 100644
index 0000000000000..aa24e939c8767
--- /dev/null
+++ b/libc/src/__support/builtins/extendhfsf2.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __extendhfsf2 implementation as
+/// builtins::extendhfsf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFSF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFSF2_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Extend float16 to float; mirrors compiler-rt's __extendhfsf2.
+LIBC_INLINE float extendhfsf2(uint16_t bits) {
+  return fpconvert_from_bits<float, float16>(bits);
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFSF2_H
diff --git a/libc/src/__support/builtins/extendhftf2.h b/libc/src/__support/builtins/extendhftf2.h
new file mode 100644
index 0000000000000..54f34b57d7ba2
--- /dev/null
+++ b/libc/src/__support/builtins/extendhftf2.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 __extendhftf2 implementation as
+/// builtins::extendhftf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFTF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFTF2_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT128) && defined(LIBC_TYPES_HAS_FLOAT16)
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/builtins/fpconvert_helper.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Extend float16 to float128; mirrors compiler-rt's __extendhftf2.
+LIBC_INLINE float128 extendhftf2(uint16_t bits) {
+  return fpconvert_from_bits<float128, float16>(bits);
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFTF2_H
diff --git a/libc/src/__support/builtins/fpconvert_helper.h b/libc/src/__support/builtins/fpconvert_helper.h
index 2b7b331b44b5c..7a84197ad9af0 100644
--- a/libc/src/__support/builtins/fpconvert_helper.h
+++ b/libc/src/__support/builtins/fpconvert_helper.h
@@ -17,6 +17,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_BUILTINS_FPCONVERT_HELPER_H
 
 #include "hdr/fenv_macros.h"
+#include "hdr/stdint_proxy.h"
 #include "src/__support/CPP/algorithm.h"
 #include "src/__support/CPP/bit.h"
 #include "src/__support/CPP/type_traits.h"
@@ -42,14 +43,17 @@ namespace builtins {
 // 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>;
+namespace internal {
+
+// Shared conversion body, taking the source as FPBits.  FPBits is backed by an
+// unsigned integer (uint16_t for the 16-bit floats), so passing it -- rather
+// than a From value -- keeps clang from emitting a circular __extendhfsf2 for a
+// float16 argument (see the TODO above).
+template <typename To, typename FromBits>
+LIBC_INLINE constexpr To fpconvert(FromBits x_bits) {
   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);
@@ -65,14 +69,31 @@ LIBC_INLINE constexpr To fpconvert(From x) {
   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 and subnormals fall through: DyadicFloat 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);
+  fputil::DyadicFloat<cpp::bit_ceil(MAX_FRACTION_LEN)> xd(x_bits.get_val());
   return xd.template as<To, /*ShouldSignalExceptions=*/true>();
 }
 
+} // namespace internal
+
+// Convert a floating-point value from From to To (extend or truncate).
+template <typename To, typename From>
+LIBC_INLINE constexpr To fpconvert(From x) {
+  return internal::fpconvert<To>(fputil::FPBits<From>(x));
+}
+
+// Same, for a float16/bfloat16 source delivered as raw bits.  Reconstructing
+// the value here (instead of taking a From parameter) avoids clang's _Float16
+// ABI lowering, which would emit a circular __extendhfsf2.
+template <typename To, typename From>
+LIBC_INLINE constexpr To fpconvert_from_bits(uint16_t bits) {
+  return internal::fpconvert<To>(
+      fputil::FPBits<From>(cpp::bit_cast<From>(bits)));
+}
+
 } // namespace builtins
 } // namespace LIBC_NAMESPACE_DECL
 
diff --git a/libc/src/__support/builtins/truncdfhf2.h b/libc/src/__support/builtins/truncdfhf2.h
new file mode 100644
index 0000000000000..af2506b1a93c8
--- /dev/null
+++ b/libc/src/__support/builtins/truncdfhf2.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 __truncdfhf2 implementation as
+/// builtins::truncdfhf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFHF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFHF2_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate double to float16; mirrors compiler-rt's __truncdfhf2.
+LIBC_INLINE uint16_t truncdfhf2(double x) {
+  return cpp::bit_cast<uint16_t>(fputil::cast<float16>(x));
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFHF2_H
diff --git a/libc/src/__support/builtins/truncsfhf2.h b/libc/src/__support/builtins/truncsfhf2.h
new file mode 100644
index 0000000000000..a323e7ee76acf
--- /dev/null
+++ b/libc/src/__support/builtins/truncsfhf2.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 __truncsfhf2 implementation as
+/// builtins::truncsfhf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFHF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFHF2_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate float to float16; mirrors compiler-rt's __truncsfhf2.
+LIBC_INLINE uint16_t truncsfhf2(float x) {
+  return cpp::bit_cast<uint16_t>(fputil::cast<float16>(x));
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFHF2_H
diff --git a/libc/src/__support/builtins/trunctfhf2.h b/libc/src/__support/builtins/trunctfhf2.h
new file mode 100644
index 0000000000000..db9790a5f3ea1
--- /dev/null
+++ b/libc/src/__support/builtins/trunctfhf2.h
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 __trunctfhf2 implementation as
+/// builtins::trunctfhf2 so that it can be reused by compiler-rt's builtins.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFHF2_H
+#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFHF2_H
+
+#include "include/llvm-libc-types/float128.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_FLOAT128) && defined(LIBC_TYPES_HAS_FLOAT16)
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace builtins {
+
+// Truncate float128 to float16; mirrors compiler-rt's __trunctfhf2.
+LIBC_INLINE uint16_t trunctfhf2(float128 x) {
+  return cpp::bit_cast<uint16_t>(fputil::cast<float16>(x));
+}
+
+} // namespace builtins
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFHF2_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 96ec6eff5e324..721a1986a9f42 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -832,6 +832,9 @@ add_fp_unittest(
     libc.src.__support.builtins.divtf3
     libc.src.__support.builtins.extendbfsf2
     libc.src.__support.builtins.extenddftf2
+    libc.src.__support.builtins.extendhfdf2
+    libc.src.__support.builtins.extendhfsf2
+    libc.src.__support.builtins.extendhftf2
     libc.src.__support.builtins.extendsfdf2
     libc.src.__support.builtins.extendsftf2
     libc.src.__support.builtins.extendxftf2
@@ -840,10 +843,13 @@ add_fp_unittest(
     libc.src.__support.builtins.multf3
     libc.src.__support.builtins.subtf3
     libc.src.__support.builtins.truncdfbf2
+    libc.src.__support.builtins.truncdfhf2
     libc.src.__support.builtins.truncdfsf2
     libc.src.__support.builtins.truncsfbf2
+    libc.src.__support.builtins.truncsfhf2
     libc.src.__support.builtins.trunctfbf2
     libc.src.__support.builtins.trunctfdf2
+    libc.src.__support.builtins.trunctfhf2
     libc.src.__support.builtins.trunctfsf2
     libc.src.__support.builtins.trunctfxf2
 )

>From 407d4f441f5da508fedac02e43229387c175b0f8 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 24 Jul 2026 21:23:48 +0300
Subject: [PATCH 7/7] Revert "[compiler-rt][builtins] libc-backed bfloat16
 extend/truncate builtins"

This reverts commit 60fa6676d3538ed4d6ece4cf3d3e9406fe882e11.
---
 compiler-rt/lib/builtins/CMakeLists.txt   |  4 ---
 compiler-rt/lib/builtins/extendbfsf2.cpp  | 24 --------------
 compiler-rt/lib/builtins/truncdfbf2.cpp   | 24 --------------
 compiler-rt/lib/builtins/truncsfbf2.cpp   | 24 --------------
 compiler-rt/lib/builtins/trunctfbf2.cpp   | 29 -----------------
 libc/shared/builtins.h                    |  6 ----
 libc/shared/builtins/extendbfsf2.h        | 29 -----------------
 libc/shared/builtins/truncdfbf2.h         | 29 -----------------
 libc/shared/builtins/truncsfbf2.h         | 29 -----------------
 libc/shared/builtins/trunctfbf2.h         | 35 --------------------
 libc/src/__support/builtins/extendbfsf2.h | 35 --------------------
 libc/src/__support/builtins/truncdfbf2.h  | 33 -------------------
 libc/src/__support/builtins/truncsfbf2.h  | 33 -------------------
 libc/src/__support/builtins/trunctfbf2.h  | 39 -----------------------
 libc/test/shared/CMakeLists.txt           |  2 +-
 libc/test/shared/shared_builtins_test.cpp |  6 ----
 16 files changed, 1 insertion(+), 380 deletions(-)
 delete mode 100644 compiler-rt/lib/builtins/extendbfsf2.cpp
 delete mode 100644 compiler-rt/lib/builtins/truncdfbf2.cpp
 delete mode 100644 compiler-rt/lib/builtins/truncsfbf2.cpp
 delete mode 100644 compiler-rt/lib/builtins/trunctfbf2.cpp
 delete mode 100644 libc/shared/builtins/extendbfsf2.h
 delete mode 100644 libc/shared/builtins/truncdfbf2.h
 delete mode 100644 libc/shared/builtins/truncsfbf2.h
 delete mode 100644 libc/shared/builtins/trunctfbf2.h
 delete mode 100644 libc/src/__support/builtins/extendbfsf2.h
 delete mode 100644 libc/src/__support/builtins/truncdfbf2.h
 delete mode 100644 libc/src/__support/builtins/truncsfbf2.h
 delete mode 100644 libc/src/__support/builtins/trunctfbf2.h

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 6a9e600e8a1e0..adea03f6b9c33 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -286,7 +286,6 @@ if(COMPILER_RT_USE_LIBC_MATH)
   add_definitions(-DCOMPILER_RT_USE_LIBC_MATH)
 
   use_libc_builtin(GENERIC_TF_SOURCES addtf3)
-  use_libc_builtin(BF16_SOURCES       extendbfsf2)
   use_libc_builtin(GENERIC_TF_SOURCES extenddftf2)
   use_libc_builtin(GENERIC_SOURCES    extendhfsf2)
   use_libc_builtin(GENERIC_SOURCES    extendhfdf2)
@@ -295,9 +294,6 @@ if(COMPILER_RT_USE_LIBC_MATH)
   use_libc_builtin(GENERIC_TF_SOURCES extendsftf2)
   use_libc_builtin(x86_80_BIT_SOURCES extendxftf2)
   use_libc_builtin(GENERIC_SOURCES    truncdfsf2)  
-  use_libc_builtin(BF16_SOURCES       truncdfbf2)
-  use_libc_builtin(BF16_SOURCES       truncsfbf2)
-  use_libc_builtin(BF16_SOURCES       trunctfbf2)
   use_libc_builtin(GENERIC_TF_SOURCES trunctfdf2)
   use_libc_builtin(GENERIC_SOURCES    truncsfhf2)
   use_libc_builtin(GENERIC_SOURCES    truncdfhf2)
diff --git a/compiler-rt/lib/builtins/extendbfsf2.cpp b/compiler-rt/lib/builtins/extendbfsf2.cpp
deleted file mode 100644
index 74c56803d5c65..0000000000000
--- a/compiler-rt/lib/builtins/extendbfsf2.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __extendbfsf2, extend bfloat16 to float,
-/// on top of LLVM-libc's shared::extendbfsf2.
-///
-//===----------------------------------------------------------------------===//
-
-#define SRC_BFLOAT16
-#define DST_SINGLE
-#include "fp_extend.h"
-
-#include "fp_libc_config.h"
-#include "shared/builtins/extendbfsf2.h"
-
-extern "C" COMPILER_RT_ABI dst_t __extendbfsf2(src_t a) {
-  return LIBC_NAMESPACE::shared::extendbfsf2(__builtin_bit_cast(uint16_t, a));
-}
diff --git a/compiler-rt/lib/builtins/truncdfbf2.cpp b/compiler-rt/lib/builtins/truncdfbf2.cpp
deleted file mode 100644
index a5ae505b03553..0000000000000
--- a/compiler-rt/lib/builtins/truncdfbf2.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __truncdfbf2, truncate double to
-/// bfloat16, on top of LLVM-libc's shared::truncdfbf2.
-///
-//===----------------------------------------------------------------------===//
-
-#define SRC_DOUBLE
-#define DST_BFLOAT
-#include "fp_trunc.h"
-
-#include "fp_libc_config.h"
-#include "shared/builtins/truncdfbf2.h"
-
-extern "C" COMPILER_RT_ABI dst_t __truncdfbf2(src_t a) {
-  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::truncdfbf2(a));
-}
diff --git a/compiler-rt/lib/builtins/truncsfbf2.cpp b/compiler-rt/lib/builtins/truncsfbf2.cpp
deleted file mode 100644
index 91011f086ad85..0000000000000
--- a/compiler-rt/lib/builtins/truncsfbf2.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __truncsfbf2, truncate float to bfloat16,
-/// on top of LLVM-libc's shared::truncsfbf2.
-///
-//===----------------------------------------------------------------------===//
-
-#define SRC_SINGLE
-#define DST_BFLOAT
-#include "fp_trunc.h"
-
-#include "fp_libc_config.h"
-#include "shared/builtins/truncsfbf2.h"
-
-extern "C" COMPILER_RT_ABI dst_t __truncsfbf2(src_t a) {
-  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::truncsfbf2(a));
-}
diff --git a/compiler-rt/lib/builtins/trunctfbf2.cpp b/compiler-rt/lib/builtins/trunctfbf2.cpp
deleted file mode 100644
index d789111907fd3..0000000000000
--- a/compiler-rt/lib/builtins/trunctfbf2.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __trunctfbf2, truncate float128 to
-/// bfloat16, on top of LLVM-libc's shared::trunctfbf2.
-///
-//===----------------------------------------------------------------------===//
-
-#define QUAD_PRECISION
-#include "fp_lib.h"
-
-#include "fp_libc_config.h"
-#include "shared/builtins/trunctfbf2.h"
-
-#if defined(CRT_HAS_TF_MODE) && defined(__x86_64__)
-#define SRC_QUAD
-#define DST_BFLOAT
-#include "fp_trunc.h"
-
-extern "C" COMPILER_RT_ABI dst_t __trunctfbf2(src_t a) {
-  return __builtin_bit_cast(dst_t, LIBC_NAMESPACE::shared::trunctfbf2(a));
-}
-#endif
diff --git a/libc/shared/builtins.h b/libc/shared/builtins.h
index 6d28476c511f3..cba5b2546715b 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -20,7 +20,6 @@
 #include "builtins/adddf3.h"
 #include "builtins/addtf3.h"
 #include "builtins/divtf3.h"
-#include "builtins/extendbfsf2.h"
 #include "builtins/extenddftf2.h"
 #include "builtins/extendhfdf2.h"
 #include "builtins/extendhfsf2.h"
@@ -32,12 +31,7 @@
 #include "builtins/mulsf3.h"
 #include "builtins/multf3.h"
 #include "builtins/subtf3.h"
-#include "builtins/truncdfbf2.h"
-#include "builtins/truncdfhf2.h"
 #include "builtins/truncdfsf2.h"
-#include "builtins/truncsfbf2.h"
-#include "builtins/truncsfhf2.h"
-#include "builtins/trunctfbf2.h"
 #include "builtins/trunctfdf2.h"
 #include "builtins/trunctfhf2.h"
 #include "builtins/trunctfsf2.h"
diff --git a/libc/shared/builtins/extendbfsf2.h b/libc/shared/builtins/extendbfsf2.h
deleted file mode 100644
index f660301a5e31c..0000000000000
--- a/libc/shared/builtins/extendbfsf2.h
+++ /dev/null
@@ -1,29 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __extendbfsf2 implementation as
-/// shared::extendbfsf2 so that it can be reused by compiler-rt's builtins.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SHARED_BUILTINS_EXTENDBFSF2_H
-#define LLVM_LIBC_SHARED_BUILTINS_EXTENDBFSF2_H
-
-#include "shared/libc_common.h"
-#include "src/__support/builtins/extendbfsf2.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace shared {
-
-using builtins::extendbfsf2;
-
-} // namespace shared
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDBFSF2_H
diff --git a/libc/shared/builtins/truncdfbf2.h b/libc/shared/builtins/truncdfbf2.h
deleted file mode 100644
index 2846c484fa813..0000000000000
--- a/libc/shared/builtins/truncdfbf2.h
+++ /dev/null
@@ -1,29 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __truncdfbf2 implementation as
-/// shared::truncdfbf2 so that it can be reused by compiler-rt's builtins.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCDFBF2_H
-#define LLVM_LIBC_SHARED_BUILTINS_TRUNCDFBF2_H
-
-#include "shared/libc_common.h"
-#include "src/__support/builtins/truncdfbf2.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace shared {
-
-using builtins::truncdfbf2;
-
-} // namespace shared
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCDFBF2_H
diff --git a/libc/shared/builtins/truncsfbf2.h b/libc/shared/builtins/truncsfbf2.h
deleted file mode 100644
index 93f4bfb256f84..0000000000000
--- a/libc/shared/builtins/truncsfbf2.h
+++ /dev/null
@@ -1,29 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __truncsfbf2 implementation as
-/// shared::truncsfbf2 so that it can be reused by compiler-rt's builtins.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCSFBF2_H
-#define LLVM_LIBC_SHARED_BUILTINS_TRUNCSFBF2_H
-
-#include "shared/libc_common.h"
-#include "src/__support/builtins/truncsfbf2.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace shared {
-
-using builtins::truncsfbf2;
-
-} // namespace shared
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCSFBF2_H
diff --git a/libc/shared/builtins/trunctfbf2.h b/libc/shared/builtins/trunctfbf2.h
deleted file mode 100644
index 76aca0aa13565..0000000000000
--- a/libc/shared/builtins/trunctfbf2.h
+++ /dev/null
@@ -1,35 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __trunctfbf2 implementation as
-/// shared::trunctfbf2 so that it can be reused by compiler-rt's builtins.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SHARED_BUILTINS_TRUNCTFBF2_H
-#define LLVM_LIBC_SHARED_BUILTINS_TRUNCTFBF2_H
-
-#include "include/llvm-libc-types/float128.h"
-
-#ifdef LIBC_TYPES_HAS_FLOAT128
-
-#include "shared/libc_common.h"
-#include "src/__support/builtins/trunctfbf2.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace shared {
-
-using builtins::trunctfbf2;
-
-} // namespace shared
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LIBC_TYPES_HAS_FLOAT128
-
-#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCTFBF2_H
diff --git a/libc/src/__support/builtins/extendbfsf2.h b/libc/src/__support/builtins/extendbfsf2.h
deleted file mode 100644
index 1ce0009a71055..0000000000000
--- a/libc/src/__support/builtins/extendbfsf2.h
+++ /dev/null
@@ -1,35 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __extendbfsf2 implementation as
-/// builtins::extendbfsf2 so that it can be reused by compiler-rt's builtins.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDBFSF2_H
-#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDBFSF2_H
-
-#include "hdr/stdint_proxy.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace builtins {
-
-// Extend bfloat16 to float; mirrors compiler-rt's __extendbfsf2.
-LIBC_INLINE float extendbfsf2(uint16_t bits) {
-  bfloat16 x;
-  x.bits = bits;
-  return fputil::cast<float>(x);
-}
-
-} // namespace builtins
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDBFSF2_H
diff --git a/libc/src/__support/builtins/truncdfbf2.h b/libc/src/__support/builtins/truncdfbf2.h
deleted file mode 100644
index 4bccb2db9de6d..0000000000000
--- a/libc/src/__support/builtins/truncdfbf2.h
+++ /dev/null
@@ -1,33 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __truncdfbf2 implementation as
-/// builtins::truncdfbf2 so that it can be reused by compiler-rt's builtins.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFBF2_H
-#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFBF2_H
-
-#include "hdr/stdint_proxy.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace builtins {
-
-// Truncate double to bfloat16; mirrors compiler-rt's __truncdfbf2.
-LIBC_INLINE uint16_t truncdfbf2(double x) {
-  return fputil::cast<bfloat16>(x).bits;
-}
-
-} // namespace builtins
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFBF2_H
diff --git a/libc/src/__support/builtins/truncsfbf2.h b/libc/src/__support/builtins/truncsfbf2.h
deleted file mode 100644
index 08611f20fb2a2..0000000000000
--- a/libc/src/__support/builtins/truncsfbf2.h
+++ /dev/null
@@ -1,33 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __truncsfbf2 implementation as
-/// builtins::truncsfbf2 so that it can be reused by compiler-rt's builtins.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFBF2_H
-#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFBF2_H
-
-#include "hdr/stdint_proxy.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace builtins {
-
-// Truncate float to bfloat16; mirrors compiler-rt's __truncsfbf2.
-LIBC_INLINE uint16_t truncsfbf2(float x) {
-  return fputil::cast<bfloat16>(x).bits;
-}
-
-} // namespace builtins
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFBF2_H
diff --git a/libc/src/__support/builtins/trunctfbf2.h b/libc/src/__support/builtins/trunctfbf2.h
deleted file mode 100644
index 99e2833c39058..0000000000000
--- a/libc/src/__support/builtins/trunctfbf2.h
+++ /dev/null
@@ -1,39 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 __trunctfbf2 implementation as
-/// builtins::trunctfbf2 so that it can be reused by compiler-rt's builtins.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFBF2_H
-#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFBF2_H
-
-#include "include/llvm-libc-types/float128.h"
-
-#ifdef LIBC_TYPES_HAS_FLOAT128
-
-#include "hdr/stdint_proxy.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace builtins {
-
-// Truncate float128 to bfloat16; mirrors compiler-rt's __trunctfbf2.
-LIBC_INLINE uint16_t trunctfbf2(float128 x) {
-  return fputil::cast<bfloat16>(x).bits;
-}
-
-} // namespace builtins
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LIBC_TYPES_HAS_FLOAT128
-
-#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFBF2_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 721a1986a9f42..75eb35319e031 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -830,7 +830,6 @@ add_fp_unittest(
     libc.src.__support.builtins.adddf3
     libc.src.__support.builtins.addtf3
     libc.src.__support.builtins.divtf3
-    libc.src.__support.builtins.extendbfsf2
     libc.src.__support.builtins.extenddftf2
     libc.src.__support.builtins.extendhfdf2
     libc.src.__support.builtins.extendhfsf2
@@ -848,6 +847,7 @@ add_fp_unittest(
     libc.src.__support.builtins.truncsfbf2
     libc.src.__support.builtins.truncsfhf2
     libc.src.__support.builtins.trunctfbf2
+    libc.src.__support.builtins.truncdfsf2
     libc.src.__support.builtins.trunctfdf2
     libc.src.__support.builtins.trunctfhf2
     libc.src.__support.builtins.trunctfsf2
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index 77d4a315d8eac..c378482c0bf19 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -37,7 +37,6 @@ TEST(LlvmLibcSharedBuiltinsTest, AllFloat128) {
 
 TEST(LlvmLibcSharedBuiltinsTest, ExtendConversion) {
   EXPECT_FP_EQ(1.5, shared::extendsfdf2(1.5f));
-  EXPECT_FP_EQ(1.5f, shared::extendbfsf2(static_cast<uint16_t>(0x3FC0)));
 #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));
@@ -48,12 +47,7 @@ TEST(LlvmLibcSharedBuiltinsTest, ExtendConversion) {
 }
 
 TEST(LlvmLibcSharedBuiltinsTest, TruncateConversion) {
-  EXPECT_EQ(static_cast<uint16_t>(0x3FC0), shared::truncdfbf2(1.5));
   EXPECT_FP_EQ(1.5f, shared::truncdfsf2(1.5));
-  EXPECT_EQ(static_cast<uint16_t>(0x3FC0), shared::truncsfbf2(1.5f));
-#ifdef LIBC_TYPES_HAS_FLOAT128
-  EXPECT_EQ(static_cast<uint16_t>(0x3FC0), shared::trunctfbf2(float128(1.5)));
-#endif // LIBC_TYPES_HAS_FLOAT128
 #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)));



More information about the libc-commits mailing list