1 | #include <string.h>
|
---|
2 |
|
---|
3 | #ifdef _MSC_VER
|
---|
4 | _inline void *
|
---|
5 | #else
|
---|
6 | static __inline__ void *
|
---|
7 | #endif
|
---|
8 | my_inline_memchr(const void *pv, int ch, register size_t cb)
|
---|
9 | {
|
---|
10 | register const unsigned int uch = (unsigned)ch;
|
---|
11 | register const unsigned char *pb = (const unsigned char *)pv;
|
---|
12 | #if 0 /* 8-byte loop unroll */
|
---|
13 | while (cb >= 8)
|
---|
14 | {
|
---|
15 | if (*pb == uch)
|
---|
16 | return (unsigned char *)pb;
|
---|
17 | if (pb[1] == uch)
|
---|
18 | return (unsigned char *)pb + 1;
|
---|
19 | if (pb[2] == uch)
|
---|
20 | return (unsigned char *)pb + 2;
|
---|
21 | if (pb[3] == uch)
|
---|
22 | return (unsigned char *)pb + 3;
|
---|
23 | if (pb[4] == uch)
|
---|
24 | return (unsigned char *)pb + 4;
|
---|
25 | if (pb[5] == uch)
|
---|
26 | return (unsigned char *)pb + 5;
|
---|
27 | if (pb[6] == uch)
|
---|
28 | return (unsigned char *)pb + 6;
|
---|
29 | if (pb[7] == uch)
|
---|
30 | return (unsigned char *)pb + 7;
|
---|
31 | cb -= 8;
|
---|
32 | pb += 8;
|
---|
33 | }
|
---|
34 | switch (cb & 7)
|
---|
35 | {
|
---|
36 | case 0:
|
---|
37 | break;
|
---|
38 | case 1:
|
---|
39 | if (*pb == uch)
|
---|
40 | return (unsigned char *)pb;
|
---|
41 | break;
|
---|
42 | case 2:
|
---|
43 | if (*pb == uch)
|
---|
44 | return (unsigned char *)pb;
|
---|
45 | if (pb[1] == uch)
|
---|
46 | return (unsigned char *)pb + 1;
|
---|
47 | break;
|
---|
48 | case 3:
|
---|
49 | if (*pb == uch)
|
---|
50 | return (unsigned char *)pb;
|
---|
51 | if (pb[1] == uch)
|
---|
52 | return (unsigned char *)pb + 1;
|
---|
53 | if (pb[2] == uch)
|
---|
54 | return (unsigned char *)pb + 2;
|
---|
55 | break;
|
---|
56 | case 4:
|
---|
57 | if (*pb == uch)
|
---|
58 | return (unsigned char *)pb;
|
---|
59 | if (pb[1] == uch)
|
---|
60 | return (unsigned char *)pb + 1;
|
---|
61 | if (pb[2] == uch)
|
---|
62 | return (unsigned char *)pb + 2;
|
---|
63 | if (pb[3] == uch)
|
---|
64 | return (unsigned char *)pb + 3;
|
---|
65 | break;
|
---|
66 | case 5:
|
---|
67 | if (*pb == uch)
|
---|
68 | return (unsigned char *)pb;
|
---|
69 | if (pb[1] == uch)
|
---|
70 | return (unsigned char *)pb + 1;
|
---|
71 | if (pb[2] == uch)
|
---|
72 | return (unsigned char *)pb + 2;
|
---|
73 | if (pb[3] == uch)
|
---|
74 | return (unsigned char *)pb + 3;
|
---|
75 | if (pb[4] == uch)
|
---|
76 | return (unsigned char *)pb + 4;
|
---|
77 | break;
|
---|
78 | case 6:
|
---|
79 | if (*pb == uch)
|
---|
80 | return (unsigned char *)pb;
|
---|
81 | if (pb[1] == uch)
|
---|
82 | return (unsigned char *)pb + 1;
|
---|
83 | if (pb[2] == uch)
|
---|
84 | return (unsigned char *)pb + 2;
|
---|
85 | if (pb[3] == uch)
|
---|
86 | return (unsigned char *)pb + 3;
|
---|
87 | if (pb[4] == uch)
|
---|
88 | return (unsigned char *)pb + 4;
|
---|
89 | if (pb[5] == uch)
|
---|
90 | return (unsigned char *)pb + 5;
|
---|
91 | break;
|
---|
92 | case 7:
|
---|
93 | if (*pb == uch)
|
---|
94 | return (unsigned char *)pb;
|
---|
95 | if (pb[1] == uch)
|
---|
96 | return (unsigned char *)pb + 1;
|
---|
97 | if (pb[2] == uch)
|
---|
98 | return (unsigned char *)pb + 2;
|
---|
99 | if (pb[3] == uch)
|
---|
100 | return (unsigned char *)pb + 3;
|
---|
101 | if (pb[4] == uch)
|
---|
102 | return (unsigned char *)pb + 4;
|
---|
103 | if (pb[5] == uch)
|
---|
104 | return (unsigned char *)pb + 5;
|
---|
105 | if (pb[6] == uch)
|
---|
106 | return (unsigned char *)pb + 6;
|
---|
107 | break;
|
---|
108 | }
|
---|
109 |
|
---|
110 | #elif 1 /* 4 byte loop unroll */
|
---|
111 | while (cb >= 4)
|
---|
112 | {
|
---|
113 | if (*pb == uch)
|
---|
114 | return (unsigned char *)pb;
|
---|
115 | if (pb[1] == uch)
|
---|
116 | return (unsigned char *)pb + 1;
|
---|
117 | if (pb[2] == uch)
|
---|
118 | return (unsigned char *)pb + 2;
|
---|
119 | if (pb[3] == uch)
|
---|
120 | return (unsigned char *)pb + 3;
|
---|
121 | cb -= 4;
|
---|
122 | pb += 4;
|
---|
123 | }
|
---|
124 | switch (cb & 3)
|
---|
125 | {
|
---|
126 | case 0:
|
---|
127 | break;
|
---|
128 | case 1:
|
---|
129 | if (*pb == uch)
|
---|
130 | return (unsigned char *)pb;
|
---|
131 | break;
|
---|
132 | case 2:
|
---|
133 | if (*pb == uch)
|
---|
134 | return (unsigned char *)pb;
|
---|
135 | if (pb[1] == uch)
|
---|
136 | return (unsigned char *)pb + 1;
|
---|
137 | break;
|
---|
138 | case 3:
|
---|
139 | if (*pb == uch)
|
---|
140 | return (unsigned char *)pb;
|
---|
141 | if (pb[1] == uch)
|
---|
142 | return (unsigned char *)pb + 1;
|
---|
143 | if (pb[2] == uch)
|
---|
144 | return (unsigned char *)pb + 2;
|
---|
145 | break;
|
---|
146 | }
|
---|
147 |
|
---|
148 | #else /* the basic loop */
|
---|
149 | while (cb > 0)
|
---|
150 | {
|
---|
151 | if (*pb == uch)
|
---|
152 | return (void *)pb;
|
---|
153 | cb--;
|
---|
154 | pb++;
|
---|
155 | }
|
---|
156 | #endif
|
---|
157 | return 0;
|
---|
158 | }
|
---|
159 |
|
---|
160 | #define memchr my_inline_memchr
|
---|
161 |
|
---|