summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/sparcv9/gen/strcpy.s
blob: 4dcac62fe8b5a35da480a9772eab95f2574a7f25 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

	.file	"strcpy.s"

/*
 * strcpy(s1, s2)
 *
 * Copy string s2 to s1.  s1 must be large enough. Return s1.
 *
 * Fast assembler language version of the following C-program strcpy
 * which represents the `standard' for the C-library.
 *
 *	char *
 *	strcpy(s1, s2)
 *	register char *s1;
 *	register const char *s2;
 *	{
 *		char *os1 = s1;
 *	
 *		while(*s1++ = *s2++)
 *			;
 *		return(os1);
 *	}
 *
 */

#include <sys/asm_linkage.h>

	! This implementation of strcpy works by first checking the
	! source alignment and copying byte, half byte, or word
	! quantities until the source ptr is aligned at an extended
	! word boundary.  Once this has occurred, the string is copied,
	! checking for zero bytes, depending upon its dst ptr alignment.
	! (methods for xword, word, half-word, and byte copies are present)

	ENTRY(strcpy)

	.align 32

	sub	%o1, %o0, %o3		! src - dst
	andcc	%o1, 7, %o4		! dword aligned ?
	bz,pn	%ncc, .srcaligned	! yup
	mov	%o0, %o2		! save dst

.chkbyte:
	andcc	%o1, 1, %g0		! need to copy byte ?
	bz,pn	%ncc, .chkhalfword	! nope, maybe halfword
	sub	%g0, %o1, %g1		! %g1<2:0> = # of unaligned bytes
	ldub	[%o2 + %o3], %o5	! src[0]
	tst	%o5			! src[0] == 0 ?
	stb	%o5, [%o2]		! dst[0] = src[0]
	bz,pn	%ncc, .done		! yup, done
	inc	%o2			! src++, dst++

.chkhalfword:
	andcc	%g1, 2, %g0		! need to copy half-word ?
	bz,pn	%ncc, .chkword		! nope, maybe word
	nop				! 
	lduh	[%o2 + %o3], %o5	! load src halfword
	srl	%o5, 8, %o4		! extract first byte
	tst	%o4			! first byte == 0 ?
	bz,pn	%ncc, .done		! yup, done
	stb	%o4, [%o2]		! store first byte
	andcc	%o5, 0xff, %g0		! extract second byte
	stb 	%o5, [%o2 + 1]		! store second byte
	bz,pn	%ncc, .done		! yup, 2nd byte zero, done
	add	%o2, 2, %o2		! src += 2

.chkword:
	andcc	%g1, 4, %g0		! need to copy word ?
	bz,pn	%ncc, .srcaligned	! nope
	nop				! 
	lduw	[%o2 + %o3], %o5	! load src word
	srl	%o5, 24, %o4		! extract first byte
	tst	%o4			! is first byte zero ?
	bz,pn	%ncc, .done		! yup, done
	stb	%o4, [%o2]		! store first byte
	srl	%o5, 16, %o4		! extract second byte
	andcc	%o4, 0xff, %g0		! is second byte zero ?
	bz,pn	%ncc, .done		! yup, done
	stb	%o4, [%o2 + 1]		! store second byte
	srl	%o5, 8, %o4		! extract third byte
	andcc	%o4, 0xff, %g0		! third byte zero ?
	bz,pn	%ncc, .done		! yup, done
	stb	%o4, [%o2 + 2]		! store third byte
	andcc	%o5, 0xff, %g0		! fourth byte zero ?
	stb 	%o5, [%o2 + 3]		! store fourth byte
	bz,pn	%ncc, .done		! yup, fourth byte zero, done
	add	%o2, 4, %o2		! src += 2

.srcaligned:	
	sethi	%hi(0x01010101), %o4	! Alan Mycroft's magic1
	or	%o4, %lo(0x01010101),%o4!  finish loading magic1
	sllx	%o4, 32, %o1		! spread magic1
	and	%o2, 3, %g4		! dst<1:0> to examine offset
	or	%o4, %o1, %o4		!   to all 64 bits
	cmp	%g4, 1			! dst offset of 1 or 5
	sllx	%o4, 7, %o5		!  Alan Mycroft's magic2
	be,pn	%ncc, .storebyte1241	! store 1, 2, 4, 1 bytes
	cmp	%g4, 3			! dst offset of 3 or 7
	be,pn	%ncc, .storebyte1421	! store 1, 4, 2, 1 bytes
	cmp	%g4, 2			! dst halfword aligned ?
	be,pn	%ncc, .storehalfword	! yup, store half-word wise
	andcc	%o2, 7, %g0		! dst word aligned ?
	bnz,pn	%ncc, .storeword2	! yup, store word wise
	.empty	
	
.storedword:
	ldx	[%o2 + %o3], %o1	! src dword
	add	%o2, 8, %o2		! src += 8, dst += 8
	andn	%o5, %o1, %g1		! ~dword & 0x8080808080808080
	sub	%o1, %o4, %g4		! dword - 0x0101010101010101
	andcc	%g4, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080)
	bz,a,pt	%ncc, .storedword	! no zero byte if magic expression == 0
	stx	%o1, [%o2 - 8]		! store word to dst (address pre-incremented)

