2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <machine/asm.h>
32 RCSID("$NetBSD: strlen_arm.S,v 1.9 2014/05/06 16:02:11 joerg Exp $")
34 #if defined(__thumb__) && !defined(_ARM_ARCH_T2)
35 #error Only Thumb2 or ARM supported
39 #define BYTE0 0x000000ff
40 #define BYTE1 0x0000ff00
41 #define BYTE2 0x00ff0000
42 #define BYTE3 0xff000000
44 #define BYTE0 0xff000000
45 #define BYTE1 0x00ff0000
46 #define BYTE2 0x0000ff00
47 #define BYTE3 0x000000ff
51 #define FUNCNAME strnlen
53 #define FUNCNAME strlen
58 #if defined(__ARM_EABI__) && defined(__UNWIND_TABLES__)
59 # if !defined(__ARM_DWARF_EH__)
65 push {r4,r5} /* save some registers */
66 #if defined(__ARM_EABI__) && defined(__UNWIND_TABLES__)
67 # if !defined(__ARM_DWARF_EH__)
74 adds r5, r0, r1 /* get ptr to end of string */
75 mov r4, r1 /* save maxlen */
77 adds r2, r0, #4 /* for the final post-inc */
78 1: tst r0, #3 /* test for word alignment */
79 beq .Lpre_main_loop /* finally word aligned */
81 cmp r0, r5 /* have we gone too far? */
82 beq .Lmaxed_out /* yes, return maxlen */
84 ldrb r3, [r0], #1 /* load a byte */
85 cmp r3, #0 /* is it 0? */
86 bne 1b /* no, try next byte */
87 subs r2, r2, #3 /* subtract (4 - the NUL) */
88 subs r0, r0, r2 /* subtract start */
90 pop {r4, r5} /* restore registers */
94 #if defined(_ARM_ARCH_7)
95 movw r1, #0xfefe /* magic constant; 254 in each byte */
96 movt r1, #0xfefe /* magic constant; 254 in each byte */
97 #elif defined(_ARM_ARCH_6)
98 mov r1, #0xfe /* put 254 in low byte */
99 orr r1, r1, r1, lsl #8 /* move to next byte */
100 orr r1, r1, r1, lsl #16 /* move to next halfword */
101 #endif /* _ARM_ARCH_6 */
104 cmp r0, r5 /* gone too far? */
105 bge .Lmaxed_out /* yes, return maxlen */
107 ldr r3, [r0], #4 /* load next word */
108 #if defined(_ARM_ARCH_6)
110 * Add 254 to each byte using the UQADD8 (unsigned saturating add 8)
111 * instruction. For every non-NUL byte, the result for that byte will
112 * become 255. For NUL, it will be 254. When we complement the
113 * result, if the result is non-0 then we must have encountered a NUL.
115 uqadd8 r3, r3, r1 /* magic happens here */
116 mvns r3, r3 /* is the complemented result non-0? */
117 beq .Lmain_loop /* no, then we encountered no NULs */
120 * No fancy shortcuts so just test each byte lane for a NUL.
121 * (other tests for NULs in a word take more instructions/cycles).
123 tst r3, #BYTE0 /* is this byte 0? */
124 tstne r3, #BYTE1 /* no, is this byte 0? */
125 tstne r3, #BYTE2 /* no, is this byte 0? */
126 tstne r3, #BYTE3 /* no, is this byte 0? */
127 bne .Lmain_loop /* no, then get next word */
129 #if defined(_ARM_ARCH_6)
131 * We encountered a NUL. Find out where by doing a CLZ and then
132 * shifting right by 3. That will be the number of non-NUL bytes.
135 rev r3, r3 /* we want this in BE for the CLZ */
137 clz r3, r3 /* count how many leading zeros */
140 adds r0, r0, r3 /* divide that by 8 and add to count */
142 add r0, r0, r3, lsr #3 /* divide that by 8 and add to count */
146 * We encountered a NUL.
148 tst r3, #BYTE0 /* 1st byte was NUL? */
149 beq 1f /* yes, done adding */
150 add r0, r0, #1 /* we have one more non-NUL byte */
151 tst r3, #BYTE1 /* 2nd byte was NUL? */
152 beq 1f /* yes, done adding */
153 add r0, r0, #1 /* we have one more non-NUL byte */
154 tst r3, #BYTE2 /* 3rd byte was NUL? */
155 addne r0, r0, #1 /* no, we have one more non-NUL byte */
157 #endif /* _ARM_ARCH_6 */
159 * r0 now points to 4 past the NUL due to the post-inc. Subtract the
160 * start of the string (which also has 4 added to it to compensate for
163 subs r0, r0, r2 /* subtract start to get length */
165 cmp r0, r4 /* is it larger than maxlen? */
169 movgt r0, r4 /* yes, return maxlen */
170 pop {r4, r5} /* restore registers */
176 mov r0, r4 /* return maxlen */
177 pop {r4, r5} /* restore registers */
180 #if defined(__ARM_EABI__) && defined(__UNWIND_TABLES__)
182 # if !defined(__ARM_DWARF_EH__)