[libc-commits] [libc] [llvm] [libc][math] Refactor sqrtf16 to Header Only (PR #177726)
Prajwal KP via libc-commits
libc-commits at lists.llvm.org
Fri Jan 23 18:35:36 PST 2026
https://github.com/Prajwal-kp-18 created https://github.com/llvm/llvm-project/pull/177726
closes: #177651
>From 51cb2bc8980e87e822c691f3f03ad4fee90291cb Mon Sep 17 00:00:00 2001
From: Prajwal <prajwal.kp.1817 at gmail.com>
Date: Sat, 24 Jan 2026 08:02:35 +0530
Subject: [PATCH] [libc][math] Refactor sqrtf16 to Header Only
---
libc/shared/math.h | 1 +
libc/shared/math/sqrtf16.h | 29 +++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 8 +++++
libc/src/__support/math/sqrtf16.h | 31 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 2 +-
libc/src/math/generic/sqrtf16.cpp | 9 ++----
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 10 +++++-
9 files changed, 83 insertions(+), 9 deletions(-)
create mode 100644 libc/shared/math/sqrtf16.h
create mode 100644 libc/src/__support/math/sqrtf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index a331abcc033c7..83c541dd683ef 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -84,6 +84,7 @@
#include "math/rsqrtf.h"
#include "math/rsqrtf16.h"
#include "math/sin.h"
+#include "math/sqrtf16.h"
#include "math/tan.h"
#endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/sqrtf16.h b/libc/shared/math/sqrtf16.h
new file mode 100644
index 0000000000000..9c2043582307a
--- /dev/null
+++ b/libc/shared/math/sqrtf16.h
@@ -0,0 +1,29 @@
+//===-- Shared sqrtf16 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_SQRTF16_H
+#define LLVM_LIBC_SHARED_MATH_SQRTF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sqrtf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sqrtf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_SQRTF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a03952d5c5ed0..9a53f840e3297 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1072,6 +1072,14 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ sqrtf16
+ HDRS
+ sqrtf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.sqrt
+)
+
add_header_library(
sincos_eval
HDRS
diff --git a/libc/src/__support/math/sqrtf16.h b/libc/src/__support/math/sqrtf16.h
new file mode 100644
index 0000000000000..8c9e1e65177de
--- /dev/null
+++ b/libc/src/__support/math/sqrtf16.h
@@ -0,0 +1,31 @@
+//===-- Implementation header for sqrtf16 -----------------------*- 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_SQRTF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE static constexpr float16 sqrtf16(float16 x) {
+ return fputil::sqrt<float16>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 04ad1aa896613..a73f55a15cdf0 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3066,7 +3066,7 @@ add_entrypoint_object(
HDRS
../sqrtf16.h
DEPENDS
- libc.src.__support.FPUtil.sqrt
+ libc.src.__support.math.sqrtf16
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/sqrtf16.cpp b/libc/src/math/generic/sqrtf16.cpp
index 0aa4a201b3e68..d175f69f39ba4 100644
--- a/libc/src/math/generic/sqrtf16.cpp
+++ b/libc/src/math/generic/sqrtf16.cpp
@@ -7,14 +7,9 @@
//===----------------------------------------------------------------------===//
#include "src/math/sqrtf16.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/sqrtf16.h"
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(float16, sqrtf16, (float16 x)) {
- return fputil::sqrt<float16>(x);
-}
-
+LLVM_LIBC_FUNCTION(float16, sqrtf16, (float16 x)) { return math::sqrtf16(x); }
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 4f82d00cc6c84..5a7a9a7b50af4 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -79,6 +79,7 @@ add_fp_unittest(
libc.src.__support.math.llogbf
libc.src.__support.math.rsqrtf
libc.src.__support.math.rsqrtf16
+ libc.src.__support.math.sqrtf16
libc.src.__support.math.sin
libc.src.__support.math.tan
)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 5ec8a7b23081e..90b18e41c826e 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -18,6 +18,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::acoshf16(1.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::acospif16(1.0f16));
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::rsqrtf16(1.0f16));
+ EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::sqrtf16(1.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinf16(0.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinhf16(0.0f16));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c53b9c9a91573..b7b8799d8c423 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2405,6 +2405,14 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sqrtf16",
+ hdrs = ["src/__support/math/sqrtf16.h"],
+ deps = [
+ ":__support_fputil_sqrt",
+ ],
+)
+
libc_support_library(
name = "__support_math_asin_utils",
hdrs = ["src/__support/math/asin_utils.h"],
@@ -5151,7 +5159,7 @@ libc_math_function(
libc_math_function(
name = "sqrtf16",
additional_deps = [
- ":__support_fputil_sqrt",
+ ":__support_math_sqrtf16",
],
)
More information about the libc-commits
mailing list