xref: /titanic_52/usr/src/lib/libc/sparcv9/gen/memchr.s (revision b9bd317cda1afb3a01f4812de73e8cec888cbbd7)
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/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31/*
32 * Return the ptr in sptr at which the character c1 appears;
33 * or NULL if not found in n chars; don't stop at \0.
34 * void *
35 * memchr(const void *sptr, int c1, size_t n)
36 *  {
37 *       if (n != 0) {
38 *               unsigned char c = (unsigned char)c1;
39 *               const unsigned char *sp = sptr;
40 *
41 *               do {
42 *                       if (*sp++ == c)
43 *                               return ((void *)--sp);
44 *               } while (--n != 0);
45 *       }
46 *       return (NULL);
47 *  }
48 */
49
50#include <sys/asm_linkage.h>
51
52	! The first part of this algorithm focuses on determining
53	! whether or not the desired character is in the first few bytes
54	! of memory, aligning the memory for word-wise copies, and
55	! initializing registers to detect zero bytes
56
57	ENTRY(memchr)
58
59	.align 32
60
61	tst	%o2			! n == 0 ?
62	bz	%ncc, .notfound		! yup, c not found, return null ptr
63	andcc	%o0, 3, %o4		! s word aligned ?
64	add	%o0, %o2, %o0		! s + n
65	sub	%g0, %o2, %o2		! n = -n
66	bz	%ncc, .prepword		! yup, prepare for word-wise search
67	and	%o1, 0xff, %o1		! search only for this one byte
68
69	ldub	[%o0 + %o2], %o3	! s[0]
70	cmp	%o3, %o1		! s[0] == c ?
71	be	%ncc, .done		! yup, done
72	nop				!
73	addcc	%o2, 1, %o2		! n++, s++
74	bz	%ncc, .notfound		! c not found in first n bytes
75	cmp	%o4, 3			! only one byte needed to align?
76	bz	%ncc, .prepword2	! yup, prepare for word-wise search
77	sllx	%o1, 8, %g1		! start spreading c across word
78	ldub	[%o0 + %o2], %o3	! s[1]
79	cmp	%o3, %o1		! s[1] == c ?
80	be	%ncc, .done		! yup, done
81	nop				!
82	addcc	%o2, 1, %o2		! n++, s++
83	bz	%ncc, .notfound		! c not found in first n bytes
84	cmp	%o4, 2			! only two bytes needed to align?
85	bz	%ncc, .prepword3	! yup, prepare for word-wise search
86	sethi	%hi(0x80808080), %o5	! start loading Alan Mycroft's magic1
87	ldub	[%o0 + %o2], %o3	! s[1]
88	cmp	%o3, %o1		! s[1] == c ?
89	be	%ncc, .done		! yup, done
90	nop				!
91	addcc	%o2, 1, %o2		! n++, s++
92	bz	%ncc, .notfound		! c not found in first n bytes
93	nop				!
94
95.prepword:
96	sllx	%o1, 8, %g1		! spread c -------------+
97.prepword2:				!			!
98	sethi	%hi(0x80808080), %o5	! Alan Mycroft's magic2 !
99.prepword3:				!			!
100	or	%o1, %g1, %o1		!  across all <---------+
101	or	%o5, %lo(0x80808080),%o5! finish loading magic2	!
102	sllx	%o1, 16, %g1		!   four bytes <--------+
103	srlx	%o5, 7, %o4		! Alan Mycroft's magic1	!
104	or	%o1, %g1, %o1		!    of a word <--------+
105
106.searchchar:
107	lduw	[%o0 + %o2], %o3	! src word
108.searchchar2:
109	addcc	%o2, 4, %o2		! s+=4, n+=4
110	bcs	%ncc, .lastword		! if counter wraps, last word
111	xor	%o3, %o1, %g1		! tword = word ^ c
112	andn	%o5, %g1, %o3		! ~tword & 0x80808080
113	sub	%g1, %o4, %g4		! (tword - 0x01010101)
114	andcc	%o3, %g4, %g0		! ((tword - 0x01010101) & ~tword & 0x80808080)
115	bz,a	%ncc, .searchchar2	! c not found if magic expression == 0
116	lduw	[%o0 + %o2], %o3	! src word
117
118	! here we know "word" contains the searched character, and no byte in
119	! "word" exceeds n. If we had exceeded n, we would have gone to label
120	! .lastword. "tword" has null bytes where "word" had c. After
121	! restoring "tword" from "(tword - 0x01010101)" in %g1, examine "tword"
122
123.foundchar:
124	set	0xff000000, %o4		! mask for 1st byte
125	andcc	%g1, %o4, %g0		! first byte zero (= found c) ?
126	bz,a	%ncc, .done		! yup, done
127	sub	%o2, 4, %o2		! n -= 4 (undo counter bumping)
128	nop
129	set	0x00ff0000, %o5		! mask for 2nd byte
130	andcc	%g1, %o5, %g0		! second byte zero (= found c) ?
131	bz,a	%ncc, .done		! yup, done
132	sub	%o2, 3, %o2		! n -= 3 (undo counter bumping)
133	srlx	%o4, 16, %o4		! 0x0000ff00 = mask for 3rd byte
134	andcc	%g1, %o4, %g0		! third byte zero (= found c) ?
135	bz,a	%ncc, .done		! nope, must be fourth byte
136	sub	%o2, 2, %o2		! n -= 2 (undo counter bumping)
137	sub	%o2, 1, %o2		! n -= 1, if fourth byte
138	retl				! done with leaf function
139	add	%o0, %o2, %o0		! return pointer to c in s
140.done:
141	retl				! done with leaf function
142	add	%o0, %o2, %o0		! return pointer to c in s
143	nop
144	nop
145
146	! Here we know that "word" is the last word in the search, and that
147	! some bytes possibly exceed n. However, "word" might also contain c.
148	! "tword" (in %g1) has null bytes where "word" had c. Examine "tword"
149	! while keeping track of number of remaining bytes
150
151.lastword:
152	set	0xff000000, %o4		! mask for 1st byte
153	sub	%o2, 4, %o2		! n -= 4 (undo counter bumping)
154	andcc	%g1, %o4, %g0		! first byte zero (= found c) ?
155	bz	%ncc, .done		! yup, done
156	set	0x00ff0000, %o5		! mask for 2nd byte
157	addcc	%o2, 1, %o2		! n += 1
158	bz	%ncc, .notfound		! c not found in first n bytes
159	andcc	%g1, %o5, %g0		! second byte zero (= found c) ?
160	bz	%ncc, .done		! yup, done
161	srlx	%o4, 16, %o4		! 0x0000ff00 = mask for 3rd byte
162	addcc	%o2, 1, %o2		! n += 1
163	bz	%ncc, .notfound		! c not found in first n bytes
164	andcc	%g1, %o4, %g0		! third byte zero (= found c) ?
165	bz	%ncc, .done		! yup, done
166	nop				!
167	addcc	%o2, 1, %o2		! n += 1
168	bz	%ncc, .notfound		! c not found in first n bytes
169	andcc	%g1, 0xff, %g0		! fourth byte zero (= found c) ?
170	bz	%ncc, .done		! yup, done
171	nop
172
173.notfound:
174	retl				! done with leaf function
175	mov	%g0, %o0		! return null pointer
176
177	SET_SIZE(memchr)
178