[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
Thu Nov 26 15:22:14 PST 2026
https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/211881
>From 0ded884e561c7580a42198e42d6cf85f02de2729 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 24 Jul 2026 20:33:18 +0300
Subject: [PATCH 1/3] [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 | 36 +++++++++++++++++++
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 | 40 +++++++++++++++++++++
libc/test/shared/CMakeLists.txt | 4 +++
libc/test/shared/shared_builtins_test.cpp | 4 +++
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 bf96a992032144..80954cd59f9326 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 extendhfsf2)
use_libc_builtin(GENERIC_SOURCES extendhfdf2)
@@ -331,6 +332,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_SOURCES truncsfhf2)
use_libc_builtin(GENERIC_SOURCES truncdfhf2)
diff --git a/compiler-rt/lib/builtins/extendbfsf2.cpp b/compiler-rt/lib/builtins/extendbfsf2.cpp
new file mode 100644
index 00000000000000..74c56803d5c655
--- /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 00000000000000..a5ae505b035531
--- /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 00000000000000..91011f086ad851
--- /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 00000000000000..d789111907fd3b
--- /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 1cfbf999a22e17..f96bf442321a3e 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/extendhfdf2.h"
#include "builtins/extendhfsf2.h"
@@ -62,9 +63,12 @@
#include "builtins/subdf3.h"
#include "builtins/subsf3.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
new file mode 100644
index 00000000000000..f660301a5e31c1
--- /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 00000000000000..2846c484fa813b
--- /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 00000000000000..93f4bfb256f84d
--- /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 00000000000000..04773a6b1d64f4
--- /dev/null
+++ b/libc/shared/builtins/trunctfbf2.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 __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"
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_NATIVE_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_NATIVE_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 def0fed3c30cfc..16fe1b68857058 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -565,3 +565,45 @@ add_header_library(
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
+
+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
+ libc.src.__support.macros.properties.types
+)
diff --git a/libc/src/__support/builtins/extendbfsf2.h b/libc/src/__support/builtins/extendbfsf2.h
new file mode 100644
index 00000000000000..1ce0009a710556
--- /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 00000000000000..4bccb2db9de6dd
--- /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 00000000000000..08611f20fb2a20
--- /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 00000000000000..b4c693f6e44ead
--- /dev/null
+++ b/libc/src/__support/builtins/trunctfbf2.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 __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"
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TYPES_HAS_NATIVE_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_NATIVE_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFBF2_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index be3df9fa6e6ab6..db47fe3d1e91df 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -914,6 +914,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.extendhfdf2
libc.src.__support.builtins.extendhfsf2
@@ -953,9 +954,12 @@ 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.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
diff --git a/libc/test/shared/shared_builtins_test.cpp b/libc/test/shared/shared_builtins_test.cpp
index a5c0a674c360e7..1a94f754da22ae 100644
--- a/libc/test/shared/shared_builtins_test.cpp
+++ b/libc/test/shared/shared_builtins_test.cpp
@@ -42,6 +42,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_NATIVE_FLOAT128
EXPECT_FP_EQ(float128(1.5), shared::extenddftf2(1.5));
EXPECT_FP_EQ(float128(1.5), shared::extendsftf2(1.5f));
@@ -52,8 +53,11 @@ 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_NATIVE_FLOAT128
+ EXPECT_EQ(static_cast<uint16_t>(0x3FC0), shared::trunctfbf2(float128(1.5)));
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
>From ebf0ceeb268bac3fdedea18807b112d01994f04f Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 27 Nov 2026 00:57:58 +0200
Subject: [PATCH 2/3] use libc's bit_cast in bfloat16 extend/truncate builtins
---
compiler-rt/lib/builtins/extendbfsf2.cpp | 4 +++-
compiler-rt/lib/builtins/truncdfbf2.cpp | 4 +++-
compiler-rt/lib/builtins/truncsfbf2.cpp | 4 +++-
compiler-rt/lib/builtins/trunctfbf2.cpp | 4 +++-
4 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/compiler-rt/lib/builtins/extendbfsf2.cpp b/compiler-rt/lib/builtins/extendbfsf2.cpp
index 74c56803d5c655..94b766d080b04c 100644
--- a/compiler-rt/lib/builtins/extendbfsf2.cpp
+++ b/compiler-rt/lib/builtins/extendbfsf2.cpp
@@ -17,8 +17,10 @@
#include "fp_extend.h"
#include "fp_libc_config.h"
+#include "shared/bit.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));
+ return LIBC_NAMESPACE::shared::extendbfsf2(
+ LIBC_NAMESPACE::shared::bit_cast<uint16_t>(a));
}
diff --git a/compiler-rt/lib/builtins/truncdfbf2.cpp b/compiler-rt/lib/builtins/truncdfbf2.cpp
index a5ae505b035531..c40ec0f376eeef 100644
--- a/compiler-rt/lib/builtins/truncdfbf2.cpp
+++ b/compiler-rt/lib/builtins/truncdfbf2.cpp
@@ -17,8 +17,10 @@
#include "fp_trunc.h"
#include "fp_libc_config.h"
+#include "shared/bit.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));
+ return LIBC_NAMESPACE::shared::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
index 91011f086ad851..b3c30edfd75de8 100644
--- a/compiler-rt/lib/builtins/truncsfbf2.cpp
+++ b/compiler-rt/lib/builtins/truncsfbf2.cpp
@@ -17,8 +17,10 @@
#include "fp_trunc.h"
#include "fp_libc_config.h"
+#include "shared/bit.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));
+ return LIBC_NAMESPACE::shared::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
index d789111907fd3b..9380fe556dba73 100644
--- a/compiler-rt/lib/builtins/trunctfbf2.cpp
+++ b/compiler-rt/lib/builtins/trunctfbf2.cpp
@@ -16,6 +16,7 @@
#include "fp_lib.h"
#include "fp_libc_config.h"
+#include "shared/bit.h"
#include "shared/builtins/trunctfbf2.h"
#if defined(CRT_HAS_TF_MODE) && defined(__x86_64__)
@@ -24,6 +25,7 @@
#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));
+ return LIBC_NAMESPACE::shared::bit_cast<dst_t>(
+ LIBC_NAMESPACE::shared::trunctfbf2(a));
}
#endif
>From 7b7ff16dca3bdf95edac2edc2b961032cd1fb5b0 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 27 Nov 2026 01:20:54 +0200
Subject: [PATCH 3/3] use fpconvert_helper for bfloat16 extend/truncate
builtins
---
libc/src/__support/builtins/CMakeLists.txt | 12 ++++++++----
libc/src/__support/builtins/extendbfsf2.h | 7 +++----
libc/src/__support/builtins/truncdfbf2.h | 5 +++--
libc/src/__support/builtins/truncsfbf2.h | 5 +++--
libc/src/__support/builtins/trunctfbf2.h | 5 +++--
5 files changed, 20 insertions(+), 14 deletions(-)
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index 16fe1b68857058..ffa0c6d1ef5b84 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -572,7 +572,8 @@ add_header_library(
extendbfsf2.h
DEPENDS
libc.hdr.stdint_proxy
- libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.builtins.fpconvert_helper
libc.src.__support.macros.config
)
@@ -582,7 +583,8 @@ add_header_library(
truncsfbf2.h
DEPENDS
libc.hdr.stdint_proxy
- libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.builtins.fpconvert_helper
libc.src.__support.macros.config
)
@@ -592,7 +594,8 @@ add_header_library(
truncdfbf2.h
DEPENDS
libc.hdr.stdint_proxy
- libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.builtins.fpconvert_helper
libc.src.__support.macros.config
)
@@ -603,7 +606,8 @@ add_header_library(
DEPENDS
libc.hdr.stdint_proxy
libc.include.llvm-libc-types.float128
- libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.builtins.fpconvert_helper
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
diff --git a/libc/src/__support/builtins/extendbfsf2.h b/libc/src/__support/builtins/extendbfsf2.h
index 1ce0009a710556..73d1773f089203 100644
--- a/libc/src/__support/builtins/extendbfsf2.h
+++ b/libc/src/__support/builtins/extendbfsf2.h
@@ -16,7 +16,8 @@
#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDBFSF2_H
#include "hdr/stdint_proxy.h"
-#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
@@ -24,9 +25,7 @@ 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);
+ return fpconvert_from_bits<float, fputil::FPType::BFloat16>(bits);
}
} // namespace builtins
diff --git a/libc/src/__support/builtins/truncdfbf2.h b/libc/src/__support/builtins/truncdfbf2.h
index 4bccb2db9de6dd..4f20f656512b74 100644
--- a/libc/src/__support/builtins/truncdfbf2.h
+++ b/libc/src/__support/builtins/truncdfbf2.h
@@ -16,7 +16,8 @@
#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCDFBF2_H
#include "hdr/stdint_proxy.h"
-#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
@@ -24,7 +25,7 @@ namespace builtins {
// Truncate double to bfloat16; mirrors compiler-rt's __truncdfbf2.
LIBC_INLINE uint16_t truncdfbf2(double x) {
- return fputil::cast<bfloat16>(x).bits;
+ return fpconvert_to_bits<fputil::FPType::BFloat16>(x);
}
} // namespace builtins
diff --git a/libc/src/__support/builtins/truncsfbf2.h b/libc/src/__support/builtins/truncsfbf2.h
index 08611f20fb2a20..31c43f54992a0c 100644
--- a/libc/src/__support/builtins/truncsfbf2.h
+++ b/libc/src/__support/builtins/truncsfbf2.h
@@ -16,7 +16,8 @@
#define LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCSFBF2_H
#include "hdr/stdint_proxy.h"
-#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
@@ -24,7 +25,7 @@ namespace builtins {
// Truncate float to bfloat16; mirrors compiler-rt's __truncsfbf2.
LIBC_INLINE uint16_t truncsfbf2(float x) {
- return fputil::cast<bfloat16>(x).bits;
+ return fpconvert_to_bits<fputil::FPType::BFloat16>(x);
}
} // namespace builtins
diff --git a/libc/src/__support/builtins/trunctfbf2.h b/libc/src/__support/builtins/trunctfbf2.h
index b4c693f6e44ead..0f383f71220dc6 100644
--- a/libc/src/__support/builtins/trunctfbf2.h
+++ b/libc/src/__support/builtins/trunctfbf2.h
@@ -21,7 +21,8 @@
#ifdef LIBC_TYPES_HAS_NATIVE_FLOAT128
#include "hdr/stdint_proxy.h"
-#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
@@ -29,7 +30,7 @@ namespace builtins {
// Truncate float128 to bfloat16; mirrors compiler-rt's __trunctfbf2.
LIBC_INLINE uint16_t trunctfbf2(float128 x) {
- return fputil::cast<bfloat16>(x).bits;
+ return fpconvert_to_bits<fputil::FPType::BFloat16>(x);
}
} // namespace builtins
More information about the libc-commits
mailing list