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