[libc][NFC] Move the template implementation of integer_abs to __support.
This eliminates cross-header dependency from stdlib to string.
GitOrigin-RevId: b937908c371898555f676a64890ba07ee310afae
diff --git a/src/__support/CMakeLists.txt b/src/__support/CMakeLists.txt
index 32f538e..23ac214 100644
--- a/src/__support/CMakeLists.txt
+++ b/src/__support/CMakeLists.txt
@@ -4,3 +4,10 @@
common.h
sanitizer_annotations.h
)
+
+add_header_library(
+ integer_operations
+ HDRS
+ integer_operations.h
+)
+
diff --git a/src/stdlib/abs_utils.h b/src/__support/integer_operations.h
similarity index 80%
rename from src/stdlib/abs_utils.h
rename to src/__support/integer_operations.h
index c0943fe..733fb7e 100644
--- a/src/stdlib/abs_utils.h
+++ b/src/__support/integer_operations.h
@@ -9,9 +9,12 @@
#ifndef LLVM_LIBC_SRC_STDLIB_ABS_UTILS_H
#define LLVM_LIBC_SRC_STDLIB_ABS_UTILS_H
+#include "utils/CPP/TypeTraits.h"
+
namespace __llvm_libc {
-template <typename T> static inline T integer_abs(T n) {
+template <typename T>
+static inline cpp::EnableIfType<cpp::IsIntegral<T>::Value, T> integerAbs(T n) {
if (n < 0)
return -n;
return n;
diff --git a/src/stdlib/CMakeLists.txt b/src/stdlib/CMakeLists.txt
index a599d8a..60a380d 100644
--- a/src/stdlib/CMakeLists.txt
+++ b/src/stdlib/CMakeLists.txt
@@ -2,12 +2,6 @@
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()
-add_header_library(
- abs_utils
- HDRS
- abs_utils.h
-)
-
add_entrypoint_object(
_Exit
ALIAS
@@ -34,7 +28,7 @@
HDRS
abs.h
DEPENDS
- .abs_utils
+ libc.src.__support.integer_operations
)
add_entrypoint_object(
@@ -44,7 +38,7 @@
HDRS
labs.h
DEPENDS
- .abs_utils
+ libc.src.__support.integer_operations
)
add_entrypoint_object(
@@ -54,5 +48,5 @@
HDRS
llabs.h
DEPENDS
- .abs_utils
+ libc.src.__support.integer_operations
)
diff --git a/src/stdlib/abs.cpp b/src/stdlib/abs.cpp
index eba2b7d..d6111aa 100644
--- a/src/stdlib/abs.cpp
+++ b/src/stdlib/abs.cpp
@@ -8,13 +8,10 @@
#include "src/stdlib/abs.h"
#include "src/__support/common.h"
-#include "src/stdlib/abs_utils.h"
+#include "src/__support/integer_operations.h"
namespace __llvm_libc {
-LLVM_LIBC_FUNCTION(int, abs, (int n)) {
- // integer_abs from abs_utils.h.
- return integer_abs(n);
-}
+LLVM_LIBC_FUNCTION(int, abs, (int n)) { return integerAbs(n); }
} // namespace __llvm_libc
diff --git a/src/stdlib/labs.cpp b/src/stdlib/labs.cpp
index d2aa816..8dffc1e 100644
--- a/src/stdlib/labs.cpp
+++ b/src/stdlib/labs.cpp
@@ -8,13 +8,10 @@
#include "src/stdlib/labs.h"
#include "src/__support/common.h"
-#include "src/stdlib/abs_utils.h"
+#include "src/__support/integer_operations.h"
namespace __llvm_libc {
-LLVM_LIBC_FUNCTION(long, labs, (long n)) {
- // integer_abs from abs_utils.h.
- return integer_abs(n);
-}
+LLVM_LIBC_FUNCTION(long, labs, (long n)) { return integerAbs(n); }
} // namespace __llvm_libc
diff --git a/src/stdlib/llabs.cpp b/src/stdlib/llabs.cpp
index 7a83fcb..d74c7f4 100644
--- a/src/stdlib/llabs.cpp
+++ b/src/stdlib/llabs.cpp
@@ -8,13 +8,10 @@
#include "src/stdlib/llabs.h"
#include "src/__support/common.h"
-#include "src/stdlib/abs_utils.h"
+#include "src/__support/integer_operations.h"
namespace __llvm_libc {
-LLVM_LIBC_FUNCTION(long long, llabs, (long long n)) {
- // integer_abs from abs_utils.h.
- return integer_abs(n);
-}
+LLVM_LIBC_FUNCTION(long long, llabs, (long long n)) { return integerAbs(n); }
} // namespace __llvm_libc
diff --git a/src/string/CMakeLists.txt b/src/string/CMakeLists.txt
index 9dd8b6b..c31525a 100644
--- a/src/string/CMakeLists.txt
+++ b/src/string/CMakeLists.txt
@@ -73,7 +73,7 @@
HDRS
memmove.h
DEPENDS
- libc.src.stdlib.abs_utils
+ libc.src.__support.integer_operations
libc.src.string.memcpy
)
diff --git a/src/string/memmove.cpp b/src/string/memmove.cpp
index 0d4d3d1..ebbe464 100644
--- a/src/string/memmove.cpp
+++ b/src/string/memmove.cpp
@@ -7,8 +7,9 @@
//===----------------------------------------------------------------------===//
#include "src/string/memmove.h"
+
#include "src/__support/common.h"
-#include "src/stdlib/abs_utils.h"
+#include "src/__support/integer_operations.h"
#include "src/string/memcpy.h"
#include <stddef.h> // size_t, ptrdiff_t
@@ -32,14 +33,14 @@
const char *src_c = reinterpret_cast<const char *>(src);
// If the distance between src_c and dest_c is equal to or greater
- // than count (integer_abs(src_c - dest_c) >= count), they would not overlap.
+ // than count (integerAbs(src_c - dest_c) >= count), they would not overlap.
// e.g. greater equal overlapping
// [12345678] [12345678] [12345678]
// src_c: [_ab_____] [_ab_____] [_ab_____]
// dest_c:[_____yz_] [___yz___] [__yz____]
// Use memcpy if src_c and dest_c do not overlap.
- if (__llvm_libc::integer_abs(src_c - dest_c) >= static_cast<ptrdiff_t>(count))
+ if (__llvm_libc::integerAbs(src_c - dest_c) >= static_cast<ptrdiff_t>(count))
return __llvm_libc::memcpy(dest_c, src_c, count);
// Overlap cases.