1 /* Copyright (c) 2013 Yaakov Selkowitz <yselkowi@redhat.com> */
2 /*
3 FUNCTION
4 	<<rawmemchr>>---find character in memory
5 
6 INDEX
7 	rawmemchr
8 
9 SYNOPSIS
10 	#include <string.h>
11 	void *rawmemchr(const void *<[src]>, int <[c]>);
12 
13 DESCRIPTION
14 	This function searches memory starting at <<*<[src]>>> for the
15 	character <[c]>.  The search only ends with the first occurrence
16 	of <[c]>; in particular, <<NUL>> does not terminate the search.
17 	No bounds checking is performed, so this function should only
18 	be used when it is certain that the character <[c]> will be found.
19 
20 RETURNS
21 	A pointer to the first occurance of character <[c]>.
22 
23 PORTABILITY
24 <<rawmemchr>> is a GNU extension.
25 
26 <<rawmemchr>> requires no supporting OS subroutines.
27 
28 QUICKREF
29 	rawmemchr
30 */
31 
32 #include <_ansi.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <stdint.h>
36 
37 /* Nonzero if X is not aligned on a "long" boundary.  */
38 #define UNALIGNED(X) ((uintptr_t)X & (sizeof (long) - 1))
39 
40 /* How many bytes are loaded each iteration of the word copy loop.  */
41 #define LBLOCKSIZE (sizeof (long))
42 
43 /* Threshhold for punting to the bytewise iterator.  */
44 #define TOO_SMALL(LEN)  ((LEN) < LBLOCKSIZE)
45 
46 #if LONG_MAX == 2147483647L
47 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
48 #else
49 #if LONG_MAX == 9223372036854775807L
50 /* Nonzero if X (a long int) contains a NULL byte. */
51 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
52 #else
53 #error long int is not a 32bit or 64bit type.
54 #endif
55 #endif
56 
57 #ifndef DETECTNULL
58 #error long int is not a 32bit or 64bit byte
59 #endif
60 
61 /* DETECTCHAR returns nonzero if (long)X contains the byte used
62    to fill (long)MASK. */
63 #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
64 
65 void *
rawmemchr(const void * src_void,int c)66 rawmemchr (const void *src_void,
67 	int c)
68 {
69   const unsigned char *src = (const unsigned char *) src_void;
70   unsigned char d = c;
71 
72 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && \
73     !defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
74   unsigned long *asrc;
75   unsigned long  mask;
76   unsigned int i;
77 
78   while (UNALIGNED (src))
79     {
80       if (*src == d)
81         return (void *) src;
82       src++;
83     }
84 
85   /* If we get this far, we know that src is word-aligned. */
86   /* The fast code reads the source one word at a time and only
87      performs the bytewise search on word-sized segments if they
88      contain the search character, which is detected by XORing
89      the word-sized segment with a word-sized block of the search
90      character and then detecting for the presence of NUL in the
91      result.  */
92   asrc = (unsigned long *) src;
93   mask = d << 8 | d;
94   mask = mask << 16 | mask;
95   for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
96     mask = (mask << i) | mask;
97 
98   while (1)
99     {
100       if (DETECTCHAR (*asrc, mask))
101         break;
102       asrc++;
103     }
104 
105   /* We have the matching word, now we resort to a bytewise loop. */
106 
107   src = (unsigned char *) asrc;
108 
109 #endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */
110 
111   while (1)
112     {
113       if (*src == d)
114         return (void *) src;
115       src++;
116     }
117 }
118