[444] | 1 | /* Copyright (c) 2017 SiFive Inc. All rights reserved. |
---|
| 2 | |
---|
| 3 | This copyrighted material is made available to anyone wishing to use, |
---|
| 4 | modify, copy, or redistribute it subject to the terms and conditions |
---|
| 5 | of the FreeBSD License. This program is distributed in the hope that |
---|
| 6 | it will be useful, but WITHOUT ANY WARRANTY expressed or implied, |
---|
| 7 | including the implied warranties of MERCHANTABILITY or FITNESS FOR |
---|
| 8 | A PARTICULAR PURPOSE. A copy of this license is available at |
---|
| 9 | http://www.opensource.org/licenses. |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | #include <string.h> |
---|
| 13 | #include <stdint.h> |
---|
| 14 | |
---|
| 15 | char *strcpy(char *dst, const char *src) |
---|
| 16 | { |
---|
| 17 | char *dst0 = dst; |
---|
| 18 | |
---|
| 19 | #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) |
---|
| 20 | int misaligned = ((uintptr_t)dst | (uintptr_t)src) & (sizeof (long) - 1); |
---|
| 21 | if (__builtin_expect(!misaligned, 1)) |
---|
| 22 | { |
---|
| 23 | long *ldst = (long *)dst; |
---|
| 24 | const long *lsrc = (const long *)src; |
---|
| 25 | |
---|
| 26 | while (!__libc_detect_null(*lsrc)) |
---|
| 27 | *ldst++ = *lsrc++; |
---|
| 28 | |
---|
| 29 | dst = (char *)ldst; |
---|
| 30 | src = (const char *)lsrc; |
---|
| 31 | |
---|
| 32 | char c0 = src[0]; |
---|
| 33 | char c1 = src[1]; |
---|
| 34 | char c2 = src[2]; |
---|
| 35 | if (!(*dst++ = c0)) return dst0; |
---|
| 36 | if (!(*dst++ = c1)) return dst0; |
---|
| 37 | char c3 = src[3]; |
---|
| 38 | if (!(*dst++ = c2)) return dst0; |
---|
| 39 | if (sizeof (long) == 4) goto out; |
---|
| 40 | char c4 = src[4]; |
---|
| 41 | if (!(*dst++ = c3)) return dst0; |
---|
| 42 | char c5 = src[5]; |
---|
| 43 | if (!(*dst++ = c4)) return dst0; |
---|
| 44 | char c6 = src[6]; |
---|
| 45 | if (!(*dst++ = c5)) return dst0; |
---|
| 46 | if (!(*dst++ = c6)) return dst0; |
---|
| 47 | |
---|
| 48 | out: |
---|
| 49 | *dst++ = 0; |
---|
| 50 | return dst0; |
---|
| 51 | } |
---|
| 52 | #endif /* not PREFER_SIZE_OVER_SPEED */ |
---|
| 53 | |
---|
| 54 | char ch; |
---|
| 55 | do |
---|
| 56 | { |
---|
| 57 | ch = *src; |
---|
| 58 | src++; |
---|
| 59 | dst++; |
---|
| 60 | *(dst - 1) = ch; |
---|
| 61 | } while (ch); |
---|
| 62 | |
---|
| 63 | return dst0; |
---|
| 64 | } |
---|