xref: /titanic_41/usr/src/lib/libc/i386/gen/strncat.s (revision 70cbfe41f2338b77c15f79c6625eca6e70c412f3)
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 2004 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26	.file	"strncat.s"
27
28/
29/ strncat(s1, s2, n)
30/
31/ Concatenates s2 on the end of s1.  s1's space must be large enough.
32/ At most n characters are moved.
33/ Returns s1.
34/
35/ Fast assembly language version of the following C-program strncat
36/ which represents the `standard' for the C-library.
37/
38/	char *
39/	strncat(char *s1, const char *s2, size_t n)
40/	{
41/		char	*os1 = s1;
42/
43/		n++;
44/		while (*s1++)
45/			;
46/		--s1;
47/		while (*s1++ = *s2++)
48/			if (--n == 0) {
49/				s1[-1] = '\0';
50/				break;
51/			}
52/		return (os1);
53/	}
54/
55/ In this assembly language version, the following expression is used
56/ to check if a 32-bit word data contains a null byte or not:
57/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
58/ If the above expression geneates a value other than 0x80808080,
59/ that means the 32-bit word data contains a null byte.
60/
61
62#include "SYS.h"
63
64	ENTRY(strncat)
65	pushl	%edi			/ save register variables
66	pushl	%esi
67	movl	12(%esp), %edi		/ %edi = destination string address
68	testl	$3, %edi		/ if %edi not word aligned
69	jnz	.L1			/ goto .L1
70	.align	4
71.L2:
72	movl	(%edi), %edx		/ move 1 word from (%edi) to %edx
73	movl	$0x7f7f7f7f, %ecx
74	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
75	addl	$4, %edi		/ next word
76	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
77	orl	%edx, %ecx		/ %ecx |= %edx
78	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
79	cmpl	$0x80808080, %ecx	/ if no null byte in this word
80	je	.L2			/ goto .L2
81	subl	$4, %edi		/ post-incremented
82.L1:
83	cmpb	$0, (%edi)		/ if a byte in (%edi) is null
84	je	.L3			/ goto .L3
85	incl	%edi			/ next byte
86	testl	$3, %edi		/ if %edi not word aligned
87	jnz	.L1			/ goto .L1
88	jmp	.L2			/ goto .L2 (%edi word aligned)
89	.align	4
90.L3:
91	/ %edi points to a null byte in destination string
92	movl	16(%esp), %eax		/ %eax = source string address
93	movl	20(%esp), %esi		/ %esi = number of bytes
94
95	testl	$3, %eax		/ if %eax not word aligned
96	jnz	.L4			/ goto .L4
97	cmpl	$4, %esi		/ if number of bytes < 4
98	jb	.L7			/ goto .L7
99	.align	4
100.L5:
101	movl	(%eax), %edx		/ move 1 word from (%eax) to %edx
102	movl	$0x7f7f7f7f, %ecx
103	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
104	addl	$4, %eax		/ next word
105	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
106	orl	%edx, %ecx		/ %ecx |= %edx
107	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
108	cmpl	$0x80808080, %ecx	/ if null byte in this word
109	jne	.L6			/ goto .L6
110	movl	%edx, (%edi)		/ copy this word to (%edi)
111	subl	$4, %esi		/ decrement number of bytes by 4
112	addl	$4, %edi		/ next word
113	cmpl	$4, %esi		/ if number of bytes >= 4
114	jae	.L5			/ goto .L5
115	jmp	.L7			/ goto .L7
116.L6:
117	subl	$4, %eax		/ post-incremented
118	.align	4
119.L7:
120	/ number of bytes < 4  or  a null byte found in the word
121	cmpl	$0, %esi		/ if number of bytes == 0
122	jz	.L8			/ goto .L8 (finished)
123	movb	(%eax), %dl		/ %dl = a byte in (%eax)
124	decl	%esi			/ decrement number of bytes by 1
125	movb	%dl, (%edi)		/ copy %dl to (%edi)
126	incl	%eax			/ next byte
127	incl	%edi			/ next byte
128	cmpb	$0, %dl			/ compare %dl with a null byte
129	je	.L9			/ if %dl is a null, goto .L9
130	jmp	.L7			/ goto .L7
131	.align	4
132
133.L4:
134	/ %eax not aligned
135	cmpl	$0, %esi		/ if number of bytes == 0
136	jz	.L8			/ goto .L8 (finished)
137	movb	(%eax), %dl		/ %dl = a byte in (%eax)
138	decl	%esi			/ decrement number of bytes by 1
139	movb	%dl, (%edi)		/ copy %dl to (%edi)
140	incl	%edi			/ next byte
141	incl	%eax			/ next byte
142	cmpb	$0, %dl			/ compare %dl with a null byte
143	je	.L9			/ if %dl is a null, goto .L9
144	jmp	.L4			/ goto .L4
145	.align	4
146.L8:
147	movb	$0, (%edi)		/ null termination
148.L9:
149	movl	12(%esp), %eax		/ return the destination address
150	popl	%esi			/ restore register variables
151	popl	%edi
152	ret
153	SET_SIZE(strncat)
154