1 //===-- Implementation of memmove -----------------------------------------===//
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 
9 #include "src/string/memmove.h"
10 
11 #include "src/__support/common.h"
12 #include "src/__support/integer_operations.h"
13 #include "src/string/memory_utils/elements.h"
14 #include <stddef.h> // size_t, ptrdiff_t
15 
16 namespace __llvm_libc {
17 
inline_memmove(char * dst,const char * src,size_t count)18 static inline void inline_memmove(char *dst, const char *src, size_t count) {
19   using namespace __llvm_libc::scalar;
20   if (count == 0)
21     return;
22   if (count == 1)
23     return move<_1>(dst, src);
24   if (count <= 4)
25     return move<HeadTail<_2>>(dst, src, count);
26   if (count <= 8)
27     return move<HeadTail<_4>>(dst, src, count);
28   if (count <= 16)
29     return move<HeadTail<_8>>(dst, src, count);
30   if (count <= 32)
31     return move<HeadTail<_16>>(dst, src, count);
32   if (count <= 64)
33     return move<HeadTail<_32>>(dst, src, count);
34   if (count <= 128)
35     return move<HeadTail<_64>>(dst, src, count);
36 
37   using AlignedMoveLoop = Align<_16, Arg::Src>::Then<Loop<_64>>;
38   if (dst < src)
39     return move<AlignedMoveLoop>(dst, src, count);
40   else if (dst > src)
41     return move_backward<AlignedMoveLoop>(dst, src, count);
42 }
43 
44 LLVM_LIBC_FUNCTION(void *, memmove,
45                    (void *dst, const void *src, size_t count)) {
46   inline_memmove(reinterpret_cast<char *>(dst),
47                  reinterpret_cast<const char *>(src), count);
48   return dst;
49 }
50 
51 } // namespace __llvm_libc
52