Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / rawmemchr.c
blob56e2b5e2db4fb551a7e9f1d6888f9c2e222a76c3
1 /*
2 FUNCTION
3 <<rawmemchr>>---find character in memory
5 INDEX
6 rawmemchr
8 SYNOPSIS
9 #include <string.h>
10 void *rawmemchr(const void *<[src]>, int <[c]>);
12 DESCRIPTION
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.
19 RETURNS
20 A pointer to the first occurance of character <[c]>.
22 PORTABILITY
23 <<rawmemchr>> is a GNU extension.
25 <<rawmemchr>> requires no supporting OS subroutines.
27 QUICKREF
28 rawmemchr
31 #include <_ansi.h>
32 #include <string.h>
33 #include <limits.h>
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)
46 #else
47 #if LONG_MAX == 9223372036854775807L
48 /* Nonzero if X (a long int) contains a NULL byte. */
49 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
50 #else
51 #error long int is not a 32bit or 64bit type.
52 #endif
53 #endif
55 #ifndef DETECTNULL
56 #error long int is not a 32bit or 64bit byte
57 #endif
59 /* DETECTCHAR returns nonzero if (long)X contains the byte used
60 to fill (long)MASK. */
61 #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
63 void *
64 rawmemchr (const void *src_void,
65 int c)
67 const unsigned char *src = (const unsigned char *) src_void;
68 unsigned char d = c;
70 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
71 unsigned long *asrc;
72 unsigned long mask;
73 unsigned int i;
75 while (UNALIGNED (src))
77 if (*src == d)
78 return (void *) src;
79 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
88 result. */
89 asrc = (unsigned long *) src;
90 mask = d << 8 | d;
91 mask = mask << 16 | mask;
92 for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
93 mask = (mask << i) | mask;
95 while (1)
97 if (DETECTCHAR (*asrc, mask))
98 break;
99 asrc++;
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__ */
108 while (1)
110 if (*src == d)
111 return (void *) src;
112 src++;