xref: /titanic_41/usr/src/lib/libc/amd64/gen/memchr.s (revision 1ae0874509b6811fdde1dfd46f0d93fd09867a3f)
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/ memchr(sptr, c1, n)
33/
34/ Returns the pointer in sptr at which the character c1 appears;
35/ or NULL if not found in chars; doesn't stop at \0.
36/
37/ Fast assembly language version of the following C-program memchr
38/ which represents the `standard' for the C-library.
39/
40/	void *
41/	memchr(const void *sptr, int c1, size_t n)
42/	{
43/		if (n != 0) {
44/			unsigned char	c = (unsigned char)c1;
45/			const unsigned char	*sp = sptr;
46/
47/			do {
48/				if (*sp++ == c)
49/					return ((void *)--sp);
50/			} while (--n != 0);
51/		}
52/		return (NULL);
53/	}
54/
55
56#include "SYS.h"
57
58	.globl	memchr
59	.align	4
60
61	ENTRY(memchr) /* (void *s, uchar_t c, size_t n) */
62	movl	%esi, %eax	/ move "c" to %eax
63	cmpq	$4, %rdx	/ if number of bytes < 4
64	jb	.L1		/ goto .L1
65	testq	$3, %rdi	/ if %rdi not word aligned
66	jnz	.L2		/ goto .L2
67	.align	4
68.L3:
69	movl	(%rdi), %ecx	/ move 1 word from (%rdi) to %ecx
70	cmpb	%cl, %al	/ if the first byte is %al
71	je	.L4		/ goto .L4 (found)
72	cmpb	%ch, %al	/ if the second byte is %al
73	je	.L5		/ goto .L5 (found)
74	shrl	$16, %ecx	/ right shift 16-bit
75	cmpb	%cl, %al	/ if the third byte is %al
76	je	.L6		/ goto .L6 (found)
77	cmpb	%ch, %al	/ if the fourth is %al
78	je	.L7		/ goto .L7 (found)
79	subq	$4, %rdx	/ decrement number of bytes by 4
80	addq	$4, %rdi	/ next word
81	cmpq	$4, %rdx	/ if number of bytes >= 4
82	jae	.L3		/ goto .L3
83.L1:
84	cmpq	$0, %rdx	/ if number of bytes == 0
85	jz	.L8		/ goto .L8 (not found)
86	cmpb	(%rdi), %al	/ if a byte in (%rdi) is %al
87	je	.L4		/ goto .L4 (found)
88	decq	%rdx		/ decrement number of bytes by 1
89	incq	%rdi		/ next byte
90	jmp	.L1		/ goto .L1
91	.align	4
92.L8:
93	xorl	%eax, %eax	/ not found
94	ret			/ return (0)
95	.align	4
96.L2:
97	cmpq	$0, %rdx	/ if number of bytes == 0
98	jz	.L8		/ goto .L8 (not found)
99	cmpb	(%rdi), %al	/ if a byte in (%rdi) is %al
100	je	.L4		/ goto .L4 (found)
101	incq	%rdi		/ next byte
102	decq	%rdx		/ decrement number of bytes by 1
103	testq	$3, %rdi	/ if %rdi not word aligned
104	jnz	.L2		/ goto .L2
105	cmpq	$4, %rdx	/ if number of bytes >= 4
106	jae	.L3		/ goto .L3 (word aligned)
107	jmp	.L1		/ goto .L1
108	.align	4
109.L7:
110	/ found at the fourth byte
111	incq	%rdi
112.L6:
113	/ found at the third byte
114	incq	%rdi
115.L5:
116	/ found at the second byte
117	incq	%rdi
118.L4:
119	/ found at the first byte
120	movq	%rdi,%rax
121	ret
122	SET_SIZE(memchr)
123