[libc-commits] [libc] [llvm] [libc][math] Refactor f16sqrtf128 to Header Only. (PR #183542)

via libc-commits libc-commits at lists.llvm.org
Thu Feb 26 10:01:16 PST 2026


https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/183542

>From f1e67fe26299ea5b2c60ffce1fbfe6f1aef7bf45 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 26 Feb 2026 19:58:21 +0530
Subject: [PATCH 1/8] Refactor

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/f16sqrtf128.h                | 32 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 12 ++++++
 libc/src/__support/math/f16sqrtf128.h         | 35 +++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/f16sqrtf128.cpp         | 38 +++++++++----------
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  7 ++++
 .../llvm-project-overlay/libc/BUILD.bazel     | 14 ++++++-
 9 files changed, 120 insertions(+), 23 deletions(-)
 create mode 100644 libc/shared/math/f16sqrtf128.h
 create mode 100644 libc/src/__support/math/f16sqrtf128.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 4ab336989f794..308e921009f98 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -104,6 +104,7 @@
 #include "math/f16mull.h"
 #include "math/f16sqrt.h"
 #include "math/f16sqrtf.h"
+#include "math/f16sqrtf128.h"
 #include "math/f16sqrtl.h"
 #include "math/f16sub.h"
 #include "math/f16subf.h"
diff --git a/libc/shared/math/f16sqrtf128.h b/libc/shared/math/f16sqrtf128.h
new file mode 100644
index 0000000000000..75f03b704602c
--- /dev/null
+++ b/libc/shared/math/f16sqrtf128.h
@@ -0,0 +1,32 @@
+//===-- Shared f16sqrtf128 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_F16SQRTF128_H
+#define LLVM_LIBC_SHARED_MATH_F16SQRTF128_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "include/llvm-libc-types/float128.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/f16sqrtf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::f16sqrtf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_F16SQRTL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4e7abd280bc4b..1b6a579053f48 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1149,6 +1149,18 @@ add_header_library(
     libc.include.llvm-libc-macros.float16_macros
 )
 
