|
| 1 | +//===-- Memcpy implementation for arm ---------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H |
| 9 | +#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H |
| 10 | + |
| 11 | +#include "src/__support/macros/attributes.h" // LIBC_INLINE |
| 12 | +#include "src/__support/macros/optimization.h" // LIBC_LOOP_NOUNROLL |
| 13 | +#include "src/string/memory_utils/utils.h" // memcpy_inline, distance_to_align |
| 14 | + |
| 15 | +#include <stddef.h> // size_t |
| 16 | + |
| 17 | +// https://libc.llvm.org/compiler_support.html |
| 18 | +// Support for [[likely]] / [[unlikely]] |
| 19 | +// [X] GCC 12.2 |
| 20 | +// [X] Clang 12 |
| 21 | +// [ ] Clang 11 |
| 22 | +#define LIBC_ATTR_LIKELY [[likely]] |
| 23 | +#define LIBC_ATTR_UNLIKELY [[unlikely]] |
| 24 | + |
| 25 | +#if defined(LIBC_COMPILER_IS_CLANG) |
| 26 | +#if LIBC_COMPILER_CLANG_VER < 1200 |
| 27 | +#undef LIBC_ATTR_LIKELY |
| 28 | +#undef LIBC_ATTR_UNLIKELY |
| 29 | +#define LIBC_ATTR_LIKELY |
| 30 | +#define LIBC_ATTR_UNLIKELY |
| 31 | +#endif |
| 32 | +#endif |
| 33 | + |
| 34 | +namespace LIBC_NAMESPACE_DECL { |
| 35 | + |
| 36 | +namespace { |
| 37 | + |
| 38 | +LIBC_INLINE_VAR constexpr size_t kWordSize = sizeof(uint32_t); |
| 39 | + |
| 40 | +enum Strategy { |
| 41 | + ForceWordLdStChain, |
| 42 | + AssumeWordAligned, |
| 43 | + AssumeUnaligned, |
| 44 | +}; |
| 45 | + |
| 46 | +template <size_t bytes, Strategy strategy = AssumeUnaligned> |
| 47 | +LIBC_INLINE void copy_and_bump_pointers(Ptr &dst, CPtr &src) { |
| 48 | + if constexpr (strategy == AssumeUnaligned) { |
| 49 | + memcpy_inline<bytes>(assume_aligned<1>(dst), assume_aligned<1>(src)); |
| 50 | + } else if constexpr (strategy == AssumeWordAligned) { |
| 51 | + static_assert(bytes >= kWordSize); |
| 52 | + memcpy_inline<bytes>(assume_aligned<kWordSize>(dst), |
| 53 | + assume_aligned<kWordSize>(src)); |
| 54 | + } else if constexpr (strategy == ForceWordLdStChain) { |
| 55 | + // We restrict loads/stores to 4 byte to prevent the use of load/store |
| 56 | + // multiple (LDM, STM) and load/store double (LDRD, STRD). First, they may |
| 57 | + // fault (see notes below) and second, they use more registers which in turn |
| 58 | + // adds push/pop instructions in the hot path. |
| 59 | + static_assert((bytes % kWordSize == 0) && (bytes >= kWordSize)); |
| 60 | + LIBC_LOOP_UNROLL |
| 61 | + for (size_t i = 0; i < bytes / kWordSize; ++i) { |
| 62 | + const size_t offset = i * kWordSize; |
| 63 | + memcpy_inline<kWordSize>(dst + offset, src + offset); |
| 64 | + } |
| 65 | + } |
| 66 | + // In the 1, 2, 4 byte copy case, the compiler can fold pointer offsetting |
| 67 | + // into the load/store instructions. |
| 68 | + // e.g., |
| 69 | + // ldrb r3, [r1], #1 |
| 70 | + // strb r3, [r0], #1 |
| 71 | + dst += bytes; |
| 72 | + src += bytes; |
| 73 | +} |
| 74 | + |
| 75 | +LIBC_INLINE void copy_bytes_and_bump_pointers(Ptr &dst, CPtr &src, |
| 76 | + const size_t size) { |
| 77 | + LIBC_LOOP_NOUNROLL |
| 78 | + for (size_t i = 0; i < size; ++i) |
| 79 | + *dst++ = *src++; |
| 80 | +} |
| 81 | + |
| 82 | +template <size_t block_size, Strategy strategy> |
| 83 | +LIBC_INLINE void copy_blocks_and_update_args(Ptr &dst, CPtr &src, |
| 84 | + size_t &size) { |
| 85 | + LIBC_LOOP_NOUNROLL |
| 86 | + for (size_t i = 0; i < size / block_size; ++i) |
| 87 | + copy_and_bump_pointers<block_size, strategy>(dst, src); |
| 88 | + // Update `size` once at the end instead of once per iteration. |
| 89 | + size %= block_size; |
| 90 | +} |
| 91 | + |
| 92 | +LIBC_INLINE CPtr bitwise_or(CPtr a, CPtr b) { |
| 93 | + return cpp::bit_cast<CPtr>(cpp::bit_cast<uintptr_t>(a) | |
| 94 | + cpp::bit_cast<uintptr_t>(b)); |
| 95 | +} |
| 96 | + |
| 97 | +LIBC_INLINE auto misaligned(CPtr a) { |
| 98 | + return distance_to_align_down<kWordSize>(a); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace |
| 102 | + |
| 103 | +// Implementation for Cortex-M0, M0+, M1. |
| 104 | +// Notes: |
| 105 | +// - It compiles down to 196 bytes, but 220 bytes when used through `memcpy` |
| 106 | +// that also needs to return the `dst` ptr. |
| 107 | +// - These cores do not allow for unaligned loads/stores. |
| 108 | +// - When `src` and `dst` are coaligned, we start by aligning them and perform |
| 109 | +// bulk copies. We let the compiler know the pointers are aligned so it can |
| 110 | +// use load/store multiple (LDM, STM). This significantly increase throughput |
| 111 | +// but it also requires more registers and push/pop instructions. This impacts |
| 112 | +// latency for small size copies. |
| 113 | +// - When `src` and `dst` are misaligned, we align `dst` and recompose words |
| 114 | +// using multiple aligned loads. `load_aligned` takes care of endianness |
| 115 | +// issues. |
| 116 | +[[maybe_unused]] LIBC_INLINE void inline_memcpy_arm_low_end(Ptr dst, CPtr src, |
| 117 | + size_t size) { |
| 118 | + if (size >= 8) { |
| 119 | + if (const size_t offset = distance_to_align_up<kWordSize>(dst)) |
| 120 | + LIBC_ATTR_UNLIKELY { |
| 121 | + copy_bytes_and_bump_pointers(dst, src, offset); |
| 122 | + size -= offset; |
| 123 | + } |
| 124 | + const auto src_alignment = distance_to_align_down<kWordSize>(src); |
| 125 | + if (src_alignment == 0) |
| 126 | + LIBC_ATTR_LIKELY { |
| 127 | + // Both `src` and `dst` are now word-aligned. |
| 128 | + copy_blocks_and_update_args<64, AssumeWordAligned>(dst, src, size); |
| 129 | + copy_blocks_and_update_args<16, AssumeWordAligned>(dst, src, size); |
| 130 | + copy_blocks_and_update_args<4, AssumeWordAligned>(dst, src, size); |
| 131 | + } |
| 132 | + else { |
| 133 | + // `dst` is aligned but `src` is not. |
| 134 | + LIBC_LOOP_NOUNROLL |
| 135 | + while (size >= kWordSize) { |
| 136 | + // Recompose word from multiple loads depending on the alignment. |
| 137 | + const uint32_t value = |
| 138 | + src_alignment == 2 |
| 139 | + ? load_aligned<uint32_t, uint16_t, uint16_t>(src) |
| 140 | + : load_aligned<uint32_t, uint8_t, uint16_t, uint8_t>(src); |
| 141 | + memcpy_inline<kWordSize>(assume_aligned<kWordSize>(dst), &value); |
| 142 | + dst += kWordSize; |
| 143 | + src += kWordSize; |
| 144 | + size -= kWordSize; |
| 145 | + } |
| 146 | + } |
| 147 | + // Up to 3 bytes may still need to be copied. |
| 148 | + // Handling them with the slow loop below. |
| 149 | + } |
| 150 | + copy_bytes_and_bump_pointers(dst, src, size); |
| 151 | +} |
| 152 | + |
| 153 | +// Implementation for Cortex-M3, M4, M7, M23, M33, M35P, M52 with hardware |
| 154 | +// support for unaligned loads and stores. |
| 155 | +// Notes: |
| 156 | +// - It compiles down to 266 bytes. |
| 157 | +// - `dst` and `src` are not `__restrict` to prevent the compiler from |
| 158 | +// reordering loads/stores. |
| 159 | +// - We keep state variables to a strict minimum to keep everything in the free |
| 160 | +// registers and prevent costly push / pop. |
| 161 | +// - If unaligned single loads/stores to normal memory are supported, unaligned |
| 162 | +// accesses for load/store multiple (LDM, STM) and load/store double (LDRD, |
| 163 | +// STRD) instructions are generally not supported and will still fault so we |
| 164 | +// make sure to restrict unrolling to word loads/stores. |
| 165 | +[[maybe_unused]] LIBC_INLINE void inline_memcpy_arm_mid_end(Ptr dst, CPtr src, |
| 166 | + size_t size) { |
| 167 | + if (misaligned(bitwise_or(src, dst))) |
| 168 | + LIBC_ATTR_UNLIKELY { |
| 169 | + if (size < 8) |
| 170 | + LIBC_ATTR_UNLIKELY { |
| 171 | + if (size & 1) |
| 172 | + copy_and_bump_pointers<1>(dst, src); |
| 173 | + if (size & 2) |
| 174 | + copy_and_bump_pointers<2>(dst, src); |
| 175 | + if (size & 4) |
| 176 | + copy_and_bump_pointers<4>(dst, src); |
| 177 | + return; |
| 178 | + } |
| 179 | + if (misaligned(src)) |
| 180 | + LIBC_ATTR_UNLIKELY { |
| 181 | + const size_t offset = distance_to_align_up<kWordSize>(dst); |
| 182 | + if (offset & 1) |
| 183 | + copy_and_bump_pointers<1>(dst, src); |
| 184 | + if (offset & 2) |
| 185 | + copy_and_bump_pointers<2>(dst, src); |
| 186 | + size -= offset; |
| 187 | + } |
| 188 | + } |
| 189 | + copy_blocks_and_update_args<64, ForceWordLdStChain>(dst, src, size); |
| 190 | + copy_blocks_and_update_args<16, ForceWordLdStChain>(dst, src, size); |
| 191 | + copy_blocks_and_update_args<4, AssumeUnaligned>(dst, src, size); |
| 192 | + if (size & 1) |
| 193 | + copy_and_bump_pointers<1>(dst, src); |
| 194 | + if (size & 2) |
| 195 | + LIBC_ATTR_UNLIKELY |
| 196 | + copy_and_bump_pointers<2>(dst, src); |
| 197 | +} |
| 198 | + |
| 199 | +[[maybe_unused]] LIBC_INLINE void inline_memcpy_arm(void *__restrict dst_, |
| 200 | + const void *__restrict src_, |
| 201 | + size_t size) { |
| 202 | + Ptr dst = cpp::bit_cast<Ptr>(dst_); |
| 203 | + CPtr src = cpp::bit_cast<CPtr>(src_); |
| 204 | +#ifdef __ARM_FEATURE_UNALIGNED |
| 205 | + return inline_memcpy_arm_mid_end(dst, src, size); |
| 206 | +#else |
| 207 | + return inline_memcpy_arm_low_end(dst, src, size); |
| 208 | +#endif |
| 209 | +} |
| 210 | + |
| 211 | +} // namespace LIBC_NAMESPACE_DECL |
| 212 | + |
| 213 | +// Cleanup local macros |
| 214 | +#undef LIBC_ATTR_LIKELY |
| 215 | +#undef LIBC_ATTR_UNLIKELY |
| 216 | + |
| 217 | +#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H |
0 commit comments