fix failure to flush stderr when fflush(0) is called
[musl.git] / src / string / memmove.c
blob5dc9cdb924218cb10f284d013984797e03fd4e19
1 #include <string.h>
2 #include <stdint.h>
4 #ifdef __GNUC__
5 typedef __attribute__((__may_alias__)) size_t WT;
6 #define WS (sizeof(WT))
7 #endif
9 void *memmove(void *dest, const void *src, size_t n)
11 char *d = dest;
12 const char *s = src;
14 if (d==s) return d;
15 if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n);
17 if (d<s) {
18 #ifdef __GNUC__
19 if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
20 while ((uintptr_t)d % WS) {
21 if (!n--) return dest;
22 *d++ = *s++;
24 for (; n>=WS; n-=WS, d+=WS, s+=WS) *(WT *)d = *(WT *)s;
26 #endif
27 for (; n; n--) *d++ = *s++;
28 } else {
29 #ifdef __GNUC__
30 if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
31 while ((uintptr_t)(d+n) % WS) {
32 if (!n--) return dest;
33 d[n] = s[n];
35 while (n>=WS) n-=WS, *(WT *)(d+n) = *(WT *)(s+n);
37 #endif
38 while (n) n--, d[n] = s[n];
41 return dest;