]>
Commit | Line | Data |
---|---|---|
ad3c9f2a A |
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 _strnlen | |
28 | ||
29 | #define addr r0 | |
30 | #define maxl r1 | |
31 | #define temp r2 | |
32 | #define mask r3 | |
33 | #define save ip | |
34 | #define word lr | |
35 | #define byte lr | |
36 | #define indx r0 | |
37 | ||
38 | .macro IfHS_and_WordDoesntContainNUL_SetZ | |
39 | #if defined _ARM_ARCH_6 | |
40 | // In each word of the string, we check for NUL bytes via a saturating | |
41 | // unsigned subtraction of each byte from 0x1. The result of this is | |
42 | // non-zero if and only if the corresponding byte in the string is NUL. | |
43 | // Simply using a TST instruction checks all four bytes for NULs in one | |
44 | // go. | |
45 | uqsub8 temp, mask, word | |
46 | tsths temp, temp | |
47 | #else | |
48 | // If we're on armv5, we do not have the uqsub8 instruction, so we need | |
49 | // to use a different test for NUL. Instead, we compute: | |
50 | // | |
51 | // byte - 0x1 & ~byte | |
52 | // | |
53 | // and test the high-order bit. If it is set, then byte is NUL. Just | |
54 | // as with the other test, this can be applied simultaneously to all | |
55 | // bytes in a word. | |
56 | sub temp, word, mask | |
57 | bic temp, temp, word | |
58 | tsths temp, mask, lsl #7 | |
59 | #endif | |
60 | .endm | |
61 | ||
62 | .text | |
63 | .align 3 | |
64 | .long 0x0 // padding | |
65 | .long 0x01010101 // mask for use in finding NULs | |
66 | _strnlen: | |
67 | // Establish stack frame, load mask that we will use to find NUL bytes, | |
68 | // and set aside a copy of the pointer to the string. Subtract 4 from | |
69 | // the maxlen, and jump into a byte-by-byte search if this requires a | |
70 | // borrow, as we cannot use a word-by-word search in that case. | |
71 | push {r7,lr} | |
72 | mov r7, sp | |
73 | ldr mask, (_strnlen-4) | |
74 | add save, addr, #4 | |
75 | subs maxl, maxl, #4 | |
76 | blo L_bytewiseSearch | |
77 | ||
78 | // Load the aligned word that contains the start of the string, then OR | |
79 | // 0x01 into any bytes that preceed the start to prevent false positives | |
80 | // when we check for NUL bytes. Additionally, add the number of unused | |
81 | // bytes to maxlen. | |
82 | and temp, addr, #3 | |
83 | bic addr, addr, #3 | |
84 | add maxl, maxl, temp | |
85 | lsl temp, temp, #3 | |
86 | ldr word, [addr], #4 | |
87 | rsb temp, temp, #32 | |
88 | orr word, word, mask, lsr temp | |
89 | ||
90 | subs maxl, maxl, #4 | |
91 | IfHS_and_WordDoesntContainNUL_SetZ | |
92 | bne 1f | |
93 | ||
94 | .align 4 | |
95 | 0: ldr word, [addr], #4 | |
96 | subs maxl, maxl, #4 | |
97 | IfHS_and_WordDoesntContainNUL_SetZ | |
98 | beq 0b | |
99 | ||
100 | .align 4 | |
101 | // Either the last word that we loaded contained a NUL, or we will | |
102 | // exceed maxlen before we finish the next word in the string. Determine | |
103 | // which case we are in by repeating the check for NUL, and branch if | |
104 | // there was not a NUL byte. Padding ensures that we don't have two | |
105 | // branches in a single 16-byte fetch group, as this interferes with | |
106 | // branch prediction on Swift. | |
107 | 1: tst temp, temp | |
108 | beq L_bytewiseSearch | |
109 | ||
110 | // The last word that we loaded contained a NUL. Subtracting the saved | |
111 | // pointer from the current pointer gives us the number of bytes from | |
112 | // the start of the string to the word containing the NUL. | |
113 | sub indx, addr, save | |
114 | #if defined _ARM_ARCH_6 | |
115 | // To that we add the index of the first NUL byte in the word, computed | |
116 | // using REV and CLZ followed by a shift. | |
117 | rev temp, temp | |
118 | clz temp, temp | |
119 | add indx, indx, temp, lsr #3 | |
120 | #else | |
121 | // armv5 does not have the REV instruction, so instead we find the | |
122 | // index of the NUL byte in word with a linear search. | |
123 | tst word, #0x000000ff | |
124 | addne indx, #1 | |
125 | tstne word, #0x0000ff00 | |
126 | addne indx, #1 | |
127 | tstne word, #0x00ff0000 | |
128 | addne indx, #1 | |
129 | #endif | |
130 | pop {r7,pc} | |
131 | ||
132 | .align 4 | |
133 | L_bytewiseSearch: | |
134 | // Restore maxlen (the last thing that happened before we branched here | |
135 | // was that we subtracted 4 from maxlen), and adjust the saved string | |
136 | // pointer. Then we do a simple byte-by-byte search until we either | |
137 | // reach the end of the string or maxlen reaches zero, at which point | |
138 | // the length to return is simply the difference between the current | |
139 | // and saved pointers. | |
140 | adds maxl, maxl, #4 | |
141 | sub save, save, #4 | |
142 | beq 1f | |
143 | 0: ldrb byte, [addr] | |
144 | cmp byte, #0 | |
145 | addhi addr, #1 | |
146 | subshi maxl, #1 | |
147 | bhi 0b | |
148 | 1: sub indx, addr, save | |
149 | pop {r7,pc} |