.zerobyte:
	orn	%o4, %g0, %o4		! 0xffffffffffffffff
	sllx	%o4, 56, %o4		! 0xff00000000000000
	srlx	%o1, 56, %o3		! %o3<7:0> = first byte
	andcc	%o1, %o4, %g0		! first byte zero?
	bz,pn	%ncc, .done		! yup, done
	stb	%o3, [%o2 - 8]		! store first byte  
	srlx	%o4, 8, %o4		! 0x00ff000000000000
	srlx	%o1, 48, %o3		! %o3<7:0> = second byte
	andcc	%o1, %o4, %g0		! second byte zero?
	bz,pn	%ncc, .done		! yup, done
	stb	%o3, [%o2 - 7]		! store second byte  
	srlx	%o4, 8, %o4		! 0x0000ff0000000000
	srlx	%o1, 40, %o3		! %o3<7:0> = third byte
	andcc	%o1, %o4, %g0		! third byte zero?
	bz,pn	%ncc, .done		! yup, done
	stb	%o3, [%o2 - 6]		! store third byte  
	srlx	%o4, 8, %o4		! 0x000000ff00000000
	srlx	%o1, 32, %o3		! %o3<7:0> = fourth byte
	andcc	%o1, %o4, %g0		! fourth byte zero?
	bz,pn	%ncc, .done		! yup, done
	stb	%o3, [%o2 - 5]		! store fourth byte  
	srlx	%o4, 8, %o4		! 0x00000000ff000000
	srlx	%o1, 24, %o3		! %o3<7:0> = fifth byte
	andcc	%o1, %o4, %g0		! fifth byte zero?
	bz,pn	%ncc, .done		! yup, done
	stb	%o3, [%o2 - 4]		! store fifth byte  
	srlx	%o4, 8, %o4		! 0x0000000000ff0000
	srlx	%o1, 16, %o3		! %o3<7:0> = sixth byte    
	andcc	%o1, %o4, %g0		! sixth byte zero?
	bz,pn	%ncc, .done		! yup, done
	stb	%o3, [%o2 - 3]		! store sixth byte
	srlx	%o4, 8, %o4		! 0x000000000000ff00
	andcc	%o1, %o4, %g0		! seventh byte zero?
	srlx	%o1, 8, %o3		! %o3<7:0> = seventh byte
	bz,pn	%ncc, .done		! yup, done
	stb	%o3, [%o2 - 2]		! store seventh byte
	stb	%o1, [%o2 - 1]		! store eigth byte
.done:
	retl				! done with leaf function

	nop				! ensure following loop 16-byte aligned

.storebyte1421:
	ldx	[%o2 + %o3], %o1	! x = src[]
	add	%o2, 8, %o2		! src += 8, dst += 8
	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
	sub	%o1, %o4, %g4		! x - 0x0101010101010101
	andcc	%g4, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
	srlx	%o1, 56, %g1		! %g1<7:0> = first byte; word aligned now
	stb	%g1, [%o2 - 8]		! store first byte
	srlx	%o1, 24, %g1		! %g1<31:0> = bytes 2, 3, 4, 5
	stw	%g1, [%o2 - 7]		! store bytes 2, 3, 4, 5
	srlx	%o1, 8, %g1		! %g1<15:0> = bytes 6, 7
	sth	%g1, [%o2 - 3]		! store bytes 6, 7
	ba	.storebyte1421		! next dword
	stb	%o1, [%o2 - 1]		! store eigth byte

	nop				! ensure following loop 16-byte aligned
	nop				! ensure following loop 16-byte aligned

.storebyte1241:
	ldx	[%o2 + %o3], %o1	! x = src[]
	add	%o2, 8, %o2		! src += 8, dst += 8
	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
	sub	%o1, %o4, %g4		! x - 0x0101010101010101
	andcc	%g4, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
	srlx	%o1, 56, %g1		! %g1<7:0> = first byte; word aligned now
	stb	%g1, [%o2 - 8]		! store first byte
	srlx	%o1, 40, %g1		! %g1<15:0> = bytes 2, 3
	sth	%g1, [%o2 - 7]		! store bytes 2, 3
	srlx	%o1, 8, %g1		! %g1<31:0> = bytes 4, 5, 6, 7
	stw	%g1, [%o2 - 5]		! store bytes 4, 5, 6, 7
	ba	.storebyte1241		! next dword
	stb	%o1, [%o2 - 1]		! store eigth byte

	nop				! ensure following loop 16-byte aligned
	nop				! ensure following loop 16-byte aligned

.storehalfword:
	ldx	[%o2 + %o3], %o1	! x = src[]
	add	%o2, 8, %o2		! src += 8, dst += 8
	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
	sub	%o1, %o4, %g4		! x - 0x0101010101010101
	andcc	%g4, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
	srlx	%o1, 48, %g1		! get first and second byte
	sth	%g1, [%o2 - 8]		! store first and second byte; word aligned now
	srlx	%o1, 16, %g1		! %g1<31:0> = bytes 3, 4, 5, 6	
	stw	%g1, [%o2 - 6]		! store bytes 3, 4, 5, 6
	ba	.storehalfword		! next word
	sth	%o1, [%o2 - 2]		! store seventh and eigth byte

.storeword:
	ldx	[%o2 + %o3], %o1	! x = src[]
.storeword2:
	add	%o2, 8, %o2		! src += 8, dst += 8
	andn	%o5, %o1, %g1		! ~x & 0x0x8080808080808080
	sub	%o1, %o4, %g4		! x - 0x0101010101010101
	andcc	%g4, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
	srlx	%o1, 32, %g1		! get bytes 1,2,3,4
	stw	%g1, [%o2 - 8]		! store bytes 1,2,3,4 (address is pre-incremented)
	ba	.storeword		! no zero byte if magic expression == 0
	stw	%o1, [%o2 - 4]		! store bytes 5,6,7,8

	nop				! padding, do not remove!!!
	nop				! padding, do not remove!!!
	SET_SIZE(strcpy)