2 * Copyright (c) 2011 Apple, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
41 .macro IfWordDoesntContainNUL_SetZ
42 #if defined _ARM_ARCH_6
43 // In each word of the string, we check for NUL bytes via a saturating
44 // unsigned subtraction of each byte from 0x1. The result of this is
45 // non-zero if and only if the corresponding byte in the string is NUL.
46 // Simply using a TST instruction checks all four bytes for NULs in one
48 uqsub8 temp, mask, word
51 // If we're on armv5, we do not have the uqsub8 instruction, so we need
52 // to use a different test for NUL. Instead, we compute:
56 // and test the high-order bit. If it is set, then byte is NUL. Just
57 // as with the other test, this can be applied simultaneously to all
61 tst temp, mask, lsl #7
68 .long 0x01010101 // mask for use in finding NULs
70 // Establish stack frame, load mask that we will use to find NUL bytes,
71 // and set aside a copy of the pointer to the string.
77 // Load the aligned word that contains the start of the string, then OR
78 // 0x01 into any bytes that preceed the start of the string to prevent
79 // false positives when we check for NUL bytes.
85 orr word, word, mask, lsr temp
87 // Check if the string ends in the first word. If so, don't load any
88 // more of the string; instead jump directly to the cleanup code.
89 IfWordDoesntContainNUL_SetZ
93 // Load one word of the string on each iteration, and check it for NUL
94 // bytes. If a NUL is found, fall through into the cleanup code.
95 0: ldr word, [addr], #4
96 IfWordDoesntContainNUL_SetZ
99 // The last word that we loaded contained a NUL. Subtracting the saved
100 // pointer from the current pointer gives us the number of bytes from
101 // the start of the string to the word containing the NUL.
102 1: sub indx, addr, save
103 #if defined _ARM_ARCH_6
104 // To that we add the index of the first NUL byte in the word, computed
105 // using REV and CLZ followed by a shift.
108 add indx, indx, temp, lsr #3
110 // armv5 does not have the REV instruction, so instead we find the
111 // index of the NUL byte in word with a linear search.
112 tst word, #0x000000ff
114 tstne word, #0x0000ff00
116 tstne word, #0x00ff0000