12423ec58SCheng Wang //===-- Implementation of memmove -----------------------------------------===//
22423ec58SCheng Wang //
32423ec58SCheng Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42423ec58SCheng Wang // See https://llvm.org/LICENSE.txt for license information.
52423ec58SCheng Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62423ec58SCheng Wang //
72423ec58SCheng Wang //===----------------------------------------------------------------------===//
82423ec58SCheng Wang
92423ec58SCheng Wang #include "src/string/memmove.h"
10b937908cSSiva Chandra Reddy
112423ec58SCheng Wang #include "src/__support/common.h"
12b937908cSSiva Chandra Reddy #include "src/__support/integer_operations.h"
13*83f9b13dSGuillaume Chatelet #include "src/string/memory_utils/elements.h"
142423ec58SCheng Wang #include <stddef.h> // size_t, ptrdiff_t
152423ec58SCheng Wang
162423ec58SCheng Wang namespace __llvm_libc {
172423ec58SCheng Wang
inline_memmove(char * dst,const char * src,size_t count)18*83f9b13dSGuillaume Chatelet static inline void inline_memmove(char *dst, const char *src, size_t count) {
19*83f9b13dSGuillaume Chatelet using namespace __llvm_libc::scalar;
20*83f9b13dSGuillaume Chatelet if (count == 0)
21*83f9b13dSGuillaume Chatelet return;
22*83f9b13dSGuillaume Chatelet if (count == 1)
23*83f9b13dSGuillaume Chatelet return move<_1>(dst, src);
24*83f9b13dSGuillaume Chatelet if (count <= 4)
25*83f9b13dSGuillaume Chatelet return move<HeadTail<_2>>(dst, src, count);
26*83f9b13dSGuillaume Chatelet if (count <= 8)
27*83f9b13dSGuillaume Chatelet return move<HeadTail<_4>>(dst, src, count);
28*83f9b13dSGuillaume Chatelet if (count <= 16)
29*83f9b13dSGuillaume Chatelet return move<HeadTail<_8>>(dst, src, count);
30*83f9b13dSGuillaume Chatelet if (count <= 32)
31*83f9b13dSGuillaume Chatelet return move<HeadTail<_16>>(dst, src, count);
32*83f9b13dSGuillaume Chatelet if (count <= 64)
33*83f9b13dSGuillaume Chatelet return move<HeadTail<_32>>(dst, src, count);
34*83f9b13dSGuillaume Chatelet if (count <= 128)
35*83f9b13dSGuillaume Chatelet return move<HeadTail<_64>>(dst, src, count);
36b659b789SCheng Wang
37*83f9b13dSGuillaume Chatelet using AlignedMoveLoop = Align<_16, Arg::Src>::Then<Loop<_64>>;
38*83f9b13dSGuillaume Chatelet if (dst < src)
39*83f9b13dSGuillaume Chatelet return move<AlignedMoveLoop>(dst, src, count);
40*83f9b13dSGuillaume Chatelet else if (dst > src)
41*83f9b13dSGuillaume Chatelet return move_backward<AlignedMoveLoop>(dst, src, count);
422423ec58SCheng Wang }
432423ec58SCheng Wang
442423ec58SCheng Wang LLVM_LIBC_FUNCTION(void *, memmove,
4535828287SCheng Wang (void *dst, const void *src, size_t count)) {
46*83f9b13dSGuillaume Chatelet inline_memmove(reinterpret_cast<char *>(dst),
47*83f9b13dSGuillaume Chatelet reinterpret_cast<const char *>(src), count);
4835828287SCheng Wang return dst;
492423ec58SCheng Wang }
502423ec58SCheng Wang
512423ec58SCheng Wang } // namespace __llvm_libc
52