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

Comparing libecb/ecb.h (file contents):
Revision 1.4 by root, Thu May 26 17:26:03 2011 UTC vs.
Revision 1.8 by root, Thu May 26 17:43:41 2011 UTC

1/* 1/*
2 * libecb 2 * libecb
3 * 3 *
4 * Copyright (©) 2009-2011 Marc Alexander Lehmann 4 * Copyright (©) 2009-2011 Marc Alexander Lehmann
5 * Copyright (©) 2011 Emanuele Giaquinta
5 * All rights reserved. 6 * All rights reserved.
6 * 7 *
7 * Redistribution and use in source and binary forms, with or without modifica- 8 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 9 * tion, are permitted provided that the following conditions are met:
9 * 10 *
27 */ 28 */
28 29
29#ifndef ECB_H 30#ifndef ECB_H
30#define ECB_H 31#define ECB_H
31 32
33#include <inttypes.h>
34
32#define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 35#define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
33 36
34#ifndef __cplusplus 37#ifndef __cplusplus
35# if __STDC_VERSION__ >= 199901L 38# if __STDC_VERSION__ >= 199901L
36# define ECB_INLINE inline 39# define ECB_INLINE inline
38# else 41# else
39# define ECB_INLINE inline /* yeah! */ 42# define ECB_INLINE inline /* yeah! */
40typedef int ecb_bool; 43typedef int ecb_bool;
41# endif 44# endif
42#endif 45#endif
46
47#define ECB_CONCAT_(a, b) a ## b
48#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
49#define ECB_STRINGIFY_(a) # a
50#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
43 51
44#define ECB_HEADER_INLINE static ECB_INLINE 52#define ECB_HEADER_INLINE static ECB_INLINE
45 53
46#if ECB_GCC_VERSION(3,1) 54#if ECB_GCC_VERSION(3,1)
47# define ecb_attribute(attrlist) __attribute__(attrlist) 55# define ecb_attribute(attrlist) __attribute__(attrlist)
79#endif 87#endif
80 88
81/* put into ifs if you are very sure that the expression */ 89/* put into ifs if you are very sure that the expression */
82/* is mostly true or mosty false. note that these return */ 90/* is mostly true or mosty false. note that these return */
83/* booleans, not the expression. */ 91/* booleans, not the expression. */
84#define ecb_expect_false(expr) ecb_expect ((expr) ? 1 : 0, 0) 92#define ecb_unlikely(expr) ecb_expect ((expr) ? 1 : 0, 0)
85#define ecb_expect_true(expr) ecb_expect ((expr) ? 1 : 0, 1) 93#define ecb_likely(expr) ecb_expect ((expr) ? 1 : 0, 1)
86 94
87/* try to tell the compiler that some condition is definitely true */ 95/* try to tell the compiler that some condition is definitely true */
88#define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) 96#define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0)
89 97
90/* count trailing zero bits and count # of one bits */ 98/* count trailing zero bits and count # of one bits */
91#if ECB_GCC_VERSION(3,4) 99#if ECB_GCC_VERSION(3,4)
92ECB_HEADER_INLINE int ecb_ctz32 (unsigned int x) { return __builtin_ctz (x); } 100ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); }
93ECB_HEADER_INLINE int ecb_popcount32 (unsigned int x) { return __builtin_popcount (x); } 101ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); }
94#else 102#else
95ECB_HEADER_INLINE 103ECB_HEADER_INLINE
96ecb_ctz32 (unsigned int x) ecb_const 104ecb_ctz32 (uint32_t x) ecb_const
97{ 105{
98 int r = 0; 106 int r = 0;
99 107
100 x &= -x; /* this isolates the lowest bit */ 108 x &= -x; /* this isolates the lowest bit */
101 109
107 115
108 return r; 116 return r;
109} 117}
110 118
111ECB_HEADER_INLINE 119ECB_HEADER_INLINE
112ecb_popcount32 (unsigned int x) ecb_const 120ecb_popcount32 (uint32_t x) ecb_const
113{ 121{
114 x -= (x >> 1) & 0x55555555; 122 x -= (x >> 1) & 0x55555555;
115 x = ((x >> 2) & 0x33333333) + (x & 0x33333333); 123 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
116 x = ((x >> 4) + x) & 0x0f0f0f0f; 124 x = ((x >> 4) + x) & 0x0f0f0f0f;
117 x *= 0x01010101; 125 x *= 0x01010101;
128} 136}
129 137
130ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; }; 138ECB_HEADER_INLINE ecb_bool ecb_big_endian () ecb_const { return ecb_byteorder_helper () == 0x11; };
131ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; 139ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; };
132 140
141#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)
143template<typename T, int N>
144static inline int ecb_array_length (const T (&arr)[N])
145{
146 return N;
147}
148#else
149#define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
133#endif 150#endif
134 151
152ECB_INLINE uint32_t
153ecb_rotr32 (uint32_t c, unsigned int count)
154{
155 return (c << (32 - count)) | (c >> count);
156}
157
158ECB_INLINE uint32_t
159ecb_rotl32 (uint32_t c, unsigned int count)
160{
161 return (c >> (32 - count)) | (c << count);
162}
163
164#endif
165

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines