[libc-commits] [libc] b937908 - [libc][NFC] Move the template implementation of integer_abs to __support.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Thu Mar 11 20:21:56 PST 2021


Author: Siva Chandra Reddy
Date: 2021-03-11T20:20:18-08:00
New Revision: b937908c371898555f676a64890ba07ee310afae

URL: https://github.com/llvm/llvm-project/commit/b937908c371898555f676a64890ba07ee310afae
DIFF: https://github.com/llvm/llvm-project/commit/b937908c371898555f676a64890ba07ee310afae.diff

LOG: [libc][NFC] Move the template implementation of integer_abs to __support.

This eliminates cross-header dependency from stdlib to string.

Added: 
    libc/src/__support/integer_operations.h

Modified: 
    libc/src/__support/CMakeLists.txt
    libc/src/stdlib/CMakeLists.txt
    libc/src/stdlib/abs.cpp
    libc/src/stdlib/labs.cpp
    libc/src/stdlib/llabs.cpp
    libc/src/string/CMakeLists.txt
    libc/src/string/memmove.cpp

Removed: 
    libc/src/stdlib/abs_utils.h


################################################################################
diff  --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 32f538e02a5e4..23ac214395241 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -4,3 +4,10 @@ add_header_library(
     common.h
     sanitizer_annotations.h
 )
+
+add_header_library(
+  integer_operations
+  HDRS
+    integer_operations.h
+)
+

diff  --git a/libc/src/stdlib/abs_utils.h b/libc/src/__support/integer_operations.h
similarity index 81%
rename from libc/src/stdlib/abs_utils.h
rename to libc/src/__support/integer_operations.h
index c0943fe0ee695..733fb7ed9ce91 100644
--- a/libc/src/stdlib/abs_utils.h
+++ b/libc/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/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index a599d8a590653..60a380d1051e9 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -2,12 +2,6 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
   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 @@ add_entrypoint_object(
   HDRS
     abs.h
   DEPENDS
-    .abs_utils
+    libc.src.__support.integer_operations
 )
 
 add_entrypoint_object(
@@ -44,7 +38,7 @@ add_entrypoint_object(
   HDRS
     labs.h
   DEPENDS
-    .abs_utils
+    libc.src.__support.integer_operations
 )
 
 add_entrypoint_object(
@@ -54,5 +48,5 @@ add_entrypoint_object(
   HDRS
     llabs.h
   DEPENDS
-    .abs_utils
+    libc.src.__support.integer_operations
 )

diff  --git a/libc/src/stdlib/abs.cpp b/libc/src/stdlib/abs.cpp
index eba2b7d47db38..d6111aae75414 100644
--- a/libc/src/stdlib/abs.cpp
+++ b/libc/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/libc/src/stdlib/labs.cpp b/libc/src/stdlib/labs.cpp
index d2aa8164ea981..8dffc1ef2eb24 100644
--- a/libc/src/stdlib/labs.cpp
+++ b/libc/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/libc/src/stdlib/llabs.cpp b/libc/src/stdlib/llabs.cpp
index 7a83fcb77f53e..d74c7f4b2091c 100644
--- a/libc/src/stdlib/llabs.cpp
+++ b/libc/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/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 9dd8b6b1be245..c31525ad62088 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -73,7 +73,7 @@ add_entrypoint_object(
   HDRS
     memmove.h
   DEPENDS
-    libc.src.stdlib.abs_utils
+    libc.src.__support.integer_operations
     libc.src.string.memcpy
 )
 

diff  --git a/libc/src/string/memmove.cpp b/libc/src/string/memmove.cpp
index 0d4d3d1748397..ebbe4643e7da6 100644
--- a/libc/src/string/memmove.cpp
+++ b/libc/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, ptr
diff _t
 
@@ -32,14 +33,14 @@ LLVM_LIBC_FUNCTION(void *, memmove,
   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<ptr
diff _t>(count))
+  if (__llvm_libc::integerAbs(src_c - dest_c) >= static_cast<ptr
diff _t>(count))
     return __llvm_libc::memcpy(dest_c, src_c, count);
 
   // Overlap cases.


        


More information about the libc-commits mailing list