[libc-commits] [libc] [llvm] [libc][math] Refactor bf16divf implementation to header-only in src/__support/math folder. (PR #181086)
via libc-commits
libc-commits at lists.llvm.org
Mon Feb 16 10:08:18 PST 2026
https://github.com/bala-bhargav updated https://github.com/llvm/llvm-project/pull/181086
>From d2cb52fccd1c807be1b27001c61599b084b4202e Mon Sep 17 00:00:00 2001
From: bhargav <penugondabalabharghav at gmail.com>
Date: Thu, 12 Feb 2026 07:33:03 +0530
Subject: [PATCH] [libc][math] Refactor bf16divf implementation to header-only
in src/__support/math folder.
Move bf16divf implementation from src/math/generic/bf16divf.cpp to a
header-only LIBC_INLINE constexpr function in
src/__support/math/bf16divf.h, following the established pattern for
shared math functions.
Part of #147386.
Fixes #181023.
---
libc/shared/math.h | 1 +
libc/shared/math/bf16divf.h | 23 +++++++++++++++
libc/src/__support/math/CMakeLists.txt | 11 ++++++++
libc/src/__support/math/bf16divf.h | 28 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 6 +---
libc/src/math/generic/bf16divf.cpp | 7 ++---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 17 +++++++++++
9 files changed, 85 insertions(+), 10 deletions(-)
create mode 100644 libc/shared/math/bf16divf.h
create mode 100644 libc/src/__support/math/bf16divf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 93f7d6a8156cf..b7012583f06dd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -32,6 +32,7 @@
#include "math/atanhf16.h"
#include "math/bf16add.h"
#include "math/bf16addf128.h"
+#include "math/bf16divf.h"
#include "math/canonicalize.h"
#include "math/canonicalizebf16.h"
#include "math/canonicalizef.h"
diff --git a/libc/shared/math/bf16divf.h b/libc/shared/math/bf16divf.h
new file mode 100644
index 0000000000000..c004235d5382f
--- /dev/null
+++ b/libc/shared/math/bf16divf.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16divf 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_BF16DIVF_H
+#define LLVM_LIBC_SHARED_MATH_BF16DIVF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16divf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16divf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16DIVF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index aaab78c01a891..4d4d86272b186 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -351,6 +351,17 @@ add_header_library(
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
)
+
+add_header_library(
+ bf16divf
+ HDRS
+ bf16divf.h
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.generic.div
+ libc.src.__support.macros.config
+)
+
add_header_library(
canonicalize
HDRS
diff --git a/libc/src/__support/math/bf16divf.h b/libc/src/__support/math/bf16divf.h
new file mode 100644
index 0000000000000..ad5cb57aac828
--- /dev/null
+++ b/libc/src/__support/math/bf16divf.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for bf16divf ----------------------*- 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_BF16DIVF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVF_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 constexpr bfloat16 bf16divf(float x, float y) {
+ return fputil::generic::div<bfloat16>(x, y);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 75b51b4587bea..da0de72affc50 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5192,11 +5192,7 @@ add_entrypoint_object(
HDRS
../bf16divf.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.bf16divf
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16divf.cpp b/libc/src/math/generic/bf16divf.cpp
index 2054a6417b078..767366a42d682 100644
--- a/libc/src/math/generic/bf16divf.cpp
+++ b/libc/src/math/generic/bf16divf.cpp
@@ -7,15 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/bf16divf.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/bf16divf.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16divf, (float x, float y)) {
- return fputil::generic::div<bfloat16>(x, y);
+ return math::bf16divf(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9dfbedee66a33..a9e11f3a2ba49 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -28,6 +28,7 @@ add_fp_unittest(
libc.src.__support.math.atanhf16
libc.src.__support.math.bf16add
libc.src.__support.math.bf16addf128
+ libc.src.__support.math.bf16divf
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 2d7ee388f754d..0f9938d7bccbc 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -230,6 +230,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));
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 608180b90e87b..b51f9bb33268a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2655,6 +2655,16 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_bf16divf",
+ hdrs = ["src/__support/math/bf16divf.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"],
@@ -4425,6 +4435,13 @@ libc_math_function(
],
)
+libc_math_function(
+ name = "bf16divf",
+ additional_deps = [
+ ":__support_math_bf16divf",
+ ],
+)
+
libc_math_function(
name = "canonicalizef",
additional_deps = [
More information about the libc-commits
mailing list