aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/powerpc/powerpc64/power7/strcpy.S
blob: ce71982eaf504c0369608be336239a222f000603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Optimized strcpy/stpcpy implementation for PowerPC64/POWER7.
   Copyright (C) 2013-2014 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <sysdep.h>

/* Implements the function

   char * [r3] strcpy (char *dest [r3], const char *src [r4])

   or

   char * [r3] strcpy (char *dest [r3], const char *src [r4])

   if USE_AS_STPCPY is defined. It tries to use aligned memory accesses
   when possible using the following algorithm:

   if (((((uintptr_t)dst & 0x7UL) == 0) && ((uintptr_t)src & 0x7UL) == 0))
     goto aligned_doubleword_copy;
   if (((((uintptr_t)dst & 0x3UL) == 0) && ((uintptr_t)src & 0x3UL) == 0))
     goto aligned_word_copy;
   if (((uintptr_t)dst & 0x7UL) == ((uintptr_t)src & 0x7UL))
     goto same_alignment;
   goto unaligned;

   The aligned comparison are made using cmpb instructions.  */

#ifdef USE_AS_STPCPY
# define FUNC_NAME __stpcpy
#else
# define FUNC_NAME strcpy
#endif

	.machine  power7
EALIGN (FUNC_NAME, 4, 0)
	CALL_MCOUNT 2

#define rTMP	r0
#ifdef USE_AS_STPCPY
#define rRTN	r3	/* pointer to previous word/doubleword in dest */
#else
#define rRTN	r12	/* pointer to previous word/doubleword in dest */
#endif
#define rSRC	r4	/* pointer to previous word/doubleword in src */
#define rMASK	r5	/* mask 0xffffffff | 0xffffffffffffffff */
#define rWORD	r6	/* current word from src */
#define rALT	r7	/* alternate word from src */
#define rRTNAL	r8	/* alignment of return pointer */
#define rSRCAL	r9	/* alignment of source pointer */
#define rALCNT	r10	/* bytes to read to reach 8 bytes alignment */
#define rSUBAL	r11	/* doubleword minus unaligned displacement */

#ifndef USE_AS_STPCPY
/* Save the dst pointer to use as return value.  */
	mr	rRTN, r3
#endif
	or	rTMP, rSRC, rRTN
	clrldi.	rTMP, rTMP, 61
	bne	L(check_word_alignment)
	b	L(aligned_doubleword_copy)

L(same_alignment):
/* Src and dst with same alignment: align both to doubleword.  */
	mr	rALCNT, rRTN
	lbz	rWORD, 0(rSRC)
	subfic	rSUBAL, rRTNAL, 8
	addi	rRTN, rRTN, 1
	addi	rSRC, rSRC, 1
	cmpdi	cr7, rWORD, 0
	stb	rWORD, 0(rALCNT)
	beq	cr7, L(s2)

	add	rALCNT, rALCNT, rSUBAL
	subf	rALCNT, rRTN, rALCNT
	addi	rALCNT, rALCNT, 1
	mtctr	rALCNT
	b	L(s1)

	.align 4
L(s0):
	addi	rSRC, rSRC, 1
	lbz	rWORD, -1(rSRC)
	cmpdi	cr7, rWORD, 0
	stb	rWORD, -1(rALCNT)
	beqlr	cr7
	mr	rRTN, rALCNT
L(s1):
	addi	rALCNT, rRTN,1
	bdnz	L(s0)
	b L(aligned_doubleword_copy)
	.align 4
L(s2):
	mr	rRTN, rALCNT
	blr

/* For doubleword aligned memory, operate using doubleword load and stores.  */
	.align 4
L(aligned_doubleword_copy):
	li	rMASK, 0
	addi	rRTN, rRTN, -8
	ld	rWORD, 0(rSRC)
	b	L(g2)

	.align 4
L(g0):	ldu	rALT, 8(rSRC)
	stdu	rWORD, 8(rRTN)
	cmpb	rTMP, rALT, rMASK
	cmpdi	rTMP, 0
	bne	L(g1)
	ldu	rWORD, 8(rSRC)
	stdu	rALT, 8(rRTN)
L(g2):	cmpb	rTMP, rWORD, rMASK
	cmpdi	rTMP, 0		/* If rTMP is 0, no null's have been found.  */
	beq	L(g0)

	mr	rALT, rWORD
