[libc-commits] [libc] [llvm] [libc][WIP] Add guard for bfloat16 type (PR #221327)

via libc-commits libc-commits at lists.llvm.org
Thu Oct 29 07:00:41 PDT 2026


https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/221327

>From b11d2d3d60eee811c0c23b83aa1a2f87d8ff6058 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 5 Sep 2026 01:07:42 +0530
Subject: [PATCH 01/11] initial commit

---
 libc/include/llvm-libc-macros/CMakeLists.txt  |   6 ++
 .../llvm-libc-macros/bfloat16-macros.h        |  19 ++++
 .../macros/properties/CMakeLists.txt          | 101 +++++++++---------
 libc/test/src/__support/FPUtil/CMakeLists.txt |  12 +++
 .../FPUtil/builtin_bfloat16_test.cpp          |  27 +++++
 .../llvm-project-overlay/libc/BUILD.bazel     |   5 +
 6 files changed, 120 insertions(+), 50 deletions(-)
 create mode 100644 libc/include/llvm-libc-macros/bfloat16-macros.h
 create mode 100644 libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp

diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index ed934df81886fe..097c49f4f327d4 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -48,6 +48,12 @@ add_macro_header(
     assert-macros.h
 )
 
+add_macro_header(
+  bfloat16_macros
+  HDR
+    bfloat16-macros.h
+)
+
 add_macro_header(
   byteswap_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
new file mode 100644
index 00000000000000..5d1a9a46fcb1be
--- /dev/null
+++ b/libc/include/llvm-libc-macros/bfloat16-macros.h
@@ -0,0 +1,19 @@
+#ifndef LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
+#define LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
+
+// Clang supports __bf16 when __is_identifier(__bf16) is 0
+// GCC supports __bf16 starting in GCC 13 for x86, ARM, AArch64
+#if defined(__clang__)
+#  if !__is_identifier(__bf16)
+#    if !defined(__i386__) || defined(__SSE2__)
+#      define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+#    endif
+#  endif
+#elif defined(__GNUC__) && (__GNUC__ >= 13)
+#  if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) || \
+      defined(__aarch64__) || defined(__arm__)
+#    define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+#  endif
+#endif
+
+#endif // LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index dfa2f9c572492a..6ce47b3f724f4c 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -1,50 +1,51 @@
-add_header_library(
-  architectures
-  HDRS
-    architectures.h
-)
-
-add_header_library(
-  compiler
-  HDRS
-    compiler.h
-)
-
-add_header_library(
-  os
-  HDRS
-    os.h
-)
-
-add_header_library(
-  cpu_features
-  HDRS
-    cpu_features.h
-  DEPENDS
-    .architectures
-)
-
-add_header_library(
-  types
-  HDRS
-    types.h
-  DEPENDS
-    .architectures
-    .compiler
-    .cpu_features
-    .os
-    libc.hdr.float_macros
-    libc.hdr.stdint_proxy
-    libc.include.llvm-libc-macros.float16_macros
-    libc.include.llvm-libc-types.float128
-)
-
-add_header_library(
-  complex_types
-  HDRS
-    complex_types.h
-  DEPENDS
-    .types
-    libc.include.llvm-libc-types.cfloat16
-    libc.include.llvm-libc-types.cfloat128
-)
+add_header_library(
+  architectures
+  HDRS
+    architectures.h
+)
+
+add_header_library(
+  compiler
+  HDRS
+    compiler.h
+)
+
+add_header_library(
+  os
+  HDRS
+    os.h
+)
+
+add_header_library(
+  cpu_features
+  HDRS
+    cpu_features.h
+  DEPENDS
+    .architectures
+)
+
+add_header_library(
+  types
+  HDRS
+    types.h
+  DEPENDS
+    .architectures
+    .compiler
+    .cpu_features
+    .os
+    libc.hdr.float_macros
+    libc.hdr.stdint_proxy
+    libc.include.llvm-libc-macros.float16_macros
+    libc.include.llvm-libc-macros.bfloat16_macros
+    libc.include.llvm-libc-types.float128
+)
+
+add_header_library(
+  complex_types
+  HDRS
+    complex_types.h
+  DEPENDS
+    .types
+    libc.include.llvm-libc-types.cfloat16
+    libc.include.llvm-libc-types.cfloat128
+)
diff --git a/libc/test/src/__support/FPUtil/CMakeLists.txt b/libc/test/src/__support/FPUtil/CMakeLists.txt
index 082f13ff9d661e..dc0c014b41a69a 100644
--- a/libc/test/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/test/src/__support/FPUtil/CMakeLists.txt
@@ -1,5 +1,17 @@
 add_libc_testsuite(libc-fputil-tests PARENT libc-support-tests)
 
+add_fp_unittest(
+  builtin_bfloat16_test
+  SUITE
+    libc-fputil-tests
+  SRCS
+    builtin_bfloat16_test.cpp
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.macros.properties.types
+)
+
 add_fp_unittest(
   dyadic_float_test
   NEED_MPFR
diff --git a/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp b/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp
new file mode 100644
index 00000000000000..658db59631fed6
--- /dev/null
+++ b/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp
@@ -0,0 +1,27 @@
+//===-- Unit tests for builtin __bf16 type --------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/properties/types.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+
+using BFloat16 = LIBC_NAMESPACE::fputil::BFloat16;
+
+TEST(LlvmLibcBuiltinBFloat16Test, TypeSizeAndAlignment) {
+  static_assert(sizeof(__bf16) == 2);
+  static_assert(alignof(__bf16) == 2);
+  __bf16 a = 1.0;
+  double b = 1.0;
+  static_assert(__bf16(b) == a);
+}
+
+#endif // LIBC_TYPES_HAS_BUILTIN_BFLOAT16
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 210e2e1eb4a09a..8fcbe72e2bd988 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -544,6 +544,11 @@ libc_support_library(
     deps = [":llvm_libc_macros_float_macros"],
 )
 
+libc_support_library(
+    name = "llvm_libc_macros_bfloat16_macros",
+    hdrs = ["include/llvm-libc-macros/bfloat16-macros.h"],
+)
+
 libc_support_library(
     name = "llvm_libc_types_cfloat128",
     hdrs = ["include/llvm-libc-types/cfloat128.h"],

>From 588860f8dd9a57ba973c9343fbf1df30034ec25b Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 5 Sep 2026 01:08:11 +0530
Subject: [PATCH 02/11] clang-format

---
 .../include/llvm-libc-macros/bfloat16-macros.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
index 5d1a9a46fcb1be..dbb7b9f040cdd1 100644
--- a/libc/include/llvm-libc-macros/bfloat16-macros.h
+++ b/libc/include/llvm-libc-macros/bfloat16-macros.h
@@ -4,16 +4,16 @@
 // Clang supports __bf16 when __is_identifier(__bf16) is 0
 // GCC supports __bf16 starting in GCC 13 for x86, ARM, AArch64
 #if defined(__clang__)
-#  if !__is_identifier(__bf16)
-#    if !defined(__i386__) || defined(__SSE2__)
-#      define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
-#    endif
-#  endif
+#if !__is_identifier(__bf16)
+#if !defined(__i386__) || defined(__SSE2__)
+#define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+#endif
+#endif
 #elif defined(__GNUC__) && (__GNUC__ >= 13)
-#  if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) || \
-      defined(__aarch64__) || defined(__arm__)
-#    define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
-#  endif
+#if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) ||         \
+    defined(__aarch64__) || defined(__arm__)
+#define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+#endif
 #endif
 
 #endif // LLVM_LIBC_MACROS_BFLOAT16_MACROS_H

>From 6dc46b99ab751056e52a09d495269473d0491b6b Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 5 Sep 2026 01:14:19 +0530
Subject: [PATCH 03/11] lf

---
 .../macros/properties/CMakeLists.txt          | 102 +++++++++---------
 1 file changed, 51 insertions(+), 51 deletions(-)

diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index 6ce47b3f724f4c..7eeabbbb916b0c 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -1,51 +1,51 @@
-add_header_library(
-  architectures
-  HDRS
-    architectures.h
-)
-
-add_header_library(
-  compiler
-  HDRS
-    compiler.h
-)
-
-add_header_library(
-  os
-  HDRS
-    os.h
-)
-
-add_header_library(
-  cpu_features
-  HDRS
-    cpu_features.h
-  DEPENDS
-    .architectures
-)
-
-add_header_library(
-  types
-  HDRS
-    types.h
-  DEPENDS
-    .architectures
-    .compiler
-    .cpu_features
-    .os
-    libc.hdr.float_macros
-    libc.hdr.stdint_proxy
-    libc.include.llvm-libc-macros.float16_macros
-    libc.include.llvm-libc-macros.bfloat16_macros
-    libc.include.llvm-libc-types.float128
-)
-
-add_header_library(
-  complex_types
-  HDRS
-    complex_types.h
-  DEPENDS
-    .types
-    libc.include.llvm-libc-types.cfloat16
-    libc.include.llvm-libc-types.cfloat128
-)
+add_header_library(
+  architectures
+  HDRS
+    architectures.h
+)
+
+add_header_library(
+  compiler
+  HDRS
+    compiler.h
+)
+
+add_header_library(
+  os
+  HDRS
+    os.h
+)
+
+add_header_library(
+  cpu_features
+  HDRS
+    cpu_features.h
+  DEPENDS
+    .architectures
+)
+
+add_header_library(
+  types
+  HDRS
+    types.h
+  DEPENDS
+    .architectures
+    .compiler
+    .cpu_features
+    .os
+    libc.hdr.float_macros
+    libc.hdr.stdint_proxy
+    libc.include.llvm-libc-macros.float16_macros
+    libc.include.llvm-libc-macros.bfloat16_macros
+    libc.include.llvm-libc-types.float128
+)
+
+add_header_library(
+  complex_types
+  HDRS
+    complex_types.h
+  DEPENDS
+    .types
+    libc.include.llvm-libc-types.cfloat16
+    libc.include.llvm-libc-types.cfloat128
+)

>From ade3fb8b39640563a407f805c177292431835a96 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 24 Oct 2026 19:51:20 +0530
Subject: [PATCH 04/11] add check_bfloat16

---
 libc/cmake/modules/CheckCompilerFeatures.cmake          | 3 +++
 libc/cmake/modules/compiler_features/check_bfloat16.cpp | 5 +++++
 libc/src/__support/macros/properties/types.h            | 1 +
 3 files changed, 9 insertions(+)
 create mode 100644 libc/cmake/modules/compiler_features/check_bfloat16.cpp

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index eaf702710b1cce..4473419ea5b812 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -4,6 +4,7 @@
 
 set(
   ALL_COMPILER_FEATURES
+    "bfloat16"
     "builtin_ceil_floor_rint_trunc"
     "builtin_fmax_fmin"
     "builtin_fmaxf16_fminf16"
@@ -129,6 +130,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
       set(LIBC_TYPES_HAS_CFLOAT16 TRUE)
     elseif(${feature} STREQUAL "cfloat128")
       set(LIBC_TYPES_HAS_CFLOAT128 TRUE)
+    elseif(${feature} STREQUAL "bfloat16")
+      set(LIBC_TYPES_HAS_BUILTIN_BFLOAT16 TRUE)
     elseif(${feature} STREQUAL "builtin_ceil_floor_rint_trunc")
       set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC TRUE)
     elseif(${feature} STREQUAL "builtin_fmax_fmin")
diff --git a/libc/cmake/modules/compiler_features/check_bfloat16.cpp b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
new file mode 100644
index 00000000000000..90005a921af930
--- /dev/null
+++ b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
@@ -0,0 +1,5 @@
+#include "include/llvm-libc-macros/bfloat16-macros.h"
+
+#ifndef LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+#error unsupported
+#endif
\ No newline at end of file
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index aad4c41005c454..881430f4f92472 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -12,6 +12,7 @@
 
 #include "hdr/float_macros.h" // LDBL_MANT_DIG, LDBL_MAX_EXP
 #include "hdr/stdint_proxy.h" // UINT64_MAX, __SIZEOF_INT128__
+#include "include/llvm-libc-macros/bfloat16-macros.h" // LIBC_TYPES_HAS_BUILTIN_BFLOAT16
 #include "include/llvm-libc-macros/float16-macros.h" // LIBC_TYPES_HAS_FLOAT16
 #include "include/llvm-libc-types/float128.h"        // float128
 #include "src/__support/macros/config.h"             // LIBC_NAMESPACE_DECL

>From 35fba717954e267d648a93f80a0e698b1946f63c Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 25 Oct 2026 00:33:19 +0530
Subject: [PATCH 05/11] temp add builtin bf16

---
 .../CPP/type_traits/is_floating_point.h       |  4 ++++
 libc/src/__support/FPUtil/FPBits.h            |  4 ++++
 libc/src/__support/math/fabsbf16.h            | 20 ++++++++++++++++++-
 libc/src/math/fabsbf16.h                      |  4 ++++
 libc/src/math/generic/fabsbf16.cpp            |  4 ++++
 libc/test/src/math/fabsbf16_test.cpp          |  7 ++++++-
 libc/test/src/math/smoke/fabsbf16_test.cpp    |  7 ++++++-
 7 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/libc/src/__support/CPP/type_traits/is_floating_point.h b/libc/src/__support/CPP/type_traits/is_floating_point.h
index 4a5984e38a8cf0..c089d98588a4fc 100644
--- a/libc/src/__support/CPP/type_traits/is_floating_point.h
+++ b/libc/src/__support/CPP/type_traits/is_floating_point.h
@@ -39,6 +39,10 @@ template <typename T> struct is_floating_point {
 #ifdef LIBC_TYPES_HAS_NATIVE_FLOAT128
                               ,
                               float128
+#endif
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+                              ,
+                              __bf16
 #endif
                               ,
                               bfloat16
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 83dedeb45d4f3d..e04d3191336ea2 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -814,6 +814,10 @@ template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
 #if defined(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   else if constexpr (cpp::is_same_v<UnqualT, float128>)
     return FPType::IEEE754_Binary128;
+#endif
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  else if constexpr (cpp::is_same_v<UnqualT, __bf16>)
+    return FPType::BFloat16;
 #endif
   else if constexpr (cpp::is_same_v<UnqualT, bfloat16>)
     return FPType::BFloat16;
diff --git a/libc/src/__support/math/fabsbf16.h b/libc/src/__support/math/fabsbf16.h
index fd6593b36c3047..0157ab697c8ac7 100644
--- a/libc/src/__support/math/fabsbf16.h
+++ b/libc/src/__support/math/fabsbf16.h
@@ -9,9 +9,25 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FABSBF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_FABSBF16_H
 
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+
+#include "src/__support/FPUtil/BasicOperations.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr __bf16 fabsbf16(__bf16 x) { return fputil::abs(x); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#else // !defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+
 #include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
@@ -21,4 +37,6 @@ LIBC_INLINE constexpr bfloat16 fabsbf16(bfloat16 x) { return fputil::abs(x); }
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
 
+#endif // LIBC_TYPES_HAS_BUILTIN_BFLOAT16
+
 #endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABSBF16_H
diff --git a/libc/src/math/fabsbf16.h b/libc/src/math/fabsbf16.h
index 49936680eb28b6..ca329848fdf489 100644
--- a/libc/src/math/fabsbf16.h
+++ b/libc/src/math/fabsbf16.h
@@ -14,7 +14,11 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+__bf16 fabsbf16(__bf16 x);
+#else
 bfloat16 fabsbf16(bfloat16 x);
+#endif
 
 } // namespace LIBC_NAMESPACE_DECL
 
diff --git a/libc/src/math/generic/fabsbf16.cpp b/libc/src/math/generic/fabsbf16.cpp
index 1c311adacadd3f..b3442d47d9f191 100644
--- a/libc/src/math/generic/fabsbf16.cpp
+++ b/libc/src/math/generic/fabsbf16.cpp
@@ -11,8 +11,12 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+LLVM_LIBC_FUNCTION(__bf16, fabsbf16, (__bf16 x)) { return math::fabsbf16(x); }
+#else
 LLVM_LIBC_FUNCTION(bfloat16, fabsbf16, (bfloat16 x)) {
   return math::fabsbf16(x);
 }
+#endif
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/fabsbf16_test.cpp b/libc/test/src/math/fabsbf16_test.cpp
index 4e34224d2e644c..e0a2be2eb41d9e 100644
--- a/libc/test/src/math/fabsbf16_test.cpp
+++ b/libc/test/src/math/fabsbf16_test.cpp
@@ -13,7 +13,12 @@
 
 #include "FAbsTest.h"
 
-#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/properties/types.h"
 #include "src/math/fabsbf16.h"
 
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+LIST_FABS_TESTS(Fabsbf16, __bf16, LIBC_NAMESPACE::fabsbf16)
+#else
+#include "src/__support/FPUtil/bfloat16.h"
 LIST_FABS_TESTS(Fabsbf16, bfloat16, LIBC_NAMESPACE::fabsbf16)
+#endif
diff --git a/libc/test/src/math/smoke/fabsbf16_test.cpp b/libc/test/src/math/smoke/fabsbf16_test.cpp
index 16e11bb891edf9..90d12177d5c839 100644
--- a/libc/test/src/math/smoke/fabsbf16_test.cpp
+++ b/libc/test/src/math/smoke/fabsbf16_test.cpp
@@ -8,7 +8,12 @@
 
 #include "FAbsTest.h"
 
-#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/properties/types.h"
 #include "src/math/fabsbf16.h"
 
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+LIST_FABS_TESTS(Fabsbf16, __bf16, LIBC_NAMESPACE::fabsbf16)
+#else
+#include "src/__support/FPUtil/bfloat16.h"
 LIST_FABS_TESTS(Fabsbf16, bfloat16, LIBC_NAMESPACE::fabsbf16)
+#endif

>From 8d695a46d7d33797ee257e1ff3cdb20e6460aefb Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 29 Oct 2026 16:20:41 +0530
Subject: [PATCH 06/11] test function under guard

---
 libc/config/baremetal/aarch64/entrypoints.txt | 7 ++++++-
 libc/config/baremetal/arm/entrypoints.txt     | 7 ++++++-
 libc/config/baremetal/hexagon/entrypoints.txt | 7 ++++++-
 libc/config/baremetal/riscv/entrypoints.txt   | 7 ++++++-
 libc/config/darwin/aarch64/entrypoints.txt    | 7 ++++++-
 libc/config/darwin/x86_64/entrypoints.txt     | 7 ++++++-
 libc/config/freebsd/x86_64/entrypoints.txt    | 7 ++++++-
 libc/config/gpu/amdgpu/entrypoints.txt        | 7 ++++++-
 libc/config/gpu/nvptx/entrypoints.txt         | 7 ++++++-
 libc/config/linux/aarch64/entrypoints.txt     | 7 ++++++-
 libc/config/linux/arm/entrypoints.txt         | 7 ++++++-
 libc/config/linux/riscv/entrypoints.txt       | 7 ++++++-
 libc/config/linux/x86_64/entrypoints.txt      | 7 ++++++-
 libc/config/windows/entrypoints.txt           | 7 ++++++-
 libc/src/__support/math/CMakeLists.txt        | 2 ++
 libc/test/src/math/CMakeLists.txt             | 1 +
 libc/test/src/math/smoke/CMakeLists.txt       | 1 +
 17 files changed, 88 insertions(+), 14 deletions(-)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 7ec212d8709692..5e0acf797ca384 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -830,7 +830,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -885,6 +884,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C++23 mixed bfloat16 and _Float128 entrypoints
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 779d9fc23b28e8..8e0197095ff426 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -841,7 +841,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -896,6 +895,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C++23 mixed bfloat16 and _Float128 entrypoints
diff --git a/libc/config/baremetal/hexagon/entrypoints.txt b/libc/config/baremetal/hexagon/entrypoints.txt
index 61f869457b3282..9043ec5a2ef124 100644
--- a/libc/config/baremetal/hexagon/entrypoints.txt
+++ b/libc/config/baremetal/hexagon/entrypoints.txt
@@ -830,7 +830,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -885,6 +884,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C++23 mixed bfloat16 and _Float128 entrypoints
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index b847a2bb1f47f0..49299f297e30a8 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -833,7 +833,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -888,6 +887,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C++23 mixed bfloat16 and _Float128 entrypoints
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 5600b82ad3410b..ff1ac31c3f4075 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -652,7 +652,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -707,6 +706,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C++23 mixed bfloat16 and _Float128 entrypoints
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index e3fc845d2ed3af..f6e7ad272a6683 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -287,7 +287,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -343,6 +342,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 set(TARGET_LLVMLIBC_ENTRYPOINTS
   ${TARGET_LIBC_ENTRYPOINTS}
   ${TARGET_LIBM_ENTRYPOINTS}
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 8f6c178301c2f2..1bd61cbec34d0f 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -584,7 +584,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -639,6 +638,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     libc.src.math.bf16addf128
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 2eaef608e9a7ac..c05ba9b664e117 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -701,7 +701,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -756,6 +755,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 set(TARGET_LLVMLIBC_ENTRYPOINTS
   ${TARGET_LIBC_ENTRYPOINTS}
   ${TARGET_LIBM_ENTRYPOINTS}
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 79d0c733ec3c01..810069b8348c7a 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -717,7 +717,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -772,6 +771,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 set(TARGET_LLVMLIBC_ENTRYPOINTS
   ${TARGET_LIBC_ENTRYPOINTS}
   ${TARGET_LIBM_ENTRYPOINTS}
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ae4d060ad2e65c..cca8d0d2bf8263 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1062,7 +1062,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -1117,6 +1116,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C++23 mixed bfloat16 and _Float128 entrypoints
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index e35ca9c7211ab3..7208b3474b6445 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -663,7 +663,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -718,6 +717,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
   if(LLVM_LIBC_FULL_BUILD)
     list(APPEND TARGET_LIBC_ENTRYPOINTS
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index f8906e3adbfd30..bf1971e78b0347 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1140,7 +1140,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -1195,6 +1194,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C++23 mixed bfloat16 and _Float128 entrypoints
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5d67548617ea5b..e3f6169ff43673 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1162,7 +1162,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -1217,6 +1216,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_NATIVE_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C++23 mixed bfloat16 and _Float128 entrypoints
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 1712b2ad26fda4..ea39dca9f8ffce 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -378,7 +378,6 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.copysignbf16
   libc.src.math.exp2bf16
   libc.src.math.expbf16
-  libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
   libc.src.math.fmaxbf16
@@ -433,6 +432,12 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.ufromfpxbf16
 )
 
+if(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    libc.src.math.fabsbf16
+  )
+endif()
+
 set(TARGET_LLVMLIBC_ENTRYPOINTS
   ${TARGET_LIBC_ENTRYPOINTS}
   ${TARGET_LIBM_ENTRYPOINTS}
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c0751d7e15f102..9919cd3a5b9472 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3333,9 +3333,11 @@ add_header_library(
   HDRS
     fabsbf16.h
   DEPENDS
+    libc.include.llvm-libc-macros.bfloat16_macros
     libc.src.__support.FPUtil.basic_operations
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
 )
 
 add_header_library(
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index d88afe6370e27b..4938c9d4bd0cad 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -328,6 +328,7 @@ add_fp_unittest(
     libc.src.math.fabsbf16
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.macros.properties.types
 )
 
 add_fp_unittest(
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 76caeef863e342..fd49e2c5ea0d13 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -265,6 +265,7 @@ add_fp_unittest(
     FAbsTest.h
   DEPENDS
     libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.properties.types
     libc.src.math.fabsbf16
 )
 

>From ad155bfef8a9e981dfc119db6bf4b07d0a4839fb Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 29 Oct 2026 17:11:56 +0530
Subject: [PATCH 07/11] nits

---
 .../compiler_features/check_bfloat16.cpp      |  2 +-
 libc/src/__support/math/fabsbf16.h            | 25 +++++--------------
 2 files changed, 7 insertions(+), 20 deletions(-)

diff --git a/libc/cmake/modules/compiler_features/check_bfloat16.cpp b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
index 90005a921af930..9ca4cbd0ea2bd1 100644
--- a/libc/cmake/modules/compiler_features/check_bfloat16.cpp
+++ b/libc/cmake/modules/compiler_features/check_bfloat16.cpp
@@ -2,4 +2,4 @@
 
 #ifndef LIBC_TYPES_HAS_BUILTIN_BFLOAT16
 #error unsupported
-#endif
\ No newline at end of file
+#endif
diff --git a/libc/src/__support/math/fabsbf16.h b/libc/src/__support/math/fabsbf16.h
index 0157ab697c8ac7..777a10723c31bd 100644
--- a/libc/src/__support/math/fabsbf16.h
+++ b/libc/src/__support/math/fabsbf16.h
@@ -9,34 +9,21 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FABSBF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_FABSBF16_H
 
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/properties/types.h"
-
-#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
-
-#include "src/__support/FPUtil/BasicOperations.h"
-
-namespace LIBC_NAMESPACE_DECL {
-namespace math {
-
-LIBC_INLINE constexpr __bf16 fabsbf16(__bf16 x) { return fputil::abs(x); }
-
-} // namespace math
-} // namespace LIBC_NAMESPACE_DECL
-
-#else // !defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
-
 #include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
 LIBC_INLINE constexpr bfloat16 fabsbf16(bfloat16 x) { return fputil::abs(x); }
 
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+LIBC_INLINE constexpr __bf16 fabsbf16(__bf16 x) { return fputil::abs(x); }
+#endif
+
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LIBC_TYPES_HAS_BUILTIN_BFLOAT16
-
 #endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABSBF16_H

>From 0b368cd7723bc5c8c8681c5516155497e0b79d1d Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 29 Oct 2026 17:28:18 +0530
Subject: [PATCH 08/11] nits

---
 libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp b/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp
index 658db59631fed6..95070c59ec5e54 100644
--- a/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp
+++ b/libc/test/src/__support/FPUtil/builtin_bfloat16_test.cpp
@@ -19,8 +19,8 @@ using BFloat16 = LIBC_NAMESPACE::fputil::BFloat16;
 TEST(LlvmLibcBuiltinBFloat16Test, TypeSizeAndAlignment) {
   static_assert(sizeof(__bf16) == 2);
   static_assert(alignof(__bf16) == 2);
-  __bf16 a = 1.0;
-  double b = 1.0;
+  constexpr __bf16 a = 1.0;
+  constexpr double b = 1.0;
   static_assert(__bf16(b) == a);
 }
 

>From fe386ee0e349ac9d4f02958aa1cfb65898eda999 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 29 Oct 2026 17:56:17 +0530
Subject: [PATCH 09/11] guard bfloat16 for soft float and targets without fp
 registers

---
 libc/include/llvm-libc-macros/bfloat16-macros.h | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
index dbb7b9f040cdd1..cb46cf135f303f 100644
--- a/libc/include/llvm-libc-macros/bfloat16-macros.h
+++ b/libc/include/llvm-libc-macros/bfloat16-macros.h
@@ -1,17 +1,22 @@
 #ifndef LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
 #define LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
 
-// Clang supports __bf16 when __is_identifier(__bf16) is 0
-// GCC supports __bf16 starting in GCC 13 for x86, ARM, AArch64
+// Clang supports __bf16 when __is_identifier(__bf16) is 0, except on:
+// - x86 targets without SSE2
+// - 32-bit ARM targets with soft float or lacking hardware FP registers
 #if defined(__clang__)
 #if !__is_identifier(__bf16)
-#if !defined(__i386__) || defined(__SSE2__)
+#if (!defined(__i386__) || defined(__SSE2__)) &&                               \
+    (!defined(__arm__) || ((defined(__ARM_FP) && !defined(__SOFTFP__)) ||      \
+                           defined(__ARM_FEATURE_BF16)))
 #define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
 #endif
 #endif
 #elif defined(__GNUC__) && (__GNUC__ >= 13)
 #if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) ||         \
-    defined(__aarch64__) || defined(__arm__)
+    defined(__aarch64__) ||                                                    \
+    (defined(__arm__) && ((defined(__ARM_FP) && !defined(__SOFTFP__)) ||       \
+                          defined(__ARM_FEATURE_BF16)))
 #define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
 #endif
 #endif

>From a71f6195c26cf8df85263e8fbca405bb945b4602 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 29 Oct 2026 18:17:30 +0530
Subject: [PATCH 10/11] update for failure and the tet function

---
 libc/include/llvm-libc-macros/bfloat16-macros.h | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/libc/include/llvm-libc-macros/bfloat16-macros.h b/libc/include/llvm-libc-macros/bfloat16-macros.h
index cb46cf135f303f..79c2d74f33d436 100644
--- a/libc/include/llvm-libc-macros/bfloat16-macros.h
+++ b/libc/include/llvm-libc-macros/bfloat16-macros.h
@@ -1,22 +1,16 @@
 #ifndef LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
 #define LLVM_LIBC_MACROS_BFLOAT16_MACROS_H
 
-// Clang supports __bf16 when __is_identifier(__bf16) is 0, except on:
-// - x86 targets without SSE2
-// - 32-bit ARM targets with soft float or lacking hardware FP registers
+#if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) ||         \
+    defined(__aarch64__) || defined(__riscv) || defined(__AMDGCN__) ||         \
+    defined(__NVPTX__) ||                                                      \
+    (defined(__arm__) && ((defined(__ARM_FP) && !defined(__SOFTFP__)) ||       \
+                          defined(__ARM_FEATURE_BF16)))
 #if defined(__clang__)
 #if !__is_identifier(__bf16)
-#if (!defined(__i386__) || defined(__SSE2__)) &&                               \
-    (!defined(__arm__) || ((defined(__ARM_FP) && !defined(__SOFTFP__)) ||      \
-                           defined(__ARM_FEATURE_BF16)))
 #define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
 #endif
-#endif
 #elif defined(__GNUC__) && (__GNUC__ >= 13)
-#if defined(__x86_64__) || (defined(__i386__) && defined(__SSE2__)) ||         \
-    defined(__aarch64__) ||                                                    \
-    (defined(__arm__) && ((defined(__ARM_FP) && !defined(__SOFTFP__)) ||       \
-                          defined(__ARM_FEATURE_BF16)))
 #define LIBC_TYPES_HAS_BUILTIN_BFLOAT16
 #endif
 #endif

>From f73a261812faa4a3b6551881fe61793b4f254e0b Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 29 Oct 2026 19:30:18 +0530
Subject: [PATCH 11/11] overloads

---
 libc/src/math/fabsbf16.h             | 3 +--
 libc/src/math/generic/fabsbf16.cpp   | 6 +++---
 libc/test/src/math/fabsbf16_test.cpp | 7 +------
 3 files changed, 5 insertions(+), 11 deletions(-)

diff --git a/libc/src/math/fabsbf16.h b/libc/src/math/fabsbf16.h
index ca329848fdf489..b949a90bd5e644 100644
--- a/libc/src/math/fabsbf16.h
+++ b/libc/src/math/fabsbf16.h
@@ -14,10 +14,9 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
+bfloat16 fabsbf16(bfloat16 x);
 #if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
 __bf16 fabsbf16(__bf16 x);
-#else
-bfloat16 fabsbf16(bfloat16 x);
 #endif
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fabsbf16.cpp b/libc/src/math/generic/fabsbf16.cpp
index b3442d47d9f191..1495a2ab352324 100644
--- a/libc/src/math/generic/fabsbf16.cpp
+++ b/libc/src/math/generic/fabsbf16.cpp
@@ -11,12 +11,12 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
-LLVM_LIBC_FUNCTION(__bf16, fabsbf16, (__bf16 x)) { return math::fabsbf16(x); }
-#else
 LLVM_LIBC_FUNCTION(bfloat16, fabsbf16, (bfloat16 x)) {
   return math::fabsbf16(x);
 }
+
+#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
+LLVM_LIBC_FUNCTION(__bf16, fabsbf16, (__bf16 x)) { return math::fabsbf16(x); }
 #endif
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/fabsbf16_test.cpp b/libc/test/src/math/fabsbf16_test.cpp
index e0a2be2eb41d9e..4e34224d2e644c 100644
--- a/libc/test/src/math/fabsbf16_test.cpp
+++ b/libc/test/src/math/fabsbf16_test.cpp
@@ -13,12 +13,7 @@
 
 #include "FAbsTest.h"
 
-#include "src/__support/macros/properties/types.h"
+#include "src/__support/FPUtil/bfloat16.h"
 #include "src/math/fabsbf16.h"
 
-#if defined(LIBC_TYPES_HAS_BUILTIN_BFLOAT16)
-LIST_FABS_TESTS(Fabsbf16, __bf16, LIBC_NAMESPACE::fabsbf16)
-#else
-#include "src/__support/FPUtil/bfloat16.h"
 LIST_FABS_TESTS(Fabsbf16, bfloat16, LIBC_NAMESPACE::fabsbf16)
-#endif



More information about the libc-commits mailing list