[libc-commits] [libc] [llvm] [libc][math] Refactor bf16subl family to header-only (PR #194498)

via libc-commits libc-commits at lists.llvm.org
Mon Apr 27 17:02:38 PDT 2026


https://github.com/AnonMiraj created https://github.com/llvm/llvm-project/pull/194498

Refactors the bf16subl math family to be header-only.

Closes 

Target Functions:
  - bf16subl

>From 45fb323c764e768d14fa32b996d93909c6dce5a8 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Tue, 28 Apr 2026 03:02:24 +0300
Subject: [PATCH] [libc][math] Refactor bf16subl family to header-only

Refactored functions:
  - bf16subl
---
 libc/shared/math.h                            |  1 +
 libc/shared/math/bf16subl.h                   | 22 +++++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  9 +++++++
 libc/src/__support/math/bf16subl.h            | 27 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +----
 libc/src/math/generic/bf16subl.cpp            |  7 ++---
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 15 +++++++++++
 9 files changed, 79 insertions(+), 10 deletions(-)
 create mode 100644 libc/shared/math/bf16subl.h
 create mode 100644 libc/src/__support/math/bf16subl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 80078afea59ba..98f011dc0db07 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -57,6 +57,7 @@
 #include "math/bf16sub.h"
 #include "math/bf16subf.h"
 #include "math/bf16subf128.h"
+#include "math/bf16subl.h"
 #include "math/canonicalize.h"
 #include "math/canonicalizebf16.h"
 #include "math/canonicalizef.h"
diff --git a/libc/shared/math/bf16subl.h b/libc/shared/math/bf16subl.h
new file mode 100644
index 0000000000000..2b2a270fb8492
--- /dev/null
+++ b/libc/shared/math/bf16subl.h
@@ -0,0 +1,22 @@
+//===-- Shared bf16subl function --------------------------------*- 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_MATH_BF16SUBL_H
+#define LLVM_LIBC_SHARED_MATH_BF16SUBL_H
+
+#include "src/__support/math/bf16subl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16subl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16SUBL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 67c3b28109987..9c61f6322c128 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -631,6 +631,15 @@ add_header_library(
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.generic.add_sub
 )
+add_header_library(
+  bf16subl
+  HDRS
+    bf16subl.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
 
 add_header_library(
   canonicalize
diff --git a/libc/src/__support/math/bf16subl.h b/libc/src/__support/math/bf16subl.h
new file mode 100644
index 0000000000000..93fde4dfa3f56
--- /dev/null
+++ b/libc/src/__support/math/bf16subl.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for bf16subl ----------------------*- 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_SRC___SUPPORT_MATH_BF16SUBL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16SUBL_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr bfloat16 bf16subl(long double x, long double y) {
+  return fputil::generic::sub<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16SUBL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 0b91d1d4404ab..fec77a8fcaf98 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5236,11 +5236,7 @@ add_entrypoint_object(
   HDRS
     ../bf16subl.h
   DEPENDS
-    libc.src.__support.common
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.generic.add_sub
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.bf16subl
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16subl.cpp b/libc/src/math/generic/bf16subl.cpp
index d3a970cade922..faca50d36e906 100644
--- a/libc/src/math/generic/bf16subl.cpp
+++ b/libc/src/math/generic/bf16subl.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16subl.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/bf16subl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16subl, (long double x, long double y)) {
-  return fputil::generic::sub<bfloat16>(x, y);
+  return math::bf16subl(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 8f324ae814f71..f95622e943bc5 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -54,6 +54,7 @@ add_fp_unittest(
     libc.src.__support.math.bf16sub
     libc.src.__support.math.bf16subf
     libc.src.__support.math.bf16subf128
+    libc.src.__support.math.bf16subl
     libc.src.__support.math.canonicalize
     libc.src.__support.math.canonicalizebf16
     libc.src.__support.math.canonicalizef
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index d9d5c2c621d4a..bbda53faf353b 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -320,6 +320,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_FP_EQ(0.0L, canonicalizel_cx);
 
   EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addl(2.0L, 3.0L));
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::bf16subl(0.0L, 0.0L));
   EXPECT_FP_EQ(bfloat16(2.0), LIBC_NAMESPACE::shared::bf16divl(6.0L, 3.0L));
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::bf16mull(0.0L, 0.0L));
   EXPECT_FP_EQ(bfloat16(10.0),
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e09828d682d98..edd3eb1878fc9 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3710,6 +3710,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16subl",
+    hdrs = ["src/__support/math/bf16subl.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_canonicalize",
     hdrs = ["src/__support/math/canonicalize.h"],
@@ -7082,6 +7092,11 @@ libc_math_function(
     additional_deps = [":__support_math_bf16subf128"],
 )
 
+libc_math_function(
+    name = "bf16subl",
+    additional_deps = [":__support_math_bf16subl"],
+)
+
 libc_math_function(
     name = "bf16fma",
     additional_deps = [":__support_math_bf16fma"],



More information about the libc-commits mailing list