xref: /minix3/common/lib/libc/arch/arm/string/strchr_arm.S (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc/*-
2*84d9c625SLionel Sambuc * Copyright (c) 2013 The NetBSD Foundation, Inc.
3*84d9c625SLionel Sambuc * All rights reserved.
4*84d9c625SLionel Sambuc *
5*84d9c625SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
6*84d9c625SLionel Sambuc * by Matt Thomas of 3am Software Foundry.
7*84d9c625SLionel Sambuc *
8*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
10*84d9c625SLionel Sambuc * are met:
11*84d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
12*84d9c625SLionel Sambuc *    notice, this list of conditions and the following disclaimer.
13*84d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
14*84d9c625SLionel Sambuc *    notice, this list of conditions and the following disclaimer in the
15*84d9c625SLionel Sambuc *    documentation and/or other materials provided with the distribution.
16*84d9c625SLionel Sambuc *
17*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18*84d9c625SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19*84d9c625SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20*84d9c625SLionel Sambuc * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21*84d9c625SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*84d9c625SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*84d9c625SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*84d9c625SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*84d9c625SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*84d9c625SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
28*84d9c625SLionel Sambuc */
29*84d9c625SLionel Sambuc
30*84d9c625SLionel Sambuc#include <machine/asm.h>
31*84d9c625SLionel Sambuc
32*84d9c625SLionel SambucRCSID("$NetBSD: strchr_arm.S,v 1.8 2013/08/19 17:50:04 matt Exp $")
33*84d9c625SLionel Sambuc
34*84d9c625SLionel Sambuc#if defined(__thumb__) && !defined(_ARM_ARCH_T2)
35*84d9c625SLionel Sambuc#error Only Thumb2 or ARM supported
36*84d9c625SLionel Sambuc#endif
37*84d9c625SLionel Sambuc
38*84d9c625SLionel Sambuc#ifdef __ARMEL__
39*84d9c625SLionel Sambuc#define	BYTE0	0x000000ff
40*84d9c625SLionel Sambuc#define	BYTE1	0x0000ff00
41*84d9c625SLionel Sambuc#define	BYTE2	0x00ff0000
42*84d9c625SLionel Sambuc#define	BYTE3	0xff000000
43*84d9c625SLionel Sambuc#define	lshi	lsl
44*84d9c625SLionel Sambuc#define	lshis	lsls
45*84d9c625SLionel Sambuc#else
46*84d9c625SLionel Sambuc#define	BYTE0	0xff000000
47*84d9c625SLionel Sambuc#define	BYTE1	0x00ff0000
48*84d9c625SLionel Sambuc#define	BYTE2	0x0000ff00
49*84d9c625SLionel Sambuc#define	BYTE3	0x000000ff
50*84d9c625SLionel Sambuc#define	lshi	lsr
51*84d9c625SLionel Sambuc#define	lshis	lsrs
52*84d9c625SLionel Sambuc#endif
53*84d9c625SLionel Sambuc
54*84d9c625SLionel Sambuc	.text
55*84d9c625SLionel SambucENTRY(strchr)
56*84d9c625SLionel Sambuc	and	r2, r1, #0xff		/* restrict to byte value */
57*84d9c625SLionel Sambuc1:	tst	r0, #3			/* test for word alignment */
58*84d9c625SLionel Sambuc	beq	.Lpre_main_loop		/*   finally word aligned */
59*84d9c625SLionel Sambuc	ldrb	r3, [r0], #1		/* load a byte */
60*84d9c625SLionel Sambuc	cmp	r3, r2			/* is it a match? */
61*84d9c625SLionel Sambuc	beq	2f			/*   yes, return current ptr - 1 */
62*84d9c625SLionel Sambuc	cmp	r3, #0			/* no, was it 0? */
63*84d9c625SLionel Sambuc	bne	1b			/*   no, try next byte */
64*84d9c625SLionel Sambuc	movs	r0, #0			/*   yes, set return value to NULL */
65*84d9c625SLionel Sambuc	RET				/* return */
66*84d9c625SLionel Sambuc2:	subs	r0, r0, #1		/* back up by one */
67*84d9c625SLionel Sambuc	RET				/* return */
68*84d9c625SLionel Sambuc.Lpre_main_loop:
69*84d9c625SLionel Sambuc#if defined(_ARM_ARCH_7)
70*84d9c625SLionel Sambuc	movw	ip, #0xfefe		/* magic constant; 254 in each byte */
71*84d9c625SLionel Sambuc	movt	ip, #0xfefe		/* magic constant; 254 in each byte */
72*84d9c625SLionel Sambuc#elif defined(_ARM_ARCH_6)
73*84d9c625SLionel Sambuc	mov	ip, #0xfe		/* put 254 in low byte */
74*84d9c625SLionel Sambuc	orr	ip, ip, ip, lsl #8	/* move to next byte */
75*84d9c625SLionel Sambuc	orr	ip, ip, ip, lsl #16	/* move to next halfword */
76*84d9c625SLionel Sambuc#endif /* _ARM_ARCH_6 */
77*84d9c625SLionel Sambuc	orr	r2, r2, r2, lsl #8	/* move to next byte */
78*84d9c625SLionel Sambuc	orr	r2, r2, r2, lsl #16	/* move to next halfword */
79*84d9c625SLionel Sambuc.Lmain_loop:
80*84d9c625SLionel Sambuc	ldr	r3, [r0], #4		/* load next word */
81*84d9c625SLionel Sambuc#if defined(_ARM_ARCH_6)
82*84d9c625SLionel Sambuc	/*
83*84d9c625SLionel Sambuc	 * Add 254 to each byte using the UQADD8 (unsigned saturating add 8)
84*84d9c625SLionel Sambuc	 * instruction.  For every non-NUL byte, the result for that byte will
85*84d9c625SLionel Sambuc	 * become 255.  For NUL, it will be 254.  When we complement the
86*84d9c625SLionel Sambuc	 * result, if the result is non-0 then we must have encountered a NUL.
87*84d9c625SLionel Sambuc	 */
88*84d9c625SLionel Sambuc	uqadd8	r1, r3, ip		/* NUL detection happens here */
89*84d9c625SLionel Sambuc	eors	r3, r3, r2		/* xor to clear each lane */
90*84d9c625SLionel Sambuc	uqadd8	r3, r3, ip		/* char detection happens here */
91*84d9c625SLionel Sambuc	ands	r3, r3, r1		/* merge results */
92*84d9c625SLionel Sambuc	mvns	r3, r3			/* is the complement non-0? */
93*84d9c625SLionel Sambuc	beq	.Lmain_loop		/*    no, then keep going */
94*84d9c625SLionel Sambuc
95*84d9c625SLionel Sambuc	/*
96*84d9c625SLionel Sambuc	 * We've encountered a NUL or a match but we don't know which happened
97*84d9c625SLionel Sambuc	 * first.
98*84d9c625SLionel Sambuc	 */
99*84d9c625SLionel Sambuc#if defined(__thumb__) && defined(_ARM_ARCH_T2)
100*84d9c625SLionel Sambuc	cbz	r2, .Lfind_match	/* searching for NUL? yes, find it */
101*84d9c625SLionel Sambuc#else
102*84d9c625SLionel Sambuc	cmp	r2, #0			/* searching for NUL? */
103*84d9c625SLionel Sambuc	beq	.Lfind_match		/*   yes, find the match */
104*84d9c625SLionel Sambuc#endif
105*84d9c625SLionel Sambuc	mvns	r1, r1			/* did we encounter a NUL? */
106*84d9c625SLionel Sambuc	beq	.Lfind_match		/*   no, find the match */
107*84d9c625SLionel Sambuc	bics	r3, r3, r1		/* clear match for the NUL(s) */
108*84d9c625SLionel Sambuc	beq	.Lnomatch		/*   any left set? if not, no match */
109*84d9c625SLionel Sambuc	lshis	r1, r1, #8		/* replicate NUL bit to other bytes */
110*84d9c625SLionel Sambuc#ifdef __thumb__
111*84d9c625SLionel Sambuc	itt	ne
112*84d9c625SLionel Sambuc#endif
113*84d9c625SLionel Sambuc	orrne	r1, r1, r1, lshi #8	/* replicate NUL bit to other bytes */
114*84d9c625SLionel Sambuc	orrne	r1, r1, r1, lshi #8	/* replicate NUL bit to other bytes */
115*84d9c625SLionel Sambuc	bics	r3, r3, r1		/* clear any match bits after the NUL */
116*84d9c625SLionel Sambuc	beq	.Lnomatch		/*   any left set? if not, no match */
117*84d9c625SLionel Sambuc.Lfind_match:
118*84d9c625SLionel Sambuc#ifdef __ARMEL__
119*84d9c625SLionel Sambuc	rev	r3, r3			/* we want this in BE for the CLZ */
120*84d9c625SLionel Sambuc#endif
121*84d9c625SLionel Sambuc	clz	r3, r3			/* count how many leading zeros */
122*84d9c625SLionel Sambuc	add	r0, r0, r3, lsr #3	/* divide that by 8 and add to count */
123*84d9c625SLionel Sambuc	subs	r0, r0, #4		/* compensate for the post-inc */
124*84d9c625SLionel Sambuc	RET
125*84d9c625SLionel Sambuc.Lnomatch:
126*84d9c625SLionel Sambuc	movs	r0, #0
127*84d9c625SLionel Sambuc	RET
128*84d9c625SLionel Sambuc#else
129*84d9c625SLionel Sambuc	/*
130*84d9c625SLionel Sambuc	 * No fancy shortcuts so just test each byte lane for a NUL.
131*84d9c625SLionel Sambuc	 * (other tests for NULs in a word take more instructions/cycles).
132*84d9c625SLionel Sambuc	 */
133*84d9c625SLionel Sambuc	eor	r1, r3, r2		/* xor .. */
134*84d9c625SLionel Sambuc	tst	r3, #BYTE0		/* is this byte NUL? */
135*84d9c625SLionel Sambuc	tstne	r1, #BYTE0		/*   no, does this byte match? */
136*84d9c625SLionel Sambuc	tstne	r3, #BYTE1		/*   no, is this byte NUL? */
137*84d9c625SLionel Sambuc	tstne	r1, #BYTE1		/*   no, does this byte match? */
138*84d9c625SLionel Sambuc	tstne	r3, #BYTE2		/*   no, is this byte NUL? */
139*84d9c625SLionel Sambuc	tstne	r1, #BYTE2		/*   no, does this byte match? */
140*84d9c625SLionel Sambuc	tstne	r3, #BYTE3		/*   no, is this byte NUL? */
141*84d9c625SLionel Sambuc	tstne	r1, #BYTE3		/*   no, does this byte match? */
142*84d9c625SLionel Sambuc	bne	.Lmain_loop
143*84d9c625SLionel Sambuc
144*84d9c625SLionel Sambuc	sub	r2, r0, #4		/* un post-inc */
145*84d9c625SLionel Sambuc	mov	r0, #0			/* assume no match */
146*84d9c625SLionel Sambuc
147*84d9c625SLionel Sambuc	tst	r1, #BYTE0		/* does this byte match? */
148*84d9c625SLionel Sambuc	moveq	r0, r2			/*   yes, point to it */
149*84d9c625SLionel Sambuc	RETc(eq)			/*        and return */
150*84d9c625SLionel Sambuc	tst	r3, #BYTE0		/* is this byte NUL? */
151*84d9c625SLionel Sambuc	RETc(eq)			/*   yes, return NULL */
152*84d9c625SLionel Sambuc
153*84d9c625SLionel Sambuc	tst	r1, #BYTE1		/* does this byte match? */
154*84d9c625SLionel Sambuc	addeq	r0, r2, #1		/*   yes, point to it */
155*84d9c625SLionel Sambuc	RETc(eq)			/*        and return */
156*84d9c625SLionel Sambuc	tst	r3, #BYTE1		/* is this byte NUL? */
157*84d9c625SLionel Sambuc	RETc(eq)			/*   yes, return NULL */
158*84d9c625SLionel Sambuc
159*84d9c625SLionel Sambuc	tst	r1, #BYTE2		/* does this byte match? */
160*84d9c625SLionel Sambuc	addeq	r0, r2, #2		/*   yes, point to it */
161*84d9c625SLionel Sambuc	RETc(eq)			/*        and return */
162*84d9c625SLionel Sambuc	tst	r3, #BYTE2		/* is this byte NUL? */
163*84d9c625SLionel Sambuc	RETc(eq)			/*   yes, return NULL */
164*84d9c625SLionel Sambuc
165*84d9c625SLionel Sambuc	tst	r1, #BYTE3		/* does this byte match? */
166*84d9c625SLionel Sambuc	addeq	r0, r2, #3		/*   yes, point to it */
167*84d9c625SLionel Sambuc	/*
168*84d9c625SLionel Sambuc	 * Since no NULs and no matches this must be the only case left.
169*84d9c625SLionel Sambuc	 */
170*84d9c625SLionel Sambuc	RET				/* return */
171*84d9c625SLionel Sambuc#endif /* _ARM_ARCH_6 */
172*84d9c625SLionel SambucEND(strchr)
173