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

Comparing libecb/ecb.h (file contents):
Revision 1.13 by root, Thu May 26 18:21:04 2011 UTC vs.
Revision 1.14 by root, Thu May 26 18:26:59 2011 UTC

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 */
103ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const;
104ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const;
101#if ECB_GCC_VERSION(3,4) 105#if ECB_GCC_VERSION(3,4)
102ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); } 106ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); }
103ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); } 107ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); }
104#else 108#else
105ECB_HEADER_INLINE 109ECB_HEADER_INLINE
106ecb_ctz32 (uint32_t x) ecb_const 110ecb_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
121ECB_HEADER_INLINE 125ECB_HEADER_INLINE
122ecb_popcount32 (uint32_t x) ecb_const 126ecb_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
137ECB_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)
134ECB_GCC_VERSION uint32_t ecb_bswap32 (uint32_t x) { return __builtin_bswap32 (x); } 139ECB_HEADER_INLINE uint32_t ecb_bswap32 (uint32_t x) { return __builtin_bswap32 (x); }
135#else 140#else
136ECB_GCC_VERSION uint32_t 141ECB_HEADER_INLINE uint32_t
137ecb_bswap32 (uint32_t x) 142ecb_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
151ECB_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 () 153ECB_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 :/ */
150ECB_HEADER_INLINE void ecb_unreachable () ecb_attribute ((noreturn));
151ECB_HEADER_INLINE void ecb_unreachable () { } 156ECB_HEADER_INLINE void ecb_unreachable (void) { }
152#endif 157#endif
153 158
159ECB_HEADER_INLINE unsigned char ecb_byteorder_helper () ecb_const;
160
154ECB_HEADER_INLINE unsigned char 161ECB_HEADER_INLINE unsigned char
155ecb_byteorder_helper () ecb_const 162ecb_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
168ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const;
161ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; }; 169ECB_HEADER_INLINE ecb_bool ecb_big_endian () { return ecb_byteorder_helper () == 0x11; };
170ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const;
162ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; 171ECB_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)
166template<typename T, int N> 175template<typename T, int N>
167static inline int ecb_array_length (const T (&arr)[N]) 176static 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
184ECB_INLINE uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
175ECB_INLINE uint32_t 185ECB_INLINE uint32_t
176ecb_rotr32 (uint32_t x, unsigned int count) 186ecb_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
191ECB_INLINE uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
181ECB_INLINE uint32_t 192ECB_INLINE uint32_t
182ecb_rotl32 (uint32_t x, unsigned int count) 193ecb_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}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines