xref: /titanic_41/usr/src/lib/libc/i386/gen/strcat.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	"strcat.s"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate/
297c478bd9Sstevel@tonic-gate/ strcat(s1, s2)
307c478bd9Sstevel@tonic-gate/
317c478bd9Sstevel@tonic-gate/ Concatenates s2 on the end of s1.  s1's space must be large enough.
327c478bd9Sstevel@tonic-gate/ Returns s1.
337c478bd9Sstevel@tonic-gate/
347c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program strcat
357c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library.
367c478bd9Sstevel@tonic-gate/
377c478bd9Sstevel@tonic-gate/	char *
387c478bd9Sstevel@tonic-gate/	strcat(char *s1, const char *s2)
397c478bd9Sstevel@tonic-gate/	{
407c478bd9Sstevel@tonic-gate/		char	*os1 = s1;
417c478bd9Sstevel@tonic-gate/
427c478bd9Sstevel@tonic-gate/		while (*s1++)
437c478bd9Sstevel@tonic-gate/			;
447c478bd9Sstevel@tonic-gate/		--s1;
457c478bd9Sstevel@tonic-gate/		while (*s1++ = *s2++)
467c478bd9Sstevel@tonic-gate/			;
477c478bd9Sstevel@tonic-gate/		return (os1);
487c478bd9Sstevel@tonic-gate/	}
497c478bd9Sstevel@tonic-gate/
507c478bd9Sstevel@tonic-gate/ In this assembly language version, the following expression is used
517c478bd9Sstevel@tonic-gate/ to check if a 32-bit word data contains a null byte or not:
527c478bd9Sstevel@tonic-gate/	(((A & 0x7f7f7f7f) + 0x7f7f7f7f) | A) & 0x80808080
537c478bd9Sstevel@tonic-gate/ If the above expression geneates a value other than 0x80808080,
547c478bd9Sstevel@tonic-gate/ that means the 32-bit word data contains a null byte.
557c478bd9Sstevel@tonic-gate/
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate#include "SYS.h"
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate	ENTRY(strcat)
607c478bd9Sstevel@tonic-gate	pushl	%edi			/ save register variable
617c478bd9Sstevel@tonic-gate	/ find a null byte in destination string
627c478bd9Sstevel@tonic-gate	movl	8(%esp), %edi		/ %edi = destination string address
637c478bd9Sstevel@tonic-gate	testl	$3, %edi		/ if %edi not word aligned
647c478bd9Sstevel@tonic-gate	jnz	.L1			/ goto .L1
657c478bd9Sstevel@tonic-gate	.align	4
667c478bd9Sstevel@tonic-gate.L2:
677c478bd9Sstevel@tonic-gate	movl	(%edi), %edx		/ move 1 word from (%edi) to %edx
687c478bd9Sstevel@tonic-gate	movl	$0x7f7f7f7f, %ecx
697c478bd9Sstevel@tonic-gate	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
707c478bd9Sstevel@tonic-gate	addl	$4, %edi		/ next word
717c478bd9Sstevel@tonic-gate	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
727c478bd9Sstevel@tonic-gate	orl	%edx, %ecx		/ %ecx |= %edx
737c478bd9Sstevel@tonic-gate	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
747c478bd9Sstevel@tonic-gate	cmpl	$0x80808080, %ecx	/ if no null byte in this word
757c478bd9Sstevel@tonic-gate	je	.L2			/ goto .L2
767c478bd9Sstevel@tonic-gate	subl	$4, %edi		/ post-incremented
777c478bd9Sstevel@tonic-gate.L1:
787c478bd9Sstevel@tonic-gate	cmpb	$0, (%edi)		/ if a byte in (%edi) is null
797c478bd9Sstevel@tonic-gate	je	.L3			/ goto .L3
807c478bd9Sstevel@tonic-gate	incl	%edi			/ next byte
817c478bd9Sstevel@tonic-gate	testl	$3, %edi		/ if %edi not word aligned
827c478bd9Sstevel@tonic-gate	jnz	.L1			/ goto .L1
837c478bd9Sstevel@tonic-gate	jmp	.L2			/ goto .L2 (%edi word aligned)
847c478bd9Sstevel@tonic-gate	.align	4
857c478bd9Sstevel@tonic-gate.L3:
867c478bd9Sstevel@tonic-gate	/ %edi points to a null byte in destination string
877c478bd9Sstevel@tonic-gate	movl	12(%esp), %eax		/ %eax = source string address
887c478bd9Sstevel@tonic-gate	testl	$3, %eax		/ if %eax not word aligned
897c478bd9Sstevel@tonic-gate	jnz	.L4			/ goto .L4
907c478bd9Sstevel@tonic-gate	.align	4
917c478bd9Sstevel@tonic-gate.L5:
927c478bd9Sstevel@tonic-gate	movl	(%eax), %edx		/ move 1 word from (%eax) to %edx
937c478bd9Sstevel@tonic-gate	movl	$0x7f7f7f7f, %ecx
947c478bd9Sstevel@tonic-gate	andl	%edx, %ecx		/ %ecx = %edx & 0x7f7f7f7f
957c478bd9Sstevel@tonic-gate	addl	$4, %eax		/ next word
967c478bd9Sstevel@tonic-gate	addl	$0x7f7f7f7f, %ecx	/ %ecx += 0x7f7f7f7f
977c478bd9Sstevel@tonic-gate	orl	%edx, %ecx		/ %ecx |= %edx
987c478bd9Sstevel@tonic-gate	andl	$0x80808080, %ecx	/ %ecx &= 0x80808080
997c478bd9Sstevel@tonic-gate	cmpl	$0x80808080, %ecx	/ if null byte in this word
1007c478bd9Sstevel@tonic-gate	jne	.L7			/ goto .L7
1017c478bd9Sstevel@tonic-gate	movl	%edx, (%edi)		/ copy this word to (%edi)
1027c478bd9Sstevel@tonic-gate	addl	$4, %edi		/ next word
1037c478bd9Sstevel@tonic-gate	jmp	.L5			/ goto .L5
1047c478bd9Sstevel@tonic-gate.L7:
1057c478bd9Sstevel@tonic-gate	subl	$4, %eax		/ post-incremented
1067c478bd9Sstevel@tonic-gate	.align	4
1077c478bd9Sstevel@tonic-gate.L4:
1087c478bd9Sstevel@tonic-gate	movb	(%eax), %dl		/ %dl = a byte in (%eax)
1097c478bd9Sstevel@tonic-gate	cmpb	$0, %dl			/ compare %dl with a null byte
1107c478bd9Sstevel@tonic-gate	movb	%dl, (%edi)		/ copy %dl to (%edi)
1117c478bd9Sstevel@tonic-gate	je	.L6			/ if %dl is a null, goto .L6
1127c478bd9Sstevel@tonic-gate	incl	%eax			/ next byte
1137c478bd9Sstevel@tonic-gate	incl	%edi			/ next byte
1147c478bd9Sstevel@tonic-gate	testl	$3, %eax		/ if %eax not word aligned
1157c478bd9Sstevel@tonic-gate	jnz	.L4			/ goto .L4
1167c478bd9Sstevel@tonic-gate	jmp	.L5			/ goto .L5 (%eax word aligned)
1177c478bd9Sstevel@tonic-gate	.align	4
1187c478bd9Sstevel@tonic-gate.L6:
1197c478bd9Sstevel@tonic-gate	movl	8(%esp), %eax		/ return the destination address
1207c478bd9Sstevel@tonic-gate	popl	%edi			/ restore register variable
1217c478bd9Sstevel@tonic-gate	ret
1227c478bd9Sstevel@tonic-gate	SET_SIZE(strcat)
123