Skip to content

[libc][math] Refactor bf16divf128 to Header Only#186641

Merged
bassiounix merged 2 commits into
llvm:mainfrom
ThanSin02426:refactor-bf16divf128
Mar 18, 2026
Merged

[libc][math] Refactor bf16divf128 to Header Only#186641
bassiounix merged 2 commits into
llvm:mainfrom
ThanSin02426:refactor-bf16divf128

Conversation

@ThanSin02426

Copy link
Copy Markdown
Contributor

closes #181024

@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Mar 15, 2026
@llvmbot

llvmbot commented Mar 15, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-libc

Author: Manthan Singla (ThanSin02426)

Changes

closes #181024


Full diff: https://github.com/llvm/llvm-project/pull/186641.diff

9 Files Affected:

  • (modified) libc/shared/math.h (+1)
  • (added) libc/shared/math/bf16divf128.h (+23)
  • (modified) libc/src/__support/math/CMakeLists.txt (+10)
  • (added) libc/src/__support/math/bf16divf128.h (+25)
  • (modified) libc/src/math/generic/CMakeLists.txt (+3-3)
  • (modified) libc/src/math/generic/bf16divf128.cpp (+2-3)
  • (modified) libc/test/shared/CMakeLists.txt (+1)
  • (modified) libc/test/shared/shared_math_test.cpp (+4)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+17)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 61c3ddffd74e5..a8e2dcbd23af4 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -40,6 +40,7 @@
 #include "math/bf16addl.h"
 #include "math/bf16div.h"
 #include "math/bf16divf.h"
+#include "math/bf16divf128.h"
 #include "math/bf16divl.h"
 #include "math/bf16fmaf.h"
 #include "math/bf16fmaf128.h"
diff --git a/libc/shared/math/bf16divf128.h b/libc/shared/math/bf16divf128.h
new file mode 100644
index 0000000000000..db5796255ba48
--- /dev/null
+++ b/libc/shared/math/bf16divf128.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16divf128 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_BF16DIVF128_H
+#define LLVM_LIBC_SHARED_MATH_BF16DIVF128_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16divf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16divf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_BF16DIVF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 8bb935d863a9b..89dcc02e81648 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -452,6 +452,16 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  bf16divf128
+  HDRS
+    bf16divf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.generic.div
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   bf16fmaf
   HDRS
