… | |
… | |
36 | * what their idiot authors think are the "more important" extensions, |
36 | * what their idiot authors think are the "more important" extensions, |
37 | * causing enourmous grief for some better fake benchmark numbers or so. |
37 | * causing enourmous grief for some better fake benchmark numbers or so. |
38 | * we try to detect these and simply assume they are not gcc - if they have |
38 | * we try to detect these and simply assume they are not gcc - if they have |
39 | * an issue with that they should have done it right in the first place. |
39 | * an issue with that they should have done it right in the first place. |
40 | */ |
40 | */ |
|
|
41 | #ifndef ECB_GCC_VERSION |
41 | #if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__llvm__) |
42 | # if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__llvm__) |
42 | # define ECB_GCC_VERSION(major,minor) 0 |
43 | # define ECB_GCC_VERSION(major,minor) 0 |
43 | #else |
44 | # else |
44 | # define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
45 | # define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
|
|
46 | # endif |
45 | #endif |
47 | #endif |
46 | |
48 | |
47 | #ifndef __cplusplus |
49 | #ifndef __cplusplus |
48 | # if __STDC_VERSION__ >= 199901L |
50 | # if __STDC_VERSION__ >= 199901L |
49 | # define ECB_INLINE inline |
51 | # define ECB_INLINE inline |
… | |
… | |
96 | |
98 | |
97 | /* try to tell the compiler that some condition is definitely true */ |
99 | /* try to tell the compiler that some condition is definitely true */ |
98 | #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) |
100 | #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) |
99 | |
101 | |
100 | /* count trailing zero bits and count # of one bits */ |
102 | /* count trailing zero bits and count # of one bits */ |
|
|
103 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const; |
|
|
104 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const; |
101 | #if ECB_GCC_VERSION(3,4) |
105 | #if ECB_GCC_VERSION(3,4) |
102 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); } |
106 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); } |
103 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); } |
107 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); } |
104 | #else |
108 | #else |
105 | ECB_HEADER_INLINE |
109 | ECB_HEADER_INLINE |
106 | ecb_ctz32 (uint32_t x) ecb_const |
110 | ecb_ctz32 (uint32_t x) |
107 | { |
111 | { |
108 | int r = 0; |
112 | int r = 0; |
109 | |
113 | |
110 | x &= -x; /* this isolates the lowest bit */ |
114 | x &= -x; /* this isolates the lowest bit */ |
111 | |
115 | |
… | |
… | |
117 | |
121 | |
118 | return r; |
122 | return r; |
119 | } |
123 | } |
120 | |
124 | |
121 | ECB_HEADER_INLINE |
125 | ECB_HEADER_INLINE |
122 | ecb_popcount32 (uint32_t x) ecb_const |
126 | ecb_popcount32 (uint32_t x) |
123 | { |
127 | { |
124 | x -= (x >> 1) & 0x55555555; |
128 | x -= (x >> 1) & 0x55555555; |
125 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
129 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
126 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
130 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
127 | x *= 0x01010101; |
131 | x *= 0x01010101; |
128 | |
132 | |
129 | return x >> 24; |
133 | return x >> 24; |
130 | } |
134 | } |
131 | #endif |
135 | #endif |
132 | |
136 | |
|
|
137 | ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
133 | #if ECB_GCC_VERSION(4,3) |
138 | #if ECB_GCC_VERSION(4,3) |
134 | ECB_GCC_VERSION uint32_t ecb_bswap32 (uint32_t x) { return __builtin_bswap32 (x); } |
139 | ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) { return __builtin_bswap32 (x); } |
135 | #else |
140 | #else |
136 | ECB_GCC_VERSION uint32_t |
141 | ECB_HEADER_INLINE uint32_t |
137 | ecb_bswap32 (uint32_t x) |
142 | ecb_bswap32 (uint32_t x) |
138 | { |
143 | { |
139 | return (x >> 24) |
144 | return (x >> 24) |
140 | | ((x >> 8) & 0x0000ff00) |
145 | | ((x >> 8) & 0x0000ff00) |
141 | | ((x << 8) & 0x00ff0000) |
146 | | ((x << 8) & 0x00ff0000) |
142 | | (x << 24); |
147 | | (x << 24); |
143 | } |
148 | } |
144 | #endif |
149 | #endif |
145 | |
150 | |
|
|
151 | ECB_HEADER_INLINE void ecb_unreachable (void) ecb_attribute ((noreturn)); |
146 | #if ECB_GCC_VERSION(4,5) |
152 | #if ECB_GCC_VERSION(4,5) |
147 | # define ecb_unreachable() __builtin_unreachable () |
153 | ECB_HEADER_INLINE void ecb_unreachable (void) { __builtin_unreachable (); } |
148 | #else |
154 | #else |
149 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
155 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
150 | ECB_HEADER_INLINE void ecb_unreachable () ecb_attribute ((noreturn)); |
|
|
151 | ECB_HEADER_INLINE void ecb_unreachable () { } |
156 | ECB_HEADER_INLINE void ecb_unreachable (void) { } |
152 | #endif |
157 | #endif |
153 | |
158 | |
|
|
159 | ECB_HEADER_INLINE unsigned char ecb_byteorder_helper () ecb_const; |
|
|
160 | |
154 | ECB_HEADER_INLINE unsigned char |
161 | ECB_HEADER_INLINE unsigned char |
155 | ecb_byteorder_helper () ecb_const |
162 | ecb_byteorder_helper () |
156 | { |
163 | { |
157 | const uint32_t u = 0x11223344; |
164 | const uint32_t u = 0x11223344; |
158 | return *(unsigned char *)&u; |
165 | return *(unsigned char *)&u; |
159 | } |
166 | } |
160 | |
167 | |
|
|
168 | ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const; |
161 | ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; }; |
169 | ECB_HEADER_INLINE ecb_bool ecb_big_endian () { return ecb_byteorder_helper () == 0x11; }; |
|
|
170 | ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const; |
162 | ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; |
171 | ECB_HEADER_INLINE ecb_bool ecb_little_endian () { return ecb_byteorder_helper () == 0x44; }; |
163 | |
172 | |
164 | #if ecb_cplusplus_does_not_suck |
173 | #if ecb_cplusplus_does_not_suck |
165 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
174 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
166 | template<typename T, int N> |
175 | template<typename T, int N> |
167 | static inline int ecb_array_length (const T (&arr)[N]) |
176 | static inline int ecb_array_length (const T (&arr)[N]) |
… | |
… | |
170 | } |
179 | } |
171 | #else |
180 | #else |
172 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
181 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
173 | #endif |
182 | #endif |
174 | |
183 | |
|
|
184 | ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
175 | ECB_INLINE uint32_t |
185 | ECB_INLINE uint32_t |
176 | ecb_rotr32 (uint32_t x, unsigned int count) |
186 | ecb_rotr32 (uint32_t x, unsigned int count) |
177 | { |
187 | { |
178 | return (x << (32 - count)) | (x >> count); |
188 | return (x << (32 - count)) | (x >> count); |
179 | } |
189 | } |
180 | |
190 | |
|
|
191 | ECB_INLINE uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
181 | ECB_INLINE uint32_t |
192 | ECB_INLINE uint32_t |
182 | ecb_rotl32 (uint32_t x, unsigned int count) |
193 | ecb_rotl32 (uint32_t x, unsigned int count) |
183 | { |
194 | { |
184 | return (x >> (32 - count)) | (x << count); |
195 | return (x >> (32 - count)) | (x << count); |
185 | } |
196 | } |