]> git.proxmox.com Git - wasi-libc.git/blame - libc-top-half/musl/src/string/memmove.c
Use bulk memory opcodes when possible
[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
CS
11#if defined(__wasm_bulk_memory__)
12 return __builtin_memmove(dest, src, n);
13#else
320054e8
DG
14 char *d = dest;
15 const char *s = src;
16
17 if (d==s) return d;
18 if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n);
19
20 if (d<s) {
21#ifdef __GNUC__
22 if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
23 while ((uintptr_t)d % WS) {
24 if (!n--) return dest;
25 *d++ = *s++;
26 }
27 for (; n>=WS; n-=WS, d+=WS, s+=WS) *(WT *)d = *(WT *)s;
28 }
29#endif
30 for (; n; n--) *d++ = *s++;
31 } else {
32#ifdef __GNUC__
33 if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
34 while ((uintptr_t)(d+n) % WS) {
35 if (!n--) return dest;
36 d[n] = s[n];
37 }
38 while (n>=WS) n-=WS, *(WT *)(d+n) = *(WT *)(s+n);
39 }
40#endif
41 while (n) n--, d[n] = s[n];
42 }
43
44 return dest;
75fdabe1 45#endif // __wasm_bulk_memory__
320054e8 46}