… | |
… | |
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | |
28 | |
29 | #ifndef ECB_H |
29 | #ifndef ECB_H |
30 | #define ECB_H |
30 | #define ECB_H |
|
|
31 | |
|
|
32 | #include <inttypes.h> |
31 | |
33 | |
32 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
34 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
33 | |
35 | |
34 | #ifndef __cplusplus |
36 | #ifndef __cplusplus |
35 | # if __STDC_VERSION__ >= 199901L |
37 | # if __STDC_VERSION__ >= 199901L |
… | |
… | |
79 | #endif |
81 | #endif |
80 | |
82 | |
81 | /* put into ifs if you are very sure that the expression */ |
83 | /* put into ifs if you are very sure that the expression */ |
82 | /* is mostly true or mosty false. note that these return */ |
84 | /* is mostly true or mosty false. note that these return */ |
83 | /* booleans, not the expression. */ |
85 | /* booleans, not the expression. */ |
84 | #define ecb_expect_false(expr) ecb_expect ((expr) ? 1 : 0, 0) |
86 | #define ecb_unlikely(expr) ecb_expect ((expr) ? 1 : 0, 0) |
85 | #define ecb_expect_true(expr) ecb_expect ((expr) ? 1 : 0, 1) |
87 | #define ecb_likely(expr) ecb_expect ((expr) ? 1 : 0, 1) |
86 | |
88 | |
87 | /* try to tell the compiler that some condition is definitely true */ |
89 | /* try to tell the compiler that some condition is definitely true */ |
88 | #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) |
90 | #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) |
89 | |
91 | |
90 | /* count trailing zero bits and count # of one bits */ |
92 | /* count trailing zero bits and count # of one bits */ |
91 | #if ECB_GCC_VERSION(3,4) |
93 | #if ECB_GCC_VERSION(3,4) |
92 | ECB_HEADER_INLINE int ecb_ctz32 (unsigned int x) { return __builtin_ctz (x); } |
94 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); } |
93 | ECB_HEADER_INLINE int ecb_popcount32 (unsigned int x) { return __builtin_popcount (x); } |
95 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); } |
94 | #else |
96 | #else |
95 | ECB_HEADER_INLINE |
97 | ECB_HEADER_INLINE |
96 | ecb_ctz32 (unsigned int x) ecb_const |
98 | ecb_ctz32 (uint32_t x) ecb_const |
97 | { |
99 | { |
98 | int r = 0; |
100 | int r = 0; |
99 | |
101 | |
100 | x &= -x; /* this isolates the lowest bit */ |
102 | x &= -x; /* this isolates the lowest bit */ |
101 | |
103 | |
… | |
… | |
107 | |
109 | |
108 | return r; |
110 | return r; |
109 | } |
111 | } |
110 | |
112 | |
111 | ECB_HEADER_INLINE |
113 | ECB_HEADER_INLINE |
112 | ecb_popcount32 (unsigned int x) ecb_const |
114 | ecb_popcount32 (uint32_t x) ecb_const |
113 | { |
115 | { |
114 | x -= (x >> 1) & 0x55555555; |
116 | x -= (x >> 1) & 0x55555555; |
115 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
117 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
116 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
118 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
117 | x *= 0x01010101; |
119 | x *= 0x01010101; |
… | |
… | |
139 | } |
141 | } |
140 | #else |
142 | #else |
141 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
143 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
142 | #endif |
144 | #endif |
143 | |
145 | |
|
|
146 | ECB_INLINE uint32_t |
|
|
147 | ecb_rotate32_right (uint32_t c, unsigned int count = 1) |
|
|
148 | { |
|
|
149 | return (c << (32 - count)) | (c >> count); |
|
|
150 | } |
|
|
151 | |
|
|
152 | ECB_INLINE uint32_t |
|
|
153 | ecb_rotate32_left (uint32_t c, unsigned int count = 1) |
|
|
154 | { |
|
|
155 | return (c >> (32 - count)) | (c << count); |
|
|
156 | } |
|
|
157 | |
144 | #endif |
158 | #endif |
145 | |
159 | |