1 /* Optimized strcpy/stpcpy implementation for PowerPC64/POWER8.
2 Copyright (C) 2015-2019 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 <https://www.gnu.org/licenses/>. */
23 # define FUNC_NAME __stpcpy
25 # define FUNC_NAME STPCPY
29 # define FUNC_NAME strcpy
31 # define FUNC_NAME STRCPY
33 #endif /* !USE_AS_STPCPY */
35 /* Implements the function
37 char * [r3] strcpy (char *dest [r3], const char *src [r4])
41 char * [r3] stpcpy (char *dest [r3], const char *src [r4])
43 if USE_AS_STPCPY is defined.
45 The implementation uses unaligned doubleword access to avoid specialized
46 code paths depending of data alignment. Although recent powerpc64 uses
47 64K as default, the page cross handling assumes minimum page size of
51 ENTRY_TOCLESS (FUNC_NAME, 4)
52 li r0,0 /* Doubleword with null chars to use
55 /* Check if the [src]+15 will cross a 4K page by checking if the bit
56 indicating the page size changes. Basically:
58 uint64_t srcin = (uint64_t)src;
59 uint64_t ob = srcin & 4096UL;
60 uint64_t nb = (srcin+15UL) & 4096UL;
69 /* For short string (less than 16 bytes), just calculate its size as
70 strlen and issues a memcpy if null is found. */
72 ld r12,0(r7) /* Load doubleword from memory. */
73 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
74 cmpdi cr7,r10,0 /* If r10 == 0, no null's have been found. */
86 clrrdi r7,r4,3 /* Align the address to doubleword boundary. */
87 rlwinm r6,r4,3,26,28 /* Calculate padding. */
88 li r5,-1 /* MASK = 0xffffffffffffffff. */
89 ld r12,0(r7) /* Load doubleword from memory. */
90 #ifdef __LITTLE_ENDIAN__
93 srd r5,r5,r6 /* MASK = MASK >> padding. */
95 orc r9,r12,r5 /* Mask bits that are not part of the string. */
96 cmpb r10,r9,r0 /* Check for null bytes in DWORD1. */
97 cmpdi cr7,r10,0 /* If r10 == 0, no null's have been found. */
115 /* We checked for 24 - x bytes, with x being the source alignment
116 (0 <= x <= 16), and no zero has been found. Start the loop
117 copy with doubleword aligned address. */
123 /* Save the two doublewords read from source and align the source
124 to 16 bytes for the loop. */
132 /* Source is adjusted to 16B alignment and destination r11 is
133 also moved based on that adjustment. Now check if r11 is
134 also 16B aligned to move to vectorized loop. */
138 /* Prepare for the loop. */
139 subf r4, r9, r4 /* Adjust r4 based on alignment. */
140 li r7, 16 /* Load required offsets. */
145 /* Are we 64-byte aligned? If so, jump to the vectorized loop.
146 Else copy 16B till r4 is 64B aligned. */
150 lvx v6, 0, r4 /* Load 16 bytes from memory. */
151 vcmpequb. v5, v0, v6 /* Check for null. */
153 stvx v6, 0, r11 /* Store 16 bytes. */
154 addi r4, r4, 16 /* Increment the address. */
177 lvx v1, r4, r0 /* Load 4 quadwords. */
181 vminub v5, v1, v2 /* Compare and merge into one VR for speed. */
184 vcmpequb. v7, v7, v0 /* Check for NULLs. */
185 bne cr6, L(qw_loop_done)
186 stvx v1, r11, r0 /* Store 4 quadwords. */
190 addi r4, r4, 64 /* Adjust address for the next iteration. */
191 addi r11, r11, 64 /* Adjust address for the next iteration. */
193 lvx v1, r4, r0 /* Load 4 quadwords. */
197 vminub v5, v1, v2 /* Compare and merge into one VR for speed. */
200 vcmpequb. v7, v7, v0 /* Check for NULLs. */
201 bne cr6, L(qw_loop_done)
202 stvx v1, r11, r0 /* Store 4 quadwords. */
206 addi r4, r4, 64 /* Adjust address for the next iteration. */
207 addi r11, r11, 64 /* Adjust address for the next iteration. */
209 lvx v1, r4, r0 /* Load 4 quadwords. */
213 vminub v5, v1, v2 /* Compare and merge into one VR for speed. */
216 vcmpequb. v7, v7, v0 /* Check for NULLs. */
217 bne cr6, L(qw_loop_done)
218 stvx v1, r11, r0 /* Store 4 quadwords. */
222 addi r4, r4, 64 /* Adjust address for the next iteration. */
223 addi r11, r11, 64 /* Adjust address for the next iteration. */
228 /* Null found in one of the 4 loads. */
232 /* Not on the first 16B, So store it. */
239 /* Not on the second 16B, So store it. */
246 /* Not on the third 16B, So store it. */
255 /* Move the result to GPR. */
256 #ifdef __LITTLE_ENDIAN__
262 /* Check for null in the first 8 bytes. */
266 /* Null found in second doubleword. */
267 #ifdef __LITTLE_ENDIAN__
283 /* Load two doublewords, compare and merge in a
284 single register for speed. This is an attempt
285 to speed up the null-checking process for bigger strings. */
291 or r8,r9,r10 /* Merge everything in one doubleword. */
296 /* OK, one (or both) of the doublewords contains a null byte. Check
297 the first doubleword and decrement the address in case the first
298 doubleword really contains a null byte. */
305 /* The null byte must be in the second doubleword. Adjust the address
306 again and move the result of cmpb to r10 so we can calculate the
313 /* r10 has the output of the cmpb instruction, that is, it contains
314 0xff in the same position as the null byte in the original
315 doubleword from the string. Use that to calculate the length. */
319 #ifdef __LITTLE_ENDIAN__
320 addi r9, r10, -1 /* Form a mask from trailing zeros. */
322 popcntd r6, r9 /* Count the bits in the mask. */
324 cntlzd r6,r10 /* Count leading zeros before the match. */
327 srdi r6,r6,3 /* Convert leading/trailing zeros to bytes. */
328 add r8,r5,r6 /* Compute final length. */
330 /* stpcpy returns the dest address plus the size not counting the
334 addi r8,r8,1 /* Final '/0'. */
343 /* Handle copies of 0~31 bytes. */
346 /* At least 6 bytes to go. */
356 8: /* Copy 8 bytes. */
364 /* Copies 4~7 bytes. */
378 /* Copies 2~3 bytes. */
402 /* Handles copies of 0~8 bytes. */
411 #ifndef USE_AS_STPCPY
412 libc_hidden_builtin_def (strcpy)