[libc-commits] [libc] 6a0a18b - [libc][math] Refactor sqrtl family to header-only (#194510)
via libc-commits
libc-commits at lists.llvm.org
Mon Apr 27 20:58:35 PDT 2026
Author: Anonmiraj
Date: 2026-04-28T06:58:31+03:00
New Revision: 6a0a18b1bb6098065833f73c3709f631f0b130e5
URL: https://github.com/llvm/llvm-project/commit/6a0a18b1bb6098065833f73c3709f631f0b130e5
DIFF: https://github.com/llvm/llvm-project/commit/6a0a18b1bb6098065833f73c3709f631f0b130e5.diff
LOG: [libc][math] Refactor sqrtl family to header-only (#194510)
part of: #147386
---------
Co-authored-by: bassiounix <muhammad.m.bassiouni at gmail.com>
Added:
libc/shared/math/sqrtl.h
libc/src/__support/math/sqrtl.h
Modified:
libc/shared/math.h
libc/src/__support/math/CMakeLists.txt
libc/src/math/generic/CMakeLists.txt
libc/src/math/generic/sqrtl.cpp
libc/test/shared/CMakeLists.txt
libc/test/shared/shared_math_constexpr_test.cpp
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 f1243f7925527..9892da63043e6 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -284,6 +284,7 @@
#include "math/sqrtf.h"
#include "math/sqrtf128.h"
#include "math/sqrtf16.h"
+#include "math/sqrtl.h"
#include "math/tan.h"
#include "math/tanf.h"
#include "math/tanf16.h"
diff --git a/libc/shared/math/sqrtl.h b/libc/shared/math/sqrtl.h
new file mode 100644
index 0000000000000..95aac57588bd4
--- /dev/null
+++ b/libc/shared/math/sqrtl.h
@@ -0,0 +1,29 @@
+//===-- Shared sqrtl 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_SQRTL_H
+#define LLVM_LIBC_SHARED_MATH_SQRTL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/macros/properties/types.h"
+
+#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+
+#include "src/__support/math/sqrtl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sqrtl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+
+#endif // LLVM_LIBC_SHARED_MATH_SQRTL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 04172ac516514..f62dc5998053f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3500,6 +3500,14 @@ add_header_library(
libc.src.__support.uint128
libc.include.llvm-libc-types.float128
)
+add_header_library(
+ sqrtl
+ HDRS
+ sqrtl.h
+ DEPENDS
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.config
+)
add_header_library(
tan
diff --git a/libc/src/__support/math/sqrtl.h b/libc/src/__support/math/sqrtl.h
new file mode 100644
index 0000000000000..4d43067ccf904
--- /dev/null
+++ b/libc/src/__support/math/sqrtl.h
@@ -0,0 +1,29 @@
+//===-- Implementation header for sqrtl -------------------------*- 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_SQRTL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTL_H
+
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+
+#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE LIBC_CONSTEXPR long double sqrtl(long double x) {
+ return fputil::sqrt<long double>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 42b2046a1eb94..d72131a608ddb 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2841,7 +2841,7 @@ add_entrypoint_object(
HDRS
../sqrtl.h
DEPENDS
- libc.src.__support.FPUtil.sqrt
+ libc.src.__support.math.sqrtl
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/sqrtl.cpp b/libc/src/math/generic/sqrtl.cpp
index 2368182740ca8..b7a0b8bdce70a 100644
--- a/libc/src/math/generic/sqrtl.cpp
+++ b/libc/src/math/generic/sqrtl.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/sqrtl.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/sqrtl.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(long double, sqrtl, (long double x)) {
- return fputil::sqrt<long double>(x);
+ return math::sqrtl(x);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 8f054499b202f..e9664b18a9e98 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -282,6 +282,7 @@ add_fp_unittest(
libc.src.__support.math.sqrt
libc.src.__support.math.sqrtbf16
libc.src.__support.math.sqrtf
+ libc.src.__support.math.sqrtl
libc.src.__support.math.tan
libc.src.__support.math.tanf
libc.src.__support.math.tanf16
@@ -344,6 +345,7 @@ add_fp_unittest(
libc.src.__support.math.log
libc.src.__support.math.logbbf16
libc.src.__support.math.ilogbbf16
+ libc.src.__support.math.sqrtl
)
add_fp_unittest(
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 6df46ecc48154..2bb9a15cb4684 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -65,6 +65,7 @@ static_assert(1.0L == LIBC_NAMESPACE::shared::fdiml(1.0L, 0.0L));
static_assert(0.0f == LIBC_NAMESPACE::shared::fdivl(0.0L, 1.0L));
static_assert(0.0L == LIBC_NAMESPACE::shared::floorl(0.0L));
static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::bf16subl(0.0L, 0.0L));
+static_assert(0.0L == LIBC_NAMESPACE::shared::sqrtl(0.0L));
#endif
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 74e6a0484d1cb..7ba056a08fa9d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -364,6 +364,8 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
EXPECT_FP_EQ(10.0f16, LIBC_NAMESPACE::shared::f16fmal(2.0L, 3.0L, 4.0L));
#endif // LIBC_TYPES_HAS_FLOAT16
+
+ EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::sqrtl(0.0L));
}
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 3e9bd3b6f6cf6..822292fc70202 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6149,6 +6149,15 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sqrtl",
+ hdrs = ["src/__support/math/sqrtl.h"],
+ deps = [
+ ":__support_fputil_sqrt",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_logf16",
hdrs = ["src/__support/math/logf16.h"],
@@ -9182,7 +9191,9 @@ libc_math_function(
libc_math_function(
name = "sqrtl",
- additional_deps = [":__support_fputil_sqrt"],
+ additional_deps = [
+ ":__support_math_sqrtl",
+ ],
)
libc_math_function(
More information about the libc-commits
mailing list