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