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! */ |
40 | typedef int ecb_bool; |
43 | typedef 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), 0) |
85 | #define ecb_expect_true(expr) ecb_expect ((expr) ? 1 : 0, 1) |
93 | #define ecb_likely(expr) ecb_expect (!!(expr), 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) |
92 | ECB_HEADER_INLINE int ecb_ctz32 (unsigned int x) { return __builtin_ctz (x); } |
100 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); } |
93 | ECB_HEADER_INLINE int ecb_popcount32 (unsigned int x) { return __builtin_popcount (x); } |
101 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); } |
94 | #else |
102 | #else |
95 | ECB_HEADER_INLINE |
103 | ECB_HEADER_INLINE |
96 | ecb_ctz32 (unsigned int x) ecb_const |
104 | ecb_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 | |
111 | ECB_HEADER_INLINE |
119 | ECB_HEADER_INLINE |
112 | ecb_popcount32 (unsigned int x) ecb_const |
120 | ecb_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; |
… | |
… | |
131 | ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; |
139 | ECB_HEADER_INLINE ecb_bool ecb_little_endian () ecb_const { return ecb_byteorder_helper () == 0x44; }; |
132 | |
140 | |
133 | #if ecb_cplusplus_does_not_suck |
141 | #if ecb_cplusplus_does_not_suck |
134 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
142 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
135 | template<typename T, int N> |
143 | template<typename T, int N> |
136 | static inline int array_length (const T (&arr)[N]) |
144 | static inline int ecb_array_length (const T (&arr)[N]) |
137 | { |
145 | { |
138 | return N; |
146 | return N; |
139 | } |
147 | } |
140 | #else |
148 | #else |
141 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
149 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
142 | #endif |
150 | #endif |
143 | |
151 | |
|
|
152 | ECB_INLINE uint32_t |
|
|
153 | ecb_rotr32 (uint32_t x, unsigned int count) |
|
|
154 | { |
|
|
155 | return (x << (32 - count)) | (x >> count); |
|
|
156 | } |
|
|
157 | |
|
|
158 | ECB_INLINE uint32_t |
|
|
159 | ecb_rotl32 (uint32_t x, unsigned int count) |
|
|
160 | { |
|
|
161 | return (x >> (32 - count)) | (x << count); |
|
|
162 | } |
|
|
163 | |
|
|
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 | } |
144 | #endif |
175 | #endif |
145 | |
176 | |
|
|
177 | #endif |
|
|
178 | |