2 * Copyright (c) 2005-2012 Apple Computer, 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@
28 * This file implements strlen( ) for the x86_64 architecture.
33 /*****************************************************************************
35 *****************************************************************************/
42 .macro ClearFrameAndReturn
47 /*****************************************************************************
49 *****************************************************************************/
54 // size_t strlen(const char *s);
56 // returns the length of the string s (i.e. the distance in bytes from
57 // s to the first NUL byte following s). We look for NUL bytes using
58 // pcmpeqb on 16-byte aligned blocks. Although this may read past the
59 // end of the string, because all access is aligned, it will never
60 // read past the end of the string across a page boundary, or even
61 // accross a cacheline.
66 // Load the 16-byte block containing the first byte of the string, and
67 // compare each byte to zero. If any NUL bytes are present in this
68 // block, the corresponding *bit* in esi will be set to 1.
74 // The 16 bytes that we checked for NUL included some bytes preceeding
75 // the start of the string, if s is not 16-byte aligned. We create a
76 // mask based on the alignment of s which covers only those bits
77 // corresponding to bytes that do not preceed s, and check for NULs
78 // only in those bits. If we do not find one, we jump to our main
87 // The last 16-byte block that we searched contained at least one NUL.
88 // We use bsf to identify the first NUL, and compute the distance from
89 // that byte to the start of the string.
97 // Main search loop: check for NUL in a 16-byte block, continuing
98 // loop until one is found.
101 pcmpeqb (%rdi), %xmm0