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