… | |
… | |
152 | { |
152 | { |
153 | int r = 0; |
153 | int r = 0; |
154 | |
154 | |
155 | x &= ~x + 1; /* this isolates the lowest bit */ |
155 | x &= ~x + 1; /* this isolates the lowest bit */ |
156 | |
156 | |
|
|
157 | #if ECB_branchless_on_i386 |
|
|
158 | r += !!(x & 0xaaaaaaaa) << 0; |
|
|
159 | r += !!(x & 0xcccccccc) << 1; |
|
|
160 | r += !!(x & 0xf0f0f0f0) << 2; |
|
|
161 | r += !!(x & 0xff00ff00) << 3; |
|
|
162 | r += !!(x & 0xffff0000) << 4; |
|
|
163 | #else |
157 | if (x & 0xaaaaaaaa) r += 1; |
164 | if (x & 0xaaaaaaaa) r += 1; |
158 | if (x & 0xcccccccc) r += 2; |
165 | if (x & 0xcccccccc) r += 2; |
159 | if (x & 0xf0f0f0f0) r += 4; |
166 | if (x & 0xf0f0f0f0) r += 4; |
160 | if (x & 0xff00ff00) r += 8; |
167 | if (x & 0xff00ff00) r += 8; |
161 | if (x & 0xffff0000) r += 16; |
168 | if (x & 0xffff0000) r += 16; |
|
|
169 | #endif |
162 | |
170 | |
163 | return r; |
171 | return r; |
164 | } |
172 | } |
165 | |
173 | |
166 | ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; |
174 | ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; |
167 | ecb_function_ int |
175 | ecb_function_ int |
168 | ecb_ctz64 (uint64_t x) |
176 | ecb_ctz64 (uint64_t x) |
169 | { |
177 | { |
170 | int shift = x & 0xffffffffU ? 0 : 32; |
178 | int shift = x & 0xffffffffU ? 0 : 32; |
171 | return ecb_ctz (x >> shift) + shift; |
179 | return ecb_ctz32 (x >> shift) + shift; |
172 | } |
180 | } |
173 | |
181 | |
174 | ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; |
182 | ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; |
175 | ecb_function_ int |
183 | ecb_function_ int |
176 | ecb_popcount32 (uint32_t x) |
184 | ecb_popcount32 (uint32_t x) |
… | |
… | |
187 | /* something using lots of bit arithmetic and a simple loop */ |
195 | /* something using lots of bit arithmetic and a simple loop */ |
188 | /* we went for the loop */ |
196 | /* we went for the loop */ |
189 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
197 | ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; |
190 | ecb_function_ int ecb_ld32 (uint32_t x) |
198 | ecb_function_ int ecb_ld32 (uint32_t x) |
191 | { |
199 | { |
192 | int r = -1; |
200 | int r = 0; |
193 | |
201 | |
194 | do |
202 | if (x >> 16) { x >>= 16; r += 16; } |
195 | { |
203 | if (x >> 8) { x >>= 8; r += 8; } |
196 | x >>= 1; |
204 | if (x >> 4) { x >>= 4; r += 4; } |
197 | ++r; |
205 | if (x >> 2) { x >>= 2; r += 2; } |
198 | } |
206 | if (x >> 1) { r += 1; } |
199 | while (x); |
|
|
200 | |
207 | |
201 | return r; |
208 | return r; |
202 | } |
209 | } |
203 | |
210 | |
204 | ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; |
211 | ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; |
205 | ecb_function_ int ecb_ld64 (uint64_t x) |
212 | ecb_function_ int ecb_ld64 (uint64_t x) |
206 | { |
213 | { |
207 | int r = -1; |
214 | int r = 0; |
208 | |
215 | |
209 | do |
216 | if (x >> 32) { x >>= 32; r += 32; } |
210 | { |
|
|
211 | x >>= 1; |
|
|
212 | ++r; |
|
|
213 | } |
|
|
214 | while (x); |
|
|
215 | |
217 | |
216 | return r; |
218 | return r + ecb_ld32 (x); |
217 | } |
219 | } |
218 | #endif |
220 | #endif |
219 | |
221 | |
220 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
222 | /* popcount64 is only available on 64 bit cpus as gcc builtin */ |
221 | /* so for this version we are lazy */ |
223 | /* so for this version we are lazy */ |
… | |
… | |
224 | ecb_popcount64 (uint64_t x) |
226 | ecb_popcount64 (uint64_t x) |
225 | { |
227 | { |
226 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
228 | return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); |
227 | } |
229 | } |
228 | |
230 | |
|
|
231 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const; |
|
|
232 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const; |
|
|
233 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const; |
|
|
234 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const; |
|
|
235 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
|
|
236 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
|
|
237 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const; |
|
|
238 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const; |
|
|
239 | |
|
|
240 | ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
|
|
241 | ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
|
|
242 | ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
|
|
243 | ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
|
|
244 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
|
|
245 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
|
|
246 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
|
|
247 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
|
|
248 | |
229 | #if ECB_GCC_VERSION(4,3) |
249 | #if ECB_GCC_VERSION(4,3) |
230 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
250 | #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) |
231 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
251 | #define ecb_bswap32(x) __builtin_bswap32 (x) |
232 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
252 | #define ecb_bswap64(x) __builtin_bswap64 (x) |
233 | #else |
253 | #else |
234 | ecb_function_ uint32_t ecb_bswap16 (uint32_t x) ecb_const; |
254 | ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; |
235 | ecb_function_ uint32_t |
255 | ecb_function_ uint16_t |
236 | ecb_bswap16 (uint32_t x) |
256 | ecb_bswap16 (uint16_t x) |
237 | { |
257 | { |
238 | return ((x >> 8) & 0xff) |
258 | return ecb_rotl16 (x, 8); |
239 | | ((x << 8) & 0x00ff0000) |
|
|
240 | | (x << 24); |
|
|
241 | } |
259 | } |
242 | |
260 | |
243 | ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
261 | ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
244 | ecb_function_ uint32_t |
262 | ecb_function_ uint32_t |
245 | ecb_bswap32 (uint32_t x) |
263 | ecb_bswap32 (uint32_t x) |
246 | { |
264 | { |
247 | return (x >> 24) |
265 | return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); |
248 | | ((x >> 8) & 0x0000ff00) |
|
|
249 | | ((x << 8) & 0x00ff0000) |
|
|
250 | | (x << 24); |
|
|
251 | } |
266 | } |
252 | |
267 | |
253 | ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; |
268 | ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; |
254 | ecb_function_ uint64_t |
269 | ecb_function_ uint64_t |
255 | ecb_bswap64 (uint64_t x) |
270 | ecb_bswap64 (uint64_t x) |
256 | { |
271 | { |
257 | return (((uint64_t)ecb_bswap32 (x)) << 32) |
272 | return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); |
258 | | ecb_bswap32 (x >> 32); |
|
|
259 | } |
273 | } |
260 | #endif |
274 | #endif |
261 | |
275 | |
262 | #if ECB_GCC_VERSION(4,5) |
276 | #if ECB_GCC_VERSION(4,5) |
263 | #define ecb_unreachable() __builtin_unreachable () |
277 | #define ecb_unreachable() __builtin_unreachable () |
… | |
… | |
298 | } |
312 | } |
299 | #else |
313 | #else |
300 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
314 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
301 | #endif |
315 | #endif |
302 | |
316 | |
303 | ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
|
|
304 | ecb_inline uint32_t |
|
|
305 | ecb_rotr32 (uint32_t x, unsigned int count) |
|
|
306 | { |
|
|
307 | return (x << (32 - count)) | (x >> count); |
|
|
308 | } |
|
|
309 | |
|
|
310 | ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
|
|
311 | ecb_inline uint32_t |
|
|
312 | ecb_rotl32 (uint32_t x, unsigned int count) |
|
|
313 | { |
|
|
314 | return (x >> (32 - count)) | (x << count); |
|
|
315 | } |
|
|
316 | |
|
|
317 | ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const; |
|
|
318 | ecb_inline uint64_t |
|
|
319 | ecb_rotr64 (uint64_t x, unsigned int count) |
|
|
320 | { |
|
|
321 | return (x << (64 - count)) | (x >> count); |
|
|
322 | } |
|
|
323 | |
|
|
324 | ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const; |
|
|
325 | ecb_inline uint64_t |
|
|
326 | ecb_rotl64 (uint64_t x, unsigned int count) |
|
|
327 | { |
|
|
328 | return (x >> (64 - count)) | (x << count); |
|
|
329 | } |
|
|
330 | |
|
|
331 | #endif |
317 | #endif |
332 | |
318 | |