[libc-commits] [libc] aa62224 - [libc][math] Refactor sqrtbf16 function header-only (#187849)

via libc-commits libc-commits at lists.llvm.org
Sat Mar 21 15:51:45 PDT 2026


Author: Zorojuro
Date: 2026-03-22T00:51:40+02:00
New Revision: aa62224a73b10da9154aa0c1111e009a8cd48ef2

URL: https://github.com/llvm/llvm-project/commit/aa62224a73b10da9154aa0c1111e009a8cd48ef2
DIFF: https://github.com/llvm/llvm-project/commit/aa62224a73b10da9154aa0c1111e009a8cd48ef2.diff

LOG: [libc][math] Refactor sqrtbf16 function header-only (#187849)

Added: 
    libc/shared/math/sqrtbf16.h
    libc/src/__support/math/sqrtbf16.h

Modified: 
    libc/shared/math.h
    libc/src/__support/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/src/math/generic/sqrtbf16.cpp
    libc/test/shared/CMakeLists.txt
    libc/test/shared/shared_math_test.cpp
    utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/shared/math.h b/libc/shared/math.h
index 3d81216f1bcfb..6add9628cabdd 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -246,6 +246,7 @@
 #include "math/sinpif.h"
 #include "math/sinpif16.h"
 #include "math/sqrt.h"
+#include "math/sqrtbf16.h"
 #include "math/sqrtf.h"
 #include "math/sqrtf128.h"
 #include "math/sqrtf16.h"

diff  --git a/libc/shared/math/sqrtbf16.h b/libc/shared/math/sqrtbf16.h
new file mode 100644
index 0000000000000..0b10869704f27
--- /dev/null
+++ b/libc/shared/math/sqrtbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared sqrtbf16 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_SQRTBF16_H
+#define LLVM_LIBC_SHARED_MATH_SQRTBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sqrtbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sqrtbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SQRTBF16_H

diff  --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 6266b26b81ba8..fbef4b85f98a1 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3087,6 +3087,17 @@ add_header_library(
     libc.src.__support.FPUtil.sqrt
 )
 
+add_header_library(
+  sqrtbf16
+  HDRS
+    sqrtbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   sqrtf
   HDRS

diff  --git a/libc/src/__support/math/sqrtbf16.h b/libc/src/__support/math/sqrtbf16.h
new file mode 100644
index 0000000000000..365618fde892b
--- /dev/null
+++ b/libc/src/__support/math/sqrtbf16.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for sqrtbf16 ----------------------*- 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_SQRTBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTBF16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 sqrtbf16(bfloat16 x) { return fputil::sqrt<bfloat16>(x); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTBF16_H

diff  --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index fdc3fa777d1fa..40d21ffaaa524 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2897,11 +2897,7 @@ add_entrypoint_object(
   HDRS
     ../sqrtbf16.h
   DEPENDS
-    libc.src.__support.common
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.sqrt
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.sqrtbf16
 )
 
 add_entrypoint_object(

diff  --git a/libc/src/math/generic/sqrtbf16.cpp b/libc/src/math/generic/sqrtbf16.cpp
index 061e5522a4d8f..762378dedb0f0 100644
--- a/libc/src/math/generic/sqrtbf16.cpp
+++ b/libc/src/math/generic/sqrtbf16.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/sqrtbf16.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/sqrtbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, sqrtbf16, (bfloat16 x)) {
-  return fputil::sqrt<bfloat16>(x);
+  return math::sqrtbf16(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 36262012ad0f0..dd5a9ea12a219 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -246,6 +246,7 @@ add_fp_unittest(
     libc.src.__support.math.sinpif
     libc.src.__support.math.sinpif16
     libc.src.__support.math.sqrt
+    libc.src.__support.math.sqrtbf16
     libc.src.__support.math.sqrtf
     libc.src.__support.math.tan
     libc.src.__support.math.tanf

diff  --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 45b77df069658..71ea49ee32651 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -494,4 +494,5 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
                LIBC_NAMESPACE::shared::nexttowardbf16(bfloat16(0.0), 0.0L));
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::nextafterbf16(
                                   bfloat16(0.0), bfloat16(0.0)));
+  EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::sqrtbf16(bfloat16(1.0)));
 }

diff  --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 491e846cbe49a..192e6f9eaa575 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5760,6 +5760,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_sqrtbf16",
+    hdrs = ["src/__support/math/sqrtbf16.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_sqrtf",
     hdrs = ["src/__support/math/sqrtf.h"],
@@ -8351,6 +8362,11 @@ libc_math_function(
     additional_deps = [":__support_math_sqrt"],
 )
 
+libc_math_function(
+    name = "sqrtbf16",
+    additional_deps = [":__support_math_sqrtbf16"],
+)
+
 libc_math_function(
     name = "sqrtf",
     additional_deps = [":__support_math_sqrtf"],


        


More information about the libc-commits mailing list