2N/A/* Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2004, 2006, 2008-2010 2N/A Free Software Foundation, Inc. 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/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/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/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/AYou should have received a copy of the GNU General Public License 2N/A/* Search no more than N bytes of S for C. */ 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 /* 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 /* All these elucidatory comments refer to 4-byte longwords, 2N/A but the theory applies equally well to any size longwords. */ 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 /* 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 ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7). 2N/A That is, we perform the following operations: 2N/A 1. Subtract repeated_one. 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 So, the test whether any byte in longword1 is zero is equivalent to 2N/A testing whether tmp is nonzero. */ 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. */