xref: /titanic_41/usr/src/lib/libc/amd64/gen/strrchr.s (revision 450396635f70344c58b6b1e4db38cf17ff34445c)
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)		/* (char *s, int c) */
54	movl	$0, %eax	/ %rax = NULL (current occurrence)
55	movl	%esi, %ecx	/ %cl == char to search for
56	testq	$3, %rdi	/ if %rdi not word aligned
57	jnz	.L1		/ goto .L1
58	.align	4
59.L3:
60	movl	(%rdi), %edx	/ move 1 word from (%rdi) to %edx
61	cmpb	%cl, %dl	/ if the fist byte is not %cl
62	jne	.L4		/ goto .L4
63	movq	%rdi, %rax	/ save this address to %rax
64.L4:
65	cmpb	$0, %dl		/ if a null termination
66	je	.L8		/ goto .L8
67
68	cmpb	%cl, %dh	/ if the second byte is not %cl
69	jne	.L5		/ goto .L5
70	leaq	1(%rdi), %rax	/ save this address to %rax
71.L5:
72	cmpb	$0, %dh		/ if a null termination
73	je	.L8		/ goto .L8
74
75	shrl	$16, %edx	/ right shift 16-bit
76	cmpb	%cl, %dl	/ if the third byte is not %cl
77	jne	.L6		/ goto .L6
78	leaq	2(%rdi), %rax	/ save this address to %rax
79.L6:
80	cmpb	$0, %dl		/ if a null termination
81	je	.L8		/ goto .L8
82
83	cmpb	%cl, %dh	/ if the fourth byte is not %cl
84	jne	.L7		/ goto .L7
85	leaq	3(%rdi), %rax	/ save this address to %rax
86.L7:
87	cmpb	$0, %dh		/ if a null termination
88	je	.L8		/ goto .L8
89
90	addq	$4, %rdi	/ next word
91	jmp	.L3		/ goto .L3
92	.align	4
93.L1:
94	movb	(%rdi), %dl	/ move 1 byte from (%rdi) to %dl
95	cmpb	%cl, %dl	/ if %dl is not %cl
96	jne	.L2		/ goto .L2
97	movq	%rdi, %rax	/ save this address to %rax
98.L2:
99	cmpb	$0, %dl		/ if a null termination
100	je	.L8		/ goto .L8
101
102	incq	%rdi		/ next byte
103	testq	$3, %rdi	/ if %rdi not word aligned
104	jnz	.L1		/ goto .L1
105	jmp	.L3		/ goto .L3 (word aligned)
106	.align	4
107.L8:
108	ret			/ %rax points to the last occurrence or NULL
109	SET_SIZE(strrchr)
110