]>
Commit | Line | Data |
---|---|---|
8e029c65 | 1 | /* |
6465356a | 2 | * Copyright (c) 2005-2012 Apple Computer, Inc. All rights reserved. |
8e029c65 | 3 | * |
6465356a A |
4 | * @APPLE_OSREFERENCE_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. 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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8e029c65 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
6465356a A |
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. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
8e029c65 | 27 | * |
6465356a | 28 | * This file implements strlen( ) for the x86_64 architecture. |
8e029c65 A |
29 | */ |
30 | ||
6465356a | 31 | .globl _strlen |
8e029c65 | 32 | |
6465356a A |
33 | /***************************************************************************** |
34 | * Macros * | |
35 | *****************************************************************************/ | |
8e029c65 | 36 | |
6465356a A |
37 | .macro EstablishFrame |
38 | push %rbp | |
39 | mov %rsp, %rbp | |
40 | .endm | |
41 | ||
42 | .macro ClearFrameAndReturn | |
43 | pop %rbp | |
8e029c65 | 44 | ret |
6465356a A |
45 | .endm |
46 | ||
47 | /***************************************************************************** | |
48 | * Entrypoint * | |
49 | *****************************************************************************/ | |
50 | ||
51 | .text | |
52 | .align 5 | |
53 | _strlen: | |
54 | // size_t strlen(const char *s); | |
55 | // | |
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. | |
62 | EstablishFrame | |
63 | mov %rdi, %rcx | |
64 | mov %rdi, %rdx | |
65 | ||
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. | |
69 | and $-16, %rdi | |
70 | pxor %xmm0, %xmm0 | |
71 | pcmpeqb (%rdi), %xmm0 | |
72 | pmovmskb %xmm0, %esi | |
73 | ||
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 | |
79 | // search loop. | |
80 | and $0xf, %rcx | |
81 | or $-1, %rax | |
82 | shl %cl, %rax | |
83 | and %eax, %esi | |
84 | jz L_loop | |
8e029c65 | 85 | |
6465356a A |
86 | L_foundNUL: |
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. | |
90 | bsf %esi, %eax | |
91 | sub %rdx, %rdi | |
92 | add %rdi, %rax | |
93 | ClearFrameAndReturn | |
8e029c65 | 94 | |
6465356a A |
95 | .align 4 |
96 | L_loop: | |
97 | // Main search loop: check for NUL in a 16-byte block, continuing | |
98 | // loop until one is found. | |
99 | add $16, %rdi | |
100 | pxor %xmm0, %xmm0 | |
101 | pcmpeqb (%rdi), %xmm0 | |
102 | pmovmskb %xmm0, %esi | |
103 | test %esi, %esi | |
104 | jz L_loop | |
105 | jmp L_foundNUL |