| 1 |
#include <string.h> |
| 2 |
#include <stdint.h> |
| 3 |
#include <endian.h> |
| 4 |
|
| 5 |
void *memcpy(void *restrict dest, const void *restrict src, size_t n) |
| 6 |
{ |
| 7 |
#if defined(__wasm_bulk_memory__) |
| 8 |
if (n > BULK_MEMORY_THRESHOLD) |
| 9 |
return __builtin_memcpy(dest, src, n); |
| 10 |
#endif |
| 11 |
unsigned char *d = dest; |
| 12 |
const unsigned char *s = src; |
| 13 |
|
| 14 |
#ifdef __GNUC__ |
| 15 |
|
| 16 |
#if __BYTE_ORDER == __LITTLE_ENDIAN |
| 17 |
#define LS >> |
| 18 |
#define RS << |
| 19 |
#else |
| 20 |
#define LS << |
| 21 |
#define RS >> |
| 22 |
#endif |
| 23 |
|
| 24 |
typedef uint32_t __attribute__((__may_alias__)) u32; |
| 25 |
uint32_t w, x; |
| 26 |
|
| 27 |
for (; (uintptr_t)s % 4 && n; n--) *d++ = *s++; |
| 28 |
|
| 29 |
if ((uintptr_t)d % 4 == 0) { |
| 30 |
for (; n>=16; s+=16, d+=16, n-=16) { |
| 31 |
*(u32 *)(d+0) = *(u32 *)(s+0); |
| 32 |
*(u32 *)(d+4) = *(u32 *)(s+4); |
| 33 |
*(u32 *)(d+8) = *(u32 *)(s+8); |
| 34 |
*(u32 *)(d+12) = *(u32 *)(s+12); |
| 35 |
} |
| 36 |
if (n&8) { |
| 37 |
*(u32 *)(d+0) = *(u32 *)(s+0); |
| 38 |
*(u32 *)(d+4) = *(u32 *)(s+4); |
| 39 |
d += 8; s += 8; |
| 40 |
} |
| 41 |
if (n&4) { |
| 42 |
*(u32 *)(d+0) = *(u32 *)(s+0); |
| 43 |
d += 4; s += 4; |
| 44 |
} |
| 45 |
if (n&2) { |
| 46 |
*d++ = *s++; *d++ = *s++; |
| 47 |
} |
| 48 |
if (n&1) { |
| 49 |
*d = *s; |
| 50 |
} |
| 51 |
return dest; |
| 52 |
} |
| 53 |
|
| 54 |
if (n >= 32) switch ((uintptr_t)d % 4) { |
| 55 |
case 1: |
| 56 |
w = *(u32 *)s; |
| 57 |
*d++ = *s++; |
| 58 |
*d++ = *s++; |
| 59 |
*d++ = *s++; |
| 60 |
n -= 3; |
| 61 |
for (; n>=17; s+=16, d+=16, n-=16) { |
| 62 |
x = *(u32 *)(s+1); |
| 63 |
*(u32 *)(d+0) = (w LS 24) | (x RS 8); |
| 64 |
w = *(u32 *)(s+5); |
| 65 |
*(u32 *)(d+4) = (x LS 24) | (w RS 8); |
| 66 |
x = *(u32 *)(s+9); |
| 67 |
*(u32 *)(d+8) = (w LS 24) | (x RS 8); |
| 68 |
w = *(u32 *)(s+13); |
| 69 |
*(u32 *)(d+12) = (x LS 24) | (w RS 8); |
| 70 |
} |
| 71 |
break; |
| 72 |
case 2: |
| 73 |
w = *(u32 *)s; |
| 74 |
*d++ = *s++; |
| 75 |
*d++ = *s++; |
| 76 |
n -= 2; |
| 77 |
for (; n>=18; s+=16, d+=16, n-=16) { |
| 78 |
x = *(u32 *)(s+2); |
| 79 |
*(u32 *)(d+0) = (w LS 16) | (x RS 16); |
| 80 |
w = *(u32 *)(s+6); |
| 81 |
*(u32 *)(d+4) = (x LS 16) | (w RS 16); |
| 82 |
x = *(u32 *)(s+10); |
| 83 |
*(u32 *)(d+8) = (w LS 16) | (x RS 16); |
| 84 |
w = *(u32 *)(s+14); |
| 85 |
*(u32 *)(d+12) = (x LS 16) | (w RS 16); |
| 86 |
} |
| 87 |
break; |
| 88 |
case 3: |
| 89 |
w = *(u32 *)s; |
| 90 |
*d++ = *s++; |
| 91 |
n -= 1; |
| 92 |
for (; n>=19; s+=16, d+=16, n-=16) { |
| 93 |
x = *(u32 *)(s+3); |
| 94 |
*(u32 *)(d+0) = (w LS 8) | (x RS 24); |
| 95 |
w = *(u32 *)(s+7); |
| 96 |
*(u32 *)(d+4) = (x LS 8) | (w RS 24); |
| 97 |
x = *(u32 *)(s+11); |
| 98 |
*(u32 *)(d+8) = (w LS 8) | (x RS 24); |
| 99 |
w = *(u32 *)(s+15); |
| 100 |
*(u32 *)(d+12) = (x LS 8) | (w RS 24); |
| 101 |
} |
| 102 |
break; |
| 103 |
} |
| 104 |
if (n&16) { |
| 105 |
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 106 |
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 107 |
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 108 |
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 109 |
} |
| 110 |
if (n&8) { |
| 111 |
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 112 |
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 113 |
} |
| 114 |
if (n&4) { |
| 115 |
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 116 |
} |
| 117 |
if (n&2) { |
| 118 |
*d++ = *s++; *d++ = *s++; |
| 119 |
} |
| 120 |
if (n&1) { |
| 121 |
*d = *s; |
| 122 |
} |
| 123 |
return dest; |
| 124 |
#endif |
| 125 |
|
| 126 |
for (; n; n--) *d++ = *s++; |
| 127 |
return dest; |
| 128 |
} |