… | |
… | |
90 | * causing enormous grief in return for some better fake benchmark numbers. |
90 | * causing enormous grief in return for some better fake benchmark numbers. |
91 | * or so. |
91 | * or so. |
92 | * we try to detect these and simply assume they are not gcc - if they have |
92 | * we try to detect these and simply assume they are not gcc - if they have |
93 | * an issue with that they should have done it right in the first place. |
93 | * an issue with that they should have done it right in the first place. |
94 | */ |
94 | */ |
95 | #ifndef ECB_GCC_VERSION |
|
|
96 | #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__ |
97 | #define ECB_GCC_VERSION(major,minor) 0 |
96 | #define ECB_GCC_VERSION(major,minor) 0 |
98 | #else |
97 | #else |
99 | #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))) |
100 | #endif |
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 |
101 | #endif |
107 | #endif |
102 | |
108 | |
103 | #define ECB_CPP (__cplusplus+0) |
109 | #define ECB_CPP (__cplusplus+0) |
104 | #define ECB_CPP11 (__cplusplus >= 201103L) |
110 | #define ECB_CPP11 (__cplusplus >= 201103L) |
105 | |
111 | |
… | |
… | |
298 | #define ECB_STRINGIFY_(a) # a |
304 | #define ECB_STRINGIFY_(a) # a |
299 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
305 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
300 | |
306 | |
301 | #define ecb_function_ ecb_inline |
307 | #define ecb_function_ ecb_inline |
302 | |
308 | |
303 | #if ECB_GCC_VERSION(3,1) |
309 | #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8) |
304 | #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) |
305 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
316 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
306 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
307 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
308 | #else |
317 | #else |
309 | #define ecb_attribute(attrlist) |
|
|
310 | |
|
|
311 | /* possible C11 impl for integral types |
318 | /* possible C11 impl for integral types |
312 | typedef struct ecb_is_constant_struct ecb_is_constant_struct; |
319 | typedef struct ecb_is_constant_struct ecb_is_constant_struct; |
313 | #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)) */ |
314 | |
321 | |
315 | #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 |
316 | #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 |
317 | #define ecb_prefetch(addr,rw,locality) |
334 | #define ecb_prefetch(addr,rw,locality) |
318 | #endif |
335 | #endif |
319 | |
336 | |
320 | /* no emulation for ecb_decltype */ |
337 | /* no emulation for ecb_decltype */ |
321 | #if ECB_GCC_VERSION(4,5) |
338 | #if ECB_GCC_VERSION(4,5) |
… | |
… | |
360 | /* for compatibility to the rest of the world */ |
377 | /* for compatibility to the rest of the world */ |
361 | #define ecb_likely(expr) ecb_expect_true (expr) |
378 | #define ecb_likely(expr) ecb_expect_true (expr) |
362 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
379 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
363 | |
380 | |
364 | /* count trailing zero bits and count # of one bits */ |
381 | /* count trailing zero bits and count # of one bits */ |
365 | #if ECB_GCC_VERSION(3,4) |
382 | #if ECB_GCC_VERSION(3,4) \ |
|
|
383 | || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \ |
|
|
384 | && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \ |
|
|
385 | && ECB_CLANG_BUILTIN(__builtin_popcount)) |
366 | /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ |
386 | /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ |
367 | #define ecb_ld32(x) (__builtin_clz (x) ^ 31) |
387 | #define ecb_ld32(x) (__builtin_clz (x) ^ 31) |
368 | #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) |
388 | #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) |
369 | #define ecb_ctz32(x) __builtin_ctz (x) |
389 | #define ecb_ctz32(x) __builtin_ctz (x) |
370 | #define ecb_ctz64(x) __builtin_ctzll (x) |
390 | #define ecb_ctz64(x) __builtin_ctzll (x) |
… | |
… | |
501 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
521 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
502 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
522 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
503 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
523 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
504 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
524 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
505 | |
525 | |
506 | #if ECB_GCC_VERSION(4,3) |
526 | #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64)) |
507 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
527 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
508 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
528 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
509 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
529 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
510 | #else |
530 | #else |
511 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
531 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
… | |
… | |
528 | { |
548 | { |
529 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
549 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
530 | } |
550 | } |
531 | #endif |
551 | #endif |
532 | |
552 | |
533 | #if ECB_GCC_VERSION(4,5) |
553 | #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable) |
534 | #define ecb_unreachable() __builtin_unreachable () |
554 | #define ecb_unreachable() __builtin_unreachable () |
535 | #else |
555 | #else |
536 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
556 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
537 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
557 | ecb_inline void ecb_unreachable (void) ecb_noreturn; |
538 | ecb_inline void ecb_unreachable (void) { } |
558 | ecb_inline void ecb_unreachable (void) { } |