… | |
… | |
40 | |
40 | |
41 | #ifndef ECB_H |
41 | #ifndef ECB_H |
42 | #define ECB_H |
42 | #define ECB_H |
43 | |
43 | |
44 | /* 16 bits major, 16 bits minor */ |
44 | /* 16 bits major, 16 bits minor */ |
45 | #define ECB_VERSION 0x00010005 |
45 | #define ECB_VERSION 0x00010007 |
46 | |
46 | |
47 | #ifdef _WIN32 |
47 | #ifdef _WIN32 |
48 | typedef signed char int8_t; |
48 | typedef signed char int8_t; |
49 | typedef unsigned char uint8_t; |
49 | typedef unsigned char uint8_t; |
50 | typedef signed short int16_t; |
50 | typedef signed short int16_t; |
… | |
… | |
77 | #endif |
77 | #endif |
78 | |
78 | |
79 | #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) |
79 | #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__) |
80 | #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) |
80 | #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64) |
81 | |
81 | |
|
|
82 | #ifndef ECB_OPTIMIZE_SIZE |
|
|
83 | #if __OPTIMIZE_SIZE__ |
|
|
84 | #define ECB_OPTIMIZE_SIZE 1 |
|
|
85 | #else |
|
|
86 | #define ECB_OPTIMIZE_SIZE 0 |
|
|
87 | #endif |
|
|
88 | #endif |
|
|
89 | |
82 | /* work around x32 idiocy by defining proper macros */ |
90 | /* work around x32 idiocy by defining proper macros */ |
83 | #if ECB_GCC_AMD64 || ECB_MSVC_AMD64 |
91 | #if ECB_GCC_AMD64 || ECB_MSVC_AMD64 |
84 | #if _ILP32 |
92 | #if _ILP32 |
85 | #define ECB_AMD64_X32 1 |
93 | #define ECB_AMD64_X32 1 |
86 | #else |
94 | #else |
… | |
… | |
115 | #define ECB_CLANG_EXTENSION(x) 0 |
123 | #define ECB_CLANG_EXTENSION(x) 0 |
116 | #endif |
124 | #endif |
117 | |
125 | |
118 | #define ECB_CPP (__cplusplus+0) |
126 | #define ECB_CPP (__cplusplus+0) |
119 | #define ECB_CPP11 (__cplusplus >= 201103L) |
127 | #define ECB_CPP11 (__cplusplus >= 201103L) |
|
|
128 | #define ECB_CPP14 (__cplusplus >= 201402L) |
|
|
129 | #define ECB_CPP17 (__cplusplus >= 201703L) |
120 | |
130 | |
121 | #if ECB_CPP |
131 | #if ECB_CPP |
122 | #define ECB_C 0 |
132 | #define ECB_C 0 |
123 | #define ECB_STDC_VERSION 0 |
133 | #define ECB_STDC_VERSION 0 |
124 | #else |
134 | #else |
… | |
… | |
126 | #define ECB_STDC_VERSION __STDC_VERSION__ |
136 | #define ECB_STDC_VERSION __STDC_VERSION__ |
127 | #endif |
137 | #endif |
128 | |
138 | |
129 | #define ECB_C99 (ECB_STDC_VERSION >= 199901L) |
139 | #define ECB_C99 (ECB_STDC_VERSION >= 199901L) |
130 | #define ECB_C11 (ECB_STDC_VERSION >= 201112L) |
140 | #define ECB_C11 (ECB_STDC_VERSION >= 201112L) |
|
|
141 | #define ECB_C17 (ECB_STDC_VERSION >= 201710L) |
131 | |
142 | |
132 | #if ECB_CPP |
143 | #if ECB_CPP |
133 | #define ECB_EXTERN_C extern "C" |
144 | #define ECB_EXTERN_C extern "C" |
134 | #define ECB_EXTERN_C_BEG ECB_EXTERN_C { |
145 | #define ECB_EXTERN_C_BEG ECB_EXTERN_C { |
135 | #define ECB_EXTERN_C_END } |
146 | #define ECB_EXTERN_C_END } |
… | |
… | |
161 | #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ |
172 | #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */ |
162 | #endif |
173 | #endif |
163 | |
174 | |
164 | #ifndef ECB_MEMORY_FENCE |
175 | #ifndef ECB_MEMORY_FENCE |
165 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
176 | #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
|
|
177 | #define ECB_MEMORY_FENCE_RELAXED __asm__ __volatile__ ("" : : : "memory") |
166 | #if __i386 || __i386__ |
178 | #if __i386 || __i386__ |
167 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
179 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
168 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
180 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory") |
169 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") |
181 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory") |
170 | #elif ECB_GCC_AMD64 |
182 | #elif ECB_GCC_AMD64 |
… | |
… | |
220 | #if ECB_GCC_VERSION(4,7) |
232 | #if ECB_GCC_VERSION(4,7) |
221 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
233 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
222 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
234 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
223 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
235 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
224 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
236 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
237 | #define ECB_MEMORY_FENCE_RELAXED __atomic_thread_fence (__ATOMIC_RELAXED) |
225 | |
238 | |
226 | #elif ECB_CLANG_EXTENSION(c_atomic) |
239 | #elif ECB_CLANG_EXTENSION(c_atomic) |
227 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
240 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
228 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
241 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
229 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
242 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
230 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
243 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
244 | #define ECB_MEMORY_FENCE_RELAXED __c11_atomic_thread_fence (__ATOMIC_RELAXED) |
231 | |
245 | |
232 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
246 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
233 | #define ECB_MEMORY_FENCE __sync_synchronize () |
247 | #define ECB_MEMORY_FENCE __sync_synchronize () |
234 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
248 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
235 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
249 | /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */ |
… | |
… | |
245 | #elif defined _WIN32 |
259 | #elif defined _WIN32 |
246 | #include <WinNT.h> |
260 | #include <WinNT.h> |
247 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
261 | #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ |
248 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
262 | #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 |
249 | #include <mbarrier.h> |
263 | #include <mbarrier.h> |
250 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
264 | #define ECB_MEMORY_FENCE __machine_rw_barrier () |
251 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () |
265 | #define ECB_MEMORY_FENCE_ACQUIRE __machine_acq_barrier () |
252 | #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () |
266 | #define ECB_MEMORY_FENCE_RELEASE __machine_rel_barrier () |
|
|
267 | #define ECB_MEMORY_FENCE_RELAXED __compiler_barrier () |
253 | #elif __xlC__ |
268 | #elif __xlC__ |
254 | #define ECB_MEMORY_FENCE __sync () |
269 | #define ECB_MEMORY_FENCE __sync () |
255 | #endif |
270 | #endif |
256 | #endif |
271 | #endif |
257 | |
272 | |
258 | #ifndef ECB_MEMORY_FENCE |
273 | #ifndef ECB_MEMORY_FENCE |
259 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
274 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
260 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
275 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
261 | /* not just C11 atomics and atomic accesses */ |
276 | /* not just C11 atomics and atomic accesses */ |
262 | #include <stdatomic.h> |
277 | #include <stdatomic.h> |
263 | /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ |
|
|
264 | /* any fence other than seq_cst, which isn't very efficient for us. */ |
|
|
265 | /* Why that is, we don't know - either the C11 memory model is quite useless */ |
|
|
266 | /* for most usages, or gcc and clang have a bug */ |
|
|
267 | /* I *currently* lean towards the latter, and inefficiently implement */ |
|
|
268 | /* all three of ecb's fences as a seq_cst fence */ |
|
|
269 | /* Update, gcc-4.8 generates mfence for all c++ fences, but nothing */ |
|
|
270 | /* for all __atomic_thread_fence's except seq_cst */ |
|
|
271 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
278 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
|
|
279 | #define ECB_MEMORY_FENCE_ACQUIRE atomic_thread_fence (memory_order_acquire) |
|
|
280 | #define ECB_MEMORY_FENCE_RELEASE atomic_thread_fence (memory_order_release) |
272 | #endif |
281 | #endif |
273 | #endif |
282 | #endif |
274 | |
283 | |
275 | #ifndef ECB_MEMORY_FENCE |
284 | #ifndef ECB_MEMORY_FENCE |
276 | #if !ECB_AVOID_PTHREADS |
285 | #if !ECB_AVOID_PTHREADS |
… | |
… | |
294 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
303 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
295 | #endif |
304 | #endif |
296 | |
305 | |
297 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
306 | #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE |
298 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
307 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
308 | #endif |
|
|
309 | |
|
|
310 | #if !defined ECB_MEMORY_FENCE_RELAXED && defined ECB_MEMORY_FENCE |
|
|
311 | #define ECB_MEMORY_FENCE_RELAXED ECB_MEMORY_FENCE /* very heavy-handed */ |
299 | #endif |
312 | #endif |
300 | |
313 | |
301 | /*****************************************************************************/ |
314 | /*****************************************************************************/ |
302 | |
315 | |
303 | #if ECB_CPP |
316 | #if ECB_CPP |