xref: /titanic_51/usr/src/lib/libc/i386/gen/strncat.s (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
17c478bd9Sstevel@tonic-gate/*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*9a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License").
6*9a70fc3bSMark J. Nelson * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate/*
227c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
26*9a70fc3bSMark J. Nelson	.file	"strncat.s"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate/
297c478bd9Sstevel@tonic-gate/ strncat(s1, s2, n)
307c478bd9Sstevel@tonic-gate/
317c478bd9Sstevel@tonic-gate/ Concatenates s2 on the end of s1.  s1's space must be large enough.
327c478bd9Sstevel@tonic-gate/ At most n characters are moved.
337c478bd9Sstevel@tonic-gate/ Returns s1.
347c478bd9Sstevel@tonic-gate/
357c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program strncat
367c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library.
377c478bd9Sstevel@tonic-gate/
387c478bd9Sstevel@tonic-gate/	char *
397c478bd9Sstevel@tonic-gate/	strncat(char *s1, const char *s2, size_t n)
407c478bd9Sstevel@tonic-gate/	{
417c478bd9Sstevel@tonic-gate/		char	*os1 = s1;
427c478bd9Sstevel@tonic-gate/
437c478bd9Sstevel@tonic-gate/		n++;
447c478bd9Sstevel@tonic-gate/		while (*s1++)
457c478bd9Sstevel@tonic-gate/			;
467c478bd9Sstevel@tonic-gate/		--s1;
477c478bd9Sstevel@tonic-gate/		while (*s1++ = *s2++)
487c478bd9Sstevel@tonic-gate/			if (--n == 0) {
497c478bd9Sstevel@tonic-gate/				s1[-1] = '\0';
507c478bd9Sstevel@tonic-gate/				break;
517c478bd9Sstevel@tonic-gate/			}
527c478bd9Sstevel@tonic-gate/		return (os1);
537c478bd9Sstevel@tonic-gate/	}
547c478bd9Sstevel@tonic-gate/
557c478bd9Sstevel@tonic-gate/ In this assembly language version, the following expression is used
567c478bd9Sstevel@tonic-gate/ to check if a 32-bit word data contains a null byte or not:
577c478bd9Sstevel@tonic-gate/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
587c478bd9Sstevel@tonic-gate/ If the above expression geneates a value other than 0x80808080,
597c478bd9Sstevel@tonic-gate/ that means the 32-bit word data contains a null byte.
607c478bd9Sstevel@tonic-gate/
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate#include "SYS.h"
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate	ENTRY(strncat)
657c478bd9Sstevel@tonic-gate	pushl	%edi			/ save register variables
667c478bd9Sstevel@tonic-gate	pushl	%esi
677c478bd9Sstevel@tonic-gate	movl	12(%esp), %edi		/ %edi = destination string address
687c478bd9Sstevel@tonic-gate	testl	$3, %edi		/ if %edi not word aligned
697c478bd9Sstevel@tonic-gate	jnz	.L1			/ goto .L1
707c478bd9Sstevel@tonic-gate	.align	4
717c478bd9Sstevel@tonic-gate.L2:
727c478bd9Sstevel@tonic-gate	movl	(%edi), %edx		/ move 1 word from (%edi) to %edx
737c478bd9Sstevel@tonic-gate	movl	$0x7f7f7f7f, %ecx
747c478bd9Sstevel@tonic-gate	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
757c478bd9Sstevel@tonic-gate	addl	$4, %edi		/ next word
767c478bd9Sstevel@tonic-gate	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
777c478bd9Sstevel@tonic-gate	orl	%edx, %ecx		/ %ecx |= %edx
787c478bd9Sstevel@tonic-gate	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
797c478bd9Sstevel@tonic-gate	cmpl	$0x80808080, %ecx	/ if no null byte in this word
807c478bd9Sstevel@tonic-gate	je	.L2			/ goto .L2
817c478bd9Sstevel@tonic-gate	subl	$4, %edi		/ post-incremented
827c478bd9Sstevel@tonic-gate.L1:
837c478bd9Sstevel@tonic-gate	cmpb	$0, (%edi)		/ if a byte in (%edi) is null
847c478bd9Sstevel@tonic-gate	je	.L3			/ goto .L3
857c478bd9Sstevel@tonic-gate	incl	%edi			/ next byte
867c478bd9Sstevel@tonic-gate	testl	$3, %edi		/ if %edi not word aligned
877c478bd9Sstevel@tonic-gate	jnz	.L1			/ goto .L1
887c478bd9Sstevel@tonic-gate	jmp	.L2			/ goto .L2 (%edi word aligned)
897c478bd9Sstevel@tonic-gate	.align	4
907c478bd9Sstevel@tonic-gate.L3:
917c478bd9Sstevel@tonic-gate	/ %edi points to a null byte in destination string
927c478bd9Sstevel@tonic-gate	movl	16(%esp), %eax		/ %eax = source string address
937c478bd9Sstevel@tonic-gate	movl	20(%esp), %esi		/ %esi = number of bytes
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate	testl	$3, %eax		/ if %eax not word aligned
967c478bd9Sstevel@tonic-gate	jnz	.L4			/ goto .L4
977c478bd9Sstevel@tonic-gate	cmpl	$4, %esi		/ if number of bytes < 4
987c478bd9Sstevel@tonic-gate	jb	.L7			/ goto .L7
997c478bd9Sstevel@tonic-gate	.align	4
1007c478bd9Sstevel@tonic-gate.L5:
1017c478bd9Sstevel@tonic-gate	movl	(%eax), %edx		/ move 1 word from (%eax) to %edx
1027c478bd9Sstevel@tonic-gate	movl	$0x7f7f7f7f, %ecx
1037c478bd9Sstevel@tonic-gate	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
1047c478bd9Sstevel@tonic-gate	addl	$4, %eax		/ next word
1057c478bd9Sstevel@tonic-gate	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
1067c478bd9Sstevel@tonic-gate	orl	%edx, %ecx		/ %ecx |= %edx
1077c478bd9Sstevel@tonic-gate	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
1087c478bd9Sstevel@tonic-gate	cmpl	$0x80808080, %ecx	/ if null byte in this word
1097c478bd9Sstevel@tonic-gate	jne	.L6			/ goto .L6
1107c478bd9Sstevel@tonic-gate	movl	%edx, (%edi)		/ copy this word to (%edi)
1117c478bd9Sstevel@tonic-gate	subl	$4, %esi		/ decrement number of bytes by 4
1127c478bd9Sstevel@tonic-gate	addl	$4, %edi		/ next word
1137c478bd9Sstevel@tonic-gate	cmpl	$4, %esi		/ if number of bytes >= 4
1147c478bd9Sstevel@tonic-gate	jae	.L5			/ goto .L5
1157c478bd9Sstevel@tonic-gate	jmp	.L7			/ goto .L7
1167c478bd9Sstevel@tonic-gate.L6:
1177c478bd9Sstevel@tonic-gate	subl	$4, %eax		/ post-incremented
1187c478bd9Sstevel@tonic-gate	.align	4
1197c478bd9Sstevel@tonic-gate.L7:
1207c478bd9Sstevel@tonic-gate	/ number of bytes < 4  or  a null byte found in the word
1217c478bd9Sstevel@tonic-gate	cmpl	$0, %esi		/ if number of bytes == 0
1227c478bd9Sstevel@tonic-gate	jz	.L8			/ goto .L8 (finished)
1237c478bd9Sstevel@tonic-gate	movb	(%eax), %dl		/ %dl = a byte in (%eax)
1247c478bd9Sstevel@tonic-gate	decl	%esi			/ decrement number of bytes by 1
1257c478bd9Sstevel@tonic-gate	movb	%dl, (%edi)		/ copy %dl to (%edi)
1267c478bd9Sstevel@tonic-gate	incl	%eax			/ next byte
1277c478bd9Sstevel@tonic-gate	incl	%edi			/ next byte
1287c478bd9Sstevel@tonic-gate	cmpb	$0, %dl			/ compare %dl with a null byte
1297c478bd9Sstevel@tonic-gate	je	.L9			/ if %dl is a null, goto .L9
1307c478bd9Sstevel@tonic-gate	jmp	.L7			/ goto .L7
1317c478bd9Sstevel@tonic-gate	.align	4
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate.L4:
1347c478bd9Sstevel@tonic-gate	/ %eax not aligned
1357c478bd9Sstevel@tonic-gate	cmpl	$0, %esi		/ if number of bytes == 0
1367c478bd9Sstevel@tonic-gate	jz	.L8			/ goto .L8 (finished)
1377c478bd9Sstevel@tonic-gate	movb	(%eax), %dl		/ %dl = a byte in (%eax)
1387c478bd9Sstevel@tonic-gate	decl	%esi			/ decrement number of bytes by 1
1397c478bd9Sstevel@tonic-gate	movb	%dl, (%edi)		/ copy %dl to (%edi)
1407c478bd9Sstevel@tonic-gate	incl	%edi			/ next byte
1417c478bd9Sstevel@tonic-gate	incl	%eax			/ next byte
1427c478bd9Sstevel@tonic-gate	cmpb	$0, %dl			/ compare %dl with a null byte
1437c478bd9Sstevel@tonic-gate	je	.L9			/ if %dl is a null, goto .L9
1447c478bd9Sstevel@tonic-gate	jmp	.L4			/ goto .L4
1457c478bd9Sstevel@tonic-gate	.align	4
1467c478bd9Sstevel@tonic-gate.L8:
1477c478bd9Sstevel@tonic-gate	movb	$0, (%edi)		/ null termination
1487c478bd9Sstevel@tonic-gate.L9:
1497c478bd9Sstevel@tonic-gate	movl	12(%esp), %eax		/ return the destination address
1507c478bd9Sstevel@tonic-gate	popl	%esi			/ restore register variables
1517c478bd9Sstevel@tonic-gate	popl	%edi
1527c478bd9Sstevel@tonic-gate	ret
1537c478bd9Sstevel@tonic-gate	SET_SIZE(strncat)
154