xref: /titanic_52/usr/src/lib/libc/i386/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
5*9a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License").
6*9a70fc3bSMark J. Nelson * 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 */
217c478bd9Sstevel@tonic-gate/*
227c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
26*9a70fc3bSMark J. Nelson	.file	"strcpy.s"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate/
297c478bd9Sstevel@tonic-gate/ strcpy(s1, s2)
307c478bd9Sstevel@tonic-gate/
317c478bd9Sstevel@tonic-gate/ Copies string s2 to s1.  s1 must be large enough.
327c478bd9Sstevel@tonic-gate/ Returns s1
337c478bd9Sstevel@tonic-gate/
347c478bd9Sstevel@tonic-gate/
357c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program strcpy
367c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library.
377c478bd9Sstevel@tonic-gate/
387c478bd9Sstevel@tonic-gate/	char *
397c478bd9Sstevel@tonic-gate/	strcpy(char *s1, const char *s2)
407c478bd9Sstevel@tonic-gate/	{
417c478bd9Sstevel@tonic-gate/		char	*os1 = s1;
427c478bd9Sstevel@tonic-gate/
437c478bd9Sstevel@tonic-gate/		while (*s1++ = *s2++)
447c478bd9Sstevel@tonic-gate/			;
457c478bd9Sstevel@tonic-gate/		return (os1);
467c478bd9Sstevel@tonic-gate/	}
477c478bd9Sstevel@tonic-gate/
487c478bd9Sstevel@tonic-gate/ In this assembly language version, the following expression is used
497c478bd9Sstevel@tonic-gate/ to check if a 32-bit word data contains a null byte or not:
507c478bd9Sstevel@tonic-gate/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
517c478bd9Sstevel@tonic-gate/ If the above expression geneates a value other than 0x80808080,
527c478bd9Sstevel@tonic-gate/ that means the 32-bit word data contains a null byte.
537c478bd9Sstevel@tonic-gate/
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate#include "SYS.h"
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate	ENTRY(strcpy)
587c478bd9Sstevel@tonic-gate	push	%edi				/ save reg as per calling cvntn
597c478bd9Sstevel@tonic-gate	mov	12(%esp), %ecx			/ src ptr
607c478bd9Sstevel@tonic-gate	mov	8(%esp), %edi			/ dst ptr
617c478bd9Sstevel@tonic-gate	mov	%ecx, %eax			/ src
627c478bd9Sstevel@tonic-gate	sub	%edi, %ecx			/ src - dst
637c478bd9Sstevel@tonic-gate	and	$3, %eax			/ check src alignment
647c478bd9Sstevel@tonic-gate	jz	load
657c478bd9Sstevel@tonic-gate	sub	$4, %eax
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gatebyte_loop:
687c478bd9Sstevel@tonic-gate	movb	(%edi, %ecx, 1), %dl		/ load src byte
697c478bd9Sstevel@tonic-gate	movb	%dl, (%edi)			/ load dest byte
707c478bd9Sstevel@tonic-gate	inc	%edi				/ increment src and dest
717c478bd9Sstevel@tonic-gate	testb	%dl, %dl			/ is src zero?
727c478bd9Sstevel@tonic-gate	jz 	done
737c478bd9Sstevel@tonic-gate	inc	%eax				/ check src alignment
747c478bd9Sstevel@tonic-gate	jnz	byte_loop
757c478bd9Sstevel@tonic-gate	jmp 	load
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gatestore:
787c478bd9Sstevel@tonic-gate	mov	%eax, (%edi)			/ store word
797c478bd9Sstevel@tonic-gate	add	$4, %edi			/ incrment src and dest by 4
807c478bd9Sstevel@tonic-gateload:
817c478bd9Sstevel@tonic-gate	mov	(%edi, %ecx, 1), %eax		/ load word
827c478bd9Sstevel@tonic-gate	lea	-0x01010101(%eax), %edx		/ (word - 0x01010101)
837c478bd9Sstevel@tonic-gate	not	%eax				/ ~word
847c478bd9Sstevel@tonic-gate	and	%eax, %edx			/ (word - 0x01010101) & ~word
857c478bd9Sstevel@tonic-gate	not	%eax				/ word
867c478bd9Sstevel@tonic-gate	and	$0x80808080, %edx	/ (wd - 0x01010101) & ~wd & 0x80808080
877c478bd9Sstevel@tonic-gate	jz	store				/ store word w/o zero byte
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gatehas_zero_byte:
907c478bd9Sstevel@tonic-gate	movb	%al, (%edi)			/ store first byte
917c478bd9Sstevel@tonic-gate	testb	%al, %al			/ check first byte for zero
927c478bd9Sstevel@tonic-gate	jz	done
937c478bd9Sstevel@tonic-gate	movb	%ah, 1(%edi)			/ continue storing and checking
947c478bd9Sstevel@tonic-gate	testb	%ah, %ah
957c478bd9Sstevel@tonic-gate	jz	done
967c478bd9Sstevel@tonic-gate	shr	$16, %eax			/ grab last two bytes
977c478bd9Sstevel@tonic-gate	movb	%al, 2(%edi)
987c478bd9Sstevel@tonic-gate	testb	%al, %al
997c478bd9Sstevel@tonic-gate	jz	done
1007c478bd9Sstevel@tonic-gate	movb	%ah, 3(%edi)
1017c478bd9Sstevel@tonic-gatedone:
1027c478bd9Sstevel@tonic-gate	mov	8(%esp), %eax			/ return ptr to dest
1037c478bd9Sstevel@tonic-gate	pop	%edi				/ restore as per calling cvntn
1047c478bd9Sstevel@tonic-gate	ret
1057c478bd9Sstevel@tonic-gate	SET_SIZE(strcpy)
106