ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libecb/ecb.h
(Generate patch)

Comparing libecb/ecb.h (file contents):
Revision 1.34 by root, Fri Jun 10 12:20:14 2011 UTC vs.
Revision 1.35 by root, Sat Jun 11 13:25:25 2011 UTC

84# define ecb_decltype(x) __decltype(x) 84# define ecb_decltype(x) __decltype(x)
85#elif ECB_GCC_VERSION(3,0) 85#elif ECB_GCC_VERSION(3,0)
86# define ecb_decltype(x) typeof(x) 86# define ecb_decltype(x) typeof(x)
87#endif 87#endif
88 88
89#define ecb_artificial ecb_attribute ((__artificial__)) /* 4.3 */
90#define ecb_noinline ecb_attribute ((__noinline__)) 89#define ecb_noinline ecb_attribute ((__noinline__))
91#define ecb_noreturn ecb_attribute ((__noreturn__)) 90#define ecb_noreturn ecb_attribute ((__noreturn__))
92#define ecb_unused ecb_attribute ((__unused__)) 91#define ecb_unused ecb_attribute ((__unused__))
93#define ecb_const ecb_attribute ((__const__)) 92#define ecb_const ecb_attribute ((__const__))
94#define ecb_pure ecb_attribute ((__pure__)) 93#define ecb_pure ecb_attribute ((__pure__))
94
95#if ECB_GCC_VERSION(4,3)
96 #define ecb_artificial ecb_attribute ((__artificial__)) /* 4.3 */
95#define ecb_hot ecb_attribute ((__hot__)) /* 4.3 */ 97 #define ecb_hot ecb_attribute ((__hot__)) /* 4.3 */
96#define ecb_cold ecb_attribute ((__cold__)) /* 4.3 */ 98 #define ecb_cold ecb_attribute ((__cold__)) /* 4.3 */
99#else
100 #define ecb_artificial
101 #define ecb_hot
102 #define ecb_cold
103#endif
97 104
98/* put into if's if you are very sure that the expression */ 105/* put into if's if you are very sure that the expression */
99/* is mostly true or mostly false. note that these return */ 106/* is mostly true or mostly false. note that these return */
100/* booleans, not the expression. */ 107/* booleans, not the expression. */
101#define ecb_expect_false(expr) ecb_expect (!!(expr), 0) 108#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
106/* try to tell the compiler that some condition is definitely true */ 113/* try to tell the compiler that some condition is definitely true */
107#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) 114#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
108 115
109/* count trailing zero bits and count # of one bits */ 116/* count trailing zero bits and count # of one bits */
110#if ECB_GCC_VERSION(3,4) 117#if ECB_GCC_VERSION(3,4)
111#define ecb_ctz32(x) __builtin_ctz (x) 118 #define ecb_ctz32(x) __builtin_ctz (x)
112#define ecb_popcount32(x) __builtin_popcount (x) 119 #define ecb_popcount32(x) __builtin_popcount (x)
113#else 120#else
114ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const; 121 ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const;
115ECB_HEADER_INLINE int 122 ECB_HEADER_INLINE int
116ecb_ctz32 (uint32_t x) 123 ecb_ctz32 (uint32_t x)
117{ 124 {
118 int r = 0; 125 int r = 0;
119 126
120 x &= -x; /* this isolates the lowest bit */ 127 x &= -x; /* this isolates the lowest bit */
121 128
122 if (x & 0xaaaaaaaa) r += 1; 129 if (x & 0xaaaaaaaa) r += 1;
123 if (x & 0xcccccccc) r += 2; 130 if (x & 0xcccccccc) r += 2;
124 if (x & 0xf0f0f0f0) r += 4; 131 if (x & 0xf0f0f0f0) r += 4;
125 if (x & 0xff00ff00) r += 8; 132 if (x & 0xff00ff00) r += 8;
126 if (x & 0xffff0000) r += 16; 133 if (x & 0xffff0000) r += 16;
127 134
128 return r; 135 return r;
129} 136 }
130 137
131ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const; 138 ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const;
132ECB_HEADER_INLINE int 139 ECB_HEADER_INLINE int
133ecb_popcount32 (uint32_t x) 140 ecb_popcount32 (uint32_t x)
134{ 141 {
135 x -= (x >> 1) & 0x55555555; 142 x -= (x >> 1) & 0x55555555;
136 x = ((x >> 2) & 0x33333333) + (x & 0x33333333); 143 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
137 x = ((x >> 4) + x) & 0x0f0f0f0f; 144 x = ((x >> 4) + x) & 0x0f0f0f0f;
138 x *= 0x01010101; 145 x *= 0x01010101;
139 146
140 return x >> 24; 147 return x >> 24;
141} 148 }
142#endif 149#endif
143 150
144#if ECB_GCC_VERSION(4,3) 151#if ECB_GCC_VERSION(4,3)
145# define ecb_bswap32(x) __builtin_bswap32 (x) 152 #define ecb_bswap32(x) __builtin_bswap32 (x)
146# define ecb_bswap16(x) (__builtin_bswap32(x) >> 16) 153 #define ecb_bswap16(x) (__builtin_bswap32(x) >> 16)
147#else 154#else
148ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) ecb_const; 155 ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) ecb_const;
149ECB_HEADER_INLINE uint32_t 156 ECB_HEADER_INLINE uint32_t
150ecb_bswap32 (uint32_t x) 157 ecb_bswap32 (uint32_t x)
151{ 158 {
152 return (x >> 24) 159 return (x >> 24)
153 | ((x >> 8) & 0x0000ff00) 160 | ((x >> 8) & 0x0000ff00)
154 | ((x << 8) & 0x00ff0000) 161 | ((x << 8) & 0x00ff0000)
155 | (x << 24); 162 | (x << 24);
156} 163 }
157 164
158ECB_HEADER_INLINE uint32_t ecb_bswap16 (uint32_t x) ecb_const; 165 ECB_HEADER_INLINE uint32_t ecb_bswap16 (uint32_t x) ecb_const;
159ECB_HEADER_INLINE uint32_t 166 ECB_HEADER_INLINE uint32_t
160ecb_bswap16 (uint32_t x) 167 ecb_bswap16 (uint32_t x)
161{ 168 {
162 return ((x >> 8) & 0xff) 169 return ((x >> 8) & 0xff)
163 | ((x << 8) & 0x00ff0000) 170 | ((x << 8) & 0x00ff0000)
164 | (x << 24); 171 | (x << 24);
165} 172 }
166#endif 173#endif
167 174
168#if ECB_GCC_VERSION(4,5) 175#if ECB_GCC_VERSION(4,5)
169# define ecb_unreachable() __builtin_unreachable () 176 #define ecb_unreachable() __builtin_unreachable ()
170#else 177#else
171/* this seems to work fine, but gcc always emits a warning for it :/ */ 178 /* this seems to work fine, but gcc always emits a warning for it :/ */
172ECB_HEADER_INLINE void ecb_unreachable (void) ecb_noreturn; 179 ECB_HEADER_INLINE void ecb_unreachable (void) ecb_noreturn;
173ECB_HEADER_INLINE void ecb_unreachable (void) { } 180 ECB_HEADER_INLINE void ecb_unreachable (void) { }
174#endif 181#endif
175 182
176ECB_HEADER_INLINE unsigned char ecb_byteorder_helper (void) ecb_const; 183ECB_HEADER_INLINE unsigned char ecb_byteorder_helper (void) ecb_const;
177ECB_HEADER_INLINE unsigned char 184ECB_HEADER_INLINE unsigned char
178ecb_byteorder_helper (void) 185ecb_byteorder_helper (void)
185ECB_HEADER_INLINE ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }; 192ECB_HEADER_INLINE ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; };
186ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) ecb_const; 193ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) ecb_const;
187ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }; 194ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; };
188 195
189#if ECB_GCC_VERSION(3,0) 196#if ECB_GCC_VERSION(3,0)
190# define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) 197 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
191#else 198#else
192# define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) 199 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
193#endif 200#endif
194 201
195#if ecb_cplusplus_does_not_suck 202#if ecb_cplusplus_does_not_suck
196// does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) 203 // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm)
197template<typename T, int N> 204 template<typename T, int N>
198static inline int ecb_array_length (const T (&arr)[N]) 205 static inline int ecb_array_length (const T (&arr)[N])
199{ 206 {
200 return N; 207 return N;
201} 208 }
202#else 209#else
203#define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) 210 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
204#endif 211#endif
205 212
206ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; 213ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
207ECB_INLINE uint32_t 214ECB_INLINE uint32_t
208ecb_rotr32 (uint32_t x, unsigned int count) 215ecb_rotr32 (uint32_t x, unsigned int count)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines