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 #include <_ansi.h>
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   unsigned long *asrc;
76   unsigned long  mask;
77   unsigned int i;
78 
79   while (UNALIGNED (src))
80     {
81       if (!length--)
82         return NULL;
83       if (*src == d)
84         return (void *) src;
85       src--;
86     }
87 
88   if (!TOO_SMALL (length))
89     {
90       /* If we get this far, we know that length is large and src is
91          word-aligned. */
92       /* The fast code reads the source one word at a time and only
93          performs the bytewise search on word-sized segments if they
94          contain the search character, which is detected by XORing
95          the word-sized segment with a word-sized block of the search
96          character and then detecting for the presence of NUL in the
97          result.  */
98       asrc = (unsigned long *) (src - LBLOCKSIZE + 1);
99       mask = d << 8 | d;
100       mask = mask << 16 | mask;
101       for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
102         mask = (mask << i) | mask;
103 
104       while (length >= LBLOCKSIZE)
105         {
106           if (DETECTCHAR (*asrc, mask))
107             break;
108           length -= LBLOCKSIZE;
109           asrc--;
110         }
111 
112       /* If there are fewer than LBLOCKSIZE characters left,
113          then we resort to the bytewise loop.  */
114 
115       src = (unsigned char *) asrc + LBLOCKSIZE - 1;
116     }
117 
118 #endif /* not PREFER_SIZE_OVER_SPEED */
119 
120   while (length--)
121     {
122       if (*src == d)
123         return (void *) src;
124       src--;
125     }
126 
127   return NULL;
128 }
129