#include #include #include void *memcpy(void *restrict dest, const void *restrict src, size_t n) { unsigned char *d = dest; const unsigned char *s = src; uint32_t w, x; for (; (uintptr_t)s % 8 && n; n--) *d++ = *s++; if (!n) return dest; if (n>=4) switch ((uintptr_t)d % 4) { case 0: if (!(uintptr_t)d % 8) for (; n>=8; s+=8, d+=8, n-=8) *(uint64_t *)d = *(uint64_t *)s; else for (; n>=4; s+=4, d+=4, n-=4) *(uint32_t *)d = *(uint32_t *)s; break; case 1: if (!(union { int i; char c; }){1}.c) break; w = *(uint32_t *)s; *d++ = *s++; *d++ = *s++; *d++ = *s++; n -= 3; for (; n>=17; s+=16, d+=16, n-=16) { x = *(uint32_t *)(s+1); *(uint32_t *)(d+0) = (w>>24) | (x<<8); w = *(uint32_t *)(s+5); *(uint32_t *)(d+4) = (x>>24) | (w<<8); x = *(uint32_t *)(s+9); *(uint32_t *)(d+8) = (w>>24) | (x<<8); w = *(uint32_t *)(s+13); *(uint32_t *)(d+12) = (x>>24) | (w<<8); } break; case 2: if (!(union { int i; char c; }){1}.c) break; w = *(uint32_t *)s; *d++ = *s++; *d++ = *s++; n -= 2; for (; n>=18; s+=16, d+=16, n-=16) { x = *(uint32_t *)(s+2); *(uint32_t *)(d+0) = (w>>16) | (x<<16); w = *(uint32_t *)(s+6); *(uint32_t *)(d+4) = (x>>16) | (w<<16); x = *(uint32_t *)(s+10); *(uint32_t *)(d+8) = (w>>16) | (x<<16); w = *(uint32_t *)(s+14); *(uint32_t *)(d+12) = (x>>16) | (w<<16); } break; case 3: if (!(union { int i; char c; }){1}.c) break; w = *(uint32_t *)s; *d++ = *s++; n -= 1; for (; n>=19; s+=16, d+=16, n-=16) { x = *(uint32_t *)(s+3); *(uint32_t *)(d+0) = (w>>8) | (x<<24); w = *(uint32_t *)(s+7); *(uint32_t *)(d+4) = (x>>8) | (w<<24); x = *(uint32_t *)(s+11); *(uint32_t *)(d+8) = (w>>8) | (x<<24); w = *(uint32_t *)(s+15); *(uint32_t *)(d+12) = (x>>8) | (w<<24); } break; } for (; n; n--) *d++ = *s++; return dest; }