[libc-commits] [libc] bd3ecdc - [libc][math] Refactor f16sqrtl to Header Only. (#176333)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 26 10:06:47 PST 2026


Author: Cheng Lingfei
Date: 2026-01-26T20:06:40+02:00
New Revision: bd3ecdc59bb69a5528dee06f1e47c733bb89a25f

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

LOG: [libc][math] Refactor f16sqrtl to Header Only. (#176333)

builds correctly with both `clang`, `gcc`, `cmake`, and `Bazel`.

Closes https://github.com/llvm/llvm-project/issues/175331.

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

Modified: 
    libc/shared/math.h
    libc/src/__support/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/src/math/generic/f16sqrtl.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 5126e46c9772e..d05cf0c550df9 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -61,6 +61,7 @@
 #include "math/expm1f16.h"
 #include "math/f16fma.h"
 #include "math/f16fmal.h"
+#include "math/f16sqrtl.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"

diff  --git a/libc/shared/math/f16sqrtl.h b/libc/shared/math/f16sqrtl.h
new file mode 100644
index 0000000000000..cb187fa52c5e6
--- /dev/null
+++ b/libc/shared/math/f16sqrtl.h
@@ -0,0 +1,27 @@
+//===-- Shared f16sqrt 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_F16SQRTL_H
+#define LLVM_LIBC_SHARED_MATH_F16SQRTL_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/f16sqrtl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::f16sqrtl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+#endif // LLVM_LIBC_SHARED_MATH_F16SQRTL_H

diff  --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index d18ebc03ba7b8..999725bfc21e9 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -593,6 +593,15 @@ add_header_library(
     libc.src.__support.math.exp10_float16_constants
 )
 
+add_header_library(
+  f16sqrtl
+  HDRS
+    f16sqrtl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   frexpf128
   HDRS

diff  --git a/libc/src/__support/math/f16sqrtl.h b/libc/src/__support/math/f16sqrtl.h
new file mode 100644
index 0000000000000..86f0e594408e1
--- /dev/null
+++ b/libc/src/__support/math/f16sqrtl.h
@@ -0,0 +1,33 @@
+//===-- Implementation header of f16sqrt 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTL_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#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 static constexpr float16 f16sqrtl(long double x) {
+  return fputil::sqrt<float16>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_F16SQRTL_H

diff  --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3addfab6c6603..44e7891bee048 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5155,8 +5155,8 @@ add_entrypoint_object(
   HDRS
     ../f16sqrtl.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.math.f16sqrtl
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(

diff  --git a/libc/src/math/generic/f16sqrtl.cpp b/libc/src/math/generic/f16sqrtl.cpp
index c6ce73de7fec9..2c81ca2a37030 100644
--- a/libc/src/math/generic/f16sqrtl.cpp
+++ b/libc/src/math/generic/f16sqrtl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16sqrtl.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16sqrtl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, f16sqrtl, (long double x)) {
-  return fputil::sqrt<float16>(x);
+  return math::f16sqrtl(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index dabf5f6b168cc..6bb409447ff02 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -57,6 +57,7 @@ add_fp_unittest(
     libc.src.__support.math.expf16
     libc.src.__support.math.f16fma
     libc.src.__support.math.f16fmal
+    libc.src.__support.math.f16sqrtl
     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 cd389c41cb7b2..8fe8c804be363 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -50,6 +50,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::logbf16(1.0f16));
 
   EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
+  EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT16

diff  --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 346864d2276c0..c3b1b8156c9be 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1416,6 +1416,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16sqrtl",
+    hdrs = ["src/__support/math/f16sqrtl.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
 fma_common_hdrs = [
     "src/__support/FPUtil/FMA.h",
     "src/__support/FPUtil/generic/FMA.h",
@@ -4248,7 +4259,8 @@ libc_math_function(
 libc_math_function(
     name = "f16sqrtl",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_f16sqrtl",
+        ":errno",
     ],
 )
 


        


More information about the libc-commits mailing list