strchr_arm.S revision 1.2.2.2 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.2 yamt RCSID("$NetBSD: strchr_arm.S,v 1.2.2.2 2013/01/23 00:04:06 yamt Exp $")
33 1.2.2.2 yamt
34 1.2.2.2 yamt #ifdef __ARMEL__
35 1.2.2.2 yamt #define BYTE0 0x000000ff
36 1.2.2.2 yamt #define BYTE1 0x0000ff00
37 1.2.2.2 yamt #define BYTE2 0x00ff0000
38 1.2.2.2 yamt #define BYTE3 0xff000000
39 1.2.2.2 yamt #define lshi lsl
40 1.2.2.2 yamt #else
41 1.2.2.2 yamt #define BYTE0 0xff000000
42 1.2.2.2 yamt #define BYTE1 0x00ff0000
43 1.2.2.2 yamt #define BYTE2 0x0000ff00
44 1.2.2.2 yamt #define BYTE3 0x000000ff
45 1.2.2.2 yamt #define lshi lsr
46 1.2.2.2 yamt #endif
47 1.2.2.2 yamt
48 1.2.2.2 yamt .text
49 1.2.2.2 yamt ENTRY(strchr)
50 1.2.2.2 yamt and r2, r1, #0xff /* restrict to byte value */
51 1.2.2.2 yamt 1: tst r0, #3 /* test for word alignment */
52 1.2.2.2 yamt beq .Lpre_main_loop /* finally word aligned */
53 1.2.2.2 yamt ldrb r3, [r0], #1 /* load a byte */
54 1.2.2.2 yamt cmp r3, r2 /* is it a match? */
55 1.2.2.2 yamt beq 2f /* yes, return current ptr - 1 */
56 1.2.2.2 yamt teq r3, #0 /* no, was it 0? */
57 1.2.2.2 yamt bne 1b /* no, try next byte */
58 1.2.2.2 yamt mov r0, #0 /* yes, set return value to NULL */
59 1.2.2.2 yamt RET /* return */
60 1.2.2.2 yamt 2: sub r0, r0, #1 /* back up by one */
61 1.2.2.2 yamt RET /* return */
62 1.2.2.2 yamt .Lpre_main_loop:
63 1.2.2.2 yamt #if defined(_ARM_ARCH_7)
64 1.2.2.2 yamt movw r1, #0xfefe /* magic constant; 254 in each byte */
65 1.2.2.2 yamt movt r1, #0xfefe /* magic constant; 254 in each byte */
66 1.2.2.2 yamt #elif defined(_ARM_ARCH_6)
67 1.2.2.2 yamt mov r1, #0xfe /* put 254 in low byte */
68 1.2.2.2 yamt orr r1, r1, r1, lsl #8 /* move to next byte */
69 1.2.2.2 yamt orr r1, r1, r1, lsl #16 /* move to next halfword */
70 1.2.2.2 yamt #endif /* _ARM_ARCH_6 */
71 1.2.2.2 yamt orr r2, r2, r2, lsl #8 /* move to next byte */
72 1.2.2.2 yamt orr r2, r2, r2, lsl #16 /* move to next halfword */
73 1.2.2.2 yamt .Lmain_loop:
74 1.2.2.2 yamt ldr r3, [r0], #4 /* load next word */
75 1.2.2.2 yamt #if defined(_ARM_ARCH_6)
76 1.2.2.2 yamt /*
77 1.2.2.2 yamt * Add 254 to each byte using the UQADD8 (unsigned saturating add 8)
78 1.2.2.2 yamt * instruction. For every non-NUL byte, the result for that byte will
79 1.2.2.2 yamt * become 255. For NUL, it will be 254. When we complement the
80 1.2.2.2 yamt * result, if the result is non-0 then we must have encountered a NUL.
81 1.2.2.2 yamt */
82 1.2.2.2 yamt uqadd8 ip, r3, r1 /* NUL detection happens here */
83 1.2.2.2 yamt eor r3, r3, r2 /* xor to clear each lane */
84 1.2.2.2 yamt uqadd8 r3, r3, r1 /* char detection happens here */
85 1.2.2.2 yamt and r3, r3, ip /* merge results */
86 1.2.2.2 yamt mvns r3, r3 /* is the complement non-0? */
87 1.2.2.2 yamt beq .Lmain_loop /* no, then keep going */
88 1.2.2.2 yamt
89 1.2.2.2 yamt /*
90 1.2.2.2 yamt * We've encountered a NUL or a match but we don't know which happened
91 1.2.2.2 yamt * first.
92 1.2.2.2 yamt */
93 1.2.2.2 yamt mvns ip, ip /* did we encounter a NUL? */
94 1.2.2.2 yamt beq .Lfind_match /* no, find the match */
95 1.2.2.2 yamt movs ip, ip, lshi #8 /* replicate NUL bit to other bytes */
96 1.2.2.2 yamt orrne ip, ip, lshi #8 /* replicate NUL bit to other bytes */
97 1.2.2.2 yamt orrne ip, ip, lshi #8 /* replicate NUL bit to other bytes */
98 1.2.2.2 yamt bics r3, r3, ip /* clear any match bits after the NUL */
99 1.2.2.2 yamt beq .Lnomatch /* any left set? if not, no match */
100 1.2.2.2 yamt .Lfind_match:
101 1.2.2.2 yamt #ifdef __ARMEL__
102 1.2.2.2 yamt rev r3, r3 /* we want this in BE for the CLZ */
103 1.2.2.2 yamt #endif
104 1.2.2.2 yamt clz r3, r3 /* count how many leading zeros */
105 1.2.2.2 yamt add r0, r0, r3, lsr #3 /* divide that by 8 and add to count */
106 1.2.2.2 yamt sub r0, r0, #4 /* compensate for the post-inc */
107 1.2.2.2 yamt RET
108 1.2.2.2 yamt .Lnomatch:
109 1.2.2.2 yamt mov r0, #0
110 1.2.2.2 yamt RET
111 1.2.2.2 yamt #else
112 1.2.2.2 yamt /*
113 1.2.2.2 yamt * No fancy shortcuts so just test each byte lane for a NUL.
114 1.2.2.2 yamt * (other tests for NULs in a word take more instructions/cycles).
115 1.2.2.2 yamt */
116 1.2.2.2 yamt eor ip, r3, r2 /* xor .. */
117 1.2.2.2 yamt tst r3, #BYTE0 /* is this byte NUL? */
118 1.2.2.2 yamt tstne ip, #BYTE0 /* no, does this byte match? */
119 1.2.2.2 yamt tstne r3, #BYTE1 /* no, is this byte NUL? */
120 1.2.2.2 yamt tstne ip, #BYTE1 /* no, does this byte match? */
121 1.2.2.2 yamt tstne r3, #BYTE2 /* no, is this byte NUL? */
122 1.2.2.2 yamt tstne ip, #BYTE2 /* no, does this byte match? */
123 1.2.2.2 yamt tstne r3, #BYTE3 /* no, is this byte NUL? */
124 1.2.2.2 yamt tstne ip, #BYTE3 /* no, does this byte match? */
125 1.2.2.2 yamt bne .Lmain_loop
126 1.2.2.2 yamt
127 1.2.2.2 yamt sub r2, r0, #4 /* un post-inc */
128 1.2.2.2 yamt mov r0, #0 /* assume no match */
129 1.2.2.2 yamt
130 1.2.2.2 yamt tst ip, #BYTE0 /* does this byte match? */
131 1.2.2.2 yamt moveq r0, r2 /* yes, point to it */
132 1.2.2.2 yamt RETc(eq) /* and return */
133 1.2.2.2 yamt tst r3, #BYTE0 /* is this byte NUL? */
134 1.2.2.2 yamt RETc(eq) /* yes, return NULL */
135 1.2.2.2 yamt
136 1.2.2.2 yamt tst ip, #BYTE1 /* does this byte match? */
137 1.2.2.2 yamt addeq r0, r2, #1 /* yes, point to it */
138 1.2.2.2 yamt RETc(eq) /* and return */
139 1.2.2.2 yamt tst r3, #BYTE1 /* is this byte NUL? */
140 1.2.2.2 yamt RETc(eq) /* yes, return NULL */
141 1.2.2.2 yamt
142 1.2.2.2 yamt tst ip, #BYTE2 /* does this byte match? */
143 1.2.2.2 yamt addeq r0, r2, #2 /* yes, point to it */
144 1.2.2.2 yamt RETc(eq) /* and return */
145 1.2.2.2 yamt tst r3, #BYTE2 /* is this byte NUL? */
146 1.2.2.2 yamt RETc(eq) /* yes, return NULL */
147 1.2.2.2 yamt
148 1.2.2.2 yamt tst ip, #BYTE3 /* does this byte match? */
149 1.2.2.2 yamt addeq r0, r2, #3 /* yes, point to it */
150 1.2.2.2 yamt /*
151 1.2.2.2 yamt * Since no NULs and no matches this must be the only case left.
152 1.2.2.2 yamt */
153 1.2.2.2 yamt RET /* return */
154 1.2.2.2 yamt #endif /* _ARM_ARCH_6 */
155 1.2.2.2 yamt END(strchr)
156