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

Mohamed Emad via libc-commits libc-commits at lists.llvm.org
Wed Feb 18 11:55:54 PST 2026


https://github.com/hulxv created https://github.com/llvm/llvm-project/pull/182115

Refactors the bf16sub math family to be header-only.

Closes https://github.com/llvm/llvm-project/issues/182114

Target Functions:
  - bf16sub
  - bf16subf
  - bf16subf128

>From 7c60ab51b7048c46e6e5e71e7f760a292a85edd7 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 18 Feb 2026 21:55:38 +0200
Subject: [PATCH] [libc][math] Refactor bf16sub family to header-only

Refactored functions:
  - bf16sub
  - bf16subf
  - bf16subf128
---
 libc/shared/math.h                            |  3 ++
 libc/shared/math/bf16sub.h                    | 22 +++++++++
 libc/shared/math/bf16subf.h                   | 22 +++++++++
 libc/shared/math/bf16subf128.h                | 28 +++++++++++
 libc/src/__support/math/CMakeLists.txt        | 28 +++++++++++
 libc/src/__support/math/bf16sub.h             | 27 +++++++++++
 libc/src/__support/math/bf16subf.h            | 27 +++++++++++
 libc/src/__support/math/bf16subf128.h         | 33 +++++++++++++
 libc/src/math/generic/CMakeLists.txt          | 18 ++------
 libc/src/math/generic/bf16sub.cpp             |  7 +--
 libc/src/math/generic/bf16subf.cpp            |  7 +--
 libc/src/math/generic/bf16subf128.cpp         |  7 +--
 libc/test/shared/CMakeLists.txt               |  3 ++
 libc/test/shared/shared_math_test.cpp         |  7 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 46 +++++++++++++++++++
 15 files changed, 255 insertions(+), 30 deletions(-)
 create mode 100644 libc/shared/math/bf16sub.h
 create mode 100644 libc/shared/math/bf16subf.h
 create mode 100644 libc/shared/math/bf16subf128.h
 create mode 100644 libc/src/__support/math/bf16sub.h
 create mode 100644 libc/src/__support/math/bf16subf.h
 create mode 100644 libc/src/__support/math/bf16subf128.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8a5aca82c6ec3..00f57b1311dca 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -37,6 +37,9 @@
 #include "math/bf16divl.h"
 #include "math/bf16fmaf.h"
 #include "math/bf16fmal.h"
+#include "math/bf16sub.h"
+#include "math/bf16subf.h"
+#include "math/bf16subf128.h"
 #include "math/canonicalize.h"
 #include "math/canonicalizebf16.h"
 #include "math/canonicalizef.h"
diff --git a/libc/shared/math/bf16sub.h b/libc/shared/math/bf16sub.h
new file mode 100644
index 0000000000000..1ead52bbf04ef
--- /dev/null
+++ b/libc/shared/math/bf16sub.h
@@ -0,0 +1,22 @@
+//===-- Shared bf16sub 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_BF16SUB_H
+#define LLVM_LIBC_SHARED_MATH_BF16SUB_H
+
+#include "src/__support/math/bf16sub.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16sub;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16SUB_H
diff --git a/libc/shared/math/bf16subf.h b/libc/shared/math/bf16subf.h
new file mode 100644
index 0000000000000..b3078b5fe3564
--- /dev/null
+++ b/libc/shared/math/bf16subf.h
@@ -0,0 +1,22 @@
+//===-- Shared bf16subf 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_BF16SUBF_H
+#define LLVM_LIBC_SHARED_MATH_BF16SUBF_H
+
+#include "src/__support/math/bf16subf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16subf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16SUBF_H
diff --git a/libc/shared/math/bf16subf128.h b/libc/shared/math/bf16subf128.h
new file mode 100644
index 0000000000000..e238fc3739f1b
--- /dev/null
+++ b/libc/shared/math/bf16subf128.h
@@ -0,0 +1,28 @@
+//===-- Shared bf16subf128 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_BF16SUBF128_H
+#define LLVM_LIBC_SHARED_MATH_BF16SUBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/bf16subf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16subf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16SUBF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e21fe8ef0ab93..488166d1ae172 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -381,6 +381,34 @@ add_header_library(
     libc.src.__support.FPUtil.fma
     libc.src.__support.macros.config
 )
