diff options
author | Andrew Waterman <waterman@cs.berkeley.edu> | 2016-05-24 18:47:43 -0700 |
---|---|---|
committer | Andrew Waterman <waterman@cs.berkeley.edu> | 2016-05-24 18:47:43 -0700 |
commit | f70ee5576a07d455bdcc5275b7fc9d471090bf64 (patch) | |
tree | c93be8d5e5121993a25bb78ca91c3e1f8c2adcce | |
parent | e3afbd7c24f8d64eae3eff09a2074b73fd195de5 (diff) | |
download | pk-f70ee5576a07d455bdcc5275b7fc9d471090bf64.zip pk-f70ee5576a07d455bdcc5275b7fc9d471090bf64.tar.gz pk-f70ee5576a07d455bdcc5275b7fc9d471090bf64.tar.bz2 |
speed up memcpy for aligned bases but misaligned length
-rw-r--r-- | util/string.c | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/util/string.c b/util/string.c index e896379..41855c2 100644 --- a/util/string.c +++ b/util/string.c @@ -4,17 +4,20 @@ void* memcpy(void* dest, const void* src, size_t len) { - if ((((uintptr_t)dest | (uintptr_t)src | len) & (sizeof(uintptr_t)-1)) == 0) { - const uintptr_t* s = src; - uintptr_t *d = dest; - while (d < (uintptr_t*)(dest + len)) - *d++ = *s++; - } else { - const char* s = src; - char *d = dest; - while (d < (char*)(dest + len)) - *d++ = *s++; + const char* s = src; + char *d = dest; + + if ((((uintptr_t)dest | (uintptr_t)src) & (sizeof(uintptr_t)-1)) == 0) { + while ((void*)d < (dest + len - (sizeof(uintptr_t)-1))) { + *(uintptr_t*)d = *(const uintptr_t*)s; + d += sizeof(uintptr_t); + s += sizeof(uintptr_t); + } } + + while (d < (char*)(dest + len)) + *d++ = *s++; + return dest; } |