xref: /onnv-gate/usr/src/lib/libc/amd64/gen/strrchr.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	"strrchr.s"
270Sstevel@tonic-gate
280Sstevel@tonic-gate/
290Sstevel@tonic-gate/ strrchr(sp, c)
300Sstevel@tonic-gate/
310Sstevel@tonic-gate/ Returns the pointer in sp at which the character c last
320Sstevel@tonic-gate/ appears; NULL if no found
330Sstevel@tonic-gate/
340Sstevel@tonic-gate/ Fast assembly language version of the following C-program strrchr
350Sstevel@tonic-gate/ which represents the `standard' for the C-library.
360Sstevel@tonic-gate/
370Sstevel@tonic-gate/	char *
380Sstevel@tonic-gate/	strrchr(const char *sp, int c)
390Sstevel@tonic-gate/	{
400Sstevel@tonic-gate/		char	*r = NULL;
410Sstevel@tonic-gate/
420Sstevel@tonic-gate/		do {
430Sstevel@tonic-gate/			if (*sp == (char)c)
440Sstevel@tonic-gate/				r = (char *)sp;
450Sstevel@tonic-gate/		} while (*sp++);
460Sstevel@tonic-gate/
470Sstevel@tonic-gate/		return (r);
480Sstevel@tonic-gate/	}
490Sstevel@tonic-gate/
500Sstevel@tonic-gate
510Sstevel@tonic-gate#include "SYS.h"
520Sstevel@tonic-gate
530Sstevel@tonic-gate	ENTRY(strrchr)		/* (char *s, int c) */
540Sstevel@tonic-gate	movl	$0, %eax	/ %rax = NULL (current occurrence)
550Sstevel@tonic-gate	movl	%esi, %ecx	/ %cl == char to search for
560Sstevel@tonic-gate	testq	$3, %rdi	/ if %rdi not word aligned
570Sstevel@tonic-gate	jnz	.L1		/ goto .L1
580Sstevel@tonic-gate	.align	4
590Sstevel@tonic-gate.L3:
600Sstevel@tonic-gate	movl	(%rdi), %edx	/ move 1 word from (%rdi) to %edx
610Sstevel@tonic-gate	cmpb	%cl, %dl	/ if the fist byte is not %cl
620Sstevel@tonic-gate	jne	.L4		/ goto .L4
630Sstevel@tonic-gate	movq	%rdi, %rax	/ save this address to %rax
640Sstevel@tonic-gate.L4:
650Sstevel@tonic-gate	cmpb	$0, %dl		/ if a null termination
660Sstevel@tonic-gate	je	.L8		/ goto .L8
670Sstevel@tonic-gate
680Sstevel@tonic-gate	cmpb	%cl, %dh	/ if the second byte is not %cl
690Sstevel@tonic-gate	jne	.L5		/ goto .L5
700Sstevel@tonic-gate	leaq	1(%rdi), %rax	/ save this address to %rax
710Sstevel@tonic-gate.L5:
720Sstevel@tonic-gate	cmpb	$0, %dh		/ if a null termination
730Sstevel@tonic-gate	je	.L8		/ goto .L8
740Sstevel@tonic-gate
750Sstevel@tonic-gate	shrl	$16, %edx	/ right shift 16-bit
760Sstevel@tonic-gate	cmpb	%cl, %dl	/ if the third byte is not %cl
770Sstevel@tonic-gate	jne	.L6		/ goto .L6
780Sstevel@tonic-gate	leaq	2(%rdi), %rax	/ save this address to %rax
790Sstevel@tonic-gate.L6:
800Sstevel@tonic-gate	cmpb	$0, %dl		/ if a null termination
810Sstevel@tonic-gate	je	.L8		/ goto .L8
820Sstevel@tonic-gate
830Sstevel@tonic-gate	cmpb	%cl, %dh	/ if the fourth byte is not %cl
840Sstevel@tonic-gate	jne	.L7		/ goto .L7
850Sstevel@tonic-gate	leaq	3(%rdi), %rax	/ save this address to %rax
860Sstevel@tonic-gate.L7:
870Sstevel@tonic-gate	cmpb	$0, %dh		/ if a null termination
880Sstevel@tonic-gate	je	.L8		/ goto .L8
890Sstevel@tonic-gate
900Sstevel@tonic-gate	addq	$4, %rdi	/ next word
910Sstevel@tonic-gate	jmp	.L3		/ goto .L3
920Sstevel@tonic-gate	.align	4
930Sstevel@tonic-gate.L1:
940Sstevel@tonic-gate	movb	(%rdi), %dl	/ move 1 byte from (%rdi) to %dl
950Sstevel@tonic-gate	cmpb	%cl, %dl	/ if %dl is not %cl
960Sstevel@tonic-gate	jne	.L2		/ goto .L2
970Sstevel@tonic-gate	movq	%rdi, %rax	/ save this address to %rax
980Sstevel@tonic-gate.L2:
990Sstevel@tonic-gate	cmpb	$0, %dl		/ if a null termination
1000Sstevel@tonic-gate	je	.L8		/ goto .L8
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate	incq	%rdi		/ next byte
1030Sstevel@tonic-gate	testq	$3, %rdi	/ if %rdi not word aligned
1040Sstevel@tonic-gate	jnz	.L1		/ goto .L1
1050Sstevel@tonic-gate	jmp	.L3		/ goto .L3 (word aligned)
1060Sstevel@tonic-gate	.align	4
1070Sstevel@tonic-gate.L8:
1080Sstevel@tonic-gate	ret			/ %rax points to the last occurrence or NULL
1090Sstevel@tonic-gate	SET_SIZE(strrchr)
110