xref: /titanic_52/usr/src/lib/libc/sparcv9/gen/strcpy.s (revision 7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fe)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27.ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31/*
32 * strcpy(s1, s2)
33 *
34 * Copy string s2 to s1.  s1 must be large enough. Return s1.
35 *
36 * Fast assembler language version of the following C-program strcpy
37 * which represents the `standard' for the C-library.
38 *
39 *	char *
40 *	strcpy(s1, s2)
41 *	register char *s1;
42 *	register const char *s2;
43 *	{
44 *		char *os1 = s1;
45 *
46 *		while(*s1++ = *s2++)
47 *			;
48 *		return(os1);
49 *	}
50 *
51 */
52
53#include <sys/asm_linkage.h>
54#include "synonyms.h"
55
56	! This implementation of strcpy works by first checking the
57	! source alignment and copying byte, half byte, or word
58	! quantities until the source ptr is aligned at an extended
59	! word boundary.  Once this has occurred, the string is copied,
60	! checking for zero bytes, depending upon its dst ptr alignment.
61	! (methods for xword, word, half-word, and byte copies are present)
62
63	ENTRY(strcpy)
64
65	.align 32
66
67	sub	%o1, %o0, %o3		! src - dst
68	andcc	%o1, 7, %o4		! dword aligned ?
69	bz,pn	%ncc, .srcaligned	! yup
70	mov	%o0, %o2		! save dst
71
72.chkbyte:
73	andcc	%o1, 1, %g0		! need to copy byte ?
74	bz,pn	%ncc, .chkhalfword	! nope, maybe halfword
75	sub	%g0, %o1, %g1		! %g1<2:0> = # of unaligned bytes
76	ldub	[%o2 + %o3], %o5	! src[0]
77	tst	%o5			! src[0] == 0 ?
78	stb	%o5, [%o2]		! dst[0] = src[0]
79	bz,pn	%ncc, .done		! yup, done
80	inc	%o2			! src++, dst++
81
82.chkhalfword:
83	andcc	%g1, 2, %g0		! need to copy half-word ?
84	bz,pn	%ncc, .chkword		! nope, maybe word
85	nop				!
86	lduh	[%o2 + %o3], %o5	! load src halfword
87	srl	%o5, 8, %o4		! extract first byte
88	tst	%o4			! first byte == 0 ?
89	bz,pn	%ncc, .done		! yup, done
90	stb	%o4, [%o2]		! store first byte
91	andcc	%o5, 0xff, %g0		! extract second byte
92	stb 	%o5, [%o2 + 1]		! store second byte
93	bz,pn	%ncc, .done		! yup, 2nd byte zero, done
94	add	%o2, 2, %o2		! src += 2
95
96.chkword:
97	andcc	%g1, 4, %g0		! need to copy word ?
98	bz,pn	%ncc, .srcaligned	! nope
99	nop				!
100	lduw	[%o2 + %o3], %o5	! load src word
101	srl	%o5, 24, %o4		! extract first byte
102	tst	%o4			! is first byte zero ?
103	bz,pn	%ncc, .done		! yup, done
104	stb	%o4, [%o2]		! store first byte
105	srl	%o5, 16, %o4		! extract second byte
106	andcc	%o4, 0xff, %g0		! is second byte zero ?
107	bz,pn	%ncc, .done		! yup, done
108	stb	%o4, [%o2 + 1]		! store second byte
109	srl	%o5, 8, %o4		! extract third byte
110	andcc	%o4, 0xff, %g0		! third byte zero ?
111	bz,pn	%ncc, .done		! yup, done
112	stb	%o4, [%o2 + 2]		! store third byte
113	andcc	%o5, 0xff, %g0		! fourth byte zero ?
114	stb 	%o5, [%o2 + 3]		! store fourth byte
115	bz,pn	%ncc, .done		! yup, fourth byte zero, done
116	add	%o2, 4, %o2		! src += 2
117
118.srcaligned:
119	sethi	%hi(0x01010101), %o4	! Alan Mycroft's magic1
120	or	%o4, %lo(0x01010101),%o4!  finish loading magic1
121	sllx	%o4, 32, %o1		! spread magic1
122	and	%o2, 3, %g4		! dst<1:0> to examine offset
123	or	%o4, %o1, %o4		!   to all 64 bits
124	cmp	%g4, 1			! dst offset of 1 or 5
125	sllx	%o4, 7, %o5		!  Alan Mycroft's magic2
126	be,pn	%ncc, .storebyte1241	! store 1, 2, 4, 1 bytes
127	cmp	%g4, 3			! dst offset of 3 or 7
128	be,pn	%ncc, .storebyte1421	! store 1, 4, 2, 1 bytes
129	cmp	%g4, 2			! dst halfword aligned ?
130	be,pn	%ncc, .storehalfword	! yup, store half-word wise
131	andcc	%o2, 7, %g0		! dst word aligned ?
132	bnz,pn	%ncc, .storeword2	! yup, store word wise
133	.empty
134
135.storedword:
136	ldx	[%o2 + %o3], %o1	! src dword
137	add	%o2, 8, %o2		! src += 8, dst += 8
138	andn	%o5, %o1, %g1		! ~dword & 0x8080808080808080
139	sub	%o1, %o4, %g4		! dword - 0x0101010101010101
140	andcc	%g4, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080)
141	bz,a,pt	%ncc, .storedword	! no zero byte if magic expression == 0
142	stx	%o1, [%o2 - 8]		! store word to dst (address pre-incremented)
143
144.zerobyte:
145	orn	%o4, %g0, %o4		! 0xffffffffffffffff
146	sllx	%o4, 56, %o4		! 0xff00000000000000
147	srlx	%o1, 56, %o3		! %o3<7:0> = first byte
148	andcc	%o1, %o4, %g0		! first byte zero?
149	bz,pn	%ncc, .done		! yup, done
150	stb	%o3, [%o2 - 8]		! store first byte
151	srlx	%o4, 8, %o4		! 0x00ff000000000000
152	srlx	%o1, 48, %o3		! %o3<7:0> = second byte
153	andcc	%o1, %o4, %g0		! second byte zero?
154	bz,pn	%ncc, .done		! yup, done
155	stb	%o3, [%o2 - 7]		! store second byte
156	srlx	%o4, 8, %o4		! 0x0000ff0000000000
157	srlx	%o1, 40, %o3		! %o3<7:0> = third byte
158	andcc	%o1, %o4, %g0		! third byte zero?
159	bz,pn	%ncc, .done		! yup, done
160	stb	%o3, [%o2 - 6]		! store third byte
161	srlx	%o4, 8, %o4		! 0x000000ff00000000
162	srlx	%o1, 32, %o3		! %o3<7:0> = fourth byte
163	andcc	%o1, %o4, %g0		! fourth byte zero?
164	bz,pn	%ncc, .done		! yup, done
165	stb	%o3, [%o2 - 5]		! store fourth byte
166	srlx	%o4, 8, %o4		! 0x00000000ff000000
167	srlx	%o1, 24, %o3		! %o3<7:0> = fifth byte
168	andcc	%o1, %o4, %g0		! fifth byte zero?
169	bz,pn	%ncc, .done		! yup, done
170	stb	%o3, [%o2 - 4]		! store fifth byte
171	srlx	%o4, 8, %o4		! 0x0000000000ff0000
172	srlx	%o1, 16, %o3		! %o3<7:0> = sixth byte
173	andcc	%o1, %o4, %g0		! sixth byte zero?
174	bz,pn	%ncc, .done		! yup, done
175	stb	%o3, [%o2 - 3]		! store sixth byte
176	srlx	%o4, 8, %o4		! 0x000000000000ff00
177	andcc	%o1, %o4, %g0		! seventh byte zero?
178	srlx	%o1, 8, %o3		! %o3<7:0> = seventh byte
179	bz,pn	%ncc, .done		! yup, done
180	stb	%o3, [%o2 - 2]		! store seventh byte
181	stb	%o1, [%o2 - 1]		! store eigth byte
182.done:
183	retl				! done with leaf function
184
185	nop				! ensure following loop 16-byte aligned
186
187.storebyte1421:
188	ldx	[%o2 + %o3], %o1	! x = src[]
189	add	%o2, 8, %o2		! src += 8, dst += 8
190	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
191	sub	%o1, %o4, %g4		! x - 0x0101010101010101
192	andcc	%g4, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
193	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
194	srlx	%o1, 56, %g1		! %g1<7:0> = first byte; word aligned now
195	stb	%g1, [%o2 - 8]		! store first byte
196	srlx	%o1, 24, %g1		! %g1<31:0> = bytes 2, 3, 4, 5
197	stw	%g1, [%o2 - 7]		! store bytes 2, 3, 4, 5
198	srlx	%o1, 8, %g1		! %g1<15:0> = bytes 6, 7
199	sth	%g1, [%o2 - 3]		! store bytes 6, 7
200	ba	.storebyte1421		! next dword
201	stb	%o1, [%o2 - 1]		! store eigth byte
202
203	nop				! ensure following loop 16-byte aligned
204	nop				! ensure following loop 16-byte aligned
205
206.storebyte1241:
207	ldx	[%o2 + %o3], %o1	! x = src[]
208	add	%o2, 8, %o2		! src += 8, dst += 8
209	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
210	sub	%o1, %o4, %g4		! x - 0x0101010101010101
211	andcc	%g4, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
212	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
213	srlx	%o1, 56, %g1		! %g1<7:0> = first byte; word aligned now
214	stb	%g1, [%o2 - 8]		! store first byte
215	srlx	%o1, 40, %g1		! %g1<15:0> = bytes 2, 3
216	sth	%g1, [%o2 - 7]		! store bytes 2, 3
217	srlx	%o1, 8, %g1		! %g1<31:0> = bytes 4, 5, 6, 7
218	stw	%g1, [%o2 - 5]		! store bytes 4, 5, 6, 7
219	ba	.storebyte1241		! next dword
220	stb	%o1, [%o2 - 1]		! store eigth byte
221
222	nop				! ensure following loop 16-byte aligned
223	nop				! ensure following loop 16-byte aligned
224
225.storehalfword:
226	ldx	[%o2 + %o3], %o1	! x = src[]
227	add	%o2, 8, %o2		! src += 8, dst += 8
228	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
229	sub	%o1, %o4, %g4		! x - 0x0101010101010101
230	andcc	%g4, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
231	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
232	srlx	%o1, 48, %g1		! get first and second byte
233	sth	%g1, [%o2 - 8]		! store first and second byte; word aligned now
234	srlx	%o1, 16, %g1		! %g1<31:0> = bytes 3, 4, 5, 6
235	stw	%g1, [%o2 - 6]		! store bytes 3, 4, 5, 6
236	ba	.storehalfword		! next word
237	sth	%o1, [%o2 - 2]		! store seventh and eigth byte
238
239.storeword:
240	ldx	[%o2 + %o3], %o1	! x = src[]
241.storeword2:
242	add	%o2, 8, %o2		! src += 8, dst += 8
243	andn	%o5, %o1, %g1		! ~x & 0x0x8080808080808080
244	sub	%o1, %o4, %g4		! x - 0x0101010101010101
245	andcc	%g4, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
246	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
247	srlx	%o1, 32, %g1		! get bytes 1,2,3,4
248	stw	%g1, [%o2 - 8]		! store bytes 1,2,3,4 (address is pre-incremented)
249	ba	.storeword		! no zero byte if magic expression == 0
250	stw	%o1, [%o2 - 4]		! store bytes 5,6,7,8
251
252	nop				! padding, do not remove!!!
253	nop				! padding, do not remove!!!
254	SET_SIZE(strcpy)
255
256