Skip to content
Snippets Groups Projects
Commit d60580eb authored by iabdalkader's avatar iabdalkader
Browse files

Optimize memcpy more

parent 81b2ddf5
No related branches found
No related tags found
No related merge requests found
...@@ -30,14 +30,36 @@ ...@@ -30,14 +30,36 @@
#define likely(x) __builtin_expect((x), 1) #define likely(x) __builtin_expect((x), 1)
void *memcpy(void *dst, const void *src, size_t n) { void *memcpy(void *dst, const void *src, size_t n) {
if (likely(!(n&3) && !((long)dst&3) && !((long)src&3))) { if (likely(!((long)dst&3) && !((long)src&3))) {
//aligned access, copy words //copy words from aligned pointers first
long *d = dst; long *d = dst;
const long *s = src; const long *s = src;
for (n=(n>>2); n; n--) { for (int i=(n>>2); i; i--) {
*d++ = *s++; *d++ = *s++;
} }
//copy remaining bytes
if (n&3) {
char *d8 = (char*)d;
const char *s8 =(char*) s;
switch (n&3) {
case 1:
*d8=*s8;
break;
case 2:
*d8++=*s8++;
*d8=*s8;
break;
case 3:
*d8++=*s8++;
*d8++=*s8++;
*d8=*s8;
break;
}
}
} else { } else {
//unaligned access, copy bytes //unaligned access, copy bytes
char *d = dst; char *d = dst;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment