… | |
… | |
28 | */ |
28 | */ |
29 | |
29 | |
30 | #ifndef ECB_H |
30 | #ifndef ECB_H |
31 | #define ECB_H |
31 | #define ECB_H |
32 | |
32 | |
|
|
33 | #ifdef _WIN32 |
|
|
34 | typedef signed char int8_t; |
|
|
35 | typedef unsigned char uint8_t; |
|
|
36 | typedef signed short int16_t; |
|
|
37 | typedef unsigned short uint16_t; |
|
|
38 | typedef signed int int32_t; |
|
|
39 | typedef unsigned int uint32_t; |
|
|
40 | #if __GNUC__ |
|
|
41 | typedef signed long long int64_t; |
|
|
42 | typedef unsigned long long uint64_t; |
|
|
43 | #else |
|
|
44 | typedef signed __int64 int64_t; |
|
|
45 | typedef unsigned __int64 uint64_t; |
|
|
46 | #endif |
|
|
47 | #else |
33 | #include <inttypes.h> |
48 | #include <inttypes.h> |
|
|
49 | #endif |
34 | |
50 | |
35 | /* many compilers define _GNUC_ to some versions but then only implement |
51 | /* many compilers define _GNUC_ to some versions but then only implement |
36 | * what their idiot authors think are the "more important" extensions, |
52 | * what their idiot authors think are the "more important" extensions, |
37 | * causing enourmous grief in return for some better fake benchmark numbers. |
53 | * causing enourmous grief in return for some better fake benchmark numbers. |
38 | * or so. |
54 | * or so. |
39 | * we try to detect these and simply assume they are not gcc - if they have |
55 | * we try to detect these and simply assume they are not gcc - if they have |
40 | * an issue with that they should have done it right in the first place. |
56 | * an issue with that they should have done it right in the first place. |
41 | */ |
57 | */ |
42 | #ifndef ECB_GCC_VERSION |
58 | #ifndef ECB_GCC_VERSION |
43 | #if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__llvm__) |
59 | #if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) |
44 | #define ECB_GCC_VERSION(major,minor) 0 |
60 | #define ECB_GCC_VERSION(major,minor) 0 |
45 | #else |
61 | #else |
46 | #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))) |
47 | #endif |
63 | #endif |
48 | #endif |
64 | #endif |
49 | |
65 | |
50 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
66 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
51 | |
67 | |
52 | #if __cplusplus |
68 | #if __cplusplus |
53 | #define ECB_INLINE static inline |
69 | #define ecb_inline static inline |
54 | #elif ECB_GCC_VERSION(2,5) |
70 | #elif ECB_GCC_VERSION(2,5) |
55 | #define ECB_INLINE static __inline__ |
71 | #define ecb_inline static __inline__ |
56 | #elif ECB_C99 |
72 | #elif ECB_C99 |
57 | #define ECB_INLINE static inline |
73 | #define ecb_inline static inline |
58 | #else |
74 | #else |
59 | #define ECB_INLINE static |
75 | #define ecb_inline static |
60 | #endif |
76 | #endif |
61 | |
77 | |
62 | #if ECB_GCC_VERSION(3,3) |
78 | #if ECB_GCC_VERSION(3,3) |
63 | #define ecb_restrict __restrict__ |
79 | #define ecb_restrict __restrict__ |
64 | #elif ECB_C99 |
80 | #elif ECB_C99 |
… | |
… | |
72 | #define ECB_CONCAT_(a, b) a ## b |
88 | #define ECB_CONCAT_(a, b) a ## b |
73 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
89 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
74 | #define ECB_STRINGIFY_(a) # a |
90 | #define ECB_STRINGIFY_(a) # a |
75 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
91 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
76 | |
92 | |
77 | #define ecb_function_ ECB_INLINE |
93 | #define ecb_function_ ecb_inline |
78 | |
94 | |
79 | #if ECB_GCC_VERSION(3,1) |
95 | #if ECB_GCC_VERSION(3,1) |
80 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
96 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
81 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
97 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
82 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
98 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
… | |
… | |
118 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
134 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
119 | /* for compatibility to the rest of the world */ |
135 | /* for compatibility to the rest of the world */ |
120 | #define ecb_likely(expr) ecb_expect_true (expr) |
136 | #define ecb_likely(expr) ecb_expect_true (expr) |
121 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
137 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
122 | |
138 | |
123 | /* try to tell the compiler that some condition is definitely true */ |
|
|
124 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
|
|
125 | |
|
|
126 | /* count trailing zero bits and count # of one bits */ |
139 | /* count trailing zero bits and count # of one bits */ |
127 | #if ECB_GCC_VERSION(3,4) |
140 | #if ECB_GCC_VERSION(3,4) |
128 | #define ecb_ctz32(x) __builtin_ctz (x) |
141 | #define ecb_ctz32(x) __builtin_ctz (x) |
129 | #define ecb_popcount32(x) __builtin_popcount (x) |
142 | #define ecb_popcount32(x) __builtin_popcount (x) |
130 | #else |
143 | #else |
… | |
… | |
132 | ecb_function_ int |
145 | ecb_function_ int |
133 | ecb_ctz32 (uint32_t x) |
146 | ecb_ctz32 (uint32_t x) |
134 | { |
147 | { |
135 | int r = 0; |
148 | int r = 0; |
136 | |
149 | |
137 | x &= -x; /* this isolates the lowest bit */ |
150 | x &= (uint32_t)-(int32_t)x; /* this isolates the lowest bit */ |
138 | |
151 | |
139 | if (x & 0xaaaaaaaa) r += 1; |
152 | if (x & 0xaaaaaaaa) r += 1; |
140 | if (x & 0xcccccccc) r += 2; |
153 | if (x & 0xcccccccc) r += 2; |
141 | if (x & 0xf0f0f0f0) r += 4; |
154 | if (x & 0xf0f0f0f0) r += 4; |
142 | if (x & 0xff00ff00) r += 8; |
155 | if (x & 0xff00ff00) r += 8; |
… | |
… | |
187 | #else |
200 | #else |
188 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
201 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
189 | ecb_function_ void ecb_unreachable (void) ecb_noreturn; |
202 | ecb_function_ void ecb_unreachable (void) ecb_noreturn; |
190 | ecb_function_ void ecb_unreachable (void) { } |
203 | ecb_function_ void ecb_unreachable (void) { } |
191 | #endif |
204 | #endif |
|
|
205 | |
|
|
206 | /* try to tell the compiler that some condition is definitely true */ |
|
|
207 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
192 | |
208 | |
193 | ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; |
209 | ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; |
194 | ecb_function_ unsigned char |
210 | ecb_function_ unsigned char |
195 | ecb_byteorder_helper (void) |
211 | ecb_byteorder_helper (void) |
196 | { |
212 | { |
197 | const uint32_t u = 0x11223344; |
213 | const uint32_t u = 0x11223344; |
198 | return *(unsigned char *)&u; |
214 | return *(unsigned char *)&u; |
199 | } |
215 | } |
200 | |
216 | |
201 | ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; |
217 | ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; |
202 | ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }; |
218 | ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } |
203 | ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; |
219 | ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; |
204 | ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }; |
220 | ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } |
205 | |
221 | |
206 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
222 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
207 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
223 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
208 | #else |
224 | #else |
209 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
225 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
210 | #endif |
226 | #endif |
211 | |
227 | |
212 | #if ecb_cplusplus_does_not_suck |
228 | #if ecb_cplusplus_does_not_suck |
213 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
229 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
214 | template<typename T, int N> |
230 | template<typename T, int N> |
215 | static inline int ecb_array_length (const T (&arr)[N]) |
231 | static inline int ecb_array_length (const T (&arr)[N]) |
216 | { |
232 | { |
217 | return N; |
233 | return N; |
218 | } |
234 | } |
219 | #else |
235 | #else |
220 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
236 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
221 | #endif |
237 | #endif |
222 | |
238 | |
223 | ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
239 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
224 | ECB_INLINE uint32_t |
240 | ecb_inline uint32_t |
225 | ecb_rotr32 (uint32_t x, unsigned int count) |
241 | ecb_rotr32 (uint32_t x, unsigned int count) |
226 | { |
242 | { |
227 | return (x << (32 - count)) | (x >> count); |
243 | return (x << (32 - count)) | (x >> count); |
228 | } |
244 | } |
229 | |
245 | |
230 | ECB_INLINE uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
246 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
231 | ECB_INLINE uint32_t |
247 | ecb_inline uint32_t |
232 | ecb_rotl32 (uint32_t x, unsigned int count) |
248 | ecb_rotl32 (uint32_t x, unsigned int count) |
233 | { |
249 | { |
234 | return (x >> (32 - count)) | (x << count); |
250 | return (x >> (32 - count)) | (x << count); |
235 | } |
251 | } |
236 | |
252 | |