xref: /titanic_44/usr/src/lib/libc/i386/gen/strncpy.s (revision c651b32e568cbf9b715a127805fe1cba321be5b0)
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	"strncpy.s"
27
28/
29/ strncpy(s1, s2, n)
30/
31/ Copies s2 to s1, truncating or null-padding to always copy n bytes
32/ Returns s1
33/
34/ Fast assembly language version of the following C-program strncpy
35/ which represents the `standard' for the C-library.
36/
37/	char *
38/	strncpy(char *s1, const char *s2, size_t n)
39/	{
40/		char	*os1 = s1;
41/
42/		n++;
43/		while ((--n != 0) && ((*s1++ = *s2++) != '\0'))
44/			;
45/		if (n != 0)
46/			while (--n != 0)
47/				*s1++ = '\0';
48/		return (os1);
49/	}
50/
51/ In this assembly language version, the following expression is used
52/ to check if a 32-bit word data contains a null byte or not:
53/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
54/ If the above expression geneates a value other than 0x80808080,
55/ that means the 32-bit word data contains a null byte.
56/
57
58/ Assume relatively long strings and small numbers of nulls at end.
59
60#include "SYS.h"
61
62	ENTRY(strncpy)
63	pushl	%edi			/ save register variables
64	pushl	%esi
65
66	movl	16(%esp), %eax		/ %eax = source string address
67	movl	12(%esp), %edi		/ %edi = destination string address
68	movl	20(%esp), %esi		/ %esi = number of bytes
69
70	testl	$3, %eax		/ if %eax not word aligned
71	jnz	.L1			/ goto .L1
72.L8:
73	cmpl	$4, %esi		/ if number of bytes < 4
74	jb	.L4			/ goto .L4
75	.align	4
76.L2:
77	movl	(%eax), %edx		/ move 1 word from (%eax) to %edx
78	movl	$0x7f7f7f7f, %ecx
79	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
80	addl	$4, %eax		/ next word
81	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
82	orl	%edx, %ecx		/ %ecx |= %edx
83	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
84	cmpl	$0x80808080, %ecx	/ if null byte in this word
85	jne	.L3			/ goto .L3
86	movl	%edx, (%edi)		/ copy this word to (%edi)
87	subl	$4, %esi		/ decrement number of bytes by 4
88	addl	$4, %edi		/ next word
89	cmpl	$4, %esi		/ if number of bytes >= 4
90	jae	.L2			/ goto .L2
91	jmp	.L4			/ goto .L4
92.L3:
93	subl	$4, %eax		/ post-incremented
94	.align	4
95.L4:
96	/ (number of bytes < 4) or (a null byte found in the word)
97	cmpl	$0, %esi		/ if number of bytes == 0
98	je	.L7			/ goto .L7 (finished)
99	movb	(%eax), %dl		/ %dl = a byte in (%eax)
100	decl	%esi			/ decrement number of bytes by 1
101	movb	%dl, (%edi)		/ copy %dl to (%edi)
102	incl	%eax			/ next byte
103	incl	%edi			/ next byte
104	cmpb	$0, %dl			/ compare %dl with a null byte
105	je	.L5			/ if %dl is a null, goto .L5
106	jmp	.L4			/ goto .L4
107	.align	4
108.L1:
109	/ %eax not aligned
110	cmpl	$0, %esi		/ if number of bytes == 0
111	je	.L7			/ goto .L7 (finished)
112	movb	(%eax), %dl		/ %dl = a byte in (%eax)
113	decl	%esi			/ decrement number of bytes by 1
114	movb	%dl, (%edi)		/ copy %dl to (%edi)
115	incl	%edi			/ next byte
116	incl	%eax			/ next byte
117	cmpb	$0, %dl			/ compare %dl with a null byte
118	je	.L5			/ if %dl is a null, goto .L5
119	testl	$3, %eax		/ if %eax word aligned
120	jz	.L8			/ goto .L8
121	jmp	.L1			/ goto .L1 (not word aligned)
122	.align	4
123.L5:
124	movl	%esi, %ecx		/ %ecx = length to copy null bytes
125	xorl	%eax, %eax		/ clear %eax
126	shrl	$2, %ecx		/ %ecx = words to copy null bytes
127	rep ; sstol			/ rep;sstol is optimal
128	andl	$3, %esi		/ %esi = leftover bytes
129.L6:
130	cmpl	$0, %esi		/ if number of bytes == 0
131	jz	.L7			/ goto .L7 (finished)
132	movb	$0, (%edi)		/ move a null byte to (%edi)
133	decl	%esi			/ decrement number of bytes by 1
134	incl	%edi			/ next byte
135	jmp	.L6			/ goto .L6
136	.align	4
137.L7:
138	movl	12(%esp), %eax		/ return the destination address
139	popl	%esi			/ restore register variables
140	popl	%edi
141	ret
142	SET_SIZE(strncpy)
143