… | |
… | |
61 | #else |
61 | #else |
62 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
62 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
63 | #endif |
63 | #endif |
64 | #endif |
64 | #endif |
65 | |
65 | |
|
|
66 | /*****************************************************************************/ |
|
|
67 | |
|
|
68 | #ifndef ECB_MEMORY_FENCE |
|
|
69 | #if ECB_GCC_VERSION(2,5) |
|
|
70 | #if __x86 |
|
|
71 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
|
|
72 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
73 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ |
|
|
74 | #elif __amd64 |
|
|
75 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
|
|
76 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
|
|
77 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") |
|
|
78 | #endif |
|
|
79 | #endif |
|
|
80 | #endif |
|
|
81 | |
|
|
82 | #ifndef ECB_MEMORY_FENCE |
|
|
83 | #if ECB_GCC_VERSION(4,4) |
|
|
84 | #define ECB_MEMORY_FENCE __sync_synchronize () |
|
|
85 | #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) |
|
|
86 | #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) |
|
|
87 | #elif _MSC_VER >= 1400 && 0 /* TODO: only true when using volatiles */ |
|
|
88 | #define ECB_MEMORY_FENCE do { } while (0) |
|
|
89 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
90 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
91 | #elif defined(_WIN32) |
|
|
92 | #include <WinNT.h> |
|
|
93 | #define ECB_MEMORY_FENCE MemoryBarrier () |
|
|
94 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
95 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
96 | #endif |
|
|
97 | #endif |
|
|
98 | |
|
|
99 | #ifndef ECB_MEMORY_FENCE |
|
|
100 | #include <pthread.h> |
|
|
101 | |
|
|
102 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
103 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
104 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
105 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
106 | #endif |
|
|
107 | |
|
|
108 | /*****************************************************************************/ |
|
|
109 | |
66 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
110 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
67 | |
111 | |
68 | #if __cplusplus |
112 | #if __cplusplus |
69 | #define ecb_inline static inline |
113 | #define ecb_inline static inline |
70 | #elif ECB_GCC_VERSION(2,5) |
114 | #elif ECB_GCC_VERSION(2,5) |
… | |
… | |
189 | x *= 0x01010101; |
233 | x *= 0x01010101; |
190 | |
234 | |
191 | return x >> 24; |
235 | return x >> 24; |
192 | } |
236 | } |
193 | |
237 | |
194 | /* you have the choice beetween something with a table lookup, */ |
|
|
195 | /* something using lots of bit arithmetic and a simple loop */ |
|
|
196 | /* we went for the loop */ |
|
|
197 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
238 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
198 | ecb_function_ int ecb_ld32 (uint32_t x) |
239 | ecb_function_ int ecb_ld32 (uint32_t x) |
199 | { |
240 | { |
200 | int r = 0; |
241 | int r = 0; |
201 | |
242 | |