[libc-commits] [libc] [llvm] [libc][math] Refactor sqrtl family to header-only (PR #194510)
Muhammad Bassiouni via libc-commits
libc-commits at lists.llvm.org
Mon Apr 27 20:50:08 PDT 2026
https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/194510
>From 149cb5366a528aad1faa518293ce62edf585fd99 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Tue, 28 Apr 2026 04:46:03 +0300
Subject: [PATCH 1/4] [libc][math] Refactor sqrtl family to header-only
Refactored functions:
- sqrtl
---
libc/shared/math.h | 1 +
libc/shared/math/sqrtl.h | 23 +++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 8 ++++++
libc/src/__support/math/sqrtl.h | 25 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 2 +-
libc/src/math/generic/sqrtl.cpp | 6 ++---
libc/test/shared/CMakeLists.txt | 2 ++
.../shared/shared_math_constexpr_test.cpp | 1 +
libc/test/shared/shared_math_test.cpp | 2 ++
.../llvm-project-overlay/libc/BUILD.bazel | 13 +++++++++-
10 files changed, 77 insertions(+), 6 deletions(-)
create mode 100644 libc/shared/math/sqrtl.h
create mode 100644 libc/src/__support/math/sqrtl.h
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..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(
>From 70a4cafc23bf6d41721716f00e72eb75ca1aa267 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Tue, 28 Apr 2026 05:39:17 +0300
Subject: [PATCH 2/4] fix ci
---
libc/src/__support/math/sqrtl.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/sqrtl.h b/libc/src/__support/math/sqrtl.h
index 97643d70212a1..fa67a63575c2d 100644
--- a/libc/src/__support/math/sqrtl.h
+++ b/libc/src/__support/math/sqrtl.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE constexpr long double sqrtl(long double x) {
+LIBC_INLINE LIBC_CONSTEXPR long double sqrtl(long double x) {
return fputil::sqrt<long double>(x);
}
>From 11637952c47636f0eb65c5dfb62751eec1fd3fa1 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Tue, 28 Apr 2026 05:40:32 +0300
Subject: [PATCH 3/4] nit
---
libc/test/shared/shared_math_test.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 78135ad8cc30f..7ba056a08fa9d 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -365,7 +365,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
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));
+ EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::sqrtl(0.0L));
}
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
>From 3ce4bc16ba698927fec6b4cb92c5717ebd5ba9f2 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Tue, 28 Apr 2026 06:49:50 +0300
Subject: [PATCH 4/4] fix ppc ci
---
libc/shared/math/sqrtl.h | 6 ++++++
libc/src/__support/math/sqrtl.h | 4 ++++
2 files changed, 10 insertions(+)
diff --git a/libc/shared/math/sqrtl.h b/libc/shared/math/sqrtl.h
index 889998fea4ba1..95aac57588bd4 100644
--- a/libc/shared/math/sqrtl.h
+++ b/libc/shared/math/sqrtl.h
@@ -10,6 +10,10 @@
#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 {
@@ -20,4 +24,6 @@ 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/sqrtl.h b/libc/src/__support/math/sqrtl.h
index fa67a63575c2d..4d43067ccf904 100644
--- a/libc/src/__support/math/sqrtl.h
+++ b/libc/src/__support/math/sqrtl.h
@@ -12,6 +12,8 @@
#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 {
@@ -22,4 +24,6 @@ LIBC_INLINE LIBC_CONSTEXPR long double sqrtl(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
More information about the libc-commits
mailing list