[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 09:22:39 PST 2026
https://github.com/bala-bhargav updated https://github.com/llvm/llvm-project/pull/181086
>From fa6d066a57aa2f9fb2659b0d118f0515b0713a99 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 | 10 +++++++
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 | 5 ++++
.../llvm-project-overlay/libc/BUILD.bazel | 17 +++++++++++
9 files changed, 88 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 452fe3fddc911..6e0daafca985f 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -30,6 +30,7 @@
#include "math/atanf16.h"
#include "math/atanhf.h"
#include "math/atanhf16.h"
+#include "math/bf16divf.h"
#include "math/cbrt.h"
#include "math/cbrtf.h"
#include "math/cos.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 0fa48661e58e8..5554c9b976a9f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -332,6 +332,16 @@ add_header_library(
libc.src.__support.macros.optimization
)
+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(
cbrt
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 2475aa52c3c0e..38d228c3177c1 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5320,11 +5320,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 fb9874ab3ec03..d4aea4195906a 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -26,6 +26,7 @@ add_fp_unittest(
libc.src.__support.math.atanf16
libc.src.__support.math.atanhf
libc.src.__support.math.atanhf16
+ libc.src.__support.math.bf16divf
libc.src.__support.math.cbrt
libc.src.__support.math.cbrtf
libc.src.__support.math.cos
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 92511cc55267e..bfc6241ac56af 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -101,6 +101,11 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanf(0.0f));
}
+TEST(LlvmLibcSharedMathTest, AllBFloat16) {
+ EXPECT_FP_EQ(bfloat16(2.0f),
+ LIBC_NAMESPACE::shared::bf16divf(4.0f, 2.0f));
+}
+
TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0x1.921fb54442d18p+0, LIBC_NAMESPACE::shared::acos(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::asin(0.0));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 87b2a392287a6..bf9c788cd74ac 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2638,6 +2638,16 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_bf16divf",
+ hdrs = ["src/__support/math/bf16divf.h"],
+ deps = [
+ ":__support_fputil_bfloat16",
+ ":__support_fputil_generic_div",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_cbrt",
hdrs = ["src/__support/math/cbrt.h"],
@@ -4044,6 +4054,13 @@ libc_math_function(
],
)
+libc_math_function(
+ name = "bf16divf",
+ additional_deps = [
+ ":__support_math_bf16divf",
+ ],
+)
+
libc_math_function(name = "canonicalize")
libc_math_function(name = "canonicalizef")
More information about the libc-commits
mailing list