1 /* Optimized strcpy/stpcpy implementation for PowerPC64/POWER7.
2 Copyright (C) 2013-2014 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/>. */
21 /* Implements the function
23 char * [r3] strcpy (char *dest [r3], const char *src [r4])
27 char * [r3] strcpy (char *dest [r3], const char *src [r4])
29 if USE_AS_STPCPY is defined. It tries to use aligned memory accesses
30 when possible using the following algorithm:
32 if (((((uintptr_t)dst & 0x7UL) == 0) && ((uintptr_t)src & 0x7UL) == 0))
33 goto aligned_doubleword_copy;
34 if (((((uintptr_t)dst & 0x3UL) == 0) && ((uintptr_t)src & 0x3UL) == 0))
35 goto aligned_word_copy;
36 if (((uintptr_t)dst & 0x7UL) == ((uintptr_t)src & 0x7UL))
40 The aligned comparison are made using cmpb instructions. */
43 # define FUNC_NAME __stpcpy
45 # define FUNC_NAME strcpy
49 EALIGN (FUNC_NAME, 4, 0)
54 #define rRTN r3 /* pointer to previous word/doubleword in dest */
56 #define rRTN r12 /* pointer to previous word/doubleword in dest */
58 #define rSRC r4 /* pointer to previous word/doubleword in src */
59 #define rMASK r5 /* mask 0xffffffff | 0xffffffffffffffff */
60 #define rWORD r6 /* current word from src */
61 #define rALT r7 /* alternate word from src */
62 #define rRTNAL r8 /* alignment of return pointer */
63 #define rSRCAL r9 /* alignment of source pointer */
64 #define rALCNT r10 /* bytes to read to reach 8 bytes alignment */
65 #define rSUBAL r11 /* doubleword minus unaligned displacement */
68 /* Save the dst pointer to use as return value. */
72 clrldi. rTMP, rTMP, 61
73 bne L(check_word_alignment)
74 b L(aligned_doubleword_copy)
77 /* Src and dst with same alignment: align both to doubleword. */
80 subfic rSUBAL, rRTNAL, 8
87 add rALCNT, rALCNT, rSUBAL
88 subf rALCNT, rRTN, rALCNT
89 addi rALCNT, rALCNT, 1
104 b L(aligned_doubleword_copy)
110 /* For doubleword aligned memory, operate using doubleword load and stores. */
112 L(aligned_doubleword_copy):
119 L(g0): ldu rALT, 8(rSRC)
121 cmpb rTMP, rALT, rMASK
126 L(g2): cmpb rTMP, rWORD, rMASK
127 cmpdi rTMP, 0 /* If rTMP is 0, no null's have been found. */
131 /* We've hit the end of the string. Do the rest byte-by-byte. */
133 #ifdef __LITTLE_ENDIAN__
134 extrdi. rTMP, rALT, 8, 56
137 extrdi. rTMP, rALT, 8, 48
140 extrdi. rTMP, rALT, 8, 40
143 extrdi. rTMP, rALT, 8, 32
146 extrdi. rTMP, rALT, 8, 24
149 extrdi. rTMP, rALT, 8, 16
152 extrdi. rTMP, rALT, 8, 8
155 extrdi rTMP, rALT, 8, 0
158 extrdi. rTMP, rALT, 8, 0
161 extrdi. rTMP, rALT, 8, 8
164 extrdi. rTMP, rALT, 8, 16
167 extrdi. rTMP, rALT, 8, 24
170 extrdi. rTMP, rALT, 8, 32
173 extrdi. rTMP, rALT, 8, 40
176 extrdi. rTMP, rALT, 8, 48
183 L(check_word_alignment):
184 clrldi. rTMP, rTMP, 62
185 beq L(aligned_word_copy)
186 rldicl rRTNAL, rRTN, 0, 61
187 rldicl rSRCAL, rSRC, 0, 61
188 cmpld cr7, rSRCAL, rRTNAL
189 beq cr7, L(same_alignment)
192 /* For word aligned memory, operate using word load and stores. */
194 L(aligned_word_copy):
201 L(g3): lwzu rALT, 4(rSRC)
203 cmpb rTMP, rALT, rMASK
208 L(g5): cmpb rTMP, rWORD, rMASK
209 cmpwi rTMP, 0 /* If rTMP is 0, no null in word. */
213 /* We've hit the end of the string. Do the rest byte-by-byte. */
215 #ifdef __LITTLE_ENDIAN__
216 rlwinm. rTMP, rALT, 0, 24, 31
219 rlwinm. rTMP, rALT, 24, 24, 31
222 rlwinm. rTMP, rALT, 16, 24, 31
225 rlwinm rTMP, rALT, 8, 24, 31
228 rlwinm. rTMP, rALT, 8, 24, 31
231 rlwinm. rTMP, rALT, 16, 24, 31
234 rlwinm. rTMP, rALT, 24, 24, 31
241 /* Oh well. In this case, we just do a byte-by-byte copy. */
250 L(u0): lbzu rALT, 1(rSRC)
266 L(u2): stbu rWORD, 1(rRTN)
268 L(u1): stbu rALT, 1(rRTN)
272 #ifndef USE_AS_STPCPY
273 libc_hidden_builtin_def (strcpy)