xref: /titanic_41/usr/src/lib/libc/i386/gen/strcpy.s (revision a31148363f598def767ac48c5d82e1572e44b935)
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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26	.file	"strcpy.s"
27
28/
29/ strcpy(s1, s2)
30/
31/ Copies string s2 to s1.  s1 must be large enough.
32/ Returns s1
33/
34/
35/ Fast assembly language version of the following C-program strcpy
36/ which represents the `standard' for the C-library.
37/
38/	char *
39/	strcpy(char *s1, const char *s2)
40/	{
41/		char	*os1 = s1;
42/
43/		while (*s1++ = *s2++)
44/			;
45/		return (os1);
46/	}
47/
48/ In this assembly language version, the following expression is used
49/ to check if a 32-bit word data contains a null byte or not:
50/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
51/ If the above expression geneates a value other than 0x80808080,
52/ that means the 32-bit word data contains a null byte.
53/
54
55#include "SYS.h"
56
57	ENTRY(strcpy)
58	push	%edi				/ save reg as per calling cvntn
59	mov	12(%esp), %ecx			/ src ptr
60	mov	8(%esp), %edi			/ dst ptr
61	mov	%ecx, %eax			/ src
62	sub	%edi, %ecx			/ src - dst
63	and	$3, %eax			/ check src alignment
64	jz	load
65	sub	$4, %eax
66
67byte_loop:
68	movb	(%edi, %ecx, 1), %dl		/ load src byte
69	movb	%dl, (%edi)			/ load dest byte
70	inc	%edi				/ increment src and dest
71	testb	%dl, %dl			/ is src zero?
72	jz 	done
73	inc	%eax				/ check src alignment
74	jnz	byte_loop
75	jmp 	load
76
77store:
78	mov	%eax, (%edi)			/ store word
79	add	$4, %edi			/ incrment src and dest by 4
80load:
81	mov	(%edi, %ecx, 1), %eax		/ load word
82	lea	-0x01010101(%eax), %edx		/ (word - 0x01010101)
83	not	%eax				/ ~word
84	and	%eax, %edx			/ (word - 0x01010101) & ~word
85	not	%eax				/ word
86	and	$0x80808080, %edx	/ (wd - 0x01010101) & ~wd & 0x80808080
87	jz	store				/ store word w/o zero byte
88
89has_zero_byte:
90	movb	%al, (%edi)			/ store first byte
91	testb	%al, %al			/ check first byte for zero
92	jz	done
93	movb	%ah, 1(%edi)			/ continue storing and checking
94	testb	%ah, %ah
95	jz	done
96	shr	$16, %eax			/ grab last two bytes
97	movb	%al, 2(%edi)
98	testb	%al, %al
99	jz	done
100	movb	%ah, 3(%edi)
101done:
102	mov	8(%esp), %eax			/ return ptr to dest
103	pop	%edi				/ restore as per calling cvntn
104	ret
105	SET_SIZE(strcpy)
106