[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
Tue Sep 8 09:47:29 PDT 2026
https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/211882
>From 9cc45afdba262e1a15edb2a80258f27a8b99b652 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Tue, 8 Sep 2026 19:33:15 +0300
Subject: [PATCH 01/10] [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 | 71 +++++++++++++++++++
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 | 33 +++++++--
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, 786 insertions(+), 5 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 12a3b068b3373..978009148b9ce 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -292,6 +292,9 @@ if(COMPILER_RT_USE_LIBC_MATH)
use_libc_builtin(GENERIC_SOURCES divsf3)
use_libc_builtin(GENERIC_TF_SOURCES divtf3)
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)
@@ -329,6 +332,9 @@ if(COMPILER_RT_USE_LIBC_MATH)
use_libc_builtin(GENERIC_TF_SOURCES subtf3)
use_libc_builtin(GENERIC_SOURCES truncdfsf2)
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 21c2bed4a0af1..1cfbf999a22e1 100644
--- a/libc/shared/builtins.h
+++ b/libc/shared/builtins.h
@@ -24,6 +24,9 @@
#include "builtins/divsf3.h"
#include "builtins/divtf3.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"
@@ -59,8 +62,11 @@
#include "builtins/subdf3.h"
#include "builtins/subsf3.h"
#include "builtins/subtf3.h"
+#include "builtins/truncdfhf2.h"
#include "builtins/truncdfsf2.h"
+#include "builtins/truncsfhf2.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 366b35bd0de3a..85e05b0178626 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -497,3 +497,74 @@ add_header_library(
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
+
+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 f892bc4d2a1ce..2067f1bf0bebb 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,9 +43,14 @@ 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;
@@ -69,14 +75,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 74eda7cdaf065..6700658aab7ef 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -837,6 +837,9 @@ add_fp_unittest(
libc.src.__support.builtins.divsf3
libc.src.__support.builtins.divtf3
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
@@ -872,8 +875,11 @@ add_fp_unittest(
libc.src.__support.builtins.subdf3
libc.src.__support.builtins.subsf3
libc.src.__support.builtins.subtf3
+ libc.src.__support.builtins.truncdfhf2
libc.src.__support.builtins.truncdfsf2
+ libc.src.__support.builtins.truncsfhf2
libc.src.__support.builtins.trunctfdf2
+ libc.src.__support.builtins.trunctfhf2
libc.src.__support.builtins.trunctfsf2
libc.src.__support.builtins.trunctfxf2
libc.src.__support.uint128
>From 76a0258eea5fc33da4d7f73fc1cd7daeb6174ec7 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 1 Aug 2026 22:48:53 +0300
Subject: [PATCH 02/10] fix conflict issue
---
libc/src/__support/builtins/fpconvert_helper.h | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/libc/src/__support/builtins/fpconvert_helper.h b/libc/src/__support/builtins/fpconvert_helper.h
index 2067f1bf0bebb..b0c194986b328 100644
--- a/libc/src/__support/builtins/fpconvert_helper.h
+++ b/libc/src/__support/builtins/fpconvert_helper.h
@@ -54,11 +54,6 @@ LIBC_INLINE constexpr To fpconvert(FromBits x_bits) {
using ToBits = fputil::FPBits<To>;
using ToStorageType = typename ToBits::StorageType;
- FromBits x_bits(x);
-
- if constexpr (cpp::is_same_v<To, From>)
- return x;
-
if (x_bits.is_nan()) {
typename FromBits::StorageType x_frac = x_bits.get_mantissa();
if constexpr (ToBits::FRACTION_LEN >= FromBits::FRACTION_LEN) {
@@ -88,7 +83,10 @@ LIBC_INLINE constexpr To fpconvert(FromBits x_bits) {
// 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));
+ if constexpr (cpp::is_same_v<To, From>)
+ return x;
+ else
+ return internal::fpconvert<To>(fputil::FPBits<From>(x));
}
// Same, for a float16/bfloat16 source delivered as raw bits. Reconstructing
@@ -96,8 +94,11 @@ LIBC_INLINE constexpr To fpconvert(From x) {
// 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)));
+ if constexpr (cpp::is_same_v<To, From>)
+ return cpp::bit_cast<To>(bits);
+ else
+ return internal::fpconvert<To>(
+ fputil::FPBits<From>(cpp::bit_cast<From>(bits)));
}
} // namespace builtins
>From f4f53f31b38aca5cc246468ad7249e3e896f3d10 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 6 Aug 2026 20:40:32 +0300
Subject: [PATCH 03/10] if defined -> ifdef
---
compiler-rt/lib/builtins/extendhfdf2.cpp | 2 +-
compiler-rt/lib/builtins/extendhfsf2.cpp | 2 +-
compiler-rt/lib/builtins/extendhftf2.cpp | 2 +-
compiler-rt/lib/builtins/truncdfhf2.cpp | 2 +-
compiler-rt/lib/builtins/truncsfhf2.cpp | 2 +-
compiler-rt/lib/builtins/trunctfhf2.cpp | 2 +-
6 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/compiler-rt/lib/builtins/extendhfdf2.cpp b/compiler-rt/lib/builtins/extendhfdf2.cpp
index f1d40971b7f45..b7ad3985a833a 100644
--- a/compiler-rt/lib/builtins/extendhfdf2.cpp
+++ b/compiler-rt/lib/builtins/extendhfdf2.cpp
@@ -19,7 +19,7 @@
#include "fp_libc_config.h"
#include "shared/builtins/extendhfdf2.h"
-#if defined(LIBC_TYPES_HAS_FLOAT16)
+#ifdef 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));
}
diff --git a/compiler-rt/lib/builtins/extendhfsf2.cpp b/compiler-rt/lib/builtins/extendhfsf2.cpp
index 6dc3ef17c22d5..7ab490ae39f52 100644
--- a/compiler-rt/lib/builtins/extendhfsf2.cpp
+++ b/compiler-rt/lib/builtins/extendhfsf2.cpp
@@ -19,7 +19,7 @@
#include "fp_libc_config.h"
#include "shared/builtins/extendhfsf2.h"
-#if defined(LIBC_TYPES_HAS_FLOAT16)
+#ifdef 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));
}
diff --git a/compiler-rt/lib/builtins/extendhftf2.cpp b/compiler-rt/lib/builtins/extendhftf2.cpp
index 2b035ee639b22..219e1d1ec33d4 100644
--- a/compiler-rt/lib/builtins/extendhftf2.cpp
+++ b/compiler-rt/lib/builtins/extendhftf2.cpp
@@ -23,7 +23,7 @@
#define DST_QUAD
#include "fp_extend_impl.inc"
-#if defined(LIBC_TYPES_HAS_FLOAT16)
+#ifdef 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));
}
diff --git a/compiler-rt/lib/builtins/truncdfhf2.cpp b/compiler-rt/lib/builtins/truncdfhf2.cpp
index ab08483cdabfa..4f58283888c96 100644
--- a/compiler-rt/lib/builtins/truncdfhf2.cpp
+++ b/compiler-rt/lib/builtins/truncdfhf2.cpp
@@ -19,7 +19,7 @@
#include "fp_libc_config.h"
#include "shared/builtins/truncdfhf2.h"
-#if defined(LIBC_TYPES_HAS_FLOAT16)
+#ifdef 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));
}
diff --git a/compiler-rt/lib/builtins/truncsfhf2.cpp b/compiler-rt/lib/builtins/truncsfhf2.cpp
index 3fa614d2970fe..3e6b82a18ff22 100644
--- a/compiler-rt/lib/builtins/truncsfhf2.cpp
+++ b/compiler-rt/lib/builtins/truncsfhf2.cpp
@@ -19,7 +19,7 @@
#include "fp_libc_config.h"
#include "shared/builtins/truncsfhf2.h"
-#if defined(LIBC_TYPES_HAS_FLOAT16)
+#ifdef 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));
}
diff --git a/compiler-rt/lib/builtins/trunctfhf2.cpp b/compiler-rt/lib/builtins/trunctfhf2.cpp
index a1c06a9415229..b43cd8fba9f7a 100644
--- a/compiler-rt/lib/builtins/trunctfhf2.cpp
+++ b/compiler-rt/lib/builtins/trunctfhf2.cpp
@@ -23,7 +23,7 @@
#define DST_HALF
#include "fp_trunc_impl.inc"
-#if defined(LIBC_TYPES_HAS_FLOAT16)
+#ifdef 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));
}
>From ca8d6266abd6c348b55e06bd4e1d7e85da062824 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 8 Aug 2026 23:56:10 +0300
Subject: [PATCH 04/10] make float16 extend builtins independent of _Float16
---
compiler-rt/lib/builtins/extendhfdf2.cpp | 10 ++--
compiler-rt/lib/builtins/extendhfsf2.cpp | 10 ++--
compiler-rt/lib/builtins/extendhftf2.cpp | 10 ++--
compiler-rt/lib/builtins/truncdfhf2.cpp | 4 +-
compiler-rt/lib/builtins/truncsfhf2.cpp | 4 +-
compiler-rt/lib/builtins/trunctfhf2.cpp | 4 +-
libc/shared/bit.h | 23 ++++++++
libc/shared/builtins/extendhfdf2.h | 6 ---
libc/shared/builtins/extendhfsf2.h | 6 ---
libc/shared/builtins/extendhftf2.h | 4 +-
libc/src/__support/FPUtil/FPBits.h | 3 ++
libc/src/__support/builtins/extendhfdf2.h | 9 +---
libc/src/__support/builtins/extendhfsf2.h | 9 +---
libc/src/__support/builtins/extendhftf2.h | 7 +--
.../src/__support/builtins/fpconvert_helper.h | 54 ++++++++++---------
15 files changed, 82 insertions(+), 81 deletions(-)
create mode 100644 libc/shared/bit.h
diff --git a/compiler-rt/lib/builtins/extendhfdf2.cpp b/compiler-rt/lib/builtins/extendhfdf2.cpp
index b7ad3985a833a..095259358c613 100644
--- a/compiler-rt/lib/builtins/extendhfdf2.cpp
+++ b/compiler-rt/lib/builtins/extendhfdf2.cpp
@@ -17,14 +17,10 @@
#include "fp_extend_impl.inc"
#include "fp_libc_config.h"
+#include "shared/bit.h"
#include "shared/builtins/extendhfdf2.h"
-#ifdef 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));
+ return LIBC_NAMESPACE::shared::extendhfdf2(
+ LIBC_NAMESPACE::shared::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
index 7ab490ae39f52..0bcc82058b11b 100644
--- a/compiler-rt/lib/builtins/extendhfsf2.cpp
+++ b/compiler-rt/lib/builtins/extendhfsf2.cpp
@@ -17,17 +17,13 @@
#include "fp_extend_impl.inc"
#include "fp_libc_config.h"
+#include "shared/bit.h"
#include "shared/builtins/extendhfsf2.h"
-#ifdef 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));
+ return LIBC_NAMESPACE::shared::extendhfsf2(
+ LIBC_NAMESPACE::shared::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__)
diff --git a/compiler-rt/lib/builtins/extendhftf2.cpp b/compiler-rt/lib/builtins/extendhftf2.cpp
index 219e1d1ec33d4..d7e3db737acaf 100644
--- a/compiler-rt/lib/builtins/extendhftf2.cpp
+++ b/compiler-rt/lib/builtins/extendhftf2.cpp
@@ -16,6 +16,7 @@
#include "fp_lib.h"
#include "fp_libc_config.h"
+#include "shared/bit.h"
#include "shared/builtins/extendhftf2.h"
#if defined(CRT_HAS_TF_MODE) && defined(COMPILER_RT_HAS_FLOAT16)
@@ -23,13 +24,8 @@
#define DST_QUAD
#include "fp_extend_impl.inc"
-#ifdef 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));
+ return LIBC_NAMESPACE::shared::extendhftf2(
+ LIBC_NAMESPACE::shared::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
index 4f58283888c96..77a93394b4ab6 100644
--- a/compiler-rt/lib/builtins/truncdfhf2.cpp
+++ b/compiler-rt/lib/builtins/truncdfhf2.cpp
@@ -17,11 +17,13 @@
#include "fp_trunc_impl.inc"
#include "fp_libc_config.h"
+#include "shared/bit.h"
#include "shared/builtins/truncdfhf2.h"
#ifdef 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));
+ return LIBC_NAMESPACE::shared::bit_cast<dst_t>(
+ LIBC_NAMESPACE::shared::truncdfhf2(a));
}
#else
extern "C" COMPILER_RT_ABI dst_t __truncdfhf2(src_t a) {
diff --git a/compiler-rt/lib/builtins/truncsfhf2.cpp b/compiler-rt/lib/builtins/truncsfhf2.cpp
index 3e6b82a18ff22..b0ab659562ec9 100644
--- a/compiler-rt/lib/builtins/truncsfhf2.cpp
+++ b/compiler-rt/lib/builtins/truncsfhf2.cpp
@@ -17,11 +17,13 @@
#include "fp_trunc_impl.inc"
#include "fp_libc_config.h"
+#include "shared/bit.h"
#include "shared/builtins/truncsfhf2.h"
#ifdef 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));
+ return LIBC_NAMESPACE::shared::bit_cast<dst_t>(
+ LIBC_NAMESPACE::shared::truncsfhf2(a));
}
#else
extern "C" COMPILER_RT_ABI NOINLINE dst_t __truncsfhf2(src_t a) {
diff --git a/compiler-rt/lib/builtins/trunctfhf2.cpp b/compiler-rt/lib/builtins/trunctfhf2.cpp
index b43cd8fba9f7a..a0a3f154edfb8 100644
--- a/compiler-rt/lib/builtins/trunctfhf2.cpp
+++ b/compiler-rt/lib/builtins/trunctfhf2.cpp
@@ -16,6 +16,7 @@
#include "fp_lib.h"
#include "fp_libc_config.h"
+#include "shared/bit.h"
#include "shared/builtins/trunctfhf2.h"
#if defined(CRT_HAS_TF_MODE) && defined(COMPILER_RT_HAS_FLOAT16)
@@ -25,7 +26,8 @@
#ifdef 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));
+ return LIBC_NAMESPACE::shared::bit_cast<dst_t>(
+ LIBC_NAMESPACE::shared::trunctfhf2(a));
}
#else
extern "C" COMPILER_RT_ABI dst_t __trunctfhf2(src_t a) {
diff --git a/libc/shared/bit.h b/libc/shared/bit.h
new file mode 100644
index 0000000000000..57b5a0c8a8355
--- /dev/null
+++ b/libc/shared/bit.h
@@ -0,0 +1,23 @@
+//===-- Bit manipulation utils ----------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_BIT_H
+#define LLVM_LIBC_SHARED_BIT_H
+
+#include "libc_common.h"
+#include "src/__support/CPP/bit.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using cpp::bit_cast;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_BIT_H
diff --git a/libc/shared/builtins/extendhfdf2.h b/libc/shared/builtins/extendhfdf2.h
index ebae43abfbf3b..e9eaaca15e114 100644
--- a/libc/shared/builtins/extendhfdf2.h
+++ b/libc/shared/builtins/extendhfdf2.h
@@ -15,10 +15,6 @@
#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"
@@ -30,6 +26,4 @@ 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
index 346961746bd52..cc31bc7bd6664 100644
--- a/libc/shared/builtins/extendhfsf2.h
+++ b/libc/shared/builtins/extendhfsf2.h
@@ -15,10 +15,6 @@
#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"
@@ -30,6 +26,4 @@ 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
index 28a3331fcfc5d..d6b8b2e4a966b 100644
--- a/libc/shared/builtins/extendhftf2.h
+++ b/libc/shared/builtins/extendhftf2.h
@@ -18,7 +18,7 @@
#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)
+#ifdef LIBC_TYPES_HAS_FLOAT128
#include "shared/libc_common.h"
#include "src/__support/builtins/extendhftf2.h"
@@ -31,6 +31,6 @@ using builtins::extendhftf2;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_FLOAT16
+#endif // LIBC_TYPES_HAS_FLOAT128
#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDHFTF2_H
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 6127dede03f4a..81ebecd61ecf1 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -786,6 +786,9 @@ struct FPRep : public FPRepImpl<fp_type, FPRep<fp_type>> {
} // namespace internal
+// Like 'FPBits' but named by 'FPType', so no native C++ type need exist.
+template <FPType fp_type> using FPRep = internal::FPRep<fp_type>;
+
// Returns the FPType corresponding to C++ type T on the host.
template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
using UnqualT = cpp::remove_cv_t<T>;
diff --git a/libc/src/__support/builtins/extendhfdf2.h b/libc/src/__support/builtins/extendhfdf2.h
index 36e747dc8112c..65eddd46338be 100644
--- a/libc/src/__support/builtins/extendhfdf2.h
+++ b/libc/src/__support/builtins/extendhfdf2.h
@@ -15,11 +15,8 @@
#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/FPUtil/FPBits.h"
#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
@@ -28,12 +25,10 @@ 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);
+ return fpconvert_from_bits<double, fputil::FPType::IEEE754_Binary16>(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
index aa24e939c8767..fc3636f1db314 100644
--- a/libc/src/__support/builtins/extendhfsf2.h
+++ b/libc/src/__support/builtins/extendhfsf2.h
@@ -15,11 +15,8 @@
#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/FPUtil/FPBits.h"
#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
@@ -28,12 +25,10 @@ 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);
+ return fpconvert_from_bits<float, fputil::FPType::IEEE754_Binary16>(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
index 54f34b57d7ba2..05e488a147c05 100644
--- a/libc/src/__support/builtins/extendhftf2.h
+++ b/libc/src/__support/builtins/extendhftf2.h
@@ -18,9 +18,10 @@
#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)
+#if defined(LIBC_TYPES_HAS_FLOAT128)
#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
@@ -29,12 +30,12 @@ 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);
+ return fpconvert_from_bits<float128, fputil::FPType::IEEE754_Binary16>(bits);
}
} // namespace builtins
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_FLOAT16
+#endif // LIBC_TYPES_HAS_FLOAT128
#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 b0c194986b328..ca7c61e187fab 100644
--- a/libc/src/__support/builtins/fpconvert_helper.h
+++ b/libc/src/__support/builtins/fpconvert_helper.h
@@ -45,25 +45,26 @@ namespace builtins {
// __trunc*.
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) {
+// Shared conversion body. The source is named by FPType, so it needs no
+// native C++ type; only the destination does.
+template <typename To, fputil::FPType FromFPType>
+LIBC_INLINE constexpr To
+fpconvert(typename fputil::FPRep<FromFPType>::StorageType bits) {
+ using FromRep = fputil::FPRep<FromFPType>;
using ToBits = fputil::FPBits<To>;
using ToStorageType = typename ToBits::StorageType;
+ FromRep x_bits(bits);
+
if (x_bits.is_nan()) {
- typename FromBits::StorageType x_frac = x_bits.get_mantissa();
- if constexpr (ToBits::FRACTION_LEN >= FromBits::FRACTION_LEN) {
- ToStorageType to_frac =
- static_cast<ToStorageType>(x_frac)
- << (ToBits::FRACTION_LEN - FromBits::FRACTION_LEN);
+ typename FromRep::StorageType x_frac = x_bits.get_mantissa();
+ if constexpr (ToBits::FRACTION_LEN >= FromRep::FRACTION_LEN) {
+ ToStorageType to_frac = static_cast<ToStorageType>(x_frac)
+ << (ToBits::FRACTION_LEN - FromRep::FRACTION_LEN);
return ToBits::signaling_nan(x_bits.sign(), to_frac).get_val();
}
ToStorageType to_frac = static_cast<ToStorageType>(
- x_frac >> (FromBits::FRACTION_LEN - ToBits::FRACTION_LEN));
+ x_frac >> (FromRep::FRACTION_LEN - ToBits::FRACTION_LEN));
return ToBits::quiet_nan(x_bits.sign(), to_frac).get_val();
}
@@ -71,10 +72,14 @@ LIBC_INLINE constexpr To fpconvert(FromBits x_bits) {
return ToBits::inf(x_bits.sign()).get_val();
// Zero and subnormals fall through: DyadicFloat gives a zero mantissa for
- // zero, which as<To>() maps back to a correctly-signed zero.
+ // zero, which as<To>() maps back to a correctly-signed zero. Built from
+ // parts so no From value is materialized.
constexpr size_t MAX_FRACTION_LEN =
- cpp::max(ToBits::FRACTION_LEN, FromBits::FRACTION_LEN);
- fputil::DyadicFloat<cpp::bit_ceil(MAX_FRACTION_LEN)> xd(x_bits.get_val());
+ cpp::max(ToBits::FRACTION_LEN, FromRep::FRACTION_LEN);
+ using DyadicType = fputil::DyadicFloat<cpp::bit_ceil(MAX_FRACTION_LEN)>;
+ DyadicType xd(
+ x_bits.sign(), x_bits.get_explicit_exponent() - FromRep::FRACTION_LEN,
+ typename DyadicType::MantissaType(x_bits.get_explicit_mantissa()));
return xd.template as<To, /*ShouldSignalExceptions=*/true>();
}
@@ -86,19 +91,16 @@ LIBC_INLINE constexpr To fpconvert(From x) {
if constexpr (cpp::is_same_v<To, From>)
return x;
else
- return internal::fpconvert<To>(fputil::FPBits<From>(x));
+ return internal::fpconvert<To, fputil::get_fp_type<From>()>(
+ cpp::bit_cast<typename fputil::FPBits<From>::StorageType>(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) {
- if constexpr (cpp::is_same_v<To, From>)
- return cpp::bit_cast<To>(bits);
- else
- return internal::fpconvert<To>(
- fputil::FPBits<From>(cpp::bit_cast<From>(bits)));
+// Same, for a source delivered as raw bits. Keeps _Float16 out of the
+// signature, which would otherwise lower to a circular __extendhfsf2.
+template <typename To, fputil::FPType FromFPType>
+LIBC_INLINE constexpr To
+fpconvert_from_bits(typename fputil::FPRep<FromFPType>::StorageType bits) {
+ return internal::fpconvert<To, FromFPType>(bits);
}
} // namespace builtins
>From ccacaec4342173a6d462a731102a9644b7c9093a Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sun, 9 Aug 2026 00:05:20 +0300
Subject: [PATCH 05/10] extern block
---
compiler-rt/lib/builtins/extendhfsf2.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/compiler-rt/lib/builtins/extendhfsf2.cpp b/compiler-rt/lib/builtins/extendhfsf2.cpp
index 0bcc82058b11b..0bb0c00264a3e 100644
--- a/compiler-rt/lib/builtins/extendhfsf2.cpp
+++ b/compiler-rt/lib/builtins/extendhfsf2.cpp
@@ -20,12 +20,12 @@
#include "shared/bit.h"
#include "shared/builtins/extendhfsf2.h"
-extern "C" COMPILER_RT_ABI NOINLINE dst_t __extendhfsf2(src_t a) {
+extern "C" {
+COMPILER_RT_ABI NOINLINE dst_t __extendhfsf2(src_t a) {
return LIBC_NAMESPACE::shared::extendhfsf2(
LIBC_NAMESPACE::shared::bit_cast<uint16_t>(a));
}
-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); }
>From a9afb12b1f7c90d1fb7a02268684f1f9f368b609 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Tue, 8 Sep 2026 19:06:16 +0300
Subject: [PATCH 06/10] add a convert helper that returns raw bits instead of a
value
---
libc/src/__support/FPUtil/dyadic_float.h | 62 +++++++++++--------
.../src/__support/builtins/fpconvert_helper.h | 34 ++++++++++
2 files changed, 71 insertions(+), 25 deletions(-)
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index b86caa29f688b..b6128eb6d6e3d 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -183,42 +183,45 @@ template <size_t Bits> struct DyadicFloat {
return DyadicFloat(result_sign, result_exponent, result_mantissa);
}
- template <typename T, bool ShouldSignalExceptions>
- LIBC_INLINE LIBC_CONSTEXPR_DEFAULT cpp::enable_if_t<
- cpp::is_floating_point_v<T> && (FPBits<T>::FRACTION_LEN < Bits), T>
- generic_as() const {
- using FPBits = FPBits<T>;
- using StorageType = typename FPBits::StorageType;
-
- constexpr int EXTRA_FRACTION_LEN = Bits - 1 - FPBits::FRACTION_LEN;
+ // The integer core of generic_as(). The destination is named by FPType, so
+ // it also covers formats with no usable native type (float16 without
+ // _Float16).
+ template <FPType DstType, bool ShouldSignalExceptions>
+ LIBC_INLINE LIBC_CONSTEXPR_DEFAULT typename FPRep<DstType>::StorageType
+ generic_as_bits() const {
+ using DstRep = FPRep<DstType>;
+ using StorageType = typename DstRep::StorageType;
+ static_assert(DstRep::FRACTION_LEN < Bits);
+
+ constexpr int EXTRA_FRACTION_LEN = Bits - 1 - DstRep::FRACTION_LEN;
if (mantissa == 0)
- return FPBits::zero(sign).get_val();
+ return DstRep::zero(sign).uintval();
int unbiased_exp = get_unbiased_exponent();
- if (unbiased_exp + FPBits::EXP_BIAS >= FPBits::MAX_BIASED_EXPONENT) {
+ if (unbiased_exp + DstRep::EXP_BIAS >= DstRep::MAX_BIASED_EXPONENT) {
if constexpr (ShouldSignalExceptions) {
set_errno_if_required(ERANGE);
raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
}
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
- return FPBits::inf(sign).get_val();
+ return DstRep::inf(sign).uintval();
#else // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
switch (quick_get_round()) {
case FE_TONEAREST:
- return FPBits::inf(sign).get_val();
+ return DstRep::inf(sign).uintval();
case FE_TOWARDZERO:
- return FPBits::max_normal(sign).get_val();
+ return DstRep::max_normal(sign).uintval();
case FE_DOWNWARD:
if (sign.is_pos())
- return FPBits::max_normal(Sign::POS).get_val();
- return FPBits::inf(Sign::NEG).get_val();
+ return DstRep::max_normal(Sign::POS).uintval();
+ return DstRep::inf(Sign::NEG).uintval();
case FE_UPWARD:
if (sign.is_neg())
- return FPBits::max_normal(Sign::NEG).get_val();
- return FPBits::inf(Sign::POS).get_val();
+ return DstRep::max_normal(Sign::NEG).uintval();
+ return DstRep::inf(Sign::POS).uintval();
default:
__builtin_unreachable();
}
@@ -231,10 +234,10 @@ template <size_t Bits> struct DyadicFloat {
bool sticky = false;
bool underflow = false;
- if (unbiased_exp < -FPBits::EXP_BIAS - FPBits::FRACTION_LEN) {
+ if (unbiased_exp < -DstRep::EXP_BIAS - DstRep::FRACTION_LEN) {
sticky = true;
underflow = true;
- } else if (unbiased_exp == -FPBits::EXP_BIAS - FPBits::FRACTION_LEN) {
+ } else if (unbiased_exp == -DstRep::EXP_BIAS - DstRep::FRACTION_LEN) {
round = true;
// underflow is detected pre-rounding FE_UNDERFLOW may be raised
// even if rounding produces a non-underflow result
@@ -244,12 +247,12 @@ template <size_t Bits> struct DyadicFloat {
} else {
int extra_fraction_len = EXTRA_FRACTION_LEN;
- if (unbiased_exp < 1 - FPBits::EXP_BIAS) {
+ if (unbiased_exp < 1 - DstRep::EXP_BIAS) {
underflow = true;
- extra_fraction_len += 1 - FPBits::EXP_BIAS - unbiased_exp;
+ extra_fraction_len += 1 - DstRep::EXP_BIAS - unbiased_exp;
} else {
out_biased_exp =
- static_cast<StorageType>(unbiased_exp + FPBits::EXP_BIAS);
+ static_cast<StorageType>(unbiased_exp + DstRep::EXP_BIAS);
}
if (extra_fraction_len > 0) {
@@ -265,7 +268,7 @@ template <size_t Bits> struct DyadicFloat {
bool lsb = (out_mantissa & 1) != 0;
StorageType result =
- FPBits::create_value(sign, out_biased_exp, out_mantissa).uintval();
+ DstRep::create_value(sign, out_biased_exp, out_mantissa).uintval();
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
if (round && (lsb || sticky))
@@ -291,7 +294,7 @@ template <size_t Bits> struct DyadicFloat {
if (ShouldSignalExceptions && (round || sticky)) {
int excepts = FE_INEXACT;
- if (FPBits(result).is_inf()) {
+ if (DstRep(result).is_inf()) {
set_errno_if_required(ERANGE);
excepts |= FE_OVERFLOW;
} else if (underflow) {
@@ -301,7 +304,16 @@ template <size_t Bits> struct DyadicFloat {
raise_except_if_required(excepts);
}
- return FPBits(result).get_val();
+ return result;
+ }
+
+ template <typename T, bool ShouldSignalExceptions>
+ LIBC_INLINE LIBC_CONSTEXPR_DEFAULT cpp::enable_if_t<
+ cpp::is_floating_point_v<T> && (FPBits<T>::FRACTION_LEN < Bits), T>
+ generic_as() const {
+ return FPBits<T>(
+ generic_as_bits<get_fp_type<T>(), ShouldSignalExceptions>())
+ .get_val();
}
template <typename T, bool ShouldSignalExceptions,
diff --git a/libc/src/__support/builtins/fpconvert_helper.h b/libc/src/__support/builtins/fpconvert_helper.h
index ca7c61e187fab..c6d52b283a328 100644
--- a/libc/src/__support/builtins/fpconvert_helper.h
+++ b/libc/src/__support/builtins/fpconvert_helper.h
@@ -103,6 +103,40 @@ fpconvert_from_bits(typename fputil::FPRep<FromFPType>::StorageType bits) {
return internal::fpconvert<To, FromFPType>(bits);
}
+// Same, for a destination delivered as raw bits. Keeps _Float16 out of the
+// signature, which would otherwise lower to a circular __trunc*hf2.
+template <fputil::FPType ToFPType, typename From>
+LIBC_INLINE constexpr typename fputil::FPRep<ToFPType>::StorageType
+fpconvert_to_bits(From x) {
+ using ToRep = fputil::FPRep<ToFPType>;
+ using ToStorageType = typename ToRep::StorageType;
+ using FromBits = fputil::FPBits<From>;
+
+ FromBits x_bits(x);
+
+ if (x_bits.is_nan()) {
+ typename FromBits::StorageType x_frac = x_bits.get_mantissa();
+ if constexpr (ToRep::FRACTION_LEN >= FromBits::FRACTION_LEN) {
+ ToStorageType to_frac = static_cast<ToStorageType>(x_frac)
+ << (ToRep::FRACTION_LEN - FromBits::FRACTION_LEN);
+ return ToRep::signaling_nan(x_bits.sign(), to_frac).uintval();
+ } else {
+ ToStorageType to_frac = static_cast<ToStorageType>(
+ x_frac >> (FromBits::FRACTION_LEN - ToRep::FRACTION_LEN));
+ return ToRep::quiet_nan(x_bits.sign(), to_frac).uintval();
+ }
+ }
+
+ if (x_bits.is_inf())
+ return ToRep::inf(x_bits.sign()).uintval();
+
+ constexpr size_t MAX_FRACTION_LEN =
+ cpp::max(ToRep::FRACTION_LEN, FromBits::FRACTION_LEN);
+ fputil::DyadicFloat<cpp::bit_ceil(MAX_FRACTION_LEN)> xd(x);
+ return xd.template generic_as_bits<ToFPType,
+ /*ShouldSignalExceptions=*/true>();
+}
+
} // namespace builtins
} // namespace LIBC_NAMESPACE_DECL
>From 85cf6fab27595d25d0c49ee2d2bf4893f5bee5e8 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Tue, 8 Sep 2026 19:06:17 +0300
Subject: [PATCH 07/10] make float16 truncate builtins independent of _Float16
---
libc/shared/builtins/truncdfhf2.h | 6 ------
libc/shared/builtins/truncsfhf2.h | 6 ------
libc/shared/builtins/trunctfhf2.h | 4 ++--
libc/src/__support/builtins/CMakeLists.txt | 9 +++------
libc/src/__support/builtins/truncdfhf2.h | 12 +++---------
libc/src/__support/builtins/truncsfhf2.h | 12 +++---------
libc/src/__support/builtins/trunctfhf2.h | 10 +++++-----
7 files changed, 16 insertions(+), 43 deletions(-)
diff --git a/libc/shared/builtins/truncdfhf2.h b/libc/shared/builtins/truncdfhf2.h
index 697261cc261e1..426c3eef674e4 100644
--- a/libc/shared/builtins/truncdfhf2.h
+++ b/libc/shared/builtins/truncdfhf2.h
@@ -15,10 +15,6 @@
#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"
@@ -30,6 +26,4 @@ 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
index de011638e7c12..0671d437b72c3 100644
--- a/libc/shared/builtins/truncsfhf2.h
+++ b/libc/shared/builtins/truncsfhf2.h
@@ -15,10 +15,6 @@
#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"
@@ -30,6 +26,4 @@ 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
index 00134f87c9978..31fcecad9cc0d 100644
--- a/libc/shared/builtins/trunctfhf2.h
+++ b/libc/shared/builtins/trunctfhf2.h
@@ -18,7 +18,7 @@
#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)
+#ifdef LIBC_TYPES_HAS_FLOAT128
#include "shared/libc_common.h"
#include "src/__support/builtins/trunctfhf2.h"
@@ -31,6 +31,6 @@ using builtins::trunctfhf2;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_FLOAT16
+#endif // LIBC_TYPES_HAS_FLOAT128
#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCTFHF2_H
diff --git a/libc/src/__support/builtins/CMakeLists.txt b/libc/src/__support/builtins/CMakeLists.txt
index 85e05b0178626..def0fed3c30cf 100644
--- a/libc/src/__support/builtins/CMakeLists.txt
+++ b/libc/src/__support/builtins/CMakeLists.txt
@@ -538,8 +538,7 @@ add_header_library(
truncsfhf2.h
DEPENDS
libc.hdr.stdint_proxy
- libc.src.__support.CPP.bit
- libc.src.__support.FPUtil.cast
+ libc.src.__support.builtins.fpconvert_helper
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
@@ -550,8 +549,7 @@ add_header_library(
truncdfhf2.h
DEPENDS
libc.hdr.stdint_proxy
- libc.src.__support.CPP.bit
- libc.src.__support.FPUtil.cast
+ libc.src.__support.builtins.fpconvert_helper
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
@@ -563,8 +561,7 @@ add_header_library(
DEPENDS
libc.hdr.stdint_proxy
libc.include.llvm-libc-types.float128
- libc.src.__support.CPP.bit
- libc.src.__support.FPUtil.cast
+ libc.src.__support.builtins.fpconvert_helper
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)
diff --git a/libc/src/__support/builtins/truncdfhf2.h b/libc/src/__support/builtins/truncdfhf2.h
index af2506b1a93c8..cdadeef1d87a5 100644
--- a/libc/src/__support/builtins/truncdfhf2.h
+++ b/libc/src/__support/builtins/truncdfhf2.h
@@ -15,13 +15,9 @@
#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/FPUtil/FPBits.h"
+#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
@@ -29,12 +25,10 @@ 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));
+ return fpconvert_to_bits<fputil::FPType::IEEE754_Binary16>(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
index a323e7ee76acf..72419452fbf30 100644
--- a/libc/src/__support/builtins/truncsfhf2.h
+++ b/libc/src/__support/builtins/truncsfhf2.h
@@ -15,13 +15,9 @@
#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/FPUtil/FPBits.h"
+#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
@@ -29,12 +25,10 @@ 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));
+ return fpconvert_to_bits<fputil::FPType::IEEE754_Binary16>(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
index db9790a5f3ea1..60de2e37efef8 100644
--- a/libc/src/__support/builtins/trunctfhf2.h
+++ b/libc/src/__support/builtins/trunctfhf2.h
@@ -18,11 +18,11 @@
#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)
+#ifdef LIBC_TYPES_HAS_FLOAT128
#include "hdr/stdint_proxy.h"
-#include "src/__support/CPP/bit.h"
-#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/builtins/fpconvert_helper.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
@@ -30,12 +30,12 @@ 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));
+ return fpconvert_to_bits<fputil::FPType::IEEE754_Binary16>(x);
}
} // namespace builtins
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128 && LIBC_TYPES_HAS_FLOAT16
+#endif // LIBC_TYPES_HAS_FLOAT128
#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFHF2_H
>From fff066de84f186210dfe76f162fbce546b9d8963 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Tue, 8 Sep 2026 19:06:17 +0300
Subject: [PATCH 08/10] drop the float16 ifdefs and the old C fallback
---
compiler-rt/lib/builtins/extendhftf2.cpp | 2 +-
compiler-rt/lib/builtins/truncdfhf2.cpp | 6 ------
compiler-rt/lib/builtins/truncsfhf2.cpp | 18 ++++++++----------
compiler-rt/lib/builtins/trunctfhf2.cpp | 8 +-------
4 files changed, 10 insertions(+), 24 deletions(-)
diff --git a/compiler-rt/lib/builtins/extendhftf2.cpp b/compiler-rt/lib/builtins/extendhftf2.cpp
index d7e3db737acaf..ed2543e19e79f 100644
--- a/compiler-rt/lib/builtins/extendhftf2.cpp
+++ b/compiler-rt/lib/builtins/extendhftf2.cpp
@@ -19,7 +19,7 @@
#include "shared/bit.h"
#include "shared/builtins/extendhftf2.h"
-#if defined(CRT_HAS_TF_MODE) && defined(COMPILER_RT_HAS_FLOAT16)
+#ifdef CRT_HAS_TF_MODE
#define SRC_HALF
#define DST_QUAD
#include "fp_extend_impl.inc"
diff --git a/compiler-rt/lib/builtins/truncdfhf2.cpp b/compiler-rt/lib/builtins/truncdfhf2.cpp
index 77a93394b4ab6..eb3ae2f2e17c8 100644
--- a/compiler-rt/lib/builtins/truncdfhf2.cpp
+++ b/compiler-rt/lib/builtins/truncdfhf2.cpp
@@ -20,13 +20,7 @@
#include "shared/bit.h"
#include "shared/builtins/truncdfhf2.h"
-#ifdef LIBC_TYPES_HAS_FLOAT16
extern "C" COMPILER_RT_ABI dst_t __truncdfhf2(src_t a) {
return LIBC_NAMESPACE::shared::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
index b0ab659562ec9..2f012b2dc33c1 100644
--- a/compiler-rt/lib/builtins/truncsfhf2.cpp
+++ b/compiler-rt/lib/builtins/truncsfhf2.cpp
@@ -20,27 +20,25 @@
#include "shared/bit.h"
#include "shared/builtins/truncsfhf2.h"
-#ifdef LIBC_TYPES_HAS_FLOAT16
-extern "C" COMPILER_RT_ABI NOINLINE dst_t __truncsfhf2(src_t a) {
+static inline dst_t truncsfhf2_impl(src_t a) {
return LIBC_NAMESPACE::shared::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" {
+COMPILER_RT_ABI NOINLINE dst_t __truncsfhf2(src_t a) {
+ return truncsfhf2_impl(a);
+}
+
#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); }
+AEABI_RTABI dst_t __gnu_f2h_ieee(src_t a) { return truncsfhf2_impl(a); }
+AEABI_RTABI dst_t __aeabi_f2h(src_t a) { return truncsfhf2_impl(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); }
+COMPILER_RT_ABI dst_t __gnu_f2h_ieee(src_t a) { return truncsfhf2_impl(a); }
#endif
}
diff --git a/compiler-rt/lib/builtins/trunctfhf2.cpp b/compiler-rt/lib/builtins/trunctfhf2.cpp
index a0a3f154edfb8..388a22b74a72b 100644
--- a/compiler-rt/lib/builtins/trunctfhf2.cpp
+++ b/compiler-rt/lib/builtins/trunctfhf2.cpp
@@ -19,19 +19,13 @@
#include "shared/bit.h"
#include "shared/builtins/trunctfhf2.h"
-#if defined(CRT_HAS_TF_MODE) && defined(COMPILER_RT_HAS_FLOAT16)
+#ifdef CRT_HAS_TF_MODE
#define SRC_QUAD
#define DST_HALF
#include "fp_trunc_impl.inc"
-#ifdef LIBC_TYPES_HAS_FLOAT16
extern "C" COMPILER_RT_ABI dst_t __trunctfhf2(src_t a) {
return LIBC_NAMESPACE::shared::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
>From fa4419560617eb9f0b29f6335621f3d0bc18e70e Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Tue, 8 Sep 2026 19:06:18 +0300
Subject: [PATCH 09/10] call libc's extendhfsf2 in the aliases instead of
__extendhfsf2
---
compiler-rt/lib/builtins/extendhfsf2.cpp | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/compiler-rt/lib/builtins/extendhfsf2.cpp b/compiler-rt/lib/builtins/extendhfsf2.cpp
index 0bb0c00264a3e..309888f317c6e 100644
--- a/compiler-rt/lib/builtins/extendhfsf2.cpp
+++ b/compiler-rt/lib/builtins/extendhfsf2.cpp
@@ -20,21 +20,25 @@
#include "shared/bit.h"
#include "shared/builtins/extendhfsf2.h"
-extern "C" {
-COMPILER_RT_ABI NOINLINE dst_t __extendhfsf2(src_t a) {
+static inline dst_t extendhfsf2_impl(src_t a) {
return LIBC_NAMESPACE::shared::extendhfsf2(
LIBC_NAMESPACE::shared::bit_cast<uint16_t>(a));
}
+extern "C" {
+COMPILER_RT_ABI NOINLINE dst_t __extendhfsf2(src_t a) {
+ return extendhfsf2_impl(a);
+}
+
#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); }
+AEABI_RTABI dst_t __gnu_h2f_ieee(src_t a) { return extendhfsf2_impl(a); }
+AEABI_RTABI dst_t __aeabi_h2f(src_t a) { return extendhfsf2_impl(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); }
+COMPILER_RT_ABI dst_t __gnu_h2f_ieee(src_t a) { return extendhfsf2_impl(a); }
#endif
}
>From 2eae929a96bd08cf3f43e0023016c5d6a0793583 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Tue, 8 Sep 2026 19:41:25 +0300
Subject: [PATCH 10/10] use the new name main gave the float128 macro
---
libc/shared/builtins/extendhftf2.h | 4 ++--
libc/shared/builtins/trunctfhf2.h | 4 ++--
libc/src/__support/builtins/extendhftf2.h | 4 ++--
libc/src/__support/builtins/trunctfhf2.h | 4 ++--
4 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/libc/shared/builtins/extendhftf2.h b/libc/shared/builtins/extendhftf2.h
index d6b8b2e4a966b..dd3c72ee53fe7 100644
--- a/libc/shared/builtins/extendhftf2.h
+++ b/libc/shared/builtins/extendhftf2.h
@@ -18,7 +18,7 @@
#include "include/llvm-libc-types/float128.h"
#include "src/__support/macros/properties/types.h"
-#ifdef LIBC_TYPES_HAS_FLOAT128
+#ifdef LIBC_TYPES_HAS_NATIVE_FLOAT128
#include "shared/libc_common.h"
#include "src/__support/builtins/extendhftf2.h"
@@ -31,6 +31,6 @@ using builtins::extendhftf2;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128
+#endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
#endif // LLVM_LIBC_SHARED_BUILTINS_EXTENDHFTF2_H
diff --git a/libc/shared/builtins/trunctfhf2.h b/libc/shared/builtins/trunctfhf2.h
index 31fcecad9cc0d..d6aa0211c22fc 100644
--- a/libc/shared/builtins/trunctfhf2.h
+++ b/libc/shared/builtins/trunctfhf2.h
@@ -18,7 +18,7 @@
#include "include/llvm-libc-types/float128.h"
#include "src/__support/macros/properties/types.h"
-#ifdef LIBC_TYPES_HAS_FLOAT128
+#ifdef LIBC_TYPES_HAS_NATIVE_FLOAT128
#include "shared/libc_common.h"
#include "src/__support/builtins/trunctfhf2.h"
@@ -31,6 +31,6 @@ using builtins::trunctfhf2;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128
+#endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
#endif // LLVM_LIBC_SHARED_BUILTINS_TRUNCTFHF2_H
diff --git a/libc/src/__support/builtins/extendhftf2.h b/libc/src/__support/builtins/extendhftf2.h
index 05e488a147c05..01afaf1c20e7a 100644
--- a/libc/src/__support/builtins/extendhftf2.h
+++ b/libc/src/__support/builtins/extendhftf2.h
@@ -18,7 +18,7 @@
#include "include/llvm-libc-types/float128.h"
#include "src/__support/macros/properties/types.h"
-#if defined(LIBC_TYPES_HAS_FLOAT128)
+#ifdef LIBC_TYPES_HAS_NATIVE_FLOAT128
#include "hdr/stdint_proxy.h"
#include "src/__support/FPUtil/FPBits.h"
@@ -36,6 +36,6 @@ LIBC_INLINE float128 extendhftf2(uint16_t bits) {
} // namespace builtins
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128
+#endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_EXTENDHFTF2_H
diff --git a/libc/src/__support/builtins/trunctfhf2.h b/libc/src/__support/builtins/trunctfhf2.h
index 60de2e37efef8..94869000255d6 100644
--- a/libc/src/__support/builtins/trunctfhf2.h
+++ b/libc/src/__support/builtins/trunctfhf2.h
@@ -18,7 +18,7 @@
#include "include/llvm-libc-types/float128.h"
#include "src/__support/macros/properties/types.h"
-#ifdef LIBC_TYPES_HAS_FLOAT128
+#ifdef LIBC_TYPES_HAS_NATIVE_FLOAT128
#include "hdr/stdint_proxy.h"
#include "src/__support/FPUtil/FPBits.h"
@@ -36,6 +36,6 @@ LIBC_INLINE uint16_t trunctfhf2(float128 x) {
} // namespace builtins
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TYPES_HAS_FLOAT128
+#endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
#endif // LLVM_LIBC_SRC___SUPPORT_BUILTINS_TRUNCTFHF2_H
More information about the libc-commits
mailing list