… | |
… | |
30 | #ifndef ECB_H |
30 | #ifndef ECB_H |
31 | #define ECB_H |
31 | #define ECB_H |
32 | |
32 | |
33 | #include <inttypes.h> |
33 | #include <inttypes.h> |
34 | |
34 | |
|
|
35 | /* many compilers define _GNUC_ to some versions but then only implement |
|
|
36 | * what their idiot authors think are the "more important" extensions, |
|
|
37 | * causing enourmous grief for some better fake benchmark numbers or so. |
|
|
38 | * we try to detect these and simply assume they are not gcc - if they have |
|
|
39 | * an issue with that they should have done it right in the first place. |
|
|
40 | */ |
|
|
41 | #if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__llvm__) |
|
|
42 | # define ECB_GCC_VERSION(major,minor) 0 |
|
|
43 | #else |
35 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
44 | # define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
|
|
45 | #endif |
36 | |
46 | |
37 | #ifndef __cplusplus |
47 | #ifndef __cplusplus |
38 | # if __STDC_VERSION__ >= 199901L |
48 | # if __STDC_VERSION__ >= 199901L |
39 | # define ECB_INLINE inline |
49 | # define ECB_INLINE inline |
40 | typedef _Bool ecb_bool; |
50 | typedef _Bool ecb_bool; |
… | |
… | |
76 | #define ecb_const ecb_attribute ((const)) |
86 | #define ecb_const ecb_attribute ((const)) |
77 | #define ecb_pure ecb_attribute ((pure)) |
87 | #define ecb_pure ecb_attribute ((pure)) |
78 | #define ecb_hot ecb_attribute ((hot)) /* 4.3 */ |
88 | #define ecb_hot ecb_attribute ((hot)) /* 4.3 */ |
79 | #define ecb_cold ecb_attribute ((cold)) /* 4.3 */ |
89 | #define ecb_cold ecb_attribute ((cold)) /* 4.3 */ |
80 | |
90 | |
81 | #if ECB_GCC_VERSION(4,5) |
|
|
82 | # define ecb_unreachable() __builtin_unreachable () |
|
|
83 | #else |
|
|
84 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
|
|
85 | ECB_HEADER_INLINE void ecb_unreachable () ecb_attribute ((noreturn)); |
|
|
86 | ECB_HEADER_INLINE void ecb_unreachable () { } |
|
|
87 | #endif |
|
|
88 | |
|
|
89 | /* put into ifs if you are very sure that the expression */ |
91 | /* put into ifs if you are very sure that the expression */ |
90 | /* is mostly true or mosty false. note that these return */ |
92 | /* is mostly true or mosty false. note that these return */ |
91 | /* booleans, not the expression. */ |
93 | /* booleans, not the expression. */ |
92 | #define ecb_unlikely(expr) ecb_expect ((expr) ? 1 : 0, 0) |
94 | #define ecb_unlikely(expr) ecb_expect (!!(expr), 0) |
93 | #define ecb_likely(expr) ecb_expect ((expr) ? 1 : 0, 1) |
95 | #define ecb_likely(expr) ecb_expect (!!(expr), 1) |
94 | |
96 | |
95 | /* try to tell the compiler that some condition is definitely true */ |
97 | /* try to tell the compiler that some condition is definitely true */ |
96 | #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) |
98 | #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) |
97 | |
99 | |
98 | /* count trailing zero bits and count # of one bits */ |
100 | /* count trailing zero bits and count # of one bits */ |
… | |
… | |
126 | |
128 | |
127 | return x >> 24; |
129 | return x >> 24; |
128 | } |
130 | } |
129 | #endif |
131 | #endif |
130 | |
132 | |
|
|
133 | #if ECB_GCC_VERSION(4,3) |
|
|
134 | ECB_GCC_VERSION uint32_t ecb_bswap32 (uint32_t x) { return __builtin_bswap32 (x); } |
|
|
135 | #else |
|
|
136 | ECB_GCC_VERSION uint32_t |
|
|
137 | ecb_bswap32 (uint32_t x) |
|
|
138 | { |
|
|
139 | return (x >> 24) |
|
|
140 | | ((x >> 8) & 0x0000ff00) |
|
|
141 | | ((x << 8) & 0x00ff0000) |
|
|
142 | | (x << 24); |
|
|
143 | } |
|
|
144 | #endif |
|
|
145 | |
|
|
146 | #if ECB_GCC_VERSION(4,5) |
|
|
147 | # define ecb_unreachable() __builtin_unreachable () |
|
|
148 | #else |
|
|
149 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
|
|
150 | ECB_HEADER_INLINE void ecb_unreachable () ecb_attribute ((noreturn)); |
|
|
151 | ECB_HEADER_INLINE void ecb_unreachable () { } |
|
|
152 | #endif |
|
|
153 | |
131 | ECB_HEADER_INLINE unsigned char |
154 | ECB_HEADER_INLINE unsigned char |
132 | ecb_byteorder_helper () ecb_const |
155 | ecb_byteorder_helper () ecb_const |
133 | { |
156 | { |
134 | const uint32_t u = 0x11223344; |
157 | const uint32_t u = 0x11223344; |
135 | return *(unsigned char *)&u; |
158 | return *(unsigned char *)&u; |
… | |
… | |
159 | ecb_rotl32 (uint32_t x, unsigned int count) |
182 | ecb_rotl32 (uint32_t x, unsigned int count) |
160 | { |
183 | { |
161 | return (x >> (32 - count)) | (x << count); |
184 | return (x >> (32 - count)) | (x << count); |
162 | } |
185 | } |
163 | |
186 | |
164 | #if ECB_GCC_VERSION(4,3) |
|
|
165 | # define ecb_bswap32(x) __builtin_bswap32 (x) |
|
|
166 | #else |
|
|
167 | ECB_GCC_VERSION uint32_t |
|
|
168 | ecb_bswap32 (uint32_t x) |
|
|
169 | { |
|
|
170 | return (x >> 24) |
|
|
171 | | ((x >> 8) & 0x0000ff00) |
|
|
172 | | ((x << 8) & 0x00ff0000) |
|
|
173 | | (x << 24); |
|
|
174 | } |
|
|
175 | #endif |
187 | #endif |
176 | |
188 | |
177 | #endif |
|
|
178 | |
|
|