[libc-commits] [libc] [llvm] [libc][math] Refactor bf16divl to header-only (#181025) (PR #181535)

via libc-commits libc-commits at lists.llvm.org
Wed Feb 18 03:51:21 PST 2026


https://github.com/AbdallahRashed updated https://github.com/llvm/llvm-project/pull/181535

>From c5d635271190d44b7b592fa9e9e7a4e4f8eb510b Mon Sep 17 00:00:00 2001
From: AbdallahRashed <abdallah.mrashed at gmail.com>
Date: Sun, 15 Feb 2026 09:37:33 +0100
Subject: [PATCH] [libc][math] Refactor bf16divl to header-only

Move implementation to src/__support/math/bf16divl.h to make it header-only
and add shared math wrapper. Update build files for CMake and Bazel.
---
 libc/shared/math.h                            |  1 +
 libc/shared/math/bf16divl.h                   | 23 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/bf16divl.h            | 26 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +----
 libc/src/math/generic/bf16divl.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, 80 insertions(+), 10 deletions(-)
 create mode 100644 libc/shared/math/bf16divl.h
 create mode 100644 libc/src/__support/math/bf16divl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 828980a3500df..034d6486e779e 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -34,6 +34,7 @@
 #include "math/bf16addf.h"
 #include "math/bf16addf128.h"
 #include "math/bf16divf.h"
+#include "math/bf16divl.h"
 #include "math/bf16fmaf.h"
 #include "math/canonicalize.h"
 #include "math/canonicalizebf16.h"
diff --git a/libc/shared/math/bf16divl.h b/libc/shared/math/bf16divl.h
new file mode 100644
index 0000000000000..f30cfaa012c4f
--- /dev/null
+++ b/libc/shared/math/bf16divl.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16divl 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_BF16DIVL_H
+#define LLVM_LIBC_SHARED_MATH_BF16DIVL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16divl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16divl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16DIVL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index af2c66597b75a..1f990b45ec7f9 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -440,6 +440,16 @@ add_header_library(
 )
 
 
+add_header_library(
+  bf16divl
+  HDRS
+    bf16divl.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.div
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   cbrt
   HDRS
diff --git a/libc/src/__support/math/bf16divl.h b/libc/src/__support/math/bf16divl.h
new file mode 100644
index 0000000000000..ec5a9244b98d6
--- /dev/null
+++ b/libc/src/__support/math/bf16divl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for bf16divl ----------------------*- 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_BF16DIVL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVL_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 bf16divl(long double x, long double y) {
+  return fputil::generic::div<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 47101706ce4c8..22e57148f3a42 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5189,11 +5189,7 @@ add_entrypoint_object(
   HDRS
     ../bf16divl.h
   DEPENDS
-    libc.src.__support.common
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.generic.div
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.bf16divl
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16divl.cpp b/libc/src/math/generic/bf16divl.cpp
index 21dd6b150e07a..432ed829005f0 100644
--- a/libc/src/math/generic/bf16divl.cpp
+++ b/libc/src/math/generic/bf16divl.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16divl.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/generic/div.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/bf16divl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16divl, (long double x, long double y)) {
-  return fputil::generic::div<bfloat16>(x, y);
+  return math::bf16divl(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index dfe2378269921..a5d90711c1705 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -30,6 +30,7 @@ add_fp_unittest(
     libc.src.__support.math.bf16addf
     libc.src.__support.math.bf16addf128
     libc.src.__support.math.bf16divf
+    libc.src.__support.math.bf16divl
     libc.src.__support.math.bf16fmaf
     libc.src.__support.math.canonicalize
     libc.src.__support.math.canonicalizebf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8eba836538c41..4163332b5e532 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -236,6 +236,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16add(2.0, 3.0));
   EXPECT_FP_EQ(bfloat16(2.0f), LIBC_NAMESPACE::shared::bf16divf(4.0f, 2.0f));
+  EXPECT_FP_EQ(bfloat16(2.0), LIBC_NAMESPACE::shared::bf16divl(6.0L, 3.0L));
 
   bfloat16 canonicalizebf16_cx = bfloat16(0.0);
   bfloat16 canonicalizebf16_x = bfloat16(0.0);
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index a5b6823b9ca3d..156c4aa8d04af 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2675,6 +2675,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16divl",
+    hdrs = ["src/__support/math/bf16divl.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_bf16fmaf",
     hdrs = ["src/__support/math/bf16fmaf.h"],
@@ -4483,6 +4493,11 @@ libc_math_function(
     additional_deps = [":__support_math_bf16addf128"],
 )
 
+libc_math_function(
+    name = "bf16divl",
+    additional_deps = [":__support_math_bf16divl"],
+)
+
 libc_math_function(
     name = "bf16fmaf",
     additional_deps = [":__support_math_bf16fmaf"],



More information about the libc-commits mailing list