2N/A/* Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2004, 2006, 2008-2010
2N/A Free Software Foundation, Inc.
2N/A
2N/A Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
2N/A with help from Dan Sahlin (dan@sics.se) and
2N/A commentary by Jim Blandy (jimb@ai.mit.edu);
2N/A adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
2N/A and implemented by Roland McGrath (roland@ai.mit.edu).
2N/A
2N/ANOTE: The canonical source of this file is maintained with the GNU C Library.
2N/ABugs can be reported to bug-glibc@prep.ai.mit.edu.
2N/A
2N/AThis program is free software: you can redistribute it and/or modify it
2N/Aunder the terms of the GNU General Public License as published by the
2N/AFree Software Foundation; either version 3 of the License, or any
2N/Alater version.
2N/A
2N/AThis program is distributed in the hope that it will be useful,
2N/Abut WITHOUT ANY WARRANTY; without even the implied warranty of
2N/AMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/AGNU General Public License for more details.
2N/A
2N/AYou should have received a copy of the GNU General Public License
2N/Aalong with this program. If not, see <http://www.gnu.org/licenses/>. */
2N/A
2N/A#ifndef _LIBC
2N/A# include <config.h>
2N/A#endif
2N/A
2N/A#include <string.h>
2N/A
2N/A#include <stddef.h>
2N/A
2N/A#if defined _LIBC
2N/A# include <memcopy.h>
2N/A#else
2N/A# define reg_char char
2N/A#endif
2N/A
2N/A#include <limits.h>
2N/A
2N/A#if HAVE_BP_SYM_H || defined _LIBC
2N/A# include <bp-sym.h>
2N/A#else
2N/A# define BP_SYM(sym) sym
2N/A#endif
2N/A
2N/A#undef __memchr
2N/A#ifdef _LIBC
2N/A# undef memchr
2N/A#endif
2N/A
2N/A#ifndef weak_alias
2N/A# define __memchr memchr
2N/A#endif
2N/A
2N/A/* Search no more than N bytes of S for C. */
2N/Avoid *
2N/A__memchr (void const *s, int c_in, size_t n)
2N/A{
2N/A /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
2N/A long instead of a 64-bit uintmax_t tends to give better
2N/A performance. On 64-bit hardware, unsigned long is generally 64
2N/A bits already. Change this typedef to experiment with
2N/A performance. */
2N/A typedef unsigned long int longword;
2N/A
2N/A const unsigned char *char_ptr;
2N/A const longword *longword_ptr;
2N/A longword repeated_one;
2N/A longword repeated_c;
2N/A unsigned reg_char c;
2N/A
2N/A c = (unsigned char) c_in;
2N/A
2N/A /* Handle the first few bytes by reading one byte at a time.
2N/A Do this until CHAR_PTR is aligned on a longword boundary. */
2N/A for (char_ptr = (const unsigned char *) s;
2N/A n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
2N/A --n, ++char_ptr)
2N/A if (*char_ptr == c)
2N/A return (void *) char_ptr;
2N/A
2N/A longword_ptr = (const longword *) char_ptr;
2N/A
2N/A /* All these elucidatory comments refer to 4-byte longwords,
2N/A but the theory applies equally well to any size longwords. */
2N/A
2N/A /* Compute auxiliary longword values:
2N/A repeated_one is a value which has a 1 in every byte.
2N/A repeated_c has c in every byte. */
2N/A repeated_one = 0x01010101;
2N/A repeated_c = c | (c << 8);
2N/A repeated_c |= repeated_c << 16;
2N/A if (0xffffffffU < (longword) -1)
2N/A {
2N/A repeated_one |= repeated_one << 31 << 1;
2N/A repeated_c |= repeated_c << 31 << 1;
2N/A if (8 < sizeof (longword))
2N/A {
2N/A size_t i;
2N/A
2N/A for (i = 64; i < sizeof (longword) * 8; i *= 2)
2N/A {
2N/A repeated_one |= repeated_one << i;
2N/A repeated_c |= repeated_c << i;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Instead of the traditional loop which tests each byte, we will test a
2N/A longword at a time. The tricky part is testing if *any of the four*
2N/A bytes in the longword in question are equal to c. We first use an xor
2N/A with repeated_c. This reduces the task to testing whether *any of the
2N/A four* bytes in longword1 is zero.
2N/A
2N/A We compute tmp =
2N/A ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
2N/A That is, we perform the following operations:
2N/A 1. Subtract repeated_one.
2N/A 2. & ~longword1.
2N/A 3. & a mask consisting of 0x80 in every byte.
2N/A Consider what happens in each byte:
2N/A - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
2N/A and step 3 transforms it into 0x80. A carry can also be propagated
2N/A to more significant bytes.
2N/A - If a byte of longword1 is nonzero, let its lowest 1 bit be at
2N/A position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
2N/A the byte ends in a single bit of value 0 and k bits of value 1.
2N/A After step 2, the result is just k bits of value 1: 2^k - 1. After
2N/A step 3, the result is 0. And no carry is produced.
2N/A So, if longword1 has only non-zero bytes, tmp is zero.
2N/A Whereas if longword1 has a zero byte, call j the position of the least
2N/A significant zero byte. Then the result has a zero at positions 0, ...,
2N/A j-1 and a 0x80 at position j. We cannot predict the result at the more
2N/A significant bytes (positions j+1..3), but it does not matter since we
2N/A already have a non-zero bit at position 8*j+7.
2N/A
2N/A So, the test whether any byte in longword1 is zero is equivalent to
2N/A testing whether tmp is nonzero. */
2N/A
2N/A while (n >= sizeof (longword))
2N/A {
2N/A longword longword1 = *longword_ptr ^ repeated_c;
2N/A
2N/A if ((((longword1 - repeated_one) & ~longword1)
2N/A & (repeated_one << 7)) != 0)
2N/A break;
2N/A longword_ptr++;
2N/A n -= sizeof (longword);
2N/A }
2N/A
2N/A char_ptr = (const unsigned char *) longword_ptr;
2N/A
2N/A /* At this point, we know that either n < sizeof (longword), or one of the
2N/A sizeof (longword) bytes starting at char_ptr is == c. On little-endian
2N/A machines, we could determine the first such byte without any further
2N/A memory accesses, just by looking at the tmp result from the last loop
2N/A iteration. But this does not work on big-endian machines. Choose code
2N/A that works in both cases. */
2N/A
2N/A for (; n > 0; --n, ++char_ptr)
2N/A {
2N/A if (*char_ptr == c)
2N/A return (void *) char_ptr;
2N/A }
2N/A
2N/A return NULL;
2N/A}
2N/A#ifdef weak_alias
2N/Aweak_alias (__memchr, BP_SYM (memchr))
2N/A#endif