1 | /* |
1 | /* |
2 | * libecb |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
3 | * |
3 | * |
4 | * Copyright (©) 2009-2011 Marc Alexander Lehmann |
4 | * Copyright (©) 2009-2011 Marc Alexander Lehmann |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
6 | * All rights reserved. |
6 | * All rights reserved. |
7 | * |
7 | * |
… | |
… | |
30 | #ifndef ECB_H |
30 | #ifndef ECB_H |
31 | #define ECB_H |
31 | #define ECB_H |
32 | |
32 | |
33 | #include <inttypes.h> |
33 | #include <inttypes.h> |
34 | |
34 | |
|
|
35 | /* many compilers define _GNUC_ to some versions but then only implement |
|
|
36 | * what their idiot authors think are the "more important" extensions, |
|
|
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 |
|
|
39 | * an issue with that they should have done it right in the first place. |
|
|
40 | */ |
|
|
41 | #ifndef ECB_GCC_VERSION |
|
|
42 | # if defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__llvm__) |
|
|
43 | # define ECB_GCC_VERSION(major,minor) 0 |
|
|
44 | # else |
35 | #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 |
|
|
47 | #endif |
36 | |
48 | |
37 | #ifndef __cplusplus |
49 | #ifndef __cplusplus |
38 | # if __STDC_VERSION__ >= 199901L |
50 | # if __STDC_VERSION__ >= 199901L |
39 | # define ECB_INLINE inline |
51 | # define ECB_INLINE inline |
40 | typedef _Bool ecb_bool; |
52 | typedef _Bool ecb_bool; |
… | |
… | |
76 | #define ecb_const ecb_attribute ((const)) |
88 | #define ecb_const ecb_attribute ((const)) |
77 | #define ecb_pure ecb_attribute ((pure)) |
89 | #define ecb_pure ecb_attribute ((pure)) |
78 | #define ecb_hot ecb_attribute ((hot)) /* 4.3 */ |
90 | #define ecb_hot ecb_attribute ((hot)) /* 4.3 */ |
79 | #define ecb_cold ecb_attribute ((cold)) /* 4.3 */ |
91 | #define ecb_cold ecb_attribute ((cold)) /* 4.3 */ |
80 | |
92 | |
81 | #if ECB_GCC_VERSION(4,5) |
|
|
82 | # define ecb_unreachable() __builtin_unreachable () |
|
|
83 | #else |
|
|
84 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
|
|
85 | ECB_HEADER_INLINE void ecb_unreachable () ecb_attribute ((noreturn)); |
|
|
86 | ECB_HEADER_INLINE void ecb_unreachable () { } |
|
|
87 | #endif |
|
|
88 | |
|
|
89 | /* put into ifs if you are very sure that the expression */ |
93 | /* put into ifs if you are very sure that the expression */ |
90 | /* is mostly true or mosty false. note that these return */ |
94 | /* is mostly true or mosty false. note that these return */ |
91 | /* booleans, not the expression. */ |
95 | /* booleans, not the expression. */ |
92 | #define ecb_unlikely(expr) ecb_expect (!!(expr), 0) |
96 | #define ecb_unlikely(expr) ecb_expect (!!(expr), 0) |
93 | #define ecb_likely(expr) ecb_expect (!!(expr), 1) |
97 | #define ecb_likely(expr) ecb_expect (!!(expr), 1) |
… | |
… | |
95 | /* try to tell the compiler that some condition is definitely true */ |
99 | /* try to tell the compiler that some condition is definitely true */ |
96 | #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) |
100 | #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) |
97 | |
101 | |
98 | /* count trailing zero bits and count # of one bits */ |
102 | /* count trailing zero bits and count # of one bits */ |
99 | #if ECB_GCC_VERSION(3,4) |
103 | #if ECB_GCC_VERSION(3,4) |
100 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); } |
104 | #define ecb_ctz32 (x) __builtin_ctz (x) |
101 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); } |
105 | #define ecb_popcount32 (x) __builtin_popcount (x) |
102 | #else |
106 | #else |
|
|
107 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const; |
103 | ECB_HEADER_INLINE |
108 | ECB_HEADER_INLINE |
104 | ecb_ctz32 (uint32_t x) ecb_const |
109 | ecb_ctz32 (uint32_t x) |
105 | { |
110 | { |
106 | int r = 0; |
111 | int r = 0; |
107 | |
112 | |
108 | x &= -x; /* this isolates the lowest bit */ |
113 | x &= -x; /* this isolates the lowest bit */ |
109 | |
114 | |
… | |
… | |
114 | if (x & 0xffff0000) r += 16; |
119 | if (x & 0xffff0000) r += 16; |
115 | |
120 | |
116 | return r; |
121 | return r; |
117 | } |
122 | } |
118 | |
123 | |
|
|
124 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const; |
119 | ECB_HEADER_INLINE |
125 | ECB_HEADER_INLINE |
120 | ecb_popcount32 (uint32_t x) ecb_const |
126 | ecb_popcount32 (uint32_t x) |
121 | { |
127 | { |
122 | x -= (x >> 1) & 0x55555555; |
128 | x -= (x >> 1) & 0x55555555; |
123 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
129 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
124 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
130 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
125 | x *= 0x01010101; |
131 | x *= 0x01010101; |
126 | |
132 | |
127 | return x >> 24; |
133 | return x >> 24; |
128 | } |
134 | } |
129 | #endif |
135 | #endif |
130 | |
136 | |
|
|
137 | #if ECB_GCC_VERSION(4,3) |
|
|
138 | # define ecb_bswap32(x) __builtin_bswap32 (x) |
|
|
139 | #else |
|
|
140 | ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) ecb_const; |
|
|
141 | ECB_HEADER_INLINE uint32_t |
|
|
142 | ecb_bswap32 (uint32_t x) |
|
|
143 | { |
|
|
144 | return (x >> 24) |
|
|
145 | | ((x >> 8) & 0x0000ff00) |
|
|
146 | | ((x << 8) & 0x00ff0000) |
|
|
147 | | (x << 24); |
|
|
148 | } |
|
|
149 | #endif |
|
|
150 | |
|
|
151 | #if ECB_GCC_VERSION(4,5) |
|
|
152 | # define ecb_unreachable() __builtin_unreachable () |
|
|
153 | #else |
|
|
154 | /* this seems to work fine, but gcc always emits a warning for it :/ */ |
|
|
155 | ECB_HEADER_INLINE void ecb_unreachable (void) ecb_noreturn; |
|
|
156 | ECB_HEADER_INLINE void ecb_unreachable (void) { } |
|
|
157 | #endif |
|
|
158 | |
|
|
159 | ECB_HEADER_INLINE unsigned char ecb_byteorder_helper () ecb_const; |
131 | ECB_HEADER_INLINE unsigned char |
160 | ECB_HEADER_INLINE unsigned char |
132 | ecb_byteorder_helper () ecb_const |
161 | ecb_byteorder_helper () |
133 | { |
162 | { |
134 | const uint32_t u = 0x11223344; |
163 | const uint32_t u = 0x11223344; |
135 | return *(unsigned char *)&u; |
164 | return *(unsigned char *)&u; |
136 | } |
165 | } |
137 | |
166 | |
|
|
167 | ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const; |
138 | ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; }; |
168 | ECB_HEADER_INLINE ecb_bool ecb_big_endian () { return ecb_byteorder_helper () == 0x11; }; |
|
|
169 | ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const; |
139 | ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; |
170 | ECB_HEADER_INLINE ecb_bool ecb_little_endian () { return ecb_byteorder_helper () == 0x44; }; |
140 | |
171 | |
141 | #if ecb_cplusplus_does_not_suck |
172 | #if ecb_cplusplus_does_not_suck |
142 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
173 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
143 | template<typename T, int N> |
174 | template<typename T, int N> |
144 | static inline int ecb_array_length (const T (&arr)[N]) |
175 | static inline int ecb_array_length (const T (&arr)[N]) |
… | |
… | |
147 | } |
178 | } |
148 | #else |
179 | #else |
149 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
180 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
150 | #endif |
181 | #endif |
151 | |
182 | |
|
|
183 | ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; |
152 | ECB_INLINE uint32_t |
184 | ECB_INLINE uint32_t |
153 | ecb_rotr32 (uint32_t x, unsigned int count) |
185 | ecb_rotr32 (uint32_t x, unsigned int count) |
154 | { |
186 | { |
155 | return (x << (32 - count)) | (x >> count); |
187 | return (x << (32 - count)) | (x >> count); |
156 | } |
188 | } |
157 | |
189 | |
|
|
190 | ECB_INLINE uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; |
158 | ECB_INLINE uint32_t |
191 | ECB_INLINE uint32_t |
159 | ecb_rotl32 (uint32_t x, unsigned int count) |
192 | ecb_rotl32 (uint32_t x, unsigned int count) |
160 | { |
193 | { |
161 | return (x >> (32 - count)) | (x << count); |
194 | return (x >> (32 - count)) | (x << count); |
162 | } |
195 | } |
163 | |
196 | |
164 | #if ECB_GCC_VERSION(4,3) |
|
|
165 | # define ecb_bswap32(x) __builtin_bswap32 (x) |
|
|
166 | #else |
|
|
167 | ECB_GCC_VERSION uint32_t |
|
|
168 | ecb_bswap32 (uint32_t x) |
|
|
169 | { |
|
|
170 | return (x >> 24) |
|
|
171 | | ((x >> 8) & 0x0000ff00) |
|
|
172 | | ((x << 8) & 0x00ff0000) |
|
|
173 | | (x << 24); |
|
|
174 | } |
|
|
175 | #endif |
197 | #endif |
176 | |
198 | |
177 | #endif |
|
|
178 | |
|
|