ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/ecb.h
(Generate patch)

Comparing libeio/ecb.h (file contents):
Revision 1.2 by root, Fri Jun 17 18:55:42 2011 UTC vs.
Revision 1.3 by root, Mon Jun 20 07:28:14 2011 UTC

38 typedef signed int int32_t; 38 typedef signed int int32_t;
39 typedef unsigned int uint32_t; 39 typedef unsigned int uint32_t;
40 #if __GNUC__ 40 #if __GNUC__
41 typedef signed long long int64_t; 41 typedef signed long long int64_t;
42 typedef unsigned long long uint64_t; 42 typedef unsigned long long uint64_t;
43 #else 43 #else /* _MSC_VER || __BORLANDC__ */
44 typedef signed __int64 int64_t; 44 typedef signed __int64 int64_t;
45 typedef unsigned __int64 uint64_t; 45 typedef unsigned __int64 uint64_t;
46 #endif 46 #endif
47#else 47#else
48 #include <inttypes.h> 48 #include <inttypes.h>
152 { 152 {
153 int r = 0; 153 int r = 0;
154 154
155 x &= ~x + 1; /* this isolates the lowest bit */ 155 x &= ~x + 1; /* this isolates the lowest bit */
156 156
157#if ECB_branchless_on_i386
158 r += !!(x & 0xaaaaaaaa) << 0;
159 r += !!(x & 0xcccccccc) << 1;
160 r += !!(x & 0xf0f0f0f0) << 2;
161 r += !!(x & 0xff00ff00) << 3;
162 r += !!(x & 0xffff0000) << 4;
163#else
157 if (x & 0xaaaaaaaa) r += 1; 164 if (x & 0xaaaaaaaa) r += 1;
158 if (x & 0xcccccccc) r += 2; 165 if (x & 0xcccccccc) r += 2;
159 if (x & 0xf0f0f0f0) r += 4; 166 if (x & 0xf0f0f0f0) r += 4;
160 if (x & 0xff00ff00) r += 8; 167 if (x & 0xff00ff00) r += 8;
161 if (x & 0xffff0000) r += 16; 168 if (x & 0xffff0000) r += 16;
169#endif
162 170
163 return r; 171 return r;
164 } 172 }
165 173
166 ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; 174 ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const;
167 ecb_function_ int 175 ecb_function_ int
168 ecb_ctz64 (uint64_t x) 176 ecb_ctz64 (uint64_t x)
169 { 177 {
170 int shift = x & 0xffffffffU ? 0 : 32; 178 int shift = x & 0xffffffffU ? 0 : 32;
171 return ecb_ctz (x >> shift) + shift; 179 return ecb_ctz32 (x >> shift) + shift;
172 } 180 }
173 181
174 ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; 182 ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const;
175 ecb_function_ int 183 ecb_function_ int
176 ecb_popcount32 (uint32_t x) 184 ecb_popcount32 (uint32_t x)
187 /* something using lots of bit arithmetic and a simple loop */ 195 /* something using lots of bit arithmetic and a simple loop */
188 /* we went for the loop */ 196 /* we went for the loop */
189 ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; 197 ecb_function_ int ecb_ld32 (uint32_t x) ecb_const;
190 ecb_function_ int ecb_ld32 (uint32_t x) 198 ecb_function_ int ecb_ld32 (uint32_t x)
191 { 199 {
192 int r = -1; 200 int r = 0;
193 201
194 do 202 if (x >> 16) { x >>= 16; r += 16; }
195 { 203 if (x >> 8) { x >>= 8; r += 8; }
196 x >>= 1; 204 if (x >> 4) { x >>= 4; r += 4; }
197 ++r; 205 if (x >> 2) { x >>= 2; r += 2; }
198 } 206 if (x >> 1) { r += 1; }
199 while (x);
200 207
201 return r; 208 return r;
202 } 209 }
203 210
204 ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; 211 ecb_function_ int ecb_ld64 (uint64_t x) ecb_const;
205 ecb_function_ int ecb_ld64 (uint64_t x) 212 ecb_function_ int ecb_ld64 (uint64_t x)
206 { 213 {
207 int r = -1; 214 int r = 0;
208 215
209 do 216 if (x >> 32) { x >>= 32; r += 32; }
210 {
211 x >>= 1;
212 ++r;
213 }
214 while (x);
215 217
216 return r; 218 return r + ecb_ld32 (x);
217 } 219 }
218#endif 220#endif
219 221
220/* popcount64 is only available on 64 bit cpus as gcc builtin */ 222/* popcount64 is only available on 64 bit cpus as gcc builtin */
221/* so for this version we are lazy */ 223/* so for this version we are lazy */
224ecb_popcount64 (uint64_t x) 226ecb_popcount64 (uint64_t x)
225{ 227{
226 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); 228 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
227} 229}
228 230
231ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const;
232ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const;
233ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const;
234ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const;
235ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
236ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
237ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const;
238ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const;
239
240ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
241ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
242ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
243ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
244ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
245ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
246ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
247ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
248
229#if ECB_GCC_VERSION(4,3) 249#if ECB_GCC_VERSION(4,3)
230 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) 250 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
231 #define ecb_bswap32(x) __builtin_bswap32 (x) 251 #define ecb_bswap32(x) __builtin_bswap32 (x)
232 #define ecb_bswap64(x) __builtin_bswap64 (x) 252 #define ecb_bswap64(x) __builtin_bswap64 (x)
233#else 253#else
234 ecb_function_ uint32_t ecb_bswap16 (uint32_t x) ecb_const; 254 ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const;
235 ecb_function_ uint32_t 255 ecb_function_ uint16_t
236 ecb_bswap16 (uint32_t x) 256 ecb_bswap16 (uint16_t x)
237 { 257 {
238 return ((x >> 8) & 0xff) 258 return ecb_rotl16 (x, 8);
239 | ((x << 8) & 0x00ff0000)
240 | (x << 24);
241 } 259 }
242 260
243 ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; 261 ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const;
244 ecb_function_ uint32_t 262 ecb_function_ uint32_t
245 ecb_bswap32 (uint32_t x) 263 ecb_bswap32 (uint32_t x)
246 { 264 {
247 return (x >> 24) 265 return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
248 | ((x >> 8) & 0x0000ff00)
249 | ((x << 8) & 0x00ff0000)
250 | (x << 24);
251 } 266 }
252 267
253 ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; 268 ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const;
254 ecb_function_ uint64_t 269 ecb_function_ uint64_t
255 ecb_bswap64 (uint64_t x) 270 ecb_bswap64 (uint64_t x)
256 { 271 {
257 return (((uint64_t)ecb_bswap32 (x)) << 32) 272 return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
258 | ecb_bswap32 (x >> 32);
259 } 273 }
260#endif 274#endif
261 275
262#if ECB_GCC_VERSION(4,5) 276#if ECB_GCC_VERSION(4,5)
263 #define ecb_unreachable() __builtin_unreachable () 277 #define ecb_unreachable() __builtin_unreachable ()
298 } 312 }
299#else 313#else
300 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 314 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
301#endif 315#endif
302 316
303ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
304ecb_inline uint32_t
305ecb_rotr32 (uint32_t x, unsigned int count)
306{
307 return (x << (32 - count)) | (x >> count);
308}
309
310ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
311ecb_inline uint32_t
312ecb_rotl32 (uint32_t x, unsigned int count)
313{
314 return (x >> (32 - count)) | (x << count);
315}
316
317ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const;
318ecb_inline uint64_t
319ecb_rotr64 (uint64_t x, unsigned int count)
320{
321 return (x << (64 - count)) | (x >> count);
322}
323
324ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const;
325ecb_inline uint64_t
326ecb_rotl64 (uint64_t x, unsigned int count)
327{
328 return (x >> (64 - count)) | (x << count);
329}
330
331#endif 317#endif
332 318

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines