xref: /xnu-10002.61.3/libsyscall/wrappers/string/strlen.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1*0f4c859eSApple OSS Distributions /*-
2*0f4c859eSApple OSS Distributions  * Copyright (c) 2009 Xin LI <[email protected]>
3*0f4c859eSApple OSS Distributions  * All rights reserved.
4*0f4c859eSApple OSS Distributions  *
5*0f4c859eSApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
6*0f4c859eSApple OSS Distributions  * modification, are permitted provided that the following conditions
7*0f4c859eSApple OSS Distributions  * are met:
8*0f4c859eSApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
9*0f4c859eSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
10*0f4c859eSApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
11*0f4c859eSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
12*0f4c859eSApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
13*0f4c859eSApple OSS Distributions  *
14*0f4c859eSApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*0f4c859eSApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*0f4c859eSApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*0f4c859eSApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*0f4c859eSApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*0f4c859eSApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*0f4c859eSApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*0f4c859eSApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*0f4c859eSApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*0f4c859eSApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*0f4c859eSApple OSS Distributions  * SUCH DAMAGE.
25*0f4c859eSApple OSS Distributions  */
26*0f4c859eSApple OSS Distributions 
27*0f4c859eSApple OSS Distributions #include "string.h"
28*0f4c859eSApple OSS Distributions #include <limits.h>
29*0f4c859eSApple OSS Distributions 
30*0f4c859eSApple OSS Distributions /*
31*0f4c859eSApple OSS Distributions  * Portable strlen() for 32-bit and 64-bit systems.
32*0f4c859eSApple OSS Distributions  *
33*0f4c859eSApple OSS Distributions  * Rationale: it is generally much more efficient to do word length
34*0f4c859eSApple OSS Distributions  * operations and avoid branches on modern computer systems, as
35*0f4c859eSApple OSS Distributions  * compared to byte-length operations with a lot of branches.
36*0f4c859eSApple OSS Distributions  *
37*0f4c859eSApple OSS Distributions  * The expression:
38*0f4c859eSApple OSS Distributions  *
39*0f4c859eSApple OSS Distributions  *	((x - 0x01....01) & ~x & 0x80....80)
40*0f4c859eSApple OSS Distributions  *
41*0f4c859eSApple OSS Distributions  * would evaluate to a non-zero value iff any of the bytes in the
42*0f4c859eSApple OSS Distributions  * original word is zero.  However, we can further reduce ~1/3 of
43*0f4c859eSApple OSS Distributions  * time if we consider that strlen() usually operate on 7-bit ASCII
44*0f4c859eSApple OSS Distributions  * by employing the following expression, which allows false positive
45*0f4c859eSApple OSS Distributions  * when high bit of 1 and use the tail case to catch these case:
46*0f4c859eSApple OSS Distributions  *
47*0f4c859eSApple OSS Distributions  *	((x - 0x01....01) & 0x80....80)
48*0f4c859eSApple OSS Distributions  *
49*0f4c859eSApple OSS Distributions  * This is more than 5.2 times as fast as the raw implementation on
50*0f4c859eSApple OSS Distributions  * Intel T7300 under long mode for strings longer than word length.
51*0f4c859eSApple OSS Distributions  */
52*0f4c859eSApple OSS Distributions 
53*0f4c859eSApple OSS Distributions /* Magic numbers for the algorithm */
54*0f4c859eSApple OSS Distributions #if LONG_BIT == 32
55*0f4c859eSApple OSS Distributions static const unsigned long mask01 = 0x01010101;
56*0f4c859eSApple OSS Distributions static const unsigned long mask80 = 0x80808080;
57*0f4c859eSApple OSS Distributions #elif LONG_BIT == 64
58*0f4c859eSApple OSS Distributions static const unsigned long mask01 = 0x0101010101010101;
59*0f4c859eSApple OSS Distributions static const unsigned long mask80 = 0x8080808080808080;
60*0f4c859eSApple OSS Distributions #else
61*0f4c859eSApple OSS Distributions #error Unsupported word size
62*0f4c859eSApple OSS Distributions #endif
63*0f4c859eSApple OSS Distributions 
64*0f4c859eSApple OSS Distributions #define LONGPTR_MASK (sizeof(long) - 1)
65*0f4c859eSApple OSS Distributions 
66*0f4c859eSApple OSS Distributions /*
67*0f4c859eSApple OSS Distributions  * Helper macro to return string length if we caught the zero
68*0f4c859eSApple OSS Distributions  * byte.
69*0f4c859eSApple OSS Distributions  */
70*0f4c859eSApple OSS Distributions #define testbyte(x)                             \
71*0f4c859eSApple OSS Distributions 	do {                                    \
72*0f4c859eSApple OSS Distributions 	        if (p[x] == '\0')               \
73*0f4c859eSApple OSS Distributions 	            return (p - str + x);       \
74*0f4c859eSApple OSS Distributions 	} while (0)
75*0f4c859eSApple OSS Distributions 
76*0f4c859eSApple OSS Distributions __attribute__((visibility("hidden")))
77*0f4c859eSApple OSS Distributions size_t
_libkernel_strlen(const char * str)78*0f4c859eSApple OSS Distributions _libkernel_strlen(const char *str)
79*0f4c859eSApple OSS Distributions {
80*0f4c859eSApple OSS Distributions 	const char *p;
81*0f4c859eSApple OSS Distributions 	const unsigned long *lp;
82*0f4c859eSApple OSS Distributions 
83*0f4c859eSApple OSS Distributions 	/* Skip the first few bytes until we have an aligned p */
84*0f4c859eSApple OSS Distributions 	for (p = str; (uintptr_t)p & LONGPTR_MASK; p++) {
85*0f4c859eSApple OSS Distributions 		if (*p == '\0') {
86*0f4c859eSApple OSS Distributions 			return p - str;
87*0f4c859eSApple OSS Distributions 		}
88*0f4c859eSApple OSS Distributions 	}
89*0f4c859eSApple OSS Distributions 
90*0f4c859eSApple OSS Distributions 	/* Scan the rest of the string using word sized operation */
91*0f4c859eSApple OSS Distributions 	for (lp = (const unsigned long *)p;; lp++) {
92*0f4c859eSApple OSS Distributions 		if ((*lp - mask01) & mask80) {
93*0f4c859eSApple OSS Distributions 			p = (const char *)(lp);
94*0f4c859eSApple OSS Distributions 			testbyte(0);
95*0f4c859eSApple OSS Distributions 			testbyte(1);
96*0f4c859eSApple OSS Distributions 			testbyte(2);
97*0f4c859eSApple OSS Distributions 			testbyte(3);
98*0f4c859eSApple OSS Distributions #if (LONG_BIT >= 64)
99*0f4c859eSApple OSS Distributions 			testbyte(4);
100*0f4c859eSApple OSS Distributions 			testbyte(5);
101*0f4c859eSApple OSS Distributions 			testbyte(6);
102*0f4c859eSApple OSS Distributions 			testbyte(7);
103*0f4c859eSApple OSS Distributions #endif
104*0f4c859eSApple OSS Distributions 		}
105*0f4c859eSApple OSS Distributions 	}
106*0f4c859eSApple OSS Distributions 
107*0f4c859eSApple OSS Distributions 	/* NOTREACHED */
108*0f4c859eSApple OSS Distributions 	return 0;
109*0f4c859eSApple OSS Distributions }
110