… | |
… | |
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 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
|
|
69 | /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ |
|
|
70 | |
|
|
71 | #if ECB_NO_THREADS || ECB_NO_SMP |
|
|
72 | #define ECB_MEMORY_FENCE do { } while (0) |
|
|
73 | #define ECB_MEMORY_FENCE_ACQUIRE do { } while (0) |
|
|
74 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) |
|
|
75 | #endif |
|
|
76 | |
|
|
77 | #ifndef ECB_MEMORY_FENCE |
|
|
78 | #if ECB_GCC_VERSION(2,5) |
|
|
79 | #if __x86 |
|
|
80 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
|
|
81 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
|
|
82 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
|
|
83 | #elif __amd64 |
|
|
84 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
|
|
85 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
|
|
86 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ |
|
|
87 | #endif |
|
|
88 | #endif |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | #ifndef ECB_MEMORY_FENCE |
|
|
92 | #if ECB_GCC_VERSION(4,4) |
|
|
93 | #define ECB_MEMORY_FENCE __sync_synchronize () |
|
|
94 | #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) |
|
|
95 | #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) |
|
|
96 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
|
|
97 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
|
|
98 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
|
|
99 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
|
|
100 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
|
|
101 | #elif defined(_WIN32) |
|
|
102 | #include <WinNT.h> |
|
|
103 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
|
|
104 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
105 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
106 | #endif |
|
|
107 | #endif |
|
|
108 | |
|
|
109 | #ifndef ECB_MEMORY_FENCE |
|
|
110 | /* |
|
|
111 | * if you get undefined symbol references to pthread_mutex_lock, |
|
|
112 | * or failure to find pthread.h, then you should implement |
|
|
113 | * the ECB_MEMORY_FENCE operations for your cpu/compiler |
|
|
114 | * OR proide pthread.h and link against the posix thread library |
|
|
115 | * of your system. |
|
|
116 | */ |
|
|
117 | #include <pthread.h> |
|
|
118 | |
|
|
119 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
120 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
121 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
122 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
123 | #endif |
|
|
124 | |
|
|
125 | /*****************************************************************************/ |
|
|
126 | |
66 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
127 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
67 | |
128 | |
68 | #if __cplusplus |
129 | #if __cplusplus |
69 | #define ecb_inline static inline |
130 | #define ecb_inline static inline |
70 | #elif ECB_GCC_VERSION(2,5) |
131 | #elif ECB_GCC_VERSION(2,5) |
… | |
… | |
189 | x *= 0x01010101; |
250 | x *= 0x01010101; |
190 | |
251 | |
191 | return x >> 24; |
252 | return x >> 24; |
192 | } |
253 | } |
193 | |
254 | |
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; |
255 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
198 | ecb_function_ int ecb_ld32 (uint32_t x) |
256 | ecb_function_ int ecb_ld32 (uint32_t x) |
199 | { |
257 | { |
200 | int r = 0; |
258 | int r = 0; |
201 | |
259 | |