]> git.proxmox.com Git - wasi-libc.git/blame - libc-top-half/musl/src/string/memmove.c
bump version to 0.0~git20230113.4362b18-1~bpo12+pve1
[wasi-libc.git] / libc-top-half / musl / src / string / memmove.c
CommitLineData
320054e8
DG
1#include <string.h>
2#include <stdint.h>
3
4#ifdef __GNUC__
5typedef __attribute__((__may_alias__)) size_t WT;
6#define WS (sizeof(WT))
7#endif
8
9void *memmove(void *dest, const void *src, size_t n)
10{
75fdabe1 11#if defined(__wasm_bulk_memory__)
ba81b409
CS
12 if (n > BULK_MEMORY_THRESHOLD)
13 return __builtin_memmove(dest, src, n);
14#endif
320054e8
DG
15 char *d = dest;
16 const char *s = src;
17
18 if (d==s) return d;
19 if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n);
20
21 if (d<s) {
22#ifdef __GNUC__
23 if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
24 while ((uintptr_t)d % WS) {
25 if (!n--) return dest;
26 *d++ = *s++;
27 }
28 for (; n>=WS; n-=WS, d+=WS, s+=WS) *(WT *)d = *(WT *)s;
29 }
30#endif
31 for (; n; n--) *d++ = *s++;
32 } else {
33#ifdef __GNUC__
34 if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
35 while ((uintptr_t)(d+n) % WS) {
36 if (!n--) return dest;
37 d[n] = s[n];
38 }
39 while (n>=WS) n-=WS, *(WT *)(d+n) = *(WT *)(s+n);
40 }
41#endif
42 while (n) n--, d[n] = s[n];
43 }
44
45 return dest;
46}