github.com/afumu/libc@v0.0.6/musl/src/string/memmove.c (about)

     1  #include <string.h>
     2  #include <stdint.h>
     3  
     4  #ifdef __GNUC__
     5  typedef __attribute__((__may_alias__)) size_t WT;
     6  #define WS (sizeof(WT))
     7  #endif
     8  
     9  void *memmove(void *dest, const void *src, size_t n)
    10  {
    11  	char *d = dest;
    12  	const char *s = src;
    13  
    14  	if (d==s) return d;
    15  	if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n);
    16  
    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++;
    23  			}
    24  			for (; n>=WS; n-=WS, d+=WS, s+=WS) *(WT *)d = *(WT *)s;
    25  		}
    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];
    34  			}
    35  			while (n>=WS) n-=WS, *(WT *)(d+n) = *(WT *)(s+n);
    36  		}
    37  #endif
    38  		while (n) n--, d[n] = s[n];
    39  	}
    40  
    41  	return dest;
    42  }