[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:56:18 PST 2026


https://github.com/mooofin created https://github.com/llvm/llvm-project/pull/181400

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

>From fe771101eca53422950986b959e1c8989754c206 Mon Sep 17 00:00:00 2001
From: mooofin <siddharthqln at gmail.com>
Date: Fri, 13 Feb 2026 18:47:27 +0000
Subject: [PATCH] [libc] refactor bf16div to be header-only and constexpr

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
---
 libc/src/__support/FPUtil/generic/div.h       |  2 +-
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/bf16div.h             | 28 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  6 +---
 libc/src/math/generic/bf16div.cpp             |  7 ++---
 .../llvm-project-overlay/libc/BUILD.bazel     | 15 ++++++++++
 6 files changed, 57 insertions(+), 11 deletions(-)
 create mode 100644 libc/src/__support/math/bf16div.h

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")



More information about the libc-commits mailing list