xref: /titanic_50/usr/src/lib/libc/i386/gen/strcat.s (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
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 2004 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/ strcat(s1, s2)
33/
34/ Concatenates s2 on the end of s1.  s1's space must be large enough.
35/ Returns s1.
36/
37/ Fast assembly language version of the following C-program strcat
38/ which represents the `standard' for the C-library.
39/
40/	char *
41/	strcat(char *s1, const char *s2)
42/	{
43/		char	*os1 = s1;
44/
45/		while (*s1++)
46/			;
47/		--s1;
48/		while (*s1++ = *s2++)
49/			;
50/		return (os1);
51/	}
52/
53/ In this assembly language version, the following expression is used
54/ to check if a 32-bit word data contains a null byte or not:
55/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
56/ If the above expression geneates a value other than 0x80808080,
57/ that means the 32-bit word data contains a null byte.
58/
59
60#include "SYS.h"
61
62	ENTRY(strcat)
63	pushl	%edi			/ save register variable
64	/ find a null byte in destination string
65	movl	8(%esp), %edi		/ %edi = destination string address
66	testl	$3, %edi		/ if %edi not word aligned
67	jnz	.L1			/ goto .L1
68	.align	4
69.L2:
70	movl	(%edi), %edx		/ move 1 word from (%edi) to %edx
71	movl	$0x7f7f7f7f, %ecx
72	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
73	addl	$4, %edi		/ next word
74	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
75	orl	%edx, %ecx		/ %ecx |= %edx
76	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
77	cmpl	$0x80808080, %ecx	/ if no null byte in this word
78	je	.L2			/ goto .L2
79	subl	$4, %edi		/ post-incremented
80.L1:
81	cmpb	$0, (%edi)		/ if a byte in (%edi) is null
82	je	.L3			/ goto .L3
83	incl	%edi			/ next byte
84	testl	$3, %edi		/ if %edi not word aligned
85	jnz	.L1			/ goto .L1
86	jmp	.L2			/ goto .L2 (%edi word aligned)
87	.align	4
88.L3:
89	/ %edi points to a null byte in destination string
90	movl	12(%esp), %eax		/ %eax = source string address
91	testl	$3, %eax		/ if %eax not word aligned
92	jnz	.L4			/ goto .L4
93	.align	4
94.L5:
95	movl	(%eax), %edx		/ move 1 word from (%eax) to %edx
96	movl	$0x7f7f7f7f, %ecx
97	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
98	addl	$4, %eax		/ next word
99	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
100	orl	%edx, %ecx		/ %ecx |= %edx
101	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
102	cmpl	$0x80808080, %ecx	/ if null byte in this word
103	jne	.L7			/ goto .L7
104	movl	%edx, (%edi)		/ copy this word to (%edi)
105	addl	$4, %edi		/ next word
106	jmp	.L5			/ goto .L5
107.L7:
108	subl	$4, %eax		/ post-incremented
109	.align	4
110.L4:
111	movb	(%eax), %dl		/ %dl = a byte in (%eax)
112	cmpb	$0, %dl			/ compare %dl with a null byte
113	movb	%dl, (%edi)		/ copy %dl to (%edi)
114	je	.L6			/ if %dl is a null, goto .L6
115	incl	%eax			/ next byte
116	incl	%edi			/ next byte
117	testl	$3, %eax		/ if %eax not word aligned
118	jnz	.L4			/ goto .L4
119	jmp	.L5			/ goto .L5 (%eax word aligned)
120	.align	4
121.L6:
122	movl	8(%esp), %eax		/ return the destination address
123	popl	%edi			/ restore register variable
124	ret
125	SET_SIZE(strcat)
126