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