199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2009, 2010 Xin LI <delphij@FreeBSD.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Portable strlen() for 32-bit and 64-bit systems.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Rationale: it is generally much more efficient to do word length
199767f8919635c4928607450d9e0abb932109ceToomas Soome * operations and avoid branches on modern computer systems, as
199767f8919635c4928607450d9e0abb932109ceToomas Soome * compared to byte-length operations with a lot of branches.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The expression:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ((x - 0x01....01) & ~x & 0x80....80)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * would evaluate to a non-zero value iff any of the bytes in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * original word is zero.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * On multi-issue processors, we can divide the above expression into:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a) (x - 0x01....01)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * b) (~x & 0x80....80)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Where, a) and b) can be partially computed in parallel.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The algorithm above is found on "Hacker's Delight" by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Henry S. Warren, Jr.
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Magic numbers for the algorithm */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const unsigned long mask01 = 0x0101010101010101;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const unsigned long mask80 = 0x8080808080808080;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Helper macro to return string length if we caught the zero
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p[x] == '\0') \
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (p - str + x); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Before trying the hard (unaligned byte-by-byte access) way
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to figure out whether there is a nul character, try to see
199767f8919635c4928607450d9e0abb932109ceToomas Soome * if there is a nul character is within this accessible word
199767f8919635c4928607450d9e0abb932109ceToomas Soome * p and (p & ~LONGPTR_MASK) must be equally accessible since
199767f8919635c4928607450d9e0abb932109ceToomas Soome * they always fall in the same memory page, as long as page
199767f8919635c4928607450d9e0abb932109ceToomas Soome * boundaries is integral multiple of word size.
199767f8919635c4928607450d9e0abb932109ceToomas Soome lp = (const unsigned long *)((uintptr_t)str & ~LONGPTR_MASK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check if we have \0 in the first part */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (p - str);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Scan the rest of the string using word sized operation */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (; ; lp++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = (const char *)(lp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* NOTREACHED */