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

Sai Hemanth Bheemreddy via libc-commits libc-commits at lists.llvm.org
Tue Jan 13 12:54:17 PST 2026


https://github.com/bshreddy created https://github.com/llvm/llvm-project/pull/175829

Part of #175328 

in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450


>From d3075047d090ff3b550b654611237bef525798f3 Mon Sep 17 00:00:00 2001
From: Sai Hemanth Bheemreddy <sh.bheemreddy at gmail.com>
Date: Mon, 12 Jan 2026 01:18:00 -0500
Subject: [PATCH] [libc][math] Refactor f16sqrtf128 to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/f16sqrtf128.h                | 29 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  9 +++++
 libc/src/__support/math/f16sqrtf128.h         | 33 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/f16sqrtf128.cpp         |  6 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 11 ++++++-
 9 files changed, 88 insertions(+), 7 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 287f3aea1565a..5d1406dcd4a65 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -57,6 +57,7 @@
 #include "math/expm1.h"
 #include "math/expm1f.h"
 #include "math/expm1f16.h"
+#include "math/f16sqrtf128.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
diff --git a/libc/shared/math/f16sqrtf128.h b/libc/shared/math/f16sqrtf128.h
new file mode 100644
index 0000000000000..782a2ddf4c9f3
--- /dev/null
+++ b/libc/shared/math/f16sqrtf128.h
@@ -0,0 +1,29 @@
+//===-- 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-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#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_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_F16SQRTF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..d7c06871b23d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -935,6 +935,15 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  f16sqrtf128
+  HDRS
+    f16sqrtf128.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.sqrt
+)
+
 add_header_library(
   range_reduction_double
   HDRS
diff --git a/libc/src/__support/math/f16sqrtf128.h b/libc/src/__support/math/f16sqrtf128.h
new file mode 100644
index 0000000000000..2a6612944b42b
--- /dev/null
+++ b/libc/src/__support/math/f16sqrtf128.h
@@ -0,0 +1,33 @@
+//===-- 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-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LLVM_LIBC_FUNCTION(float16, f16sqrtf128, (float128 x)) {
+  return fputil::sqrt<float16>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif
+
+#endif
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..774af8f8aa38b 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5213,8 +5213,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..eca577091e0e9 100644
--- a/libc/src/math/generic/f16sqrtf128.cpp
+++ b/libc/src/math/generic/f16sqrtf128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16sqrtf128.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16sqrtf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, f16sqrtf128, (float128 x)) {
-  return fputil::sqrt<float16>(x);
+  return math::f16sqrtf128(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..6bdf3b0205247 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -53,6 +53,7 @@ add_fp_unittest(
     libc.src.__support.math.exp10f16
     libc.src.__support.math.expf
     libc.src.__support.math.expf16
+    libc.src.__support.math.f16sqrtf128
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128
     libc.src.__support.math.frexpf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 20a98a1a9138c..a98b172ca491d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -103,6 +103,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 
   EXPECT_FP_EQ(float128(0x0p+0),
                LIBC_NAMESPACE::shared::atan2f128(float128(0.0), float128(0.0)));
+  EXPECT_FP_EQ(float16(2.0),
+               LIBC_NAMESPACE::shared::f16sqrtf128(float128(4.0)));
   EXPECT_FP_EQ_ALL_ROUNDING(float128(0.75), LIBC_NAMESPACE::shared::frexpf128(
                                                 float128(24), &exponent));
   EXPECT_EQ(exponent, 5);
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c03c21b834895..bb7cec4dd0679 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2804,6 +2804,15 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16sqrtf128",
+    hdrs = ["src/__support/math/f16sqrtf128.h"],
+    deps = [
+        ":__support.macros.properties.types",
+        ":__support.FPUtil.sqrt",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -3973,7 +3982,7 @@ libc_math_function(
 libc_math_function(
     name = "f16sqrtf128",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_f16sqrtf128",
     ],
 )
 



More information about the libc-commits mailing list