xref: /titanic_41/usr/src/lib/libc/sparc/gen/strcpy.s (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
17c478bd9Sstevel@tonic-gate/*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1b4Sraf * Common Development and Distribution License (the "License").
67257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate/*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*9a70fc3bSMark J. Nelson	.file	"strcpy.s"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate/*
307c478bd9Sstevel@tonic-gate * strcpy(s1, s2)
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * Copy string s2 to s1.  s1 must be large enough. Return s1.
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * Fast assembler language version of the following C-program strcpy
357c478bd9Sstevel@tonic-gate * which represents the `standard' for the C-library.
367c478bd9Sstevel@tonic-gate *
377c478bd9Sstevel@tonic-gate *	char *
387c478bd9Sstevel@tonic-gate *	strcpy(s1, s2)
397c478bd9Sstevel@tonic-gate *	register char *s1;
407c478bd9Sstevel@tonic-gate *	register const char *s2;
417c478bd9Sstevel@tonic-gate *	{
427c478bd9Sstevel@tonic-gate *		char *os1 = s1;
437c478bd9Sstevel@tonic-gate *
447c478bd9Sstevel@tonic-gate *		while(*s1++ = *s2++)
457c478bd9Sstevel@tonic-gate *			;
467c478bd9Sstevel@tonic-gate *		return(os1);
477c478bd9Sstevel@tonic-gate *	}
487c478bd9Sstevel@tonic-gate *
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate	! This is a 32-bit implementation of strcpy.  It works by
547c478bd9Sstevel@tonic-gate	! first checking the alignment of its source pointer. And,
557c478bd9Sstevel@tonic-gate	! if it is not aligned, attempts to copy bytes until it is.
567c478bd9Sstevel@tonic-gate	! once this has occurred, the copy takes place, while checking
577c478bd9Sstevel@tonic-gate	! for zero bytes, based upon destination alignment.
587c478bd9Sstevel@tonic-gate	! Methods exist to handle per-byte, half-word, and word sized
597c478bd9Sstevel@tonic-gate	! copies.
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate	ENTRY(strcpy)
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate	.align 32
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o3		! src - dst
667c478bd9Sstevel@tonic-gate	andcc	%o1, 3, %o4		! src word aligned ?
677c478bd9Sstevel@tonic-gate	bz	.srcaligned		! yup
687c478bd9Sstevel@tonic-gate	mov	%o0, %o2		! save dst
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate	cmp	%o4, 2			! src halfword aligned
717c478bd9Sstevel@tonic-gate	be	.s2aligned		! yup
727c478bd9Sstevel@tonic-gate	ldub	[%o2 + %o3], %o1	! src[0]
737c478bd9Sstevel@tonic-gate	tst	%o1			! byte zero?
747c478bd9Sstevel@tonic-gate	stb	%o1, [%o2]		! store first byte
757c478bd9Sstevel@tonic-gate	bz	.done			! yup, done
767c478bd9Sstevel@tonic-gate	cmp	%o4, 3			! only one byte needed to align?
777c478bd9Sstevel@tonic-gate	bz	.srcaligned		! yup
787c478bd9Sstevel@tonic-gate	inc	%o2			! src++, dst++
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate.s2aligned:
817c478bd9Sstevel@tonic-gate	lduh	[%o2 + %o3], %o1	! src[]
827c478bd9Sstevel@tonic-gate	srl	%o1, 8, %o4		! %o4<7:0> = first byte
837c478bd9Sstevel@tonic-gate	tst	%o4			! first byte zero ?
847c478bd9Sstevel@tonic-gate	bz	.done			! yup, done
857c478bd9Sstevel@tonic-gate	stb	%o4, [%o2]		! store first byte
867c478bd9Sstevel@tonic-gate	andcc	%o1, 0xff, %g0		! second byte zero ?
877c478bd9Sstevel@tonic-gate	bz	.done			! yup, done
887c478bd9Sstevel@tonic-gate	stb	%o1, [%o2 + 1]		! store second byte
897c478bd9Sstevel@tonic-gate	add	%o2, 2, %o2		! src += 2, dst += 2
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate.srcaligned:
927c478bd9Sstevel@tonic-gate	sethi	%hi(0x01010101), %o4	! Alan Mycroft's magic1
937c478bd9Sstevel@tonic-gate	sethi	%hi(0x80808080), %o5	! Alan Mycroft's magic2
947c478bd9Sstevel@tonic-gate	or	%o4, %lo(0x01010101), %o4
957c478bd9Sstevel@tonic-gate	andcc	%o2, 3, %o1		! destination word aligned?
967c478bd9Sstevel@tonic-gate	bnz	.dstnotaligned		! nope
977c478bd9Sstevel@tonic-gate	or	%o5, %lo(0x80808080), %o5
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate.copyword:
1007c478bd9Sstevel@tonic-gate	lduw	[%o2 + %o3], %o1	! src word
1017c478bd9Sstevel@tonic-gate	add	%o2, 4, %o2		! src += 4, dst += 4
1027c478bd9Sstevel@tonic-gate	andn	%o5, %o1, %g1		! ~word & 0x80808080
1037c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %o1		! word - 0x01010101
1047c478bd9Sstevel@tonic-gate	andcc	%o1, %g1, %g0		! ((word - 0x01010101) & ~word & 0x80808080)
1057c478bd9Sstevel@tonic-gate	add	%o1, %o4, %o1		! restore word
1067c478bd9Sstevel@tonic-gate	bz,a	.copyword		! no zero byte if magic expression == 0
1077c478bd9Sstevel@tonic-gate	st	%o1, [%o2 - 4]		! store word to dst (address pre-incremented)
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate.zerobyte:
1107c478bd9Sstevel@tonic-gate	set	0xff000000, %o4		! mask for 1st byte
1117c478bd9Sstevel@tonic-gate	srl	%o1, 24, %o3		! %o3<7:0> = first byte
1127c478bd9Sstevel@tonic-gate	andcc	%o1, %o4, %g0		! first byte zero?
1137c478bd9Sstevel@tonic-gate	bz	.done			! yup, done
1147c478bd9Sstevel@tonic-gate	stb	%o3, [%o2 - 4]		! store first byte
1157c478bd9Sstevel@tonic-gate	set	0x00ff0000, %o5		! mask for 2nd byte
1167c478bd9Sstevel@tonic-gate	srl	%o1, 16, %o3		! %o3<7:0> = second byte
1177c478bd9Sstevel@tonic-gate	andcc	%o1, %o5, %g0		! second byte zero?
1187c478bd9Sstevel@tonic-gate	bz	.done			! yup, done
1197c478bd9Sstevel@tonic-gate	stb	%o3, [%o2 - 3]		! store second byte
1207c478bd9Sstevel@tonic-gate	srl	%o4, 16, %o4		! 0x0000ff00 = mask for 3rd byte
1217c478bd9Sstevel@tonic-gate	andcc	%o1, %o4, %g0		! third byte zero?
1227c478bd9Sstevel@tonic-gate	srl	%o1, 8, %o3		! %o3<7:0> = third byte
1237c478bd9Sstevel@tonic-gate	bz	.done			! yup, done
1247c478bd9Sstevel@tonic-gate	stb	%o3, [%o2 - 2]		! store third byte
1257c478bd9Sstevel@tonic-gate	stb	%o1, [%o2 - 1]		! store fourth byte
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate.done:
1287c478bd9Sstevel@tonic-gate	retl				! done with leaf function
1297c478bd9Sstevel@tonic-gate	.empty
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate.dstnotaligned:
1327c478bd9Sstevel@tonic-gate	cmp	%o1, 2			! dst half word aligned?
1337c478bd9Sstevel@tonic-gate	be,a	.storehalfword2		! yup, store half word at a time
1347c478bd9Sstevel@tonic-gate	lduw	[%o2 + %o3], %o1	! src word
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate.storebyte:
1377c478bd9Sstevel@tonic-gate	lduw	[%o2 + %o3], %o1	! src word
1387c478bd9Sstevel@tonic-gate	add	%o2, 4, %o2		! src += 4, dst += 4
1397c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %g1		! x - 0x01010101
1407c478bd9Sstevel@tonic-gate	andn	%g1, %o1, %g1		! (x - 0x01010101) & ~x
1417c478bd9Sstevel@tonic-gate	andcc	%g1, %o5, %g0		! ((x - 0x01010101) & ~x & 0x80808080)
1427c478bd9Sstevel@tonic-gate	bnz	.zerobyte		! word has zero byte, handle end cases
1437c478bd9Sstevel@tonic-gate	srl	%o1, 24, %g1		! %g1<7:0> = first byte
1447c478bd9Sstevel@tonic-gate	stb	%g1, [%o2 - 4]		! store first byte; half-word aligned now
1457c478bd9Sstevel@tonic-gate	srl	%o1, 8, %g1		! %g1<15:0> = byte 2, 3
1467c478bd9Sstevel@tonic-gate	sth	%g1, [%o2 - 3]		! store bytes 2, 3
1477c478bd9Sstevel@tonic-gate	ba	.storebyte		! next word
1487c478bd9Sstevel@tonic-gate	stb	%o1, [%o2 - 1]		! store fourth byte
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate.storehalfword:
1517c478bd9Sstevel@tonic-gate	lduw	[%o2 + %o3], %o1	! src word
1527c478bd9Sstevel@tonic-gate.storehalfword2:
1537c478bd9Sstevel@tonic-gate	add	%o2, 4, %o2		! src += 4, dst += 4
1547c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %g1		! x - 0x01010101
1557c478bd9Sstevel@tonic-gate	andn	%g1, %o1, %g1		! (x - 0x01010101) & ~x
1567c478bd9Sstevel@tonic-gate	andcc	%g1, %o5, %g0		! ((x - 0x01010101) & ~x & 0x80808080)
1577c478bd9Sstevel@tonic-gate	bnz	.zerobyte		! word has zero byte, handle end cases
1587c478bd9Sstevel@tonic-gate	srl	%o1, 16, %g1		! get first and second byte
1597c478bd9Sstevel@tonic-gate	sth	%g1, [%o2 - 4]		! store first and second byte
1607c478bd9Sstevel@tonic-gate	ba	.storehalfword		! next word
1617c478bd9Sstevel@tonic-gate	sth	%o1, [%o2 - 2]		! store third and fourth byte
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate	! DO NOT remove these NOPs. It will slow down the halfword loop by 15%
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate	nop				! padding
1667c478bd9Sstevel@tonic-gate	nop				! padding
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate	SET_SIZE(strcpy)
1697c478bd9Sstevel@tonic-gate
170