xref: /titanic_52/usr/src/lib/libc/amd64/gen/strrchr.s (revision c2580b931007758eab8cb5ae8726ebe1588e259b)
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)		/* (char *s, int c) */
57	movl	$0, %eax	/ %rax = NULL (current occurrence)
58	movl	%esi, %ecx	/ %cl == char to search for
59	testq	$3, %rdi	/ if %rdi not word aligned
60	jnz	.L1		/ goto .L1
61	.align	4
62.L3:
63	movl	(%rdi), %edx	/ move 1 word from (%rdi) to %edx
64	cmpb	%cl, %dl	/ if the fist byte is not %cl
65	jne	.L4		/ goto .L4
66	movq	%rdi, %rax	/ save this address to %rax
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	leaq	1(%rdi), %rax	/ save this address to %rax
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	leaq	2(%rdi), %rax	/ save this address to %rax
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	leaq	3(%rdi), %rax	/ save this address to %rax
89.L7:
90	cmpb	$0, %dh		/ if a null termination
91	je	.L8		/ goto .L8
92
93	addq	$4, %rdi	/ next word
94	jmp	.L3		/ goto .L3
95	.align	4
96.L1:
97	movb	(%rdi), %dl	/ move 1 byte from (%rdi) to %dl
98	cmpb	%cl, %dl	/ if %dl is not %cl
99	jne	.L2		/ goto .L2
100	movq	%rdi, %rax	/ save this address to %rax
101.L2:
102	cmpb	$0, %dl		/ if a null termination
103	je	.L8		/ goto .L8
104
105	incq	%rdi		/ next byte
106	testq	$3, %rdi	/ if %rdi not word aligned
107	jnz	.L1		/ goto .L1
108	jmp	.L3		/ goto .L3 (word aligned)
109	.align	4
110.L8:
111	ret			/ %rax points to the last occurrence or NULL
112	SET_SIZE(strrchr)
113