PIPS
|
#include <config.h>
#include <string.h>
#include <stddef.h>
#include <limits.h>
Go to the source code of this file.
Macros | |
#define | reg_char char |
Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2004, 2006, 2008-2014 Free Software Foundation, Inc. More... | |
#define | BP_SYM(sym) sym |
#define | __memchr memchr |
Functions | |
void * | __memchr (void const *s, int c_in, size_t n) |
Search no more than N bytes of S for C. More... | |
#define reg_char char |
Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2004, 2006, 2008-2014 Free Software Foundation, Inc.
Based on strlen implementation by Torbjorn Granlund (tege@), with help from Dan Sahlin ( sics .sedan@s) and commentary by Jim Blandy ( ics. sejimb@); adaptation to memchr suggested by Dick Karpinski ( ai.m it.ed udick@), and implemented by Roland McGrath ( cca. ucsf. edurolan). d@ai .mit. edu
NOTE: The canonical source of this file is maintained with the GNU C Library. Bugs can be reported to bug-g. libc @prep .ai. mit.e du
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Search no more than N bytes of S for C.
On 32-bit hardware, choosing longword to be a 32-bit unsigned long instead of a 64-bit uintmax_t tends to give better performance. On 64-bit hardware, unsigned long is generally 64 bits already. Change this typedef to experiment with performance.
Handle the first few bytes by reading one byte at a time. Do this until CHAR_PTR is aligned on a longword boundary.
All these elucidatory comments refer to 4-byte longwords, but the theory applies equally well to any size longwords.
Compute auxiliary longword values: repeated_one is a value which has a 1 in every byte. repeated_c has c in every byte.
Instead of the traditional loop which tests each byte, we will test a longword at a time. The tricky part is testing if any of the four bytes in the longword in question are equal to c. We first use an xor with repeated_c. This reduces the task to testing whether any of the four bytes in longword1 is zero.
We compute tmp = ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7). That is, we perform the following operations:
So, the test whether any byte in longword1 is zero is equivalent to testing whether tmp is nonzero.
At this point, we know that either n < sizeof (longword), or one of the sizeof (longword) bytes starting at char_ptr is == c. On little-endian machines, we could determine the first such byte without any further memory accesses, just by looking at the tmp result from the last loop iteration. But this does not work on big-endian machines. Choose code that works in both cases.
Definition at line 59 of file memchr.c.