[libc-commits] [libc] [llvm] [libc] refactor bf16div to be header-only and constexpr (PR #181400)
via libc-commits
libc-commits at lists.llvm.org
Fri Feb 13 10:57:31 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: sid (mooofin)
<details>
<summary>Changes</summary>
Refactor bf16div to be a header-only shared math function and enable constexpr support where possible. This is part of the effort to support C++23 constexpr math functions in LLVM libc.
Part of #<!-- -->147386
---
Full diff: https://github.com/llvm/llvm-project/pull/181400.diff
6 Files Affected:
- (modified) libc/src/__support/FPUtil/generic/div.h (+1-1)
- (modified) libc/src/__support/math/CMakeLists.txt (+10)
- (added) libc/src/__support/math/bf16div.h (+28)
- (modified) libc/src/math/generic/CMakeLists.txt (+1-5)
- (modified) libc/src/math/generic/bf16div.cpp (+2-5)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+15)
``````````diff
diff --git a/libc/src/__support/FPUtil/generic/div.h b/libc/src/__support/FPUtil/generic/div.h
index bf7d0b7112ca9..993ac165670ee 100644
--- a/libc/src/__support/FPUtil/generic/div.h
+++ b/libc/src/__support/FPUtil/generic/div.h
@@ -26,7 +26,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace fputil::generic {
template <typename OutType, typename InType>
-LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
cpp::is_floating_point_v<InType> &&
sizeof(OutType) <= sizeof(InType),
OutType>
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4e6e9bd6233ec..b7c89713b553e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1459,6 +1459,16 @@ add_header_library(
libc.src.__support.macros.properties.types
)
+add_header_library(
+ bf16div
+ HDRS
+ bf16div.h
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.generic.div
+ libc.src.__support.macros.config
+)
+
add_header_library(
logbf16
HDRS
diff --git a/libc/src/__support/math/bf16div.h b/libc/src/__support/math/bf16div.h
new file mode 100644
index 0000000000000..4870148e49099
--- /dev/null
+++ b/libc/src/__support/math/bf16div.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for bf16div -----------------------*- 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_BF16DIV_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIV_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 bf16div(double x, double y) {
+ return fputil::generic::div<bfloat16>(x, y);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_BF16DIV_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 98d79e3b3826d..517849cf67779 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5221,11 +5221,7 @@ add_entrypoint_object(
HDRS
../bf16div.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.bf16div
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/bf16div.cpp b/libc/src/math/generic/bf16div.cpp
index 5e9b1b42e27f4..4d913ecb4c271 100644
--- a/libc/src/math/generic/bf16div.cpp
+++ b/libc/src/math/generic/bf16div.cpp
@@ -7,15 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/bf16div.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/bf16div.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bfloat16, bf16div, (double x, double y)) {
- return fputil::generic::div<bfloat16>(x, y);
+ return math::bf16div(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e08d759f063b0..512194d4f2837 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3288,6 +3288,16 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_bf16div",
+ hdrs = ["src/__support/math/bf16div.h"],
+ deps = [
+ ":__support_fputil_bfloat16",
+ ":__support_fputil_generic_div",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_logbf16",
hdrs = ["src/__support/math/logbf16.h"],
@@ -4253,6 +4263,11 @@ libc_math_function(
additional_deps = [":__support_math_atanhf16"],
)
+libc_math_function(
+ name = "bf16div",
+ additional_deps = [":__support_math_bf16div"],
+)
+
libc_math_function(name = "canonicalize")
libc_math_function(name = "canonicalizef")
``````````
</details>
https://github.com/llvm/llvm-project/pull/181400
More information about the libc-commits
mailing list