|
| 1 | +/* |
| 2 | + * https://git.musl-libc.org/cgit/musl/tree/src/string/memcpy.c?h=v1.2.5 |
| 3 | + * |
| 4 | + * This file has been copied from musl v1.2.5, which is licensed under the |
| 5 | + * following license: |
| 6 | + * |
| 7 | + * Copyright © 2005-2020 Rich Felker, et al. |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 10 | + * a copy of this software and associated documentation files (the |
| 11 | + * "Software"), to deal in the Software without restriction, including |
| 12 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 13 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 14 | + * permit persons to whom the Software is furnished to do so, subject to |
| 15 | + * the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be |
| 18 | + * included in all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 23 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 24 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 25 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 26 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <string.h> |
| 30 | +#include <stdint.h> |
| 31 | +#include <endian.h> |
| 32 | + |
| 33 | +void *memcpy(void *restrict dest, const void *restrict src, size_t n) |
| 34 | +{ |
| 35 | + unsigned char *d = dest; |
| 36 | + const unsigned char *s = src; |
| 37 | + |
| 38 | +#ifdef __GNUC__ |
| 39 | + |
| 40 | +#if __BYTE_ORDER == __LITTLE_ENDIAN |
| 41 | +#define LS >> |
| 42 | +#define RS << |
| 43 | +#else |
| 44 | +#define LS << |
| 45 | +#define RS >> |
| 46 | +#endif |
| 47 | + |
| 48 | + typedef uint32_t __attribute__((__may_alias__)) u32; |
| 49 | + uint32_t w, x; |
| 50 | + |
| 51 | + for (; (uintptr_t)s % 4 && n; n--) *d++ = *s++; |
| 52 | + |
| 53 | + if ((uintptr_t)d % 4 == 0) { |
| 54 | + for (; n>=16; s+=16, d+=16, n-=16) { |
| 55 | + *(u32 *)(d+0) = *(u32 *)(s+0); |
| 56 | + *(u32 *)(d+4) = *(u32 *)(s+4); |
| 57 | + *(u32 *)(d+8) = *(u32 *)(s+8); |
| 58 | + *(u32 *)(d+12) = *(u32 *)(s+12); |
| 59 | + } |
| 60 | + if (n&8) { |
| 61 | + *(u32 *)(d+0) = *(u32 *)(s+0); |
| 62 | + *(u32 *)(d+4) = *(u32 *)(s+4); |
| 63 | + d += 8; s += 8; |
| 64 | + } |
| 65 | + if (n&4) { |
| 66 | + *(u32 *)(d+0) = *(u32 *)(s+0); |
| 67 | + d += 4; s += 4; |
| 68 | + } |
| 69 | + if (n&2) { |
| 70 | + *d++ = *s++; *d++ = *s++; |
| 71 | + } |
| 72 | + if (n&1) { |
| 73 | + *d = *s; |
| 74 | + } |
| 75 | + return dest; |
| 76 | + } |
| 77 | + |
| 78 | + if (n >= 32) switch ((uintptr_t)d % 4) { |
| 79 | + case 1: |
| 80 | + w = *(u32 *)s; |
| 81 | + *d++ = *s++; |
| 82 | + *d++ = *s++; |
| 83 | + *d++ = *s++; |
| 84 | + n -= 3; |
| 85 | + for (; n>=17; s+=16, d+=16, n-=16) { |
| 86 | + x = *(u32 *)(s+1); |
| 87 | + *(u32 *)(d+0) = (w LS 24) | (x RS 8); |
| 88 | + w = *(u32 *)(s+5); |
| 89 | + *(u32 *)(d+4) = (x LS 24) | (w RS 8); |
| 90 | + x = *(u32 *)(s+9); |
| 91 | + *(u32 *)(d+8) = (w LS 24) | (x RS 8); |
| 92 | + w = *(u32 *)(s+13); |
| 93 | + *(u32 *)(d+12) = (x LS 24) | (w RS 8); |
| 94 | + } |
| 95 | + break; |
| 96 | + case 2: |
| 97 | + w = *(u32 *)s; |
| 98 | + *d++ = *s++; |
| 99 | + *d++ = *s++; |
| 100 | + n -= 2; |
| 101 | + for (; n>=18; s+=16, d+=16, n-=16) { |
| 102 | + x = *(u32 *)(s+2); |
| 103 | + *(u32 *)(d+0) = (w LS 16) | (x RS 16); |
| 104 | + w = *(u32 *)(s+6); |
| 105 | + *(u32 *)(d+4) = (x LS 16) | (w RS 16); |
| 106 | + x = *(u32 *)(s+10); |
| 107 | + *(u32 *)(d+8) = (w LS 16) | (x RS 16); |
| 108 | + w = *(u32 *)(s+14); |
| 109 | + *(u32 *)(d+12) = (x LS 16) | (w RS 16); |
| 110 | + } |
| 111 | + break; |
| 112 | + case 3: |
| 113 | + w = *(u32 *)s; |
| 114 | + *d++ = *s++; |
| 115 | + n -= 1; |
| 116 | + for (; n>=19; s+=16, d+=16, n-=16) { |
| 117 | + x = *(u32 *)(s+3); |
| 118 | + *(u32 *)(d+0) = (w LS 8) | (x RS 24); |
| 119 | + w = *(u32 *)(s+7); |
| 120 | + *(u32 *)(d+4) = (x LS 8) | (w RS 24); |
| 121 | + x = *(u32 *)(s+11); |
| 122 | + *(u32 *)(d+8) = (w LS 8) | (x RS 24); |
| 123 | + w = *(u32 *)(s+15); |
| 124 | + *(u32 *)(d+12) = (x LS 8) | (w RS 24); |
| 125 | + } |
| 126 | + break; |
| 127 | + } |
| 128 | + if (n&16) { |
| 129 | + *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 130 | + *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 131 | + *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 132 | + *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 133 | + } |
| 134 | + if (n&8) { |
| 135 | + *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 136 | + *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 137 | + } |
| 138 | + if (n&4) { |
| 139 | + *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++; |
| 140 | + } |
| 141 | + if (n&2) { |
| 142 | + *d++ = *s++; *d++ = *s++; |
| 143 | + } |
| 144 | + if (n&1) { |
| 145 | + *d = *s; |
| 146 | + } |
| 147 | + return dest; |
| 148 | +#endif |
| 149 | + |
| 150 | + for (; n; n--) *d++ = *s++; |
| 151 | + return dest; |
| 152 | +} |
0 commit comments