xref: /titanic_41/usr/src/lib/libc/amd64/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/ The above has been extended for 64-bit support.
577c478bd9Sstevel@tonic-gate/
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate#include "SYS.h"
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate	ENTRY(strcat)	/* (char *s1, char *s2) */
627c478bd9Sstevel@tonic-gate	/ find a null byte in destination string
637c478bd9Sstevel@tonic-gate	movq	%rdi,%rax		/ prepare return value
647c478bd9Sstevel@tonic-gate	movabsq	$0x7f7f7f7f7f7f7f7f, %r8	/ %r8 = 0x7f...
657c478bd9Sstevel@tonic-gate	movq	%r8, %r9
667c478bd9Sstevel@tonic-gate	notq	%r9				/ %r9 = 0x80...
677c478bd9Sstevel@tonic-gate	testq	$7, %rdi		/ if %rdi not quadword aligned
687c478bd9Sstevel@tonic-gate	jnz	.L1			/ goto .L1
697c478bd9Sstevel@tonic-gate	.align	4
707c478bd9Sstevel@tonic-gate.L2:
717c478bd9Sstevel@tonic-gate	movq	(%rdi), %rdx		/ move 1 quadword from (%rdi) to %rdx
727c478bd9Sstevel@tonic-gate	movq	%r8, %rcx
737c478bd9Sstevel@tonic-gate	andq	%rdx, %rcx		/ %rcx = %rdx & 0x7f7f7f7f7f7f7f7f
747c478bd9Sstevel@tonic-gate	addq	$8, %rdi		/ next quadword
757c478bd9Sstevel@tonic-gate	addq	%r8, %rcx		/ %rcx += 0x7f7f7f7f7f7f7f7f
767c478bd9Sstevel@tonic-gate	orq	%rdx, %rcx		/ %rcx |= %rdx
777c478bd9Sstevel@tonic-gate	andq	%r9, %rcx		/ %rcx &= 0x8080808080808080
787c478bd9Sstevel@tonic-gate	cmpq	%r9, %rcx		/ if no null byte in this quadword
797c478bd9Sstevel@tonic-gate	je	.L2			/ goto .L2
807c478bd9Sstevel@tonic-gate	subq	$8, %rdi		/ post-incremented
817c478bd9Sstevel@tonic-gate.L1:
827c478bd9Sstevel@tonic-gate	cmpb	$0, (%rdi)		/ if a byte in (%rdi) is null
837c478bd9Sstevel@tonic-gate	je	.L3			/ goto .L3
847c478bd9Sstevel@tonic-gate	incq	%rdi			/ next byte
857c478bd9Sstevel@tonic-gate	testq	$7, %rdi		/ if %rdi not quadword aligned
867c478bd9Sstevel@tonic-gate	jnz	.L1			/ goto .L1
877c478bd9Sstevel@tonic-gate	jmp	.L2			/ goto .L2 (%rdi quadword aligned)
887c478bd9Sstevel@tonic-gate	.align	4
897c478bd9Sstevel@tonic-gate.L3:
907c478bd9Sstevel@tonic-gate	/ %rdi points to a null byte in destination string
917c478bd9Sstevel@tonic-gate	testq	$7, %rsi		/ if %rsi not quadword aligned
927c478bd9Sstevel@tonic-gate	jnz	.L4			/ goto .L4
937c478bd9Sstevel@tonic-gate	.align	4
947c478bd9Sstevel@tonic-gate.L5:
957c478bd9Sstevel@tonic-gate	movq	(%rsi), %rdx		/ move 1 quadword from (%rsi) to %rdx
967c478bd9Sstevel@tonic-gate	movq	%r8, %rcx
977c478bd9Sstevel@tonic-gate	andq	%rdx, %rcx		/ %rcx = %rdx & 0x7f7f7f7f7f7f7f7f
987c478bd9Sstevel@tonic-gate	addq	$8, %rsi		/ next quadword
997c478bd9Sstevel@tonic-gate	addq	%r8, %rcx		/ %rcx += 0x7f7f7f7f7f7f7f7f
1007c478bd9Sstevel@tonic-gate	orq	%rdx, %rcx		/ %rcx |= %rdx
1017c478bd9Sstevel@tonic-gate	andq	%r9, %rcx		/ %rcx &= 0x8080808080808080
1027c478bd9Sstevel@tonic-gate	cmpq	%r9, %rcx		/ if null byte in this quadaword
1037c478bd9Sstevel@tonic-gate	jne	.L7			/ goto .L7
1047c478bd9Sstevel@tonic-gate	movq	%rdx, (%rdi)		/ copy this quadword to (%rdi)
1057c478bd9Sstevel@tonic-gate	addq	$8, %rdi		/ next quadword
1067c478bd9Sstevel@tonic-gate	jmp	.L5			/ goto .L5
1077c478bd9Sstevel@tonic-gate.L7:
1087c478bd9Sstevel@tonic-gate	subq	$8, %rsi		/ post-incremented
1097c478bd9Sstevel@tonic-gate	.align	4
1107c478bd9Sstevel@tonic-gate.L4:
1117c478bd9Sstevel@tonic-gate	movb	(%rsi), %dl		/ %dl = a byte in (%rsi)
1127c478bd9Sstevel@tonic-gate	cmpb	$0, %dl			/ compare %dl with a null byte
1137c478bd9Sstevel@tonic-gate	movb	%dl, (%rdi)		/ copy %dl to (%rdi)
1147c478bd9Sstevel@tonic-gate	je	.L6			/ if %dl is a null, goto .L6
1157c478bd9Sstevel@tonic-gate	incq	%rsi			/ next byte
1167c478bd9Sstevel@tonic-gate	incq	%rdi			/ next byte
1177c478bd9Sstevel@tonic-gate	testq	$7, %rsi		/ if %rsi not word aligned
1187c478bd9Sstevel@tonic-gate	jnz	.L4			/ goto .L4
1197c478bd9Sstevel@tonic-gate	jmp	.L5			/ goto .L5 (%rsi word aligned)
1207c478bd9Sstevel@tonic-gate	.align	4
1217c478bd9Sstevel@tonic-gate.L6:
1227c478bd9Sstevel@tonic-gate	ret
1237c478bd9Sstevel@tonic-gate	SET_SIZE(strcat)
124