[libc-commits] [libc] [llvm] [libc][math] Refactor bf16divf128 to Header Only (PR #186641)
Manthan Singla via libc-commits
libc-commits at lists.llvm.org
Wed Mar 18 08:01:20 PDT 2026
https://github.com/ThanSin02426 updated https://github.com/llvm/llvm-project/pull/186641
>From beccea7d7391b1da013935863ba0e42d5fd7c5b6 Mon Sep 17 00:00:00 2001
From: ThanSin02426 <manthan02426 at gmail.com>
Date: Sun, 15 Mar 2026 11:03:20 +0530
Subject: [PATCH 1/2] [libc][math] Refactor bf16divf128 to Header Only
---
libc/shared/math.h | 1 +
libc/shared/math/bf16divf128.h | 23 +++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 11 ++++++++
libc/src/__support/math/bf16divf128.h | 25 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 6 ++---
libc/src/math/generic/bf16divf128.cpp | 5 ++--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 4 +++
.../llvm-project-overlay/libc/BUILD.bazel | 18 +++++++++++++
9 files changed, 88 insertions(+), 6 deletions(-)
create mode 100644 libc/shared/math/bf16divf128.h
create mode 100644 libc/src/__support/math/bf16divf128.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index c110f6a696b79..4ac2fd80ff502 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -41,6 +41,7 @@
#include "math/bf16addl.h"
#include "math/bf16div.h"
#include "math/bf16divf.h"
+#include "math/bf16divf128.h"
#include "math/bf16divl.h"
#include "math/bf16fma.h"
#include "math/bf16fmaf.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 a8bd09e925b38..1227253e8489c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -469,6 +469,17 @@ 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
+ libc.src.__support.macros.properties.types
+)
+
add_header_library(
bf16fma
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 1c41a8f4980b0..654b8c502664d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5125,12 +5125,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 789286c25444f..305a2481ea730 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -38,6 +38,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.bf16fma
libc.src.__support.math.bf16fmaf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index c67078f4d5ac4..1528f60af585d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -416,6 +416,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 381c57b18c8e2..4ba5038cb3bf7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3195,6 +3195,17 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_bf16divf128",
+ hdrs = ["src/__support/math/bf16divf128.h"],
+ deps = [
+ ":__support_fputil_basic_operations",
+ ":__support_fputil_bfloat16",
+ ":__support_macros_config",
+ ":__support_macros_properties_types",
+ ],
+)
+
libc_support_library(
name = "__support_math_bf16fma",
hdrs = ["src/__support/math/bf16fma.h"],
@@ -6352,6 +6363,13 @@ libc_math_function(
],
)
+libc_math_function(
+ name = "bf16divf128",
+ additional_deps = [
+ ":__support_math_bf16divf128",
+ ],
+)
+
libc_math_function(
name = "canonicalizef",
additional_deps = [
>From 28f85c76c1bb37cc8500b02d8da7d381ad96bc69 Mon Sep 17 00:00:00 2001
From: ThanSin02426 <manthan02426 at gmail.com>
Date: Sun, 15 Mar 2026 11:16:05 +0530
Subject: [PATCH 2/2] some formatting errors
---
libc/shared/math/bf16divf128.h | 8 +++++++-
libc/src/__support/math/CMakeLists.txt | 3 ++-
libc/src/__support/math/bf16divf128.h | 16 ++++++++++++----
libc/src/math/generic/CMakeLists.txt | 4 ----
libc/src/math/generic/bf16divf128.cpp | 2 --
libc/test/shared/shared_math_test.cpp | 6 ++----
.../bazel/llvm-project-overlay/libc/BUILD.bazel | 5 +++--
7 files changed, 26 insertions(+), 18 deletions(-)
diff --git a/libc/shared/math/bf16divf128.h b/libc/shared/math/bf16divf128.h
index db5796255ba48..54a6d2703ab4a 100644
--- a/libc/shared/math/bf16divf128.h
+++ b/libc/shared/math/bf16divf128.h
@@ -1,4 +1,4 @@
-//===-- Shared bf16divf128 function -------------------------------*- C++ -*-===//
+//===-- 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.
@@ -9,6 +9,10 @@
#ifndef LLVM_LIBC_SHARED_MATH_BF16DIVF128_H
#define LLVM_LIBC_SHARED_MATH_BF16DIVF128_H
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
#include "shared/libc_common.h"
#include "src/__support/math/bf16divf128.h"
@@ -20,4 +24,6 @@ using math::bf16divf128;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
+#endif // LIBC_TYPES_HAS_FLOAT128
+
#endif // LLVM_LIBC_SHARED_MATH_BF16DIVF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 1227253e8489c..b3392e0201092 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -474,10 +474,11 @@ add_header_library(
HDRS
bf16divf128.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.include.llvm-libc-types.float128
)
add_header_library(
diff --git a/libc/src/__support/math/bf16divf128.h b/libc/src/__support/math/bf16divf128.h
index 6022043668907..8ed5335099e71 100644
--- a/libc/src/__support/math/bf16divf128.h
+++ b/libc/src/__support/math/bf16divf128.h
@@ -5,21 +5,29 @@
// 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
+
+#ifndef LLVM_LIBC_SRC__SUPPORT_MATH_BF16DIVF128_H
+#define LLVM_LIBC_SRC__SUPPORT_MATH_BF16DIVF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/common.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE constexpr bfloat16 bf16divf128(float128 x, float128 y) {
+LIBC_INLINE 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
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#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 654b8c502664d..a6c1b5b28b3f4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5125,11 +5125,7 @@ add_entrypoint_object(
bf16divf128.cpp
HDRS
../bf16divf128.h
- COMPILE_OPTIONS
- -O3
DEPENDS
- libc.src.__support.common
- libc.src.__support.macros.config
libc.src.__support.math.bf16divf128
)
diff --git a/libc/src/math/generic/bf16divf128.cpp b/libc/src/math/generic/bf16divf128.cpp
index 32d02e2d31b0e..eb1b5b241e2a3 100644
--- a/libc/src/math/generic/bf16divf128.cpp
+++ b/libc/src/math/generic/bf16divf128.cpp
@@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/math/bf16divf128.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
#include "src/__support/math/bf16divf128.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 1528f60af585d..f177f118821a4 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -377,6 +377,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
float128(0.0), float128(0.0), float128(0.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::bf16mulf128(
float128(0.0), float128(0.0)));
+ EXPECT_FP_EQ(bfloat16(2.0), LIBC_NAMESPACE::shared::bf16divf128(
+ float128(4.0), float128(2.0)));
EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::ceilf128(float128(0.0)));
EXPECT_FP_EQ(float128(0.0),
LIBC_NAMESPACE::shared::faddf128(float128(0.0), float128(0.0)));
@@ -416,10 +418,6 @@ 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 4ba5038cb3bf7..a7fea98f44721 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3199,10 +3199,11 @@ libc_support_library(
name = "__support_math_bf16divf128",
hdrs = ["src/__support/math/bf16divf128.h"],
deps = [
- ":__support_fputil_basic_operations",
+ ":__support_common",
":__support_fputil_bfloat16",
+ ":__support_fputil_generic_div",
":__support_macros_config",
- ":__support_macros_properties_types",
+ ":llvm_libc_types_float128",
],
)
More information about the libc-commits
mailing list