1 /* Optimized strcpy/stpcpy implementation for PowerPC64/POWER8.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 # define FUNC_NAME __stpcpy
24 # define FUNC_NAME strcpy
27 /* Implements the function
29 char * [r3] strcpy (char *dest [r3], const char *src [r4])
33 char * [r3] stpcpy (char *dest [r3], const char *src [r4])
35 if USE_AS_STPCPY is defined.
37 The implementation uses unaligned doubleword access to avoid specialized
38 code paths depending of data alignment. Although recent powerpc64 uses
39 64K as default, the page cross handling assumes minimum page size of
43 EALIGN (FUNC_NAME, 4, 0)
44 li r0,0 /* Doubleword with null chars to use
47 /* Check if the [src]+15 will cross a 4K page by checking if the bit
48 indicating the page size changes. Basically:
50 uint64_t srcin = (uint64_t)src;
51 uint64_t ob = srcin & 4096UL;
52 uint64_t nb = (srcin+15UL) & 4096UL;
61 /* For short string (less than 16 bytes), just calculate its size as
62 strlen and issues a memcpy if null is found. */
64 ld r12,0(r7) /* Load doubleword from memory. */
65 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
66 cmpdi cr7,r10,0 /* If r10 == 0, no null's have been found. */
78 clrrdi r7,r4,3 /* Align the address to doubleword boundary. */
79 rlwinm r6,r4,3,26,28 /* Calculate padding. */
80 li r5,-1 /* MASK = 0xffffffffffffffff. */
81 ld r12,0(r7) /* Load doubleword from memory. */
82 #ifdef __LITTLE_ENDIAN__
85 srd r5,r5,r6 /* MASK = MASK >> padding. */
87 orc r9,r12,r5 /* Mask bits that are not part of the string. */
88 cmpb r10,r9,r0 /* Check for null bytes in DWORD1. */
89 cmpdi cr7,r10,0 /* If r10 == 0, no null's have been found. */
107 /* We checked for 24 - x bytes, with x being the source alignment
108 (0 <= x <= 16), and no zero has been found. Start the loop
109 copy with doubleword aligned address. */
115 /* Save the two doublewords readed from source and align the source
116 to 16 bytes for the loop. */
132 /* Load two doublewords, compare and merge in a
133 single register for speed. This is an attempt
134 to speed up the null-checking process for bigger strings. */
140 or r8,r9,r10 /* Merge everything in one doubleword. */
145 /* OK, one (or both) of the doublewords contains a null byte. Check
146 the first doubleword and decrement the address in case the first
147 doubleword really contains a null byte. */
154 /* The null byte must be in the second doubleword. Adjust the address
155 again and move the result of cmpb to r10 so we can calculate the
162 /* r10 has the output of the cmpb instruction, that is, it contains
163 0xff in the same position as the null byte in the original
164 doubleword from the string. Use that to calculate the length. */
168 #ifdef __LITTLE_ENDIAN__
169 addi r9, r10, -1 /* Form a mask from trailing zeros. */
171 popcntd r6, r9 /* Count the bits in the mask. */
173 cntlzd r6,r10 /* Count leading zeros before the match. */
176 srdi r6,r6,3 /* Convert leading/trailing zeros to bytes. */
177 add r8,r5,r6 /* Compute final length. */
179 /* stpcpy returns the dest address plus the size not counting the
183 addi r8,r8,1 /* Final '/0'. */
192 /* Handle copies of 0~31 bytes. */
195 /* At least 6 bytes to go. */
205 8: /* Copy 8 bytes. */
213 /* Copies 4~7 bytes. */
227 /* Copies 2~3 bytes. */
251 /* Handles copies of 0~8 bytes. */
260 #ifndef USE_AS_STPCPY
261 libc_hidden_builtin_def (strcpy)