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

Comparing libecb/ecb.h (file contents):
Revision 1.3 by root, Thu May 26 16:57:04 2011 UTC vs.
Revision 1.6 by root, Thu May 26 17:35:47 2011 UTC

27 */ 27 */
28 28
29#ifndef ECB_H 29#ifndef ECB_H
30#define ECB_H 30#define ECB_H
31 31
32#include <inttypes.h>
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
36#ifndef __cplusplus
37# if __STDC_VERSION__ >= 199901L
38# define ECB_INLINE inline
39typedef _Bool ecb_bool;
40# else
41# define ECB_INLINE inline /* yeah! */
42typedef int ecb_bool;
43# endif
44#endif
45
34#define ECB_HEADER_INLINE static inline 46#define ECB_HEADER_INLINE static ECB_INLINE
35 47
36#if ECB_GCC_VERSION(3,1) 48#if ECB_GCC_VERSION(3,1)
37# define ecb_attribute(attrlist) __attribute__(attrlist) 49# define ecb_attribute(attrlist) __attribute__(attrlist)
38# define ecb_is_constant(c) __builtin_constant_p (c) 50# define ecb_is_constant(c) __builtin_constant_p (c)
39# define ecb_expect(expr,value) __builtin_expect ((expr),(value)) 51# define ecb_expect(expr,value) __builtin_expect ((expr),(value))
55#define ecb_noinline ecb_attribute ((noinline)) 67#define ecb_noinline ecb_attribute ((noinline))
56#define ecb_noreturn ecb_attribute ((noreturn)) 68#define ecb_noreturn ecb_attribute ((noreturn))
57#define ecb_unused ecb_attribute ((unused)) 69#define ecb_unused ecb_attribute ((unused))
58#define ecb_const ecb_attribute ((const)) 70#define ecb_const ecb_attribute ((const))
59#define ecb_pure ecb_attribute ((pure)) 71#define ecb_pure ecb_attribute ((pure))
60#define ecb_hot ecb_attribute ((hot)) // 4.3 72#define ecb_hot ecb_attribute ((hot)) /* 4.3 */
61#define ecb_cold ecb_attribute ((cold)) // 4.3 73#define ecb_cold ecb_attribute ((cold)) /* 4.3 */
62 74
63#if ECB_GCC_VERSION(4,5) 75#if ECB_GCC_VERSION(4,5)
64# define ecb_unreachable() __builtin_unreachable () 76# define ecb_unreachable() __builtin_unreachable ()
65#else 77#else
66 /* this seems to work fine, but gcc always emits a warning for it :/ */ 78/* this seems to work fine, but gcc always emits a warning for it :/ */
67 ECB_HEADER_INLINE void ecb_unreachable () ecb_attribute ((noreturn)); 79ECB_HEADER_INLINE void ecb_unreachable () ecb_attribute ((noreturn));
68 ECB_HEADER_INLINE void ecb_unreachable () { } 80ECB_HEADER_INLINE void ecb_unreachable () { }
69#endif 81#endif
70 82
71/* put into ifs if you are very sure that the expression */ 83/* put into ifs if you are very sure that the expression */
72/* is mostly true or mosty false. note that these return */ 84/* is mostly true or mosty false. note that these return */
73/* booleans, not the expression. */ 85/* booleans, not the expression. */
74#define ecb_expect_false(expr) ecb_expect ((expr) ? 1 : 0, 0) 86#define ecb_unlikely(expr) ecb_expect ((expr) ? 1 : 0, 0)
75#define ecb_expect_true(expr) ecb_expect ((expr) ? 1 : 0, 1) 87#define ecb_likely(expr) ecb_expect ((expr) ? 1 : 0, 1)
76 88
77/* try to tell the compiler that some condition is definitely true */ 89/* try to tell the compiler that some condition is definitely true */
78#define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) 90#define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0)
79 91
80/* count trailing zero bits and count # of one bits */ 92/* count trailing zero bits and count # of one bits */
81#if ECB_GCC_VERSION(3,4) 93#if ECB_GCC_VERSION(3,4)
82ECB_HEADER_INLINE int ecb_ctz (unsigned int x) { return __builtin_ctz (x); } 94ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); }
83ECB_HEADER_INLINE int ecb_popcount (unsigned int x) { return __builtin_popcount (x); } 95ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); }
84#else 96#else
85ECB_HEADER_INLINE 97ECB_HEADER_INLINE
86ecb_ctz (unsigned int x) ecb_const 98ecb_ctz32 (uint32_t x) ecb_const
87{ 99{
88 int r = 0; 100 int r = 0;
89 101
90 x &= -x; // this isolates the lowest bit 102 x &= -x; /* this isolates the lowest bit */
91 103
92 if (x & 0xaaaaaaaa) r += 1; 104 if (x & 0xaaaaaaaa) r += 1;
93 if (x & 0xcccccccc) r += 2; 105 if (x & 0xcccccccc) r += 2;
94 if (x & 0xf0f0f0f0) r += 4; 106 if (x & 0xf0f0f0f0) r += 4;
95 if (x & 0xff00ff00) r += 8; 107 if (x & 0xff00ff00) r += 8;
97 109
98 return r; 110 return r;
99} 111}
100 112
101ECB_HEADER_INLINE 113ECB_HEADER_INLINE
102ecb_popcount (unsigned int x) ecb_const 114ecb_popcount32 (uint32_t x) ecb_const
103{ 115{
104 x -= (x >> 1) & 0x55555555; 116 x -= (x >> 1) & 0x55555555;
105 x = ((x >> 2) & 0x33333333) + (x & 0x33333333); 117 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
106 x = ((x >> 4) + x) & 0x0f0f0f0f; 118 x = ((x >> 4) + x) & 0x0f0f0f0f;
107 x *= 0x01010101; 119 x *= 0x01010101;
115{ 127{
116 const uint32_t u = 0x11223344; 128 const uint32_t u = 0x11223344;
117 return *(unsigned char *)&u; 129 return *(unsigned char *)&u;
118} 130}
119 131
120ECB_HEADER_INLINE bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; }; 132ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; };
121ECB_HEADER_INLINE bool ecb_network () ecb_const { return big_endian (); };
122ECB_HEADER_INLINE bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; 133ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; };
123ECB_HEADER_INLINE bool ecb_vax () ecb_const { return little_endian (); };
124 134
135#if ecb_cplusplus_does_not_suck
136// does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm)
137template<typename T, int N>
138static inline int array_length (const T (&arr)[N])
139{
140 return N;
141}
142#else
143#define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
144#endif
145
146ECB_INLINE uint32_t
147ecb_rotate32_right (uint32_t c, unsigned int count = 1)
148{
149 return (c << (32 - count)) | (c >> count);
150}
151
152ECB_INLINE uint32_t
153ecb_rotate32_left (uint32_t c, unsigned int count = 1)
154{
155 return (c >> (32 - count)) | (c << count);
156}
125 157
126#endif 158#endif
127 159

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines