[libc-commits] [libc] [llvm] [libc][math] Refactor sqrti to header-only (PR #177960)
via libc-commits
libc-commits at lists.llvm.org
Mon Jan 26 05:42:10 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Vedant Neve (0bVdnt)
<details>
<summary>Changes</summary>
Move the integer square root implementation (`isqrt` and `isqrt_fast`) to a header-only implementation in `src/__support/math/sqrti.h` and expose it via `shared/math/sqrti.h`.
This follows the established pattern for shared math functions, making `sqrti` and `sqrti_fast` available as constexpr functions in the `math::` namespace.
## Changes
- Add `libc/src/__support/math/sqrti.h` - core header-only implementation
- Add `libc/shared/math/sqrti.h` - shared header exporting to `shared::` namespace
- Update `uhksqrtus.cpp` and `uksqrtui.cpp` to use `math::sqrti`
- Update CMake and Bazel build files
Fixes #<!-- -->177650
---
Full diff: https://github.com/llvm/llvm-project/pull/177960.diff
11 Files Affected:
- (modified) libc/shared/math.h (+1)
- (added) libc/shared/math/sqrti.h (+30)
- (modified) libc/src/__support/math/CMakeLists.txt (+9)
- (added) libc/src/__support/math/sqrti.h (+41)
- (modified) libc/src/stdfix/CMakeLists.txt (+2-2)
- (modified) libc/src/stdfix/uhksqrtus.cpp (+3-4)
- (modified) libc/src/stdfix/uksqrtui.cpp (+3-4)
- (modified) libc/test/src/stdfix/CMakeLists.txt (+2-2)
- (modified) libc/test/src/stdfix/uhksqrtus_test.cpp (+2-2)
- (modified) libc/test/src/stdfix/uksqrtui_test.cpp (+2-2)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+23)
``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 5126e46c9772e..4912b3a9d16cf 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -84,6 +84,7 @@
#include "math/rsqrtf.h"
#include "math/rsqrtf16.h"
#include "math/sin.h"
+#include "math/sqrti.h"
#include "math/tan.h"
#include "math/tanf.h"
diff --git a/libc/shared/math/sqrti.h b/libc/shared/math/sqrti.h
new file mode 100644
index 0000000000000..f00ef9117a672
--- /dev/null
+++ b/libc/shared/math/sqrti.h
@@ -0,0 +1,30 @@
+//===-- Shared sqrti 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_SQRTI_H
+#define LLVM_LIBC_SHARED_MATH_SQRTI_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+#ifdef LIBC_COMPILER_HAS_FIXED_POINT
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sqrti.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sqrti;
+using math::sqrti_fast;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_COMPILER_HAS_FIXED_POINT
+
+#endif // LLVM_LIBC_SHARED_MATH_SQRTI_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index d18ebc03ba7b8..525fe33cf6384 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1072,6 +1072,15 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ sqrti
+ HDRS
+ sqrti.h
+ DEPENDS
+ libc.src.__support.fixed_point.sqrt
+ libc.src.__support.macros.config
+)
+
add_header_library(
sincos_eval
HDRS
diff --git a/libc/src/__support/math/sqrti.h b/libc/src/__support/math/sqrti.h
new file mode 100644
index 0000000000000..2f7b66a2d2e23
--- /dev/null
+++ b/libc/src/__support/math/sqrti.h
@@ -0,0 +1,41 @@
+//===-- Implementation header for sqrti -------------------------*- 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_SQRTI_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTI_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+#ifdef LIBC_COMPILER_HAS_FIXED_POINT
+
+#include "src/__support/fixed_point/sqrt.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+// Integer square root - Accurate version:
+// Absolute errors < 2^(-fraction length).
+template <typename T> LIBC_INLINE constexpr auto sqrti(T x) {
+ return fixed_point::isqrt(x);
+}
+
+// Integer square root - Fast but less accurate version:
+// Relative errors < 2^(-fraction length).
+template <typename T> LIBC_INLINE constexpr auto sqrti_fast(T x) {
+ return fixed_point::isqrt_fast(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_COMPILER_HAS_FIXED_POINT
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTI_H
diff --git a/libc/src/stdfix/CMakeLists.txt b/libc/src/stdfix/CMakeLists.txt
index e9841cd940315..8ab4b0e01d74f 100644
--- a/libc/src/stdfix/CMakeLists.txt
+++ b/libc/src/stdfix/CMakeLists.txt
@@ -109,7 +109,7 @@ add_entrypoint_object(
SRCS
uhksqrtus.cpp
DEPENDS
- libc.src.__support.fixed_point.sqrt
+ libc.src.__support.math.sqrti
)
add_entrypoint_object(
@@ -119,7 +119,7 @@ add_entrypoint_object(
SRCS
uksqrtui.cpp
DEPENDS
- libc.src.__support.fixed_point.sqrt
+ libc.src.__support.math.sqrti
)
add_entrypoint_object(
diff --git a/libc/src/stdfix/uhksqrtus.cpp b/libc/src/stdfix/uhksqrtus.cpp
index f41e8794a0418..98502c2f5a4aa 100644
--- a/libc/src/stdfix/uhksqrtus.cpp
+++ b/libc/src/stdfix/uhksqrtus.cpp
@@ -8,16 +8,15 @@
#include "uhksqrtus.h"
#include "src/__support/common.h"
-#include "src/__support/fixed_point/sqrt.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/sqrti.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(unsigned short accum, uhksqrtus, (unsigned short x)) {
#ifdef LIBC_FAST_MATH
- return fixed_point::isqrt_fast(x);
+ return math::sqrti_fast(x);
#else
- return fixed_point::isqrt(x);
+ return math::sqrti(x);
#endif
}
diff --git a/libc/src/stdfix/uksqrtui.cpp b/libc/src/stdfix/uksqrtui.cpp
index 504bd06163cf4..f1d8fe4559e93 100644
--- a/libc/src/stdfix/uksqrtui.cpp
+++ b/libc/src/stdfix/uksqrtui.cpp
@@ -8,16 +8,15 @@
#include "uksqrtui.h"
#include "src/__support/common.h"
-#include "src/__support/fixed_point/sqrt.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/sqrti.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(unsigned accum, uksqrtui, (unsigned int x)) {
#ifdef LIBC_FAST_MATH
- return fixed_point::isqrt_fast(x);
+ return math::sqrti_fast(x);
#else
- return fixed_point::isqrt(x);
+ return math::sqrti(x);
#endif
}
diff --git a/libc/test/src/stdfix/CMakeLists.txt b/libc/test/src/stdfix/CMakeLists.txt
index 04bedc3e00fe2..2f5e05f5a2b71 100644
--- a/libc/test/src/stdfix/CMakeLists.txt
+++ b/libc/test/src/stdfix/CMakeLists.txt
@@ -161,7 +161,7 @@ add_libc_test(
libc.src.stdfix.uhksqrtus
libc.src.__support.CPP.bit
libc.src.__support.fixed_point.fx_rep
- libc.src.__support.fixed_point.sqrt
+ libc.src.__support.math.sqrti
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.sqrt
)
@@ -180,7 +180,7 @@ add_libc_test(
libc.src.stdfix.uksqrtui
libc.src.__support.CPP.bit
libc.src.__support.fixed_point.fx_rep
- libc.src.__support.fixed_point.sqrt
+ libc.src.__support.math.sqrti
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.sqrt
)
diff --git a/libc/test/src/stdfix/uhksqrtus_test.cpp b/libc/test/src/stdfix/uhksqrtus_test.cpp
index a33297413980d..99829034a2cd0 100644
--- a/libc/test/src/stdfix/uhksqrtus_test.cpp
+++ b/libc/test/src/stdfix/uhksqrtus_test.cpp
@@ -8,11 +8,11 @@
#include "ISqrtTest.h"
-#include "src/__support/fixed_point/sqrt.h"
+#include "src/__support/math/sqrti.h"
#include "src/stdfix/uhksqrtus.h"
unsigned short accum uhksqrtus_fast(unsigned short x) {
- return LIBC_NAMESPACE::fixed_point::isqrt_fast(x);
+ return LIBC_NAMESPACE::math::sqrti_fast(x);
}
LIST_ISQRT_TESTS(US, unsigned short, LIBC_NAMESPACE::uhksqrtus);
diff --git a/libc/test/src/stdfix/uksqrtui_test.cpp b/libc/test/src/stdfix/uksqrtui_test.cpp
index c336d0ce1f617..b9299af781a2a 100644
--- a/libc/test/src/stdfix/uksqrtui_test.cpp
+++ b/libc/test/src/stdfix/uksqrtui_test.cpp
@@ -8,11 +8,11 @@
#include "ISqrtTest.h"
-#include "src/__support/fixed_point/sqrt.h"
+#include "src/__support/math/sqrti.h"
#include "src/stdfix/uksqrtui.h"
unsigned accum uksqrtui_fast(unsigned int x) {
- return LIBC_NAMESPACE::fixed_point::isqrt_fast(x);
+ return LIBC_NAMESPACE::math::sqrti_fast(x);
}
LIST_ISQRT_TESTS(UI, unsigned int, LIBC_NAMESPACE::uksqrtui);
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 346864d2276c0..206aa24944155 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1184,6 +1184,20 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_fixed_point_sqrt",
+ hdrs = ["src/__support/fixed_point/sqrt.h"],
+ deps = [
+ ":__support_cpp_bit",
+ ":__support_cpp_limits",
+ ":__support_cpp_type_traits",
+ ":__support_fixed_point",
+ ":__support_macros_attributes",
+ ":__support_macros_optimization",
+ ":llvm_libc_macros_stdfix_macros",
+ ],
+)
+
libc_support_library(
name = "__support_fputil_generic_fmod",
hdrs = ["src/__support/FPUtil/generic/FMod.h"],
@@ -2405,6 +2419,15 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sqrti",
+ hdrs = ["src/__support/math/sqrti.h"],
+ deps = [
+ ":__support_fixed_point_sqrt",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_asin_utils",
hdrs = ["src/__support/math/asin_utils.h"],
``````````
</details>
https://github.com/llvm/llvm-project/pull/177960
More information about the libc-commits
mailing list