xref: /titanic_44/usr/src/lib/libc/i386/gen/strrchr.s (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27	.ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31/
32/ strrchr(sp, c)
33/
34/ Returns the pointer in sp at which the character c last
35/ appears; NULL if no found
36/
37/ Fast assembly language version of the following C-program strrchr
38/ which represents the `standard' for the C-library.
39/
40/	char *
41/	strrchr(const char *sp, int c)
42/	{
43/		char	*r = NULL;
44/
45/		do {
46/			if (*sp == (char)c)
47/				r = (char *)sp;
48/		} while (*sp++);
49/
50/		return (r);
51/	}
52/
53
54#include "SYS.h"
55
56	ENTRY(strrchr)
57	pushl	%edi		/ save register variable
58	movl	8(%esp), %eax	/ %eax = string address
59	movb	12(%esp), %cl	/ %cl = char sought
60	movl	$0, %edi	/ %edi = NULL (current occurrence)
61
62	testl	$3, %eax	/ if %eax not word aligned
63	jnz	.L1		/ goto .L1
64	.align	4
65.L3:
66	movl	(%eax), %edx	/ move 1 word from (%eax) to %edx
67	cmpb	%cl, %dl	/ if the fist byte is not %cl
68	jne	.L4		/ goto .L4
69	movl	%eax, %edi	/ save this address to %edi
70.L4:
71	cmpb	$0, %dl		/ if a null termination
72	je	.L8		/ goto .L8
73
74	cmpb	%cl, %dh	/ if the second byte is not %cl
75	jne	.L5		/ goto .L5
76	leal	1(%eax), %edi	/ save this address to %edi
77.L5:
78	cmpb	$0, %dh		/ if a null termination
79	je	.L8		/ goto .L8
80
81	shrl	$16, %edx	/ right shift 16-bit
82	cmpb	%cl, %dl	/ if the third byte is not %cl
83	jne	.L6		/ goto .L6
84	leal	2(%eax), %edi	/ save this address to %edi
85.L6:
86	cmpb	$0, %dl		/ if a null termination
87	je	.L8		/ goto .L8
88
89	cmpb	%cl, %dh	/ if the fourth byte is not %cl
90	jne	.L7		/ goto .L7
91	leal	3(%eax), %edi	/ save this address to %edi
92.L7:
93	cmpb	$0, %dh		/ if a null termination
94	je	.L8		/ goto .L8
95
96	addl	$4, %eax	/ next word
97	jmp	.L3		/ goto .L3
98	.align	4
99.L1:
100	movb	(%eax), %dl	/ move 1 byte from (%eax) to %dl
101	cmpb	%cl, %dl	/ if %dl is not %cl
102	jne	.L2		/ goto .L2
103	movl	%eax, %edi	/ save this address to %edi
104.L2:
105	cmpb	$0, %dl		/ if a null termination
106	je	.L8		/ goto .L8
107
108	incl	%eax		/ next byte
109	testl	$3, %eax	/ if %eax not word aligned
110	jnz	.L1		/ goto .L1
111	jmp	.L3		/ goto .L3 (word aligned)
112	.align	4
113.L8:
114	movl	%edi, %eax	/ %edi points to the last occurrence or NULL
115	popl	%edi		/ restore register variable
116	ret
117	SET_SIZE(strrchr)
118