[libc][math] Refactor sqrtl family to header-only#194510
Merged
Merged
Conversation
Member
|
@llvm/pr-subscribers-libc Author: Anonmiraj (AnonMiraj) ChangesRefactors the sqrtl math family to be header-only. part of: #147386 Target Functions:
Full diff: https://github.com/llvm/llvm-project/pull/194510.diff 10 Files Affected:
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..889998fea4ba1
--- /dev/null
+++ b/libc/shared/math/sqrtl.h
@@ -0,0 +1,23 @@
+//===-- 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/math/sqrtl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sqrtl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#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..97643d70212a1
--- /dev/null
+++ b/libc/src/__support/math/sqrtl.h
@@ -0,0 +1,25 @@
+//===-- 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"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr long double sqrtl(long double x) {
+ return fputil::sqrt<long double>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#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..78135ad8cc30f 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(0x0p+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..34aecfc02a6ce 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_normal_float",
+ ":__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(
|
f3977fa to
02641b4
Compare
Refactored functions: - sqrtl
02641b4 to
149cb53
Compare
bassiounix
approved these changes
Apr 28, 2026
yingopq
pushed a commit
to yingopq/llvm-project
that referenced
this pull request
Apr 29, 2026
part of: llvm#147386 --------- Co-authored-by: bassiounix <muhammad.m.bassiouni@gmail.com>
KHicketts
pushed a commit
to KHicketts/llvm-project
that referenced
this pull request
Apr 30, 2026
part of: llvm#147386 --------- Co-authored-by: bassiounix <muhammad.m.bassiouni@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactors the sqrtl math family to be header-only.
part of: #147386
Target Functions: