… | |
… | |
84 | # define ecb_decltype(x) __decltype(x) |
84 | # define ecb_decltype(x) __decltype(x) |
85 | #elif ECB_GCC_VERSION(3,0) |
85 | #elif ECB_GCC_VERSION(3,0) |
86 | # define ecb_decltype(x) typeof(x) |
86 | # define ecb_decltype(x) typeof(x) |
87 | #endif |
87 | #endif |
88 | |
88 | |
89 | #define ecb_artificial ecb_attribute ((__artificial__)) /* 4.3 */ |
|
|
90 | #define ecb_noinline ecb_attribute ((__noinline__)) |
89 | #define ecb_noinline ecb_attribute ((__noinline__)) |
91 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
90 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
92 | #define ecb_unused ecb_attribute ((__unused__)) |
91 | #define ecb_unused ecb_attribute ((__unused__)) |
93 | #define ecb_const ecb_attribute ((__const__)) |
92 | #define ecb_const ecb_attribute ((__const__)) |
94 | #define ecb_pure ecb_attribute ((__pure__)) |
93 | #define ecb_pure ecb_attribute ((__pure__)) |
|
|
94 | |
|
|
95 | #if ECB_GCC_VERSION(4,3) |
|
|
96 | #define ecb_artificial ecb_attribute ((__artificial__)) /* 4.3 */ |
95 | #define ecb_hot ecb_attribute ((__hot__)) /* 4.3 */ |
97 | #define ecb_hot ecb_attribute ((__hot__)) /* 4.3 */ |
96 | #define ecb_cold ecb_attribute ((__cold__)) /* 4.3 */ |
98 | #define ecb_cold ecb_attribute ((__cold__)) /* 4.3 */ |
|
|
99 | #else |
|
|
100 | #define ecb_artificial |
|
|
101 | #define ecb_hot |
|
|
102 | #define ecb_cold |
|
|
103 | #endif |
97 | |
104 | |
98 | /* put into if's if you are very sure that the expression */ |
105 | /* put into if's if you are very sure that the expression */ |
99 | /* is mostly true or mostly false. note that these return */ |
106 | /* is mostly true or mostly false. note that these return */ |
100 | /* booleans, not the expression. */ |
107 | /* booleans, not the expression. */ |
101 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
108 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
102 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
109 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
110 | /* for compatibility to the rest of the world */ |
103 | #define ecb_likely(expr) ecb_expect_true (expr) |
111 | #define ecb_likely(expr) ecb_expect_true (expr) |
104 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
112 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
105 | |
113 | |
106 | /* try to tell the compiler that some condition is definitely true */ |
114 | /* try to tell the compiler that some condition is definitely true */ |
107 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
115 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
108 | |
116 | |
109 | /* count trailing zero bits and count # of one bits */ |
117 | /* count trailing zero bits and count # of one bits */ |
110 | #if ECB_GCC_VERSION(3,4) |
118 | #if ECB_GCC_VERSION(3,4) |
111 | #define ecb_ctz32(x) __builtin_ctz (x) |
119 | #define ecb_ctz32(x) __builtin_ctz (x) |
112 | #define ecb_popcount32(x) __builtin_popcount (x) |
120 | #define ecb_popcount32(x) __builtin_popcount (x) |
113 | #else |
121 | #else |
114 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const; |
122 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const; |
115 | ECB_HEADER_INLINE int |
123 | ECB_HEADER_INLINE int |
116 | ecb_ctz32 (uint32_t x) |
124 | ecb_ctz32 (uint32_t x) |
117 | { |
125 | { |
118 | int r = 0; |
126 | int r = 0; |
119 | |
127 | |
120 | x &= -x; /* this isolates the lowest bit */ |
128 | x &= -x; /* this isolates the lowest bit */ |
121 | |
129 | |
122 | if (x & 0xaaaaaaaa) r += 1; |
130 | if (x & 0xaaaaaaaa) r += 1; |
123 | if (x & 0xcccccccc) r += 2; |
131 | if (x & 0xcccccccc) r += 2; |
124 | if (x & 0xf0f0f0f0) r += 4; |
132 | if (x & 0xf0f0f0f0) r += 4; |
125 | if (x & 0xff00ff00) r += 8; |
133 | if (x & 0xff00ff00) r += 8; |
126 | if (x & 0xffff0000) r += 16; |
134 | if (x & 0xffff0000) r += 16; |
127 | |
135 | |
128 | return r; |
136 | return r; |
129 | } |
137 | } |
130 | |
138 | |
131 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const; |
139 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const; |
132 | ECB_HEADER_INLINE int |
140 | ECB_HEADER_INLINE int |
133 | ecb_popcount32 (uint32_t x) |
141 | ecb_popcount32 (uint32_t x) |
134 | { |
142 | { |
135 | x -= (x >> 1) & 0x55555555; |
143 | x -= (x >> 1) & 0x55555555; |
136 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
144 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
137 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
145 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
138 | x *= 0x01010101; |
146 | x *= 0x01010101; |
139 | |
147 | |
140 | return x >> 24; |
148 | return x >> 24; |
141 | } |
149 | } |
142 | #endif |
150 | #endif |
143 | |
151 | |
144 | #if ECB_GCC_VERSION(4,3) |
152 | #if ECB_GCC_VERSION(4,3) |
145 | # define ecb_bswap32(x) __builtin_bswap32 (x) |
153 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
146 | # define ecb_bswap16(x) (__builtin_bswap32(x) >> 16) |
154 | #define ecb_bswap16(x) (__builtin_bswap32(x) >> 16) |
147 | #else |
155 | #else |
148 | ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
156 | ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
149 | ECB_HEADER_INLINE uint32_t |
157 | ECB_HEADER_INLINE uint32_t |
150 | ecb_bswap32 (uint32_t x) |
158 | ecb_bswap32 (uint32_t x) |
151 | { |
159 | { |
152 | return (x >> 24) |
160 | return (x >> 24) |
153 | | ((x >> 8) & 0x0000ff00) |
161 | | ((x >> 8) & 0x0000ff00) |
154 | | ((x << 8) & 0x00ff0000) |
162 | | ((x << 8) & 0x00ff0000) |
155 | | (x << 24); |
163 | | (x << 24); |
156 | } |
164 | } |
157 | |
165 | |
158 | ECB_HEADER_INLINE uint32_t ecb_bswap16 (uint32_t x) ecb_const; |
166 | ECB_HEADER_INLINE uint32_t ecb_bswap16 (uint32_t x) ecb_const; |
159 | ECB_HEADER_INLINE uint32_t |
167 | ECB_HEADER_INLINE uint32_t |
160 | ecb_bswap16 (uint32_t x) |
168 | ecb_bswap16 (uint32_t x) |
161 | { |
169 | { |
162 | return ((x >> 8) & 0xff) |
170 | return ((x >> 8) & 0xff) |
163 | | ((x << 8) & 0x00ff0000) |
171 | | ((x << 8) & 0x00ff0000) |
164 | | (x << 24); |
172 | | (x << 24); |
165 | } |
173 | } |
166 | #endif |
174 | #endif |
167 | |
175 | |
168 | #if ECB_GCC_VERSION(4,5) |
176 | #if ECB_GCC_VERSION(4,5) |
169 | # define ecb_unreachable() __builtin_unreachable () |
177 | #define ecb_unreachable() __builtin_unreachable () |
170 | #else |
178 | #else |
171 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
179 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
172 | ECB_HEADER_INLINE void ecb_unreachable (void) ecb_noreturn; |
180 | ECB_HEADER_INLINE void ecb_unreachable (void) ecb_noreturn; |
173 | ECB_HEADER_INLINE void ecb_unreachable (void) { } |
181 | ECB_HEADER_INLINE void ecb_unreachable (void) { } |
174 | #endif |
182 | #endif |
175 | |
183 | |
176 | ECB_HEADER_INLINE unsigned char ecb_byteorder_helper (void) ecb_const; |
184 | ECB_HEADER_INLINE unsigned char ecb_byteorder_helper (void) ecb_const; |
177 | ECB_HEADER_INLINE unsigned char |
185 | ECB_HEADER_INLINE unsigned char |
178 | ecb_byteorder_helper (void) |
186 | ecb_byteorder_helper (void) |
… | |
… | |
185 | ECB_HEADER_INLINE ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }; |
193 | ECB_HEADER_INLINE ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }; |
186 | ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) ecb_const; |
194 | ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) ecb_const; |
187 | ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }; |
195 | ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }; |
188 | |
196 | |
189 | #if ECB_GCC_VERSION(3,0) |
197 | #if ECB_GCC_VERSION(3,0) |
190 | # define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
198 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
191 | #else |
199 | #else |
192 | # define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
200 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
193 | #endif |
201 | #endif |
194 | |
202 | |
195 | #if ecb_cplusplus_does_not_suck |
203 | #if ecb_cplusplus_does_not_suck |
196 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
204 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
197 | template<typename T, int N> |
205 | template<typename T, int N> |
198 | static inline int ecb_array_length (const T (&arr)[N]) |
206 | static inline int ecb_array_length (const T (&arr)[N]) |
199 | { |
207 | { |
200 | return N; |
208 | return N; |
201 | } |
209 | } |
202 | #else |
210 | #else |
203 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
211 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
204 | #endif |
212 | #endif |
205 | |
213 | |
206 | ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
214 | ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
207 | ECB_INLINE uint32_t |
215 | ECB_INLINE uint32_t |
208 | ecb_rotr32 (uint32_t x, unsigned int count) |
216 | ecb_rotr32 (uint32_t x, unsigned int count) |