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