/* We've hit the end of the string.  Do the rest byte-by-byte.  */
L(g1):
#ifdef __LITTLE_ENDIAN__
	extrdi.	rTMP, rALT, 8, 56
	stbu	rALT, 8(rRTN)
	beqlr-
	extrdi.	rTMP, rALT, 8, 48
	stbu	rTMP, 1(rRTN)
	beqlr-
	extrdi.	rTMP, rALT, 8, 40
	stbu	rTMP, 1(rRTN)
	beqlr-
	extrdi.	rTMP, rALT, 8, 32
	stbu	rTMP, 1(rRTN)
	beqlr-
	extrdi.	rTMP, rALT, 8, 24
	stbu	rTMP, 1(rRTN)
	beqlr-
	extrdi.	rTMP, rALT, 8, 16
	stbu	rTMP, 1(rRTN)
	beqlr-
	extrdi.	rTMP, rALT, 8, 8
	stbu	rTMP, 1(rRTN)
	beqlr-
	extrdi	rTMP, rALT, 8, 0
	stbu	rTMP, 1(rRTN)
#else
	extrdi.	rTMP, rALT, 8, 0
	stbu	rTMP, 8(rRTN)
	beqlr
	extrdi.	rTMP, rALT, 8, 8
	stbu	rTMP, 1(rRTN)
	beqlr
	extrdi.	rTMP, rALT, 8, 16
	stbu	rTMP, 1(rRTN)
	beqlr
	extrdi.	rTMP, rALT, 8, 24
	stbu	rTMP, 1(rRTN)
	beqlr
	extrdi.	rTMP, rALT, 8, 32
	stbu	rTMP, 1(rRTN)
	beqlr
	extrdi.	rTMP, rALT, 8, 40
	stbu	rTMP, 1(rRTN)
	beqlr
	extrdi.	rTMP, rALT, 8, 48
	stbu	rTMP, 1(rRTN)
	beqlr
	stbu	rALT, 1(rRTN)
#endif
	blr

L(check_word_alignment):
	clrldi. rTMP, rTMP, 62
	beq	L(aligned_word_copy)
	rldicl	rRTNAL, rRTN, 0, 61
	rldicl	rSRCAL, rSRC, 0, 61
	cmpld	cr7, rSRCAL, rRTNAL
	beq	cr7, L(same_alignment)
	b	L(unaligned)

/* For word aligned memory, operate using word load and stores.  */
	.align	4
L(aligned_word_copy):
	li	rMASK, 0
	addi	rRTN, rRTN, -4
	lwz	rWORD, 0(rSRC)
	b	L(g5)

	.align	4
L(g3):	lwzu	rALT, 4(rSRC)
	stwu	rWORD, 4(rRTN)
	cmpb	rTMP, rALT, rMASK
	cmpwi	rTMP, 0
	bne	L(g4)
	lwzu	rWORD, 4(rSRC)
	stwu	rALT, 4(rRTN)
L(g5):	cmpb	rTMP, rWORD, rMASK
	cmpwi	rTMP, 0		/* If rTMP is 0, no null in word.  */
	beq	L(g3)

	mr      rALT, rWORD
/* We've hit the end of the string.  Do the rest byte-by-byte.  */
L(g4):
#ifdef __LITTLE_ENDIAN__
	rlwinm.	rTMP, rALT, 0, 24, 31
	stbu	rALT, 4(rRTN)
	beqlr-
	rlwinm.	rTMP, rALT, 24, 24, 31
	stbu	rTMP, 1(rRTN)
	beqlr-
	rlwinm.	rTMP, rALT, 16, 24, 31
	stbu	rTMP, 1(rRTN)
	beqlr-
	rlwinm	rTMP, rALT, 8, 24, 31
	stbu	rTMP, 1(rRTN)
#else
	rlwinm. rTMP, rALT, 8, 24, 31
	stbu    rTMP, 4(rRTN)
	beqlr
	rlwinm. rTMP, rALT, 16, 24, 31
	stbu    rTMP, 1(rRTN)
	beqlr
	rlwinm. rTMP, rALT, 24, 24, 31
	stbu    rTMP, 1(rRTN)
	beqlr
	stbu    rALT, 1(rRTN)
#endif
	blr

/* Oh well.  In this case, we just do a byte-by-byte copy.  */
	.align	4
L(unaligned):
	lbz	rWORD, 0(rSRC)
	addi	rRTN, rRTN, -1
	cmpdi	rWORD, 0
	beq	L(u2)

	.align 	5
L(u0):	lbzu	rALT, 1(rSRC)
	stbu	rWORD, 1(rRTN)
	cmpdi	rALT, 0
	beq	L(u1)
	lbzu	rWORD, 1(rSRC)
	stbu	rALT, 1(rRTN)
	cmpdi	rWORD, 0
	beq	L(u2)
	lbzu	rALT, 1(rSRC)
	stbu	rWORD, 1(rRTN)
	cmpdi	rALT, 0
	beq	L(u1)
	lbzu	rWORD, 1(rSRC)
	stbu	rALT, 1(rRTN)
	cmpdi	rWORD, 0
	bne	L(u0)
L(u2):	stbu	rWORD, 1(rRTN)
	blr
L(u1):	stbu	rALT, 1(rRTN)
	blr
END (FUNC_NAME)

#ifndef USE_AS_STPCPY
libc_hidden_builtin_def (strcpy)
#endif