199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2009, 2010 Xin LI <delphij@FreeBSD.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * are met:
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 *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/limits.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/types.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Portable strlen() for 32-bit and 64-bit systems.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The expression:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ((x - 0x01....01) & ~x & 0x80....80)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * would evaluate to a non-zero value iff any of the bytes in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * original word is zero.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * c) a & b
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Where, a) and b) can be partially computed in parallel.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The algorithm above is found on "Hacker's Delight" by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Henry S. Warren, Jr.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Magic numbers for the algorithm */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if LONG_BIT == 32
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const unsigned long mask01 = 0x01010101;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const unsigned long mask80 = 0x80808080;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#elif LONG_BIT == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const unsigned long mask01 = 0x0101010101010101;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const unsigned long mask80 = 0x8080808080808080;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#error Unsupported word size
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define LONGPTR_MASK (sizeof(long) - 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Helper macro to return string length if we caught the zero
199767f8919635c4928607450d9e0abb932109ceToomas Soome * byte.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define testbyte(x) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome do { \
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p[x] == '\0') \
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (p - str + x); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomesize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomestrlen(const char *str)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned long *lp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome long va, vb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 * first.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome lp = (const unsigned long *)((uintptr_t)str & ~LONGPTR_MASK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome va = (*lp - mask01);
199767f8919635c4928607450d9e0abb932109ceToomas Soome vb = ((~*lp) & mask80);
199767f8919635c4928607450d9e0abb932109ceToomas Soome lp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (va & vb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check if we have \0 in the first part */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (p = str; p < (const char *)lp; p++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (p - str);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Scan the rest of the string using word sized operation */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (; ; lp++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome va = (*lp - mask01);
199767f8919635c4928607450d9e0abb932109ceToomas Soome vb = ((~*lp) & mask80);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (va & vb) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = (const char *)(lp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome testbyte(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome testbyte(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome testbyte(2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome testbyte(3);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (LONG_BIT >= 64)
199767f8919635c4928607450d9e0abb932109ceToomas Soome testbyte(4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome testbyte(5);
199767f8919635c4928607450d9e0abb932109ceToomas Soome testbyte(6);
199767f8919635c4928607450d9e0abb932109ceToomas Soome testbyte(7);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* NOTREACHED */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}