+add_header_library(
+  f16sqrtf128
+  HDRS
+    f16sqrtf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.include.llvm-libc-types.float128
+    libc.include.llvm-libc-macros.float16_macros
+)
+
 add_header_library(
   f16sqrtl
   HDRS
diff --git a/libc/src/__support/math/f16sqrtf128.h b/libc/src/__support/math/f16sqrtf128.h
new file mode 100644
index 0000000000000..d7fb51ccf8df6
--- /dev/null
+++ b/libc/src/__support/math/f16sqrtf128.h
@@ -0,0 +1,35 @@
+//===-- Implementation header for f16sqrtf128 -------------------*- 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_F16SQRTF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTF128_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#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 constexpr float16 f16sqrtf(float128 x) {
+  return fputil::sqrt<float16>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index bb2c6f673f082..b2fa8a91dd18b 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4900,8 +4900,7 @@ add_entrypoint_object(
   HDRS
     ../f16sqrtf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.math.f16sqrtf128
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/f16sqrtf128.cpp b/libc/src/math/generic/f16sqrtf128.cpp
index d33d6d2963fea..b1ed45f1e03c7 100644
--- a/libc/src/math/generic/f16sqrtf128.cpp
+++ b/libc/src/math/generic/f16sqrtf128.cpp
@@ -1,20 +1,18 @@
-//===-- Implementation of f16sqrtf128 function ----------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/math/f16sqrtf128.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(float16, f16sqrtf128, (float128 x)) {
-  return fputil::sqrt<float16>(x);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
+//===-- Implementation of f16sqrtf128 function ----------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16sqrtf128.h"
+#include "src/__support/math/f16sqrtf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, f16sqrtf128, (float128 x)) {
+  return math::f16sqrtf128(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index fb26bb0e8ada6..87ff50acc46a1 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -103,6 +103,7 @@ add_fp_unittest(
     libc.src.__support.math.f16mull
     libc.src.__support.math.f16sqrt
     libc.src.__support.math.f16sqrtf
+    libc.src.__support.math.f16sqrtf128
     libc.src.__support.math.f16sqrtl
     libc.src.__support.math.f16sub
     libc.src.__support.math.f16subf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 2758e3749910c..555a3743b1972 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -76,6 +76,13 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::f16sqrt(0.0));
 
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::f16sqrtf(0.0f));
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::f16sqrtf128(float128(0.0)));
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
   EXPECT_FP_EQ(float16(10.0),
                LIBC_NAMESPACE::shared::f16fmal(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 9f10754cddd9c..200172662748f 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1546,6 +1546,18 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16sqrtf128",
+    hdrs = ["src/__support/math/f16sqrtf128.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+        ":llvm_libc_types_float128",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_f16sqrtl",
     hdrs = ["src/__support/math/f16sqrtl.h"],
@@ -6319,7 +6331,7 @@ libc_math_function(
 libc_math_function(
     name = "f16sqrtf128",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_fputil_f16sqrtf128",
     ],
 )
 

>From eab0ecdc34fbcbd668cf19ce955bac30664eb070 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 26 Feb 2026 19:59:45 +0530
Subject: [PATCH 2/8] formatted

---
 libc/src/math/generic/f16sqrtf128.cpp | 36 +++++++++++++--------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/libc/src/math/generic/f16sqrtf128.cpp b/libc/src/math/generic/f16sqrtf128.cpp
index b1ed45f1e03c7..eca577091e0e9 100644
--- a/libc/src/math/generic/f16sqrtf128.cpp
+++ b/libc/src/math/generic/f16sqrtf128.cpp
@@ -1,18 +1,18 @@
-//===-- Implementation of f16sqrtf128 function ----------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/math/f16sqrtf128.h"
-#include "src/__support/math/f16sqrtf128.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(float16, f16sqrtf128, (float128 x)) {
-  return math::f16sqrtf128(x);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
+//===-- Implementation of f16sqrtf128 function ----------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/f16sqrtf128.h"
+#include "src/__support/math/f16sqrtf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, f16sqrtf128, (float128 x)) {
+  return math::f16sqrtf128(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

>From 586ce1f7022e921fbeb8962ba0f75e3f0d9ee02e Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 26 Feb 2026 20:23:03 +0530
Subject: [PATCH 3/8] removed constexpr temporarily

---
 libc/src/__support/math/f16sqrtf128.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/math/f16sqrtf128.h b/libc/src/__support/math/f16sqrtf128.h
index d7fb51ccf8df6..1a645dce630c7 100644
--- a/libc/src/__support/math/f16sqrtf128.h
+++ b/libc/src/__support/math/f16sqrtf128.h
@@ -22,7 +22,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE constexpr float16 f16sqrtf(float128 x) {
+LIBC_INLINE float16 f16sqrtf(float128 x) {
   return fputil::sqrt<float16>(x);
 }
 

>From 2632b3ab497d6405fb3f32d6b8b08bffaf925410 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 26 Feb 2026 20:27:36 +0530
Subject: [PATCH 4/8] added constexpr

---
 libc/src/__support/math/f16sqrtf128.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/math/f16sqrtf128.h b/libc/src/__support/math/f16sqrtf128.h
index 1a645dce630c7..d7fb51ccf8df6 100644
--- a/libc/src/__support/math/f16sqrtf128.h
+++ b/libc/src/__support/math/f16sqrtf128.h
@@ -22,7 +22,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE float16 f16sqrtf(float128 x) {
+LIBC_INLINE constexpr float16 f16sqrtf(float128 x) {
   return fputil::sqrt<float16>(x);
 }
 

>From acd4bded63f9a012e320974fe32cf4a932bbcfc2 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 26 Feb 2026 20:57:04 +0530
Subject: [PATCH 5/8] fixed issue

---
 libc/shared/math/f16sqrtf128.h        | 2 +-
 libc/src/__support/math/f16sqrtf128.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/shared/math/f16sqrtf128.h b/libc/shared/math/f16sqrtf128.h
index 75f03b704602c..bd04940b4bca2 100644
--- a/libc/shared/math/f16sqrtf128.h
+++ b/libc/shared/math/f16sqrtf128.h
@@ -29,4 +29,4 @@ using math::f16sqrtf128;
 #endif // LIBC_TYPES_HAS_FLOAT16
 #endif // LIBC_TYPES_HAS_FLOAT128
 
-#endif // LLVM_LIBC_SHARED_MATH_F16SQRTL_H
+#endif // LLVM_LIBC_SHARED_MATH_F16SQRTF128_H
diff --git a/libc/src/__support/math/f16sqrtf128.h b/libc/src/__support/math/f16sqrtf128.h
index d7fb51ccf8df6..0471b1f6b1455 100644
--- a/libc/src/__support/math/f16sqrtf128.h
+++ b/libc/src/__support/math/f16sqrtf128.h
@@ -22,7 +22,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE constexpr float16 f16sqrtf(float128 x) {
+LIBC_INLINE constexpr float16 f16sqrtf128(float128 x) {
   return fputil::sqrt<float16>(x);
 }
 

>From 9ba438b50bed8a40bde7ab3b7dace6af8063bd2b Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 26 Feb 2026 21:10:32 +0530
Subject: [PATCH 6/8] bazel

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 200172662748f..ff5daad73ccad 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6331,7 +6331,7 @@ libc_math_function(
 libc_math_function(
     name = "f16sqrtf128",
     additional_deps = [
-        ":__support_fputil_f16sqrtf128",
+        ":__support_math_f16sqrtf128",
     ],
 )
 

>From 5e87a36dc5ce8bfea250619a0a261b933b5e6c60 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 26 Feb 2026 23:23:03 +0530
Subject: [PATCH 7/8] removed constexpr

---
 libc/src/__support/math/f16sqrtf128.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/math/f16sqrtf128.h b/libc/src/__support/math/f16sqrtf128.h
index 0471b1f6b1455..28f716259866b 100644
--- a/libc/src/__support/math/f16sqrtf128.h
+++ b/libc/src/__support/math/f16sqrtf128.h
@@ -22,7 +22,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE constexpr float16 f16sqrtf128(float128 x) {
+LIBC_INLINE float16 f16sqrtf128(float128 x) {
   return fputil::sqrt<float16>(x);
 }
 

>From 6d506efad78ce966b1417a08a616330fae191f15 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 26 Feb 2026 23:30:46 +0530
Subject: [PATCH 8/8] formatted

---
 libc/src/__support/math/f16sqrtf128.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libc/src/__support/math/f16sqrtf128.h b/libc/src/__support/math/f16sqrtf128.h
index 28f716259866b..3ef74136cba94 100644
--- a/libc/src/__support/math/f16sqrtf128.h
+++ b/libc/src/__support/math/f16sqrtf128.h
@@ -22,9 +22,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-LIBC_INLINE float16 f16sqrtf128(float128 x) {
-  return fputil::sqrt<float16>(x);
-}
+LIBC_INLINE float16 f16sqrtf128(float128 x) { return fputil::sqrt<float16>(x); }
 
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list