… | |
… | |
94 | */ |
94 | */ |
95 | #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__ |
95 | #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__ |
96 | #define ECB_GCC_VERSION(major,minor) 0 |
96 | #define ECB_GCC_VERSION(major,minor) 0 |
97 | #else |
97 | #else |
98 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
98 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
|
|
99 | #endif |
|
|
100 | |
|
|
101 | #define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor))) |
|
|
102 | |
|
|
103 | #if __clang__ && defined(__has_builtin) |
|
|
104 | #define ECB_CLANG_BUILTIN(x) __has_builtin(x) |
|
|
105 | #else |
|
|
106 | #define ECB_CLANG_BUILTIN(x) 0 |
99 | #endif |
107 | #endif |
100 | |
108 | |
101 | #define ECB_CPP (__cplusplus+0) |
109 | #define ECB_CPP (__cplusplus+0) |
102 | #define ECB_CPP11 (__cplusplus >= 201103L) |
110 | #define ECB_CPP11 (__cplusplus >= 201103L) |
103 | |
111 | |
… | |
… | |
296 | #define ECB_STRINGIFY_(a) # a |
304 | #define ECB_STRINGIFY_(a) # a |
297 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
305 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
298 | |
306 | |
299 | #define ecb_function_ ecb_inline |
307 | #define ecb_function_ ecb_inline |
300 | |
308 | |
301 | #if ECB_GCC_VERSION(3,1) |
309 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8) |
302 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
310 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
|
|
311 | #else |
|
|
312 | #define ecb_attribute(attrlist) |
|
|
313 | #endif |
|
|
314 | |
|
|
315 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_constant_p) |
303 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
316 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
304 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
305 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
306 | #else |
317 | #else |
307 | #define ecb_attribute(attrlist) |
|
|
308 | |
|
|
309 | /* possible C11 impl for integral types |
318 | /* possible C11 impl for integral types |
310 | typedef struct ecb_is_constant_struct ecb_is_constant_struct; |
319 | typedef struct ecb_is_constant_struct ecb_is_constant_struct; |
311 | #define ecb_is_constant(expr) _Generic ((1 ? (struct ecb_is_constant_struct *)0 : (void *)((expr) - (expr)), ecb_is_constant_struct *: 0, default: 1)) */ |
320 | #define ecb_is_constant(expr) _Generic ((1 ? (struct ecb_is_constant_struct *)0 : (void *)((expr) - (expr)), ecb_is_constant_struct *: 0, default: 1)) */ |
312 | |
321 | |
313 | #define ecb_is_constant(expr) 0 |
322 | #define ecb_is_constant(expr) 0 |
|
|
323 | #endif |
|
|
324 | |
|
|
325 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_expect) |
|
|
326 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
327 | #else |
314 | #define ecb_expect(expr,value) (expr) |
328 | #define ecb_expect(expr,value) (expr) |
|
|
329 | #endif |
|
|
330 | |
|
|
331 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_prefetch) |
|
|
332 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
333 | #else |
315 | #define ecb_prefetch(addr,rw,locality) |
334 | #define ecb_prefetch(addr,rw,locality) |
316 | #endif |
335 | #endif |
317 | |
336 | |
318 | /* no emulation for ecb_decltype */ |
337 | /* no emulation for ecb_decltype */ |
319 | #if ECB_GCC_VERSION(4,5) |
338 | #if ECB_GCC_VERSION(4,5) |
… | |
… | |
358 | /* for compatibility to the rest of the world */ |
377 | /* for compatibility to the rest of the world */ |
359 | #define ecb_likely(expr) ecb_expect_true (expr) |
378 | #define ecb_likely(expr) ecb_expect_true (expr) |
360 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
379 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
361 | |
380 | |
362 | /* count trailing zero bits and count # of one bits */ |
381 | /* count trailing zero bits and count # of one bits */ |
363 | #if ECB_GCC_VERSION(3,4) |
382 | #if ECB_GCC_VERSION(3,4) || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \ |
|
|
383 | && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \ |
|
|
384 | && ECB_CLANG_BUILTIN(__builtin_popcount)) |
364 | /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ |
385 | /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ |
365 | #define ecb_ld32(x) (__builtin_clz (x) ^ 31) |
386 | #define ecb_ld32(x) (__builtin_clz (x) ^ 31) |
366 | #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) |
387 | #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) |
367 | #define ecb_ctz32(x) __builtin_ctz (x) |
388 | #define ecb_ctz32(x) __builtin_ctz (x) |
368 | #define ecb_ctz64(x) __builtin_ctzll (x) |
389 | #define ecb_ctz64(x) __builtin_ctzll (x) |
… | |
… | |
499 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
520 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
500 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
521 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
501 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
522 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
502 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
523 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
503 | |
524 | |
504 | #if ECB_GCC_VERSION(4,3) |
525 | #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) |
505 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
526 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
506 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
527 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
507 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
528 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
508 | #else |
529 | #else |
509 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
530 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
… | |
… | |
526 | { |
547 | { |
527 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
548 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
528 | } |
549 | } |
529 | #endif |
550 | #endif |
530 | |
551 | |
531 | #if ECB_GCC_VERSION(4,5) |
552 | #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable) |
532 | #define ecb_unreachable() __builtin_unreachable () |
553 | #define ecb_unreachable() __builtin_unreachable () |
533 | #else |
554 | #else |
534 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
555 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
535 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
556 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
536 | ecb_inline void ecb_unreachable (void) { } |
557 | ecb_inline void ecb_unreachable (void) { } |