xref: /onnv-gate/usr/src/lib/libc/i386/gen/strncpy.s (revision 7298:b69e27387f74)
10Sstevel@tonic-gate/*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7298SMark.J.Nelson@Sun.COM * Common Development and Distribution License (the "License").
6*7298SMark.J.Nelson@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate/*
220Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
26*7298SMark.J.Nelson@Sun.COM	.file	"strncpy.s"
270Sstevel@tonic-gate
280Sstevel@tonic-gate/
290Sstevel@tonic-gate/ strncpy(s1, s2, n)
300Sstevel@tonic-gate/
310Sstevel@tonic-gate/ Copies s2 to s1, truncating or null-padding to always copy n bytes
320Sstevel@tonic-gate/ Returns s1
330Sstevel@tonic-gate/
340Sstevel@tonic-gate/ Fast assembly language version of the following C-program strncpy
350Sstevel@tonic-gate/ which represents the `standard' for the C-library.
360Sstevel@tonic-gate/
370Sstevel@tonic-gate/	char *
380Sstevel@tonic-gate/	strncpy(char *s1, const char *s2, size_t n)
390Sstevel@tonic-gate/	{
400Sstevel@tonic-gate/		char	*os1 = s1;
410Sstevel@tonic-gate/
420Sstevel@tonic-gate/		n++;
430Sstevel@tonic-gate/		while ((--n != 0) && ((*s1++ = *s2++) != '\0'))
440Sstevel@tonic-gate/			;
450Sstevel@tonic-gate/		if (n != 0)
460Sstevel@tonic-gate/			while (--n != 0)
470Sstevel@tonic-gate/				*s1++ = '\0';
480Sstevel@tonic-gate/		return (os1);
490Sstevel@tonic-gate/	}
500Sstevel@tonic-gate/
510Sstevel@tonic-gate/ In this assembly language version, the following expression is used
520Sstevel@tonic-gate/ to check if a 32-bit word data contains a null byte or not:
530Sstevel@tonic-gate/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
540Sstevel@tonic-gate/ If the above expression geneates a value other than 0x80808080,
550Sstevel@tonic-gate/ that means the 32-bit word data contains a null byte.
560Sstevel@tonic-gate/
570Sstevel@tonic-gate
580Sstevel@tonic-gate/ Assume relatively long strings and small numbers of nulls at end.
590Sstevel@tonic-gate
600Sstevel@tonic-gate#include "SYS.h"
610Sstevel@tonic-gate
620Sstevel@tonic-gate	ENTRY(strncpy)
630Sstevel@tonic-gate	pushl	%edi			/ save register variables
640Sstevel@tonic-gate	pushl	%esi
650Sstevel@tonic-gate
660Sstevel@tonic-gate	movl	16(%esp), %eax		/ %eax = source string address
670Sstevel@tonic-gate	movl	12(%esp), %edi		/ %edi = destination string address
680Sstevel@tonic-gate	movl	20(%esp), %esi		/ %esi = number of bytes
690Sstevel@tonic-gate
700Sstevel@tonic-gate	testl	$3, %eax		/ if %eax not word aligned
710Sstevel@tonic-gate	jnz	.L1			/ goto .L1
720Sstevel@tonic-gate.L8:
730Sstevel@tonic-gate	cmpl	$4, %esi		/ if number of bytes < 4
740Sstevel@tonic-gate	jb	.L4			/ goto .L4
750Sstevel@tonic-gate	.align	4
760Sstevel@tonic-gate.L2:
770Sstevel@tonic-gate	movl	(%eax), %edx		/ move 1 word from (%eax) to %edx
780Sstevel@tonic-gate	movl	$0x7f7f7f7f, %ecx
790Sstevel@tonic-gate	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
800Sstevel@tonic-gate	addl	$4, %eax		/ next word
810Sstevel@tonic-gate	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
820Sstevel@tonic-gate	orl	%edx, %ecx		/ %ecx |= %edx
830Sstevel@tonic-gate	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
840Sstevel@tonic-gate	cmpl	$0x80808080, %ecx	/ if null byte in this word
850Sstevel@tonic-gate	jne	.L3			/ goto .L3
860Sstevel@tonic-gate	movl	%edx, (%edi)		/ copy this word to (%edi)
870Sstevel@tonic-gate	subl	$4, %esi		/ decrement number of bytes by 4
880Sstevel@tonic-gate	addl	$4, %edi		/ next word
890Sstevel@tonic-gate	cmpl	$4, %esi		/ if number of bytes >= 4
900Sstevel@tonic-gate	jae	.L2			/ goto .L2
910Sstevel@tonic-gate	jmp	.L4			/ goto .L4
920Sstevel@tonic-gate.L3:
930Sstevel@tonic-gate	subl	$4, %eax		/ post-incremented
940Sstevel@tonic-gate	.align	4
950Sstevel@tonic-gate.L4:
960Sstevel@tonic-gate	/ (number of bytes < 4) or (a null byte found in the word)
970Sstevel@tonic-gate	cmpl	$0, %esi		/ if number of bytes == 0
980Sstevel@tonic-gate	je	.L7			/ goto .L7 (finished)
990Sstevel@tonic-gate	movb	(%eax), %dl		/ %dl = a byte in (%eax)
1000Sstevel@tonic-gate	decl	%esi			/ decrement number of bytes by 1
1010Sstevel@tonic-gate	movb	%dl, (%edi)		/ copy %dl to (%edi)
1020Sstevel@tonic-gate	incl	%eax			/ next byte
1030Sstevel@tonic-gate	incl	%edi			/ next byte
1040Sstevel@tonic-gate	cmpb	$0, %dl			/ compare %dl with a null byte
1050Sstevel@tonic-gate	je	.L5			/ if %dl is a null, goto .L5
1060Sstevel@tonic-gate	jmp	.L4			/ goto .L4
1070Sstevel@tonic-gate	.align	4
1080Sstevel@tonic-gate.L1:
1090Sstevel@tonic-gate	/ %eax not aligned
1100Sstevel@tonic-gate	cmpl	$0, %esi		/ if number of bytes == 0
1110Sstevel@tonic-gate	je	.L7			/ goto .L7 (finished)
1120Sstevel@tonic-gate	movb	(%eax), %dl		/ %dl = a byte in (%eax)
1130Sstevel@tonic-gate	decl	%esi			/ decrement number of bytes by 1
1140Sstevel@tonic-gate	movb	%dl, (%edi)		/ copy %dl to (%edi)
1150Sstevel@tonic-gate	incl	%edi			/ next byte
1160Sstevel@tonic-gate	incl	%eax			/ next byte
1170Sstevel@tonic-gate	cmpb	$0, %dl			/ compare %dl with a null byte
1180Sstevel@tonic-gate	je	.L5			/ if %dl is a null, goto .L5
1190Sstevel@tonic-gate	testl	$3, %eax		/ if %eax word aligned
1200Sstevel@tonic-gate	jz	.L8			/ goto .L8
1210Sstevel@tonic-gate	jmp	.L1			/ goto .L1 (not word aligned)
1220Sstevel@tonic-gate	.align	4
1230Sstevel@tonic-gate.L5:
1240Sstevel@tonic-gate	movl	%esi, %ecx		/ %ecx = length to copy null bytes
1250Sstevel@tonic-gate	xorl	%eax, %eax		/ clear %eax
1260Sstevel@tonic-gate	shrl	$2, %ecx		/ %ecx = words to copy null bytes
1270Sstevel@tonic-gate	rep ; sstol			/ rep;sstol is optimal
1280Sstevel@tonic-gate	andl	$3, %esi		/ %esi = leftover bytes
1290Sstevel@tonic-gate.L6:
1300Sstevel@tonic-gate	cmpl	$0, %esi		/ if number of bytes == 0
1310Sstevel@tonic-gate	jz	.L7			/ goto .L7 (finished)
1320Sstevel@tonic-gate	movb	$0, (%edi)		/ move a null byte to (%edi)
1330Sstevel@tonic-gate	decl	%esi			/ decrement number of bytes by 1
1340Sstevel@tonic-gate	incl	%edi			/ next byte
1350Sstevel@tonic-gate	jmp	.L6			/ goto .L6
1360Sstevel@tonic-gate	.align	4
1370Sstevel@tonic-gate.L7:
1380Sstevel@tonic-gate	movl	12(%esp), %eax		/ return the destination address
1390Sstevel@tonic-gate	popl	%esi			/ restore register variables
1400Sstevel@tonic-gate	popl	%edi
1410Sstevel@tonic-gate	ret
1420Sstevel@tonic-gate	SET_SIZE(strncpy)
143