[libc] [llvm] [libc] refactor bf16div to be header-only and constexpr (PR #181400)

via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 22 03:23:49 PST 2026


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

>From 19f410a53daa202c46aa8605cc2aefc1621fcf27 Mon Sep 17 00:00:00 2001
From: mooofin <siddharthqln at gmail.com>
Date: Sun, 22 Feb 2026 14:31:54 +0530
Subject: [PATCH] libc: refactor bf16div header-only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/bf16div.h                    | 23 ++++++++++++++++
 libc/src/__support/FPUtil/generic/div.h       |  2 +-
 libc/src/__support/math/CMakeLists.txt        | 10 +++++++
 libc/src/__support/math/bf16div.h             | 26 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  4 +--
 libc/src/math/generic/bf16div.cpp             |  4 +--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 +-
 .../llvm-project-overlay/libc/BUILD.bazel     | 17 ++++++++++++
 10 files changed, 83 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/bf16div.h
 create mode 100644 libc/src/__support/math/bf16div.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8737fc69d4d8d..add37117a31a7 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -33,6 +33,7 @@
 #include "math/bf16add.h"
 #include "math/bf16addf.h"
 #include "math/bf16addf128.h"
+#include "math/bf16div.h"
 #include "math/bf16divf.h"
 #include "math/bf16divl.h"
 #include "math/bf16fmaf.h"
diff --git a/libc/shared/math/bf16div.h b/libc/shared/math/bf16div.h
new file mode 100644
index 0000000000000..1e69b99f2874e
--- /dev/null
+++ b/libc/shared/math/bf16div.h
@@ -0,0 +1,23 @@
+//===-- Shared bf16div 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_BF16DIV_H
+#define LLVM_LIBC_SHARED_MATH_BF16DIV_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/bf16div.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::bf16div;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_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 696993c535f68..5def3005f4cc0 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -362,6 +362,16 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+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(
   bf16divf
   HDRS
diff --git a/libc/src/__support/math/bf16div.h b/libc/src/__support/math/bf16div.h
new file mode 100644
index 0000000000000..e8adc3c379a25
--- /dev/null
+++ b/libc/src/__support/math/bf16div.h
@@ -0,0 +1,26 @@
+//===-- 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 29105974f5af2..1b26e285dfc27 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5117,10 +5117,8 @@ add_entrypoint_object(
     ../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..028602f0a3cc5 100644
--- a/libc/src/math/generic/bf16div.cpp
+++ b/libc/src/math/generic/bf16div.cpp
@@ -8,14 +8,14 @@
 
 #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/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 14ab2eb3bc3d8..c6925ed113417 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -18,6 +18,7 @@ add_fp_unittest(
     libc.src.__support.math.asinf16
     libc.src.__support.math.asinhf
     libc.src.__support.math.asinhf16
+    libc.src.__support.math.bf16div
     libc.src.__support.math.atan
     libc.src.__support.math.atan2
     libc.src.__support.math.atan2f
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8d736f618e5a9..fdf594764f1ab 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -177,7 +177,6 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log10(1.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log1p(0.0));
-  EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log2(1.0));
   EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::shared::pow(0.0, 0.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
   EXPECT_FP_EQ(1.0, cos);
@@ -265,6 +264,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16add(2.0, 3.0));
   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));
   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 38940c4113929..5d221f829529a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3343,6 +3343,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_bf16div",
+    hdrs = ["src/__support/math/bf16div.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_ilogbf16",
     hdrs = ["src/__support/math/ilogbf16.h"],
@@ -5355,6 +5365,13 @@ libc_math_function(name = "fminimum_numf128")
 
 libc_math_function(name = "fminimum_numf16")
 
+libc_math_function(
+    name = "bf16div",
+    additional_deps = [
+        ":__support_math_bf16div",
+    ],
+)
+
 libc_math_function(
     name = "fmod",
     additional_deps = [":__support_fputil_generic_fmod"],



More information about the llvm-commits mailing list