Jens Axboe | 064c636 | 2008-06-02 09:19:37 +0200 | [diff] [blame] | 1 | #ifndef FIO_FFZ_H |
2 | #define FIO_FFZ_H | ||||
3 | |||||
Jens Axboe | 0ce8b11 | 2011-01-27 22:25:29 +0100 | [diff] [blame] | 4 | static inline int __ffs(unsigned long word) |
Jens Axboe | 064c636 | 2008-06-02 09:19:37 +0200 | [diff] [blame] | 5 | { |
Jens Axboe | 0917444 | 2008-06-02 09:57:41 +0200 | [diff] [blame] | 6 | int r = 0; |
Jens Axboe | 064c636 | 2008-06-02 09:19:37 +0200 | [diff] [blame] | 7 | |
Jens Axboe | 0ce8b11 | 2011-01-27 22:25:29 +0100 | [diff] [blame] | 8 | #if BITS_PER_LONG == 64 |
9 | if ((word & 0xffffffff) == 0) { | ||||
10 | r += 32; | ||||
11 | word >>= 32; | ||||
12 | } | ||||
13 | #endif | ||||
Jens Axboe | 064c636 | 2008-06-02 09:19:37 +0200 | [diff] [blame] | 14 | if (!(word & 0xffff)) { |
15 | word >>= 16; | ||||
16 | r += 16; | ||||
17 | } | ||||
18 | if (!(word & 0xff)) { | ||||
19 | word >>= 8; | ||||
20 | r += 8; | ||||
21 | } | ||||
22 | if (!(word & 0xf)) { | ||||
23 | word >>= 4; | ||||
24 | r += 4; | ||||
25 | } | ||||
26 | if (!(word & 3)) { | ||||
27 | word >>= 2; | ||||
28 | r += 2; | ||||
29 | } | ||||
30 | if (!(word & 1)) { | ||||
31 | word >>= 1; | ||||
32 | r += 1; | ||||
33 | } | ||||
34 | |||||
35 | return r; | ||||
36 | } | ||||
37 | |||||
Jens Axboe | 0ce8b11 | 2011-01-27 22:25:29 +0100 | [diff] [blame] | 38 | static inline int ffz(unsigned long bitmask) |
Jens Axboe | 064c636 | 2008-06-02 09:19:37 +0200 | [diff] [blame] | 39 | { |
Jens Axboe | 0917444 | 2008-06-02 09:57:41 +0200 | [diff] [blame] | 40 | return __ffs(~bitmask); |
Jens Axboe | 064c636 | 2008-06-02 09:19:37 +0200 | [diff] [blame] | 41 | } |
42 | |||||
43 | #endif |