aboutsummaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
authorGuillaume Chatelet <gchatelet@google.com>2023-07-12 16:40:46 +0000
committerGuillaume Chatelet <gchatelet@google.com>2023-07-13 10:30:38 +0000
commit8cc440b3e7bfc248a31ffad0a7b32dceb6c24470 (patch)
tree28525eac14f5ba7f2fc709a696c236e238dfb19b /libc
parent451af635519113bc8fe94852d7489f26485f6689 (diff)
downloadllvm-8cc440b3e7bfc248a31ffad0a7b32dceb6c24470.zip
llvm-8cc440b3e7bfc248a31ffad0a7b32dceb6c24470.tar.gz
llvm-8cc440b3e7bfc248a31ffad0a7b32dceb6c24470.tar.bz2
[libc][NFC] Split memcpy 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/D155099
Diffstat (limited to 'libc')
-rw-r--r--libc/src/__support/CPP/string.h2
-rw-r--r--libc/src/string/memory_utils/CMakeLists.txt1
-rw-r--r--libc/src/string/memory_utils/memcpy_implementations.h46
-rw-r--r--libc/src/string/memory_utils/riscv/memcpy_implementations.h33
4 files changed, 54 insertions, 28 deletions
diff --git a/libc/src/__support/CPP/string.h b/libc/src/__support/CPP/string.h
index fa73d53..a5611b9 100644
--- a/libc/src/__support/CPP/string.h
+++ b/libc/src/__support/CPP/string.h
@@ -56,7 +56,7 @@ public:
}
LIBC_INLINE string(const char *cstr, size_t count) {
resize(count);
- inline_memcpy((void *)buffer_, (const void *)cstr, count);
+ inline_memcpy(buffer_, cstr, count);
}
LIBC_INLINE string(const char *cstr)
: string(cstr, ::__llvm_libc::internal::string_length(cstr)) {}
diff --git a/libc/src/string/memory_utils/CMakeLists.txt b/libc/src/string/memory_utils/CMakeLists.txt
index d6fa1d9..60a2482 100644
--- a/libc/src/string/memory_utils/CMakeLists.txt
+++ b/libc/src/string/memory_utils/CMakeLists.txt
@@ -18,6 +18,7 @@ add_header_library(
op_generic.h
op_x86.h
riscv/bcmp_implementations.h
+ riscv/memcpy_implementations.h
utils.h
x86_64/bcmp_implementations.h
x86_64/memcmp_implementations.h
diff --git a/libc/src/string/memory_utils/memcpy_implementations.h b/libc/src/string/memory_utils/memcpy_implementations.h
index a7bfc9e..657fbac 100644
--- a/libc/src/string/memory_utils/memcpy_implementations.h
+++ b/libc/src/string/memory_utils/memcpy_implementations.h
@@ -9,45 +9,37 @@
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY_IMPLEMENTATIONS_H
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY_IMPLEMENTATIONS_H
-#include "src/__support/macros/config.h" // LIBC_INLINE
-#include "src/__support/macros/optimization.h" // 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_builtin.h"
-#include "src/string/memory_utils/utils.h"
+#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)
+#if defined(LIBC_COPT_MEMCPY_USE_EMBEDDED_TINY)
+#include "src/string/memory_utils/generic/byte_per_byte.h"
+#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY inline_memcpy_byte_per_byte
+#elif defined(LIBC_TARGET_ARCH_IS_X86)
#include "src/string/memory_utils/x86_64/memcpy_implementations.h"
+#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY \
+ inline_memcpy_x86_maybe_interpose_repmovsb
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
#include "src/string/memory_utils/aarch64/memcpy_implementations.h"
+#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY inline_memcpy_aarch64
+#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
+#include "src/string/memory_utils/riscv/memcpy_implementations.h"
+#define LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY inline_memcpy_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_MEMCPY inline_memcpy_byte_per_byte
#endif
namespace __llvm_libc {
-LIBC_INLINE void inline_memcpy(Ptr __restrict dst, CPtr __restrict src,
- size_t count) {
- using namespace __llvm_libc::builtin;
-#if defined(LIBC_COPT_MEMCPY_USE_EMBEDDED_TINY)
- return inline_memcpy_byte_per_byte(dst, src, count);
-#elif defined(LIBC_TARGET_ARCH_IS_X86)
- return inline_memcpy_x86_maybe_interpose_repmovsb(dst, src, count);
-#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
- return inline_memcpy_aarch64(dst, src, count);
-#elif defined(LIBC_TARGET_ARCH_IS_RISCV64)
- return inline_memcpy_aligned_access_64bit(dst, src, count);
-#elif defined(LIBC_TARGET_ARCH_IS_RISCV32)
- return inline_memcpy_aligned_access_32bit(dst, src, count);
-#else
- return inline_memcpy_byte_per_byte(dst, src, count);
-#endif
-}
-
LIBC_INLINE void inline_memcpy(void *__restrict dst, const void *__restrict src,
size_t count) {
- inline_memcpy(reinterpret_cast<Ptr>(dst), reinterpret_cast<CPtr>(src), count);
+ LIBC_SRC_STRING_MEMORY_UTILS_MEMCPY(reinterpret_cast<Ptr>(dst),
+ reinterpret_cast<CPtr>(src), count);
}
} // namespace __llvm_libc
diff --git a/libc/src/string/memory_utils/riscv/memcpy_implementations.h b/libc/src/string/memory_utils/riscv/memcpy_implementations.h
new file mode 100644
index 0000000..a633f34
--- /dev/null
+++ b/libc/src/string/memory_utils/riscv/memcpy_implementations.h
@@ -0,0 +1,33 @@
+//===-- Memcpy 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_MEMCPY_IMPLEMENTATIONS_H
+#define LIBC_SRC_STRING_MEMORY_UTILS_RISCV_MEMCPY_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 void
+inline_memcpy_riscv(Ptr __restrict dst, CPtr __restrict src, size_t count) {
+#if defined(LIBC_TARGET_ARCH_IS_RISCV64)
+ return inline_memcpy_aligned_access_64bit(dst, src, count);
+#elif defined(LIBC_TARGET_ARCH_IS_RISCV32)
+ return inline_memcpy_aligned_access_32bit(dst, src, count);
+#else
+#error "Unimplemented"
+#endif
+}
+
+} // namespace __llvm_libc
+
+#endif // LIBC_SRC_STRING_MEMORY_UTILS_RISCV_MEMCPY_IMPLEMENTATIONS_H