diff --git a/libc/src/__support/math/bf16divf128.h b/libc/src/__support/math/bf16divf128.h
new file mode 100644
index 0000000000000..6022043668907
--- /dev/null
+++ b/libc/src/__support/math/bf16divf128.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for bf16divf128 -------------------*- 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_BF16DIVF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVF128_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 bf16divf128(float128 x, float128 y) {
+  return fputil::generic::div<bfloat16>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIVF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 07c48c9a04c98..9689233bc76b5 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5140,12 +5140,12 @@ add_entrypoint_object(
     bf16divf128.cpp
   HDRS
     ../bf16divf128.h
+  COMPILE_OPTIONS
+    -O3
   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.bf16divf128
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16divf128.cpp b/libc/src/math/generic/bf16divf128.cpp
index fbe9775ce4046..32d02e2d31b0e 100644
--- a/libc/src/math/generic/bf16divf128.cpp
+++ b/libc/src/math/generic/bf16divf128.cpp
@@ -7,15 +7,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/bf16divf128.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/bf16divf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, bf16divf128, (float128 x, float128 y)) {
-  return fputil::generic::div<bfloat16>(x, y);
+  return math::bf16divf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 2e351d8051bed..c4acfad4105f2 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -37,6 +37,7 @@ add_fp_unittest(
     libc.src.__support.math.bf16addf128
     libc.src.__support.math.bf16div
     libc.src.__support.math.bf16divf
+    libc.src.__support.math.bf16divf128
     libc.src.__support.math.bf16divl
     libc.src.__support.math.bf16fmaf
     libc.src.__support.math.bf16fmaf128
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 4eb950f030a1a..604d6507bb8f8 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -415,6 +415,10 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   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));
   EXPECT_FP_EQ(bfloat16(2.0), LIBC_NAMESPACE::shared::bf16div(4.0, 2.0));
+#ifdef LIBC_TYPES_HAS_FLOAT128
+  EXPECT_FP_EQ(bfloat16(2.0), LIBC_NAMESPACE::shared::bf16divf128(
+                                  float128(4.0), float128(2.0)));
+#endif
   EXPECT_FP_EQ(bfloat16(10.0),
                LIBC_NAMESPACE::shared::bf16fmal(2.0L, 3.0L, 4.0L));
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 316fda92c04a6..9796cdcf84afd 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3178,6 +3178,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16divf128",
+    hdrs = ["src/__support/math/bf16divf128.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_generic_div",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_bf16fmaf",
     hdrs = ["src/__support/math/bf16fmaf.h"],
@@ -6300,6 +6310,13 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "bf16divf128",
+    additional_deps = [
+        ":__support_math_bf16divf128",
+    ],
+)
+
 libc_math_function(
     name = "canonicalizef",
     additional_deps = [

@github-actions

github-actions Bot commented Mar 15, 2026

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch 2 times, most recently from f7aa57e to af656c5 Compare March 15, 2026 09:41
@ThanSin02426

Copy link
Copy Markdown
Contributor Author

Hi @bassiounix please kindly review it :)

Comment thread libc/shared/math/bf16divf128.h Outdated
Comment thread libc/src/__support/math/CMakeLists.txt Outdated
Comment thread libc/src/math/generic/CMakeLists.txt Outdated
Comment thread utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch 3 times, most recently from 9a27bdd to bbd5ce3 Compare March 16, 2026 15:32
@github-actions

github-actions Bot commented Mar 16, 2026

Copy link
Copy Markdown

🐧 Linux x64 Test Results

✅ The build succeeded and no tests ran. This is expected in some build configurations.

@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch from bbd5ce3 to 03d1593 Compare March 16, 2026 15:36
@bassiounix bassiounix self-requested a review March 16, 2026 15:44
@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch from 03d1593 to 1f2ebad Compare March 16, 2026 15:45
@ThanSin02426

Copy link
Copy Markdown
Contributor Author

These fails doesn't seems related?

@bassiounix

Copy link
Copy Markdown
Member

These fails doesn't seems related?

I don't think so.

Comment thread libc/shared/math/bf16divf128.h Outdated
Comment thread libc/src/__support/math/bf16divf128.h Outdated
Comment thread libc/src/__support/math/CMakeLists.txt
Comment thread libc/test/shared/shared_math_test.cpp Outdated
Comment thread utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@vhscampos

Copy link
Copy Markdown
Member

LGTM once you fix the issues raised by @bassiounix

@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch 2 times, most recently from 09432b6 to c6eec3b Compare March 17, 2026 12:11
@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch from 4b99df6 to b828259 Compare March 17, 2026 12:48
Comment thread libc/shared/math/bf16divf128.h Outdated
Comment thread libc/shared/math/bf16divf128.h Outdated
Comment thread libc/src/__support/math/bf16divf128.h Outdated
Comment thread libc/src/__support/math/bf16divf128.h Outdated
Comment thread libc/src/__support/math/CMakeLists.txt
Comment thread libc/src/math/generic/CMakeLists.txt
Comment thread utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch 3 times, most recently from 99e7f5e to dd551e7 Compare March 18, 2026 14:21
@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch 5 times, most recently from f0ebdfa to 28f85c7 Compare March 18, 2026 15:01
@ThanSin02426 ThanSin02426 force-pushed the refactor-bf16divf128 branch from 28f85c7 to 59a147e Compare March 18, 2026 19:26

@bassiounix bassiounix left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Thank you for the refactor!

@bassiounix bassiounix merged commit 42b75ed into llvm:main Mar 18, 2026
28 checks passed
@ThanSin02426 ThanSin02426 deleted the refactor-bf16divf128 branch March 19, 2026 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bazel "Peripheral" support tier build system: utils/bazel libc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc][math] Refactor bf16divf128 to Header Only.

5 participants