ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libecb/ecb.h
(Generate patch)

Comparing libecb/ecb.h (file contents):
Revision 1.82 by root, Fri Mar 23 19:05:22 2012 UTC vs.
Revision 1.101 by root, Thu Jun 28 14:50:01 2012 UTC

28 */ 28 */
29 29
30#ifndef ECB_H 30#ifndef ECB_H
31#define ECB_H 31#define ECB_H
32 32
33/* 16 bits major, 16 bits minor */
34#define ECB_VERSION 0x00010001
35
33#ifdef _WIN32 36#ifdef _WIN32
34 typedef signed char int8_t; 37 typedef signed char int8_t;
35 typedef unsigned char uint8_t; 38 typedef unsigned char uint8_t;
36 typedef signed short int16_t; 39 typedef signed short int16_t;
37 typedef unsigned short uint16_t; 40 typedef unsigned short uint16_t;
42 typedef unsigned long long uint64_t; 45 typedef unsigned long long uint64_t;
43 #else /* _MSC_VER || __BORLANDC__ */ 46 #else /* _MSC_VER || __BORLANDC__ */
44 typedef signed __int64 int64_t; 47 typedef signed __int64 int64_t;
45 typedef unsigned __int64 uint64_t; 48 typedef unsigned __int64 uint64_t;
46 #endif 49 #endif
50 #ifdef _WIN64
51 #define ECB_PTRSIZE 8
52 typedef uint64_t uintptr_t;
53 typedef int64_t intptr_t;
54 #else
55 #define ECB_PTRSIZE 4
56 typedef uint32_t uintptr_t;
57 typedef int32_t intptr_t;
58 #endif
47#else 59#else
48 #include <inttypes.h> 60 #include <inttypes.h>
61 #if UINTMAX_MAX > 0xffffffffU
62 #define ECB_PTRSIZE 8
63 #else
64 #define ECB_PTRSIZE 4
65 #endif
49#endif 66#endif
50 67
51/* many compilers define _GNUC_ to some versions but then only implement 68/* many compilers define _GNUC_ to some versions but then only implement
52 * what their idiot authors think are the "more important" extensions, 69 * what their idiot authors think are the "more important" extensions,
53 * causing enormous grief in return for some better fake benchmark numbers. 70 * causing enormous grief in return for some better fake benchmark numbers.
54 * or so. 71 * or so.
55 * we try to detect these and simply assume they are not gcc - if they have 72 * we try to detect these and simply assume they are not gcc - if they have
56 * an issue with that they should have done it right in the first place. 73 * an issue with that they should have done it right in the first place.
57 */ 74 */
58#ifndef ECB_GCC_VERSION 75#ifndef ECB_GCC_VERSION
59 #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) 76 #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__
60 #define ECB_GCC_VERSION(major,minor) 0 77 #define ECB_GCC_VERSION(major,minor) 0
61 #else 78 #else
62 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 79 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
63 #endif 80 #endif
64#endif 81#endif
65 82
83#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
84#define ECB_C99 (__STDC_VERSION__ >= 199901L)
85#define ECB_C11 (__STDC_VERSION__ >= 201112L)
86#define ECB_CPP (__cplusplus+0)
87#define ECB_CPP11 (__cplusplus >= 201103L)
88
66/*****************************************************************************/ 89/*****************************************************************************/
67 90
68/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 91/* 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 */ 92/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
70 93
71#if ECB_NO_THREADS 94#if ECB_NO_THREADS
72# define ECB_NO_SMP 1 95 #define ECB_NO_SMP 1
73#endif 96#endif
74 97
75#if ECB_NO_THREADS || ECB_NO_SMP 98#if ECB_NO_SMP
76 #define ECB_MEMORY_FENCE do { } while (0) 99 #define ECB_MEMORY_FENCE do { } while (0)
77#endif 100#endif
78 101
79#ifndef ECB_MEMORY_FENCE 102#ifndef ECB_MEMORY_FENCE
80 #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 103 #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
81 #if __i386 || __i386__ 104 #if __i386 || __i386__
82 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 105 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
83 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ 106 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
84 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ 107 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
85 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__ 108 #elif __amd64 || __amd64__ || __x86_64 || __x86_64__
86 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") 109 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
87 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") 110 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
88 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ 111 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
89 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ 112 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
90 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 113 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
91 #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ 114 #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
92 || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) 115 || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__
93 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") 116 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
94 #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ 117 #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
95 || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) 118 || defined __ARM_ARCH_7M__ || defined __ARM_ARCH_7R__
96 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") 119 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
97 #elif __sparc || __sparc__ 120 #elif __sparc || __sparc__
98 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad | " : : : "memory") 121 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
99 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory") 122 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
100 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") 123 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
101 #elif defined(__s390__) || defined(__s390x__) 124 #elif defined __s390__ || defined __s390x__
102 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") 125 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
103 #elif defined(__mips__) 126 #elif defined __mips__
104 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 127 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
128 #elif defined __alpha__
129 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
130 #elif defined __hppa__
131 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
132 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
133 #elif defined __ia64__
134 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
105 #endif 135 #endif
106 #endif 136 #endif
107#endif 137#endif
108 138
109#ifndef ECB_MEMORY_FENCE 139#ifndef ECB_MEMORY_FENCE
140 #if ECB_GCC_VERSION(4,7)
141 /* see comment below (stdatomic.h) about the C11 memory model. */
142 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
143 #elif defined __clang && __has_feature (cxx_atomic)
144 /* see comment below (stdatomic.h) about the C11 memory model. */
145 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
110 #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) 146 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
111 #define ECB_MEMORY_FENCE __sync_synchronize () 147 #define ECB_MEMORY_FENCE __sync_synchronize ()
112 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
113 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
114 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 148 #elif _MSC_VER >= 1400 /* VC++ 2005 */
115 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 149 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
116 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 150 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
117 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ 151 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
118 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 152 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
119 #elif defined(_WIN32) 153 #elif defined _WIN32
120 #include <WinNT.h> 154 #include <WinNT.h>
121 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ 155 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
122 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110 156 #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
123 #include <mbarrier.h> 157 #include <mbarrier.h>
124 #define ECB_MEMORY_FENCE __machine_rw_barrier () 158 #define ECB_MEMORY_FENCE __machine_rw_barrier ()
125 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier () 159 #define ECB_MEMORY_FENCE_ACQUIRE __machine_r_barrier ()
126 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier () 160 #define ECB_MEMORY_FENCE_RELEASE __machine_w_barrier ()
127 #elif __xlC__ 161 #elif __xlC__
128 #define ECB_MEMORY_FENCE __lwsync () 162 #define ECB_MEMORY_FENCE __sync ()
163 #endif
164#endif
165
166#ifndef ECB_MEMORY_FENCE
167 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
168 /* we assume that these memory fences work on all variables/all memory accesses, */
169 /* not just C11 atomics and atomic accesses */
170 #include <stdatomic.h>
171 /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
172 /* any fence other than seq_cst, which isn't very efficient for us. */
173 /* Why that is, we don't know - either the C11 memory model is quite useless */
174 /* for most usages, or gcc and clang have a bug */
175 /* I *currently* lean towards the latter, and inefficiently implement */
176 /* all three of ecb's fences as a seq_cst fence */
177 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
129 #endif 178 #endif
130#endif 179#endif
131 180
132#ifndef ECB_MEMORY_FENCE 181#ifndef ECB_MEMORY_FENCE
133 #if !ECB_AVOID_PTHREADS 182 #if !ECB_AVOID_PTHREADS
145 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 194 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
146 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) 195 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
147 #endif 196 #endif
148#endif 197#endif
149 198
150#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) 199#if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
151 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 200 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
152#endif 201#endif
153 202
154#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) 203#if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
155 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE 204 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
156#endif 205#endif
157 206
158/*****************************************************************************/ 207/*****************************************************************************/
159
160#define ECB_C99 (__STDC_VERSION__ >= 199901L)
161 208
162#if __cplusplus 209#if __cplusplus
163 #define ecb_inline static inline 210 #define ecb_inline static inline
164#elif ECB_GCC_VERSION(2,5) 211#elif ECB_GCC_VERSION(2,5)
165 #define ecb_inline static __inline__ 212 #define ecb_inline static __inline__
204#elif ECB_GCC_VERSION(3,0) 251#elif ECB_GCC_VERSION(3,0)
205 #define ecb_decltype(x) __typeof(x) 252 #define ecb_decltype(x) __typeof(x)
206#endif 253#endif
207 254
208#define ecb_noinline ecb_attribute ((__noinline__)) 255#define ecb_noinline ecb_attribute ((__noinline__))
209#define ecb_noreturn ecb_attribute ((__noreturn__))
210#define ecb_unused ecb_attribute ((__unused__)) 256#define ecb_unused ecb_attribute ((__unused__))
211#define ecb_const ecb_attribute ((__const__)) 257#define ecb_const ecb_attribute ((__const__))
212#define ecb_pure ecb_attribute ((__pure__)) 258#define ecb_pure ecb_attribute ((__pure__))
259
260#if ECB_C11
261 #define ecb_noreturn _Noreturn
262#else
263 #define ecb_noreturn ecb_attribute ((__noreturn__))
264#endif
213 265
214#if ECB_GCC_VERSION(4,3) 266#if ECB_GCC_VERSION(4,3)
215 #define ecb_artificial ecb_attribute ((__artificial__)) 267 #define ecb_artificial ecb_attribute ((__artificial__))
216 #define ecb_hot ecb_attribute ((__hot__)) 268 #define ecb_hot ecb_attribute ((__hot__))
217 #define ecb_cold ecb_attribute ((__cold__)) 269 #define ecb_cold ecb_attribute ((__cold__))
308 360
309 return r + ecb_ld32 (x); 361 return r + ecb_ld32 (x);
310 } 362 }
311#endif 363#endif
312 364
365ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) ecb_const;
366ecb_function_ ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
367ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) ecb_const;
368ecb_function_ ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
369
313ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const; 370ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) ecb_const;
314ecb_function_ uint8_t ecb_bitrev8 (uint8_t x) 371ecb_function_ uint8_t ecb_bitrev8 (uint8_t x)
315{ 372{
316 return ( (x * 0x0802U & 0x22110U) 373 return ( (x * 0x0802U & 0x22110U)
317 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16; 374 | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
401 ecb_inline void ecb_unreachable (void) ecb_noreturn; 458 ecb_inline void ecb_unreachable (void) ecb_noreturn;
402 ecb_inline void ecb_unreachable (void) { } 459 ecb_inline void ecb_unreachable (void) { }
403#endif 460#endif
404 461
405/* try to tell the compiler that some condition is definitely true */ 462/* try to tell the compiler that some condition is definitely true */
406#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 463#define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
407 464
408ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const; 465ecb_inline unsigned char ecb_byteorder_helper (void) ecb_const;
409ecb_inline unsigned char 466ecb_inline unsigned char
410ecb_byteorder_helper (void) 467ecb_byteorder_helper (void)
411{ 468{
412 const uint32_t u = 0x11223344; 469 /* the union code still generates code under pressure in gcc, */
413 return *(unsigned char *)&u; 470 /* but less than using pointers, and always seem to */
471 /* successfully return a constant. */
472 /* the reason why we have this horrible preprocessor mess */
473 /* is to avoid it in all cases, at least on common architectures */
474 /* and yes, gcc defines __BYTE_ORDER__, g++ does not */
475#if __i386 || __i386__ || _M_X86 || __amd64 || __amd64__ || _M_X64
476 return 0x44;
477#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
478 return 0x44;
479#elif __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
480 retrurn 0x11;
481#else
482 union
483 {
484 uint32_t i;
485 uint8_t c;
486 } u = { 0x11223344 };
487 return u.c;
488#endif
414} 489}
415 490
416ecb_inline ecb_bool ecb_big_endian (void) ecb_const; 491ecb_inline ecb_bool ecb_big_endian (void) ecb_const;
417ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } 492ecb_inline ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
418ecb_inline ecb_bool ecb_little_endian (void) ecb_const; 493ecb_inline ecb_bool ecb_little_endian (void) ecb_const;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines