1 #ifndef _ATOMIC_H 2 #define _ATOMIC_H 3 4 #include <stdint.h> 5 6 #include "atomic_arch.h" 7 8 #ifndef a_crash 9 #define a_crash a_crash 10 static inline void a_crash() 11 { 12 *(volatile char *)0=0; 13 } 14 #endif 15 16 #ifndef a_ctz_32 17 #define a_ctz_32 a_ctz_32 18 static inline int a_ctz_32(uint32_t x) 19 { 20 #ifdef a_clz_32 21 return 31-a_clz_32(x&-x); 22 #else 23 static const char debruijn32[32] = { 24 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 25 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 26 }; 27 return debruijn32[(x&-x)*0x076be629 >> 27]; 28 #endif 29 } 30 #endif 31 32 #ifndef a_ctz_64 33 #define a_ctz_64 a_ctz_64 34 static inline int a_ctz_64(uint64_t x) 35 { 36 static const char debruijn64[64] = { 37 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, 38 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, 39 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, 40 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12 41 }; 42 if (sizeof(long) < 8) { 43 uint32_t y = x; 44 if (!y) { 45 y = x>>32; 46 return 32 + a_ctz_32(y); 47 } 48 return a_ctz_32(y); 49 } 50 return debruijn64[(x&-x)*0x022fdd63cc95386dull >> 58]; 51 } 52 #endif 53 54 static inline int a_ctz_l(unsigned long x) 55 { 56 return (sizeof(long) < 8) ? a_ctz_32(x) : a_ctz_64(x); 57 } 58 59 #ifndef a_clz_64 60 #define a_clz_64 a_clz_64 61 static inline int a_clz_64(uint64_t x) 62 { 63 #ifdef a_clz_32 64 if (x>>32) 65 return a_clz_32(x>>32); 66 return a_clz_32(x) + 32; 67 #else 68 uint32_t y; 69 int r; 70 if (x>>32) y=x>>32, r=0; else y=x, r=32; 71 if (y>>16) y>>=16; else r |= 16; 72 if (y>>8) y>>=8; else r |= 8; 73 if (y>>4) y>>=4; else r |= 4; 74 if (y>>2) y>>=2; else r |= 2; 75 return r | !(y>>1); 76 #endif 77 } 78 #endif 79 80 #ifndef a_clz_32 81 #define a_clz_32 a_clz_32 82 static inline int a_clz_32(uint32_t x) 83 { 84 x >>= 1; 85 x |= x >> 1; 86 x |= x >> 2; 87 x |= x >> 4; 88 x |= x >> 8; 89 x |= x >> 16; 90 x++; 91 return 31-a_ctz_32(x); 92 } 93 #endif 94 95 #endif 96