xref: /titanic_44/usr/src/lib/libc/amd64/gen/strrchr.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	"strrchr.s"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate/
297c478bd9Sstevel@tonic-gate/ strrchr(sp, c)
307c478bd9Sstevel@tonic-gate/
317c478bd9Sstevel@tonic-gate/ Returns the pointer in sp at which the character c last
327c478bd9Sstevel@tonic-gate/ appears; NULL if no found
337c478bd9Sstevel@tonic-gate/
347c478bd9Sstevel@tonic-gate/ Fast assembly language version of the following C-program strrchr
357c478bd9Sstevel@tonic-gate/ which represents the `standard' for the C-library.
367c478bd9Sstevel@tonic-gate/
377c478bd9Sstevel@tonic-gate/	char *
387c478bd9Sstevel@tonic-gate/	strrchr(const char *sp, int c)
397c478bd9Sstevel@tonic-gate/	{
407c478bd9Sstevel@tonic-gate/		char	*r = NULL;
417c478bd9Sstevel@tonic-gate/
427c478bd9Sstevel@tonic-gate/		do {
437c478bd9Sstevel@tonic-gate/			if (*sp == (char)c)
447c478bd9Sstevel@tonic-gate/				r = (char *)sp;
457c478bd9Sstevel@tonic-gate/		} while (*sp++);
467c478bd9Sstevel@tonic-gate/
477c478bd9Sstevel@tonic-gate/		return (r);
487c478bd9Sstevel@tonic-gate/	}
497c478bd9Sstevel@tonic-gate/
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate#include "SYS.h"
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate	ENTRY(strrchr)		/* (char *s, int c) */
547c478bd9Sstevel@tonic-gate	movl	$0, %eax	/ %rax = NULL (current occurrence)
557c478bd9Sstevel@tonic-gate	movl	%esi, %ecx	/ %cl == char to search for
567c478bd9Sstevel@tonic-gate	testq	$3, %rdi	/ if %rdi not word aligned
577c478bd9Sstevel@tonic-gate	jnz	.L1		/ goto .L1
587c478bd9Sstevel@tonic-gate	.align	4
597c478bd9Sstevel@tonic-gate.L3:
607c478bd9Sstevel@tonic-gate	movl	(%rdi), %edx	/ move 1 word from (%rdi) to %edx
617c478bd9Sstevel@tonic-gate	cmpb	%cl, %dl	/ if the fist byte is not %cl
627c478bd9Sstevel@tonic-gate	jne	.L4		/ goto .L4
637c478bd9Sstevel@tonic-gate	movq	%rdi, %rax	/ save this address to %rax
647c478bd9Sstevel@tonic-gate.L4:
657c478bd9Sstevel@tonic-gate	cmpb	$0, %dl		/ if a null termination
667c478bd9Sstevel@tonic-gate	je	.L8		/ goto .L8
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate	cmpb	%cl, %dh	/ if the second byte is not %cl
697c478bd9Sstevel@tonic-gate	jne	.L5		/ goto .L5
707c478bd9Sstevel@tonic-gate	leaq	1(%rdi), %rax	/ save this address to %rax
717c478bd9Sstevel@tonic-gate.L5:
727c478bd9Sstevel@tonic-gate	cmpb	$0, %dh		/ if a null termination
737c478bd9Sstevel@tonic-gate	je	.L8		/ goto .L8
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate	shrl	$16, %edx	/ right shift 16-bit
767c478bd9Sstevel@tonic-gate	cmpb	%cl, %dl	/ if the third byte is not %cl
777c478bd9Sstevel@tonic-gate	jne	.L6		/ goto .L6
787c478bd9Sstevel@tonic-gate	leaq	2(%rdi), %rax	/ save this address to %rax
797c478bd9Sstevel@tonic-gate.L6:
807c478bd9Sstevel@tonic-gate	cmpb	$0, %dl		/ if a null termination
817c478bd9Sstevel@tonic-gate	je	.L8		/ goto .L8
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate	cmpb	%cl, %dh	/ if the fourth byte is not %cl
847c478bd9Sstevel@tonic-gate	jne	.L7		/ goto .L7
857c478bd9Sstevel@tonic-gate	leaq	3(%rdi), %rax	/ save this address to %rax
867c478bd9Sstevel@tonic-gate.L7:
877c478bd9Sstevel@tonic-gate	cmpb	$0, %dh		/ if a null termination
887c478bd9Sstevel@tonic-gate	je	.L8		/ goto .L8
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate	addq	$4, %rdi	/ next word
917c478bd9Sstevel@tonic-gate	jmp	.L3		/ goto .L3
927c478bd9Sstevel@tonic-gate	.align	4
937c478bd9Sstevel@tonic-gate.L1:
947c478bd9Sstevel@tonic-gate	movb	(%rdi), %dl	/ move 1 byte from (%rdi) to %dl
957c478bd9Sstevel@tonic-gate	cmpb	%cl, %dl	/ if %dl is not %cl
967c478bd9Sstevel@tonic-gate	jne	.L2		/ goto .L2
977c478bd9Sstevel@tonic-gate	movq	%rdi, %rax	/ save this address to %rax
987c478bd9Sstevel@tonic-gate.L2:
997c478bd9Sstevel@tonic-gate	cmpb	$0, %dl		/ if a null termination
1007c478bd9Sstevel@tonic-gate	je	.L8		/ goto .L8
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate	incq	%rdi		/ next byte
1037c478bd9Sstevel@tonic-gate	testq	$3, %rdi	/ if %rdi not word aligned
1047c478bd9Sstevel@tonic-gate	jnz	.L1		/ goto .L1
1057c478bd9Sstevel@tonic-gate	jmp	.L3		/ goto .L3 (word aligned)
1067c478bd9Sstevel@tonic-gate	.align	4
1077c478bd9Sstevel@tonic-gate.L8:
1087c478bd9Sstevel@tonic-gate	ret			/ %rax points to the last occurrence or NULL
1097c478bd9Sstevel@tonic-gate	SET_SIZE(strrchr)
110