VirtualBox

source: kBuild/trunk/src/kmk/inlined_memchr.h@ 3387

Last change on this file since 3387 was 2060, checked in by bird, 16 years ago

kmk: fixed strsingle crash.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette