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

via libc-commits libc-commits at lists.llvm.org
Sat Aug 8 14:25:25 PDT 2026


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

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

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

diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index eb1c566d6b179..58fe09904d36e 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -19,6 +19,13 @@ TEST(LlvmLibcSharedBuiltinsTest, SinglePrecisionArithmtic) {
   EXPECT_FP_EQ(6.0f, shared::mulsf3(2.0f, 3.0f));
   EXPECT_FP_EQ(-5.0, shared::negsf2(5.0));
   EXPECT_FP_EQ(2.0f, shared::subsf3(5.0f, 3.0f));
+namespace shared = LIBC_NAMESPACE::shared;
+
+TEST(LlvmLibcSharedBuiltinsTest, AllFloat) {
+  EXPECT_FP_EQ(3.0f, LIBC_NAMESPACE::shared::addsf3(1.0f, 2.0f));
+  EXPECT_FP_EQ(3.0f, LIBC_NAMESPACE::shared::divsf3(6.0f, 2.0f));
+  EXPECT_FP_EQ(6.0f, LIBC_NAMESPACE::shared::mulsf3(2.0f, 3.0f));
+  EXPECT_FP_EQ(2.0f, LIBC_NAMESPACE::shared::subsf3(5.0f, 3.0f));
 }
 
 TEST(LlvmLibcSharedBuiltinsTest, DoublePrecisionArithmtic) {
@@ -46,6 +53,7 @@ TEST(LlvmLibcSharedBuiltinsTest, ExtendConversion) {
   EXPECT_FP_EQ(float128(1.5), shared::extenddftf2(1.5));
   EXPECT_FP_EQ(float128(1.5), shared::extendsftf2(1.5f));
 #ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+#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
@@ -57,6 +65,7 @@ TEST(LlvmLibcSharedBuiltinsTest, TruncateConversion) {
   EXPECT_FP_EQ(1.5, shared::trunctfdf2(float128(1.5)));
   EXPECT_FP_EQ(1.5f, shared::trunctfsf2(float128(1.5)));
 #ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
+#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 46f016b3aefec387c099fe8e9e2389fd78f62517 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 24 Jul 2026 20:33:18 +0300
Subject: [PATCH 2/2] [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 | 28 ++++++++++++++++
 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, 409 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 1134115934df5..abd581a206647 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -291,6 +291,7 @@ if(COMPILER_RT_USE_LIBC_MATH)
   use_libc_builtin(GENERIC_SOURCES    divdf3)
   use_libc_builtin(GENERIC_SOURCES    divsf3)
   use_libc_builtin(GENERIC_TF_SOURCES divtf3)
+  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)
@@ -316,6 +317,9 @@ if(COMPILER_RT_USE_LIBC_MATH)
   use_libc_builtin(GENERIC_SOURCES    subsf3)
   use_libc_builtin(GENERIC_TF_SOURCES subtf3)
   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 e13ef6b84a6de..3833bb8142179 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -23,6 +23,7 @@
 #include "builtins/divdf3.h"
 #include "builtins/divsf3.h"
 #include "builtins/divtf3.h"
+#include "builtins/extendbfsf2.h"
 #include "builtins/extenddftf2.h"
 #include "builtins/extendsfdf2.h"
 #include "builtins/extendsftf2.h"
@@ -47,7 +48,10 @@
 #include "builtins/subdf3.h"
 #include "builtins/subsf3.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 51212c4bff34c..477c121d6d4cb 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -230,6 +230,12 @@ add_header_library(
   DEPENDS
     libc.hdr.stdint_proxy
     libc.src.__support.builtins.fixint_helper
+  extendbfsf2
+  HDRS
+    extendbfsf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.FPUtil.bfloat16
     libc.src.__support.macros.config
 )
 
@@ -252,6 +258,12 @@ add_header_library(
   DEPENDS
     libc.hdr.stdint_proxy
     libc.src.__support.builtins.fixint_helper
+  truncsfbf2
+  HDRS
+    truncsfbf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.FPUtil.bfloat16
     libc.src.__support.macros.config
 )
 
@@ -262,6 +274,12 @@ add_header_library(
   DEPENDS
     libc.hdr.stdint_proxy
     libc.src.__support.builtins.fixint_helper
+  truncdfbf2
+  HDRS
+    truncdfbf2.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.FPUtil.bfloat16
     libc.src.__support.macros.config
 )
 
@@ -350,3 +368,13 @@ add_header_library(
     libc.src.__support.macros.properties.types
     libc.src.__support.uint128
 )
+  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 04b6a6e3aed91..22a6d09eb6369 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -834,6 +834,7 @@ add_fp_unittest(
     libc.src.__support.builtins.divdf3
     libc.src.__support.builtins.divsf3
     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
@@ -858,7 +859,10 @@ add_fp_unittest(
     libc.src.__support.builtins.subdf3
     libc.src.__support.builtins.subsf3
     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 58fe09904d36e..763d4d9dbae8a 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -49,6 +49,7 @@ TEST(LlvmLibcSharedBuiltinsTest, QuadPrecisionArithmtic) {
 
 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));
@@ -60,7 +61,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)));



More information about the libc-commits mailing list