]> git.saurik.com Git - apple/libc.git/blob - arm/string/strlen.s
Libc-825.40.1.tar.gz
[apple/libc.git] / arm / string / strlen.s
1 /*
2 * Copyright (c) 2011 Apple, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <arm/arch.h>
25 .syntax unified
26 .code 32
27 .globl _strlen
28
29 #define addr r0
30 #define word r1
31 #define temp r2
32 #define mask r3
33 #define save ip
34 #define indx r0
35
36 .macro IfWordDoesntContainNUL_SetZ
37 #if defined _ARM_ARCH_6
38 // In each word of the string, we check for NUL bytes via a saturating
39 // unsigned subtraction of each byte from 0x1. The result of this is
40 // non-zero if and only if the corresponding byte in the string is NUL.
41 // Simply using a TST instruction checks all four bytes for NULs in one
42 // go.
43 uqsub8 temp, mask, word
44 tst temp, temp
45 #else
46 // If we're on armv5, we do not have the uqsub8 instruction, so we need
47 // to use a different test for NUL. Instead, we compute:
48 //
49 // byte - 0x1 & ~byte
50 //
51 // and test the high-order bit. If it is set, then byte is NUL. Just
52 // as with the other test, this can be applied simultaneously to all
53 // bytes in a word.
54 sub temp, word, mask
55 bic temp, temp, word
56 tst temp, mask, lsl #7
57 #endif
58 .endm
59
60 .text
61 .align 4
62 .long 0x0 // padding
63 .long 0x01010101 // mask for use in finding NULs
64 _strlen:
65 // Establish stack frame, load mask that we will use to find NUL bytes,
66 // and set aside a copy of the pointer to the string.
67 push {r7,lr}
68 mov r7, sp
69 ldr mask, (_strlen-4)
70 add save, addr, #4
71
72 // Load the aligned word that contains the start of the string, then OR
73 // 0x01 into any bytes that preceed the start of the string to prevent
74 // false positives when we check for NUL bytes.
75 and temp, addr, #3
76 bic addr, addr, #3
77 lsl temp, temp, #3
78 ldr word, [addr], #4
79 rsb temp, temp, #32
80 orr word, word, mask, lsr temp
81
82 // Check if the string ends in the first word. If so, don't load any
83 // more of the string; instead jump directly to the cleanup code.
84 IfWordDoesntContainNUL_SetZ
85 bne 1f
86
87 .align 4
88 // Load one word of the string on each iteration, and check it for NUL
89 // bytes. If a NUL is found, fall through into the cleanup code.
90 0: ldr word, [addr], #4
91 IfWordDoesntContainNUL_SetZ
92 beq 0b
93
94 // The last word that we loaded contained a NUL. Subtracting the saved
95 // pointer from the current pointer gives us the number of bytes from
96 // the start of the string to the word containing the NUL.
97 1: sub indx, addr, save
98 #if defined _ARM_ARCH_6
99 // To that we add the index of the first NUL byte in the word, computed
100 // using REV and CLZ followed by a shift.
101 rev temp, temp
102 clz temp, temp
103 add indx, indx, temp, lsr #3
104 #else
105 // armv5 does not have the REV instruction, so instead we find the
106 // index of the NUL byte in word with a linear search.
107 tst word, #0x000000ff
108 addne indx, #1
109 tstne word, #0x0000ff00
110 addne indx, #1
111 tstne word, #0x00ff0000
112 addne indx, #1
113 #endif
114 pop {r7,pc}
115