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

via libc-commits libc-commits at lists.llvm.org
Sat Oct 24 03:16:03 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 1/3] 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 ed934df81886f..097c49f4f327d 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 0000000000000..5d1a9a46fcb1b
--- /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 dfa2f9c572492..6ce47b3f724f4 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 082f13ff9d661..dc0c014b41a69 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 0000000000000..658db59631fed
--- /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 210e2e1eb4a09..8fcbe72e2bd98 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 2/3] 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 5d1a9a46fcb1b..dbb7b9f040cdd 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 3/3] 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 6ce47b3f724f4..7eeabbbb916b0 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
+)



More information about the libc-commits mailing list