[libc-commits] [libc] b38dda7 - [libc][NFC] Split memcmp implementations per platform

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Mon Jul 17 04:35:41 PDT 2023


Author: Guillaume Chatelet
Date: 2023-07-17T11:35:31Z
New Revision: b38dda74fa6387a00c840f6bdbdeba0a7bda7e61

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

LOG: [libc][NFC] Split memcmp implementations per platform

This is a follow up on D154800 and D154770 to make the code structure more principled and avoid too many nested #ifdef/#endif.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D155181

Added: 
    libc/src/string/memory_utils/riscv/memcmp_implementations.h

Modified: 
    libc/src/string/memory_utils/CMakeLists.txt
    libc/src/string/memory_utils/memcmp_implementations.h
    utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/src/string/memory_utils/CMakeLists.txt b/libc/src/string/memory_utils/CMakeLists.txt
index ecccd2d55be1c6..7580b086640373 100644
--- a/libc/src/string/memory_utils/CMakeLists.txt
+++ b/libc/src/string/memory_utils/CMakeLists.txt
@@ -19,6 +19,7 @@ add_header_library(
     op_generic.h
     op_x86.h
     riscv/bcmp_implementations.h
+    riscv/memcmp_implementations.h
     riscv/memcpy_implementations.h
     riscv/memset_implementations.h
     utils.h

diff  --git a/libc/src/string/memory_utils/memcmp_implementations.h b/libc/src/string/memory_utils/memcmp_implementations.h
index 87729a73cf9db5..e18e64072732c9 100644
--- a/libc/src/string/memory_utils/memcmp_implementations.h
+++ b/libc/src/string/memory_utils/memcmp_implementations.h
@@ -9,44 +9,36 @@
 #ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP_IMPLEMENTATIONS_H
 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP_IMPLEMENTATIONS_H
 
-#include "src/__support/common.h"
-#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY LIBC_LOOP_NOUNROLL
-#include "src/__support/macros/properties/architectures.h"
-#include "src/string/memory_utils/generic/aligned_access.h"
-#include "src/string/memory_utils/generic/byte_per_byte.h"
-#include "src/string/memory_utils/op_generic.h"
-#include "src/string/memory_utils/op_riscv.h"
-#include "src/string/memory_utils/utils.h" // CPtr MemcmpReturnType
+#include "src/__support/macros/config.h"                   // LIBC_INLINE
+#include "src/__support/macros/properties/architectures.h" // LIBC_TARGET_ARCH_IS_
+#include "src/string/memory_utils/utils.h"                 // Ptr, CPtr
 
 #include <stddef.h> // size_t
 
 #if defined(LIBC_TARGET_ARCH_IS_X86)
 #include "src/string/memory_utils/x86_64/memcmp_implementations.h"
+#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP inline_memcmp_x86
 #elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
 #include "src/string/memory_utils/aarch64/memcmp_implementations.h"
+#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP inline_memcmp_aarch64
+#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
+#include "src/string/memory_utils/riscv/memcmp_implementations.h"
+#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP inline_memcmp_riscv
+#else
+// We may want to error instead of defaulting to suboptimal implementation.
+#include "src/string/memory_utils/generic/byte_per_byte.h"
+#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP inline_memcmp_byte_per_byte
 #endif
 
 namespace __llvm_libc {
 
-LIBC_INLINE MemcmpReturnType inline_memcmp(CPtr p1, CPtr p2, size_t count) {
-#if defined(LIBC_TARGET_ARCH_IS_X86)
-  return inline_memcmp_x86(p1, p2, count);
-#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
-  return inline_memcmp_aarch64(p1, p2, count);
-#elif defined(LIBC_TARGET_ARCH_IS_RISCV64)
-  return inline_memcmp_aligned_access_64bit(p1, p2, count);
-#elif defined(LIBC_TARGET_ARCH_IS_RISCV32)
-  return inline_memcmp_aligned_access_32bit(p1, p2, count);
-#else
-  return inline_memcmp_byte_per_byte(p1, p2, count);
-#endif
-}
-
 LIBC_INLINE int inline_memcmp(const void *p1, const void *p2, size_t count) {
-  return static_cast<int>(inline_memcmp(reinterpret_cast<CPtr>(p1),
-                                        reinterpret_cast<CPtr>(p2), count));
+  return static_cast<int>(LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP(
+      reinterpret_cast<CPtr>(p1), reinterpret_cast<CPtr>(p2), count));
 }
 
 } // namespace __llvm_libc
 
+#undef LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP
+
 #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMCMP_IMPLEMENTATIONS_H

diff  --git a/libc/src/string/memory_utils/riscv/memcmp_implementations.h b/libc/src/string/memory_utils/riscv/memcmp_implementations.h
new file mode 100644
index 00000000000000..8408d0525054d0
--- /dev/null
+++ b/libc/src/string/memory_utils/riscv/memcmp_implementations.h
@@ -0,0 +1,33 @@
+//===-- Memcmp implementation for riscv -------------------------*- 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 LIBC_SRC_STRING_MEMORY_UTILS_RISCV_MEMCMP_IMPLEMENTATIONS_H
+#define LIBC_SRC_STRING_MEMORY_UTILS_RISCV_MEMCMP_IMPLEMENTATIONS_H
+
+#include "src/__support/macros/attributes.h"               // LIBC_INLINE
+#include "src/__support/macros/properties/architectures.h" // LIBC_TARGET_ARCH_IS_RISCV64
+#include "src/string/memory_utils/generic/aligned_access.h"
+#include "src/string/memory_utils/utils.h" // Ptr, CPtr
+
+#include <stddef.h> // size_t
+
+namespace __llvm_libc {
+
+[[maybe_unused]] LIBC_INLINE MemcmpReturnType
+inline_memcmp_riscv(CPtr p1, CPtr p2, size_t count) {
+#if defined(LIBC_TARGET_ARCH_IS_RISCV64)
+  return inline_memcmp_aligned_access_64bit(p1, p2, count);
+#elif defined(LIBC_TARGET_ARCH_IS_RISCV32)
+  return inline_memcmp_aligned_access_32bit(p1, p2, count);
+#else
+#error "Unimplemented"
+#endif
+}
+
+} // namespace __llvm_libc
+
+#endif // LIBC_SRC_STRING_MEMORY_UTILS_RISCV_MEMCMP_IMPLEMENTATIONS_H

diff  --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 696dda0b46e048..c5f3f7e87b72ab 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2023,6 +2023,7 @@ libc_support_library(
         "src/string/memory_utils/memmove_implementations.h",
         "src/string/memory_utils/memset_implementations.h",
         "src/string/memory_utils/riscv/bcmp_implementations.h",
+        "src/string/memory_utils/riscv/memcmp_implementations.h",
         "src/string/memory_utils/riscv/memcpy_implementations.h",
         "src/string/memory_utils/riscv/memset_implementations.h",
         "src/string/memory_utils/strcmp_implementations.h",


        


More information about the libc-commits mailing list