]> wimlib.net Git - wimlib/blob - include/wimlib/bitops.h
Improved error reporting if loading capture configuration file fails
[wimlib] / include / wimlib / bitops.h
1 /*
2  * bitops.h
3  *
4  * Inline functions for bit manipulation.
5  *
6  * The author dedicates this file to the public domain.
7  * You can do whatever you want with this file.
8  */
9
10 #ifndef _WIMLIB_BITOPS_H
11 #define _WIMLIB_BITOPS_H
12
13 #include "wimlib/compiler.h"
14 #include "wimlib/types.h"
15
16 /* Find Last Set bit   */
17
18 static inline unsigned
19 fls32(u32 v)
20 {
21 #ifdef compiler_fls32
22         return compiler_fls32(v);
23 #else
24         unsigned bit = 0;
25         while ((v >>= 1) != 0)
26                 bit++;
27         return bit;
28 #endif
29 }
30
31 static inline unsigned
32 fls64(u64 v)
33 {
34 #ifdef compiler_fls64
35         return compiler_fls64(v);
36 #else
37         unsigned bit = 0;
38         while ((v >>= 1) != 0)
39                 bit++;
40         return bit;
41 #endif
42 }
43
44 static inline unsigned
45 flsw(machine_word_t v)
46 {
47         BUILD_BUG_ON(WORDSIZE != 4 && WORDSIZE != 8);
48         if (WORDSIZE == 4)
49                 return fls32(v);
50         else
51                 return fls64(v);
52 }
53
54 /* Find First Set bit   */
55
56 static inline unsigned
57 ffs32(u32 v)
58 {
59 #ifdef compiler_ffs32
60         return compiler_ffs32(v);
61 #else
62         unsigned bit;
63         for (bit = 0; !(v & 1); bit++, v >>= 1)
64                 ;
65         return bit;
66 #endif
67 }
68
69 static inline unsigned
70 ffs64(u64 v)
71 {
72 #ifdef compiler_ffs64
73         return compiler_ffs64(v);
74 #else
75         unsigned bit;
76         for (bit = 0; !(v & 1); bit++, v >>= 1)
77                 ;
78         return bit;
79 #endif
80 }
81
82 static inline unsigned
83 ffsw(machine_word_t v)
84 {
85         BUILD_BUG_ON(WORDSIZE != 4 && WORDSIZE != 8);
86         if (WORDSIZE == 4)
87                 return ffs32(v);
88         else
89                 return ffs64(v);
90 }
91
92 #endif /* _WIMLIB_BITOPS_H */