+add_header_library(
+  bf16sub
+  HDRS
+    bf16sub.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+add_header_library(
+  bf16subf
+  HDRS
+    bf16subf.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+add_header_library(
+  bf16subf128
+  HDRS
+    bf16subf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    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/bf16sub.h b/libc/src/__support/math/bf16sub.h
new file mode 100644
index 0000000000000..a6aa1dfd6e652
--- /dev/null
+++ b/libc/src/__support/math/bf16sub.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for bf16sub -----------------------*- 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_BF16SUB_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16SUB_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 bfloat16 bf16sub(double x, double y) {
+  return fputil::generic::sub<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16SUB_H
diff --git a/libc/src/__support/math/bf16subf.h b/libc/src/__support/math/bf16subf.h
new file mode 100644
index 0000000000000..347626f3eaf11
--- /dev/null
+++ b/libc/src/__support/math/bf16subf.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for bf16subf ----------------------*- 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_BF16SUBF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16SUBF_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 bfloat16 bf16subf(float x, float y) {
+  return fputil::generic::sub<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16SUBF_H
diff --git a/libc/src/__support/math/bf16subf128.h b/libc/src/__support/math/bf16subf128.h
new file mode 100644
index 0000000000000..51e42118451c9
--- /dev/null
+++ b/libc/src/__support/math/bf16subf128.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for bf16subf128 -------------------*- 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_BF16SUBF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16SUBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#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 bfloat16 bf16subf128(float128 x, float128 y) {
+  return fputil::generic::sub<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16SUBF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 57a29665318a3..766af65517124 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5299,11 +5299,7 @@ add_entrypoint_object(
   HDRS
     ../bf16sub.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.bf16sub
 )
 
 add_entrypoint_object(
@@ -5313,11 +5309,7 @@ add_entrypoint_object(
   HDRS
     ../bf16subf.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.bf16subf
 )
 
 add_entrypoint_object(
@@ -5341,9 +5333,5 @@ add_entrypoint_object(
   HDRS
     ../bf16subf128.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.bf16subf128
 )
diff --git a/libc/src/math/generic/bf16sub.cpp b/libc/src/math/generic/bf16sub.cpp
index 65eb2095dd4e9..7a3debe694940 100644
--- a/libc/src/math/generic/bf16sub.cpp
+++ b/libc/src/math/generic/bf16sub.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16sub.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/bf16sub.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16sub, (double x, double y)) {
-  return fputil::generic::sub<bfloat16>(x, y);
+  return math::bf16sub(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/bf16subf.cpp b/libc/src/math/generic/bf16subf.cpp
index 6bba4be441be6..34692a489fa9c 100644
--- a/libc/src/math/generic/bf16subf.cpp
+++ b/libc/src/math/generic/bf16subf.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16subf.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/bf16subf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16subf, (float x, float y)) {
-  return fputil::generic::sub<bfloat16>(x, y);
+  return math::bf16subf(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/bf16subf128.cpp b/libc/src/math/generic/bf16subf128.cpp
index e5fe1077f89a7..4f66d761de7e8 100644
--- a/libc/src/math/generic/bf16subf128.cpp
+++ b/libc/src/math/generic/bf16subf128.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16subf128.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/bf16subf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16subf128, (float128 x, float128 y)) {
-  return fputil::generic::sub<bfloat16>(x, y);
+  return math::bf16subf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 53ad309af4f30..bbeed90030bac 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -33,6 +33,9 @@ add_fp_unittest(
     libc.src.__support.math.bf16divl
     libc.src.__support.math.bf16fmaf
     libc.src.__support.math.bf16fmal
+    libc.src.__support.math.bf16sub
+    libc.src.__support.math.bf16subf
+    libc.src.__support.math.bf16subf128
     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 7f2f39ac7a285..e7c88cc1d1258 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -150,6 +150,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef(&canonicalizef_cx,
                                                      &canonicalizef_x));
   EXPECT_FP_EQ(0x0p+0f, canonicalizef_cx);
+
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::bf16subf(0.0f, 0.0f));
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
@@ -187,6 +189,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalize(&canonicalize_cx,
                                                     &canonicalize_x));
   EXPECT_FP_EQ(0.0, canonicalize_cx);
+
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::bf16sub(0.0, 0.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {
@@ -238,6 +242,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef128(&canonicalizef128_cx,
                                                         &canonicalizef128_x));
   EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
+
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::bf16subf128(
+                                  float128(0.0), float128(0.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index b90688af7a1d2..a53720269a3ea 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2695,6 +2695,37 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16sub",
+    hdrs = ["src/__support/math/bf16sub.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_bf16subf",
+    hdrs = ["src/__support/math/bf16subf.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_bf16subf128",
+    hdrs = ["src/__support/math/bf16subf128.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_bf16fmal",
     hdrs = ["src/__support/math/bf16fmal.h"],
@@ -4593,6 +4624,21 @@ libc_math_function(
     additional_deps = [":__support_math_bf16divl"],
 )
 
+libc_math_function(
+    name = "bf16sub",
+    additional_deps = [":__support_math_bf16sub"],
+)
+
+libc_math_function(
+    name = "bf16subf",
+    additional_deps = [":__support_math_bf16subf"],
+)
+
+libc_math_function(
+    name = "bf16subf128",
+    additional_deps = [":__support_math_bf16sub128"],
+)
+
 libc_math_function(
     name = "bf16fmaf",
     additional_deps = [":__support_math_bf16fmaf"],



More information about the libc-commits mailing list