3 <<rawmemchr>>---find character in memory
10 void *rawmemchr(const void *<[src]>, int <[c]>);
13 This function searches memory starting at <<*<[src]>>> for the
14 character <[c]>. The search only ends with the first occurrence
15 of <[c]>; in particular, <<NUL>> does not terminate the search.
16 No bounds checking is performed, so this function should only
17 be used when it is certain that the character <[c]> will be found.
20 A pointer to the first occurance of character <[c]>.
23 <<rawmemchr>> is a GNU extension.
25 <<rawmemchr>> requires no supporting OS subroutines.
35 /* Nonzero if X is not aligned on a "long" boundary. */
36 #define UNALIGNED(X) ((long)X & (sizeof (long) - 1))
38 /* How many bytes are loaded each iteration of the word copy loop. */
39 #define LBLOCKSIZE (sizeof (long))
41 /* Threshhold for punting to the bytewise iterator. */
42 #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
44 #if LONG_MAX == 2147483647L
45 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
47 #if LONG_MAX == 9223372036854775807L
48 /* Nonzero if X (a long int) contains a NULL byte. */
49 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
51 #error long int is not a 32bit or 64bit type.
56 #error long int is not a 32bit or 64bit byte
59 /* DETECTCHAR returns nonzero if (long)X contains the byte used
60 to fill (long)MASK. */
61 #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
64 rawmemchr (const void *src_void
,
67 const unsigned char *src
= (const unsigned char *) src_void
;
70 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
75 while (UNALIGNED (src
))
82 /* If we get this far, we know that src is word-aligned. */
83 /* The fast code reads the source one word at a time and only
84 performs the bytewise search on word-sized segments if they
85 contain the search character, which is detected by XORing
86 the word-sized segment with a word-sized block of the search
87 character and then detecting for the presence of NUL in the
89 asrc
= (unsigned long *) src
;
91 mask
= mask
<< 16 | mask
;
92 for (i
= 32; i
< LBLOCKSIZE
* 8; i
<<= 1)
93 mask
= (mask
<< i
) | mask
;
97 if (DETECTCHAR (*asrc
, mask
))
102 /* We have the matching word, now we resort to a bytewise loop. */
104 src
= (unsigned char *) asrc
;
106 #endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */