1 | /* |
1 | /* |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
3 | * |
3 | * |
4 | * Copyright (©) 2009-2011 Marc Alexander Lehmann |
4 | * Copyright (©) 2009-2011 Marc Alexander Lehmann <libecb@schmorp.de> |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
6 | * All rights reserved. |
6 | * All rights reserved. |
7 | * |
7 | * |
8 | * 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- |
9 | * tion, are permitted provided that the following conditions are met: |
9 | * tion, are permitted provided that the following conditions are met: |
… | |
… | |
47 | # endif |
47 | # endif |
48 | #endif |
48 | #endif |
49 | |
49 | |
50 | #ifndef __cplusplus |
50 | #ifndef __cplusplus |
51 | # if __STDC_VERSION__ >= 199901L |
51 | # if __STDC_VERSION__ >= 199901L |
52 | # define ECB_INLINE inline |
52 | # define ECB_INLINE static inline |
53 | typedef _Bool ecb_bool; |
53 | typedef _Bool ecb_bool; |
54 | # else |
54 | # else |
55 | # define ECB_INLINE inline /* yeah! */ |
55 | # define ECB_INLINE static inline /* yeah! */ |
56 | typedef int ecb_bool; |
56 | typedef int ecb_bool; |
57 | # endif |
57 | # endif |
58 | #else |
58 | #else |
59 | # define ECB_INLINE inline |
59 | # define ECB_INLINE static inline |
60 | typedef bool ecb_bool; |
60 | typedef bool ecb_bool; |
61 | #endif |
61 | #endif |
62 | |
62 | |
63 | #define ECB_CONCAT_(a, b) a ## b |
63 | #define ECB_CONCAT_(a, b) a ## b |
64 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
64 | #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) |
65 | #define ECB_STRINGIFY_(a) # a |
65 | #define ECB_STRINGIFY_(a) # a |
66 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
66 | #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) |
67 | |
67 | |
68 | #define ECB_HEADER_INLINE static ECB_INLINE |
68 | #define ECB_HEADER_INLINE ECB_INLINE |
69 | |
69 | |
70 | #if ECB_GCC_VERSION(3,1) |
70 | #if ECB_GCC_VERSION(3,1) |
71 | # define ecb_attribute(attrlist) __attribute__(attrlist) |
71 | # define ecb_attribute(attrlist) __attribute__(attrlist) |
72 | # define ecb_is_constant(expr) __builtin_constant_p (expr) |
72 | # define ecb_is_constant(expr) __builtin_constant_p (expr) |
73 | # define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
73 | # define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
… | |
… | |
94 | #define ecb_pure ecb_attribute ((__pure__)) |
94 | #define ecb_pure ecb_attribute ((__pure__)) |
95 | #define ecb_hot ecb_attribute ((__hot__)) /* 4.3 */ |
95 | #define ecb_hot ecb_attribute ((__hot__)) /* 4.3 */ |
96 | #define ecb_cold ecb_attribute ((__cold__)) /* 4.3 */ |
96 | #define ecb_cold ecb_attribute ((__cold__)) /* 4.3 */ |
97 | |
97 | |
98 | /* put into if's if you are very sure that the expression */ |
98 | /* put into if's if you are very sure that the expression */ |
99 | /* is mostly true or mosty false. note that these return */ |
99 | /* is mostly true or mostly false. note that these return */ |
100 | /* booleans, not the expression. */ |
100 | /* booleans, not the expression. */ |
101 | #define ecb_unlikely(expr) ecb_expect (!!(expr), 0) |
101 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
|
|
102 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
102 | #define ecb_likely(expr) ecb_expect (!!(expr), 1) |
103 | #define ecb_likely(expr) ecb_expect_true (expr) |
|
|
104 | #define ecb_unlikely(expr) ecb_expect_false (expr) |
103 | |
105 | |
104 | /* try to tell the compiler that some condition is definitely true */ |
106 | /* try to tell the compiler that some condition is definitely true */ |
105 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
107 | #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) |
106 | |
108 | |
107 | /* count trailing zero bits and count # of one bits */ |
109 | /* count trailing zero bits and count # of one bits */ |
108 | #if ECB_GCC_VERSION(3,4) |
110 | #if ECB_GCC_VERSION(3,4) |
109 | #define ecb_ctz32(x) __builtin_ctz (x) |
111 | #define ecb_ctz32(x) __builtin_ctz (x) |
110 | #define ecb_popcount32(x) __builtin_popcount (x) |
112 | #define ecb_popcount32(x) __builtin_popcount (x) |
111 | #else |
113 | #else |
112 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const; |
114 | ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) ecb_const; |
113 | ECB_HEADER_INLINE |
115 | ECB_HEADER_INLINE int |
114 | ecb_ctz32 (uint32_t x) |
116 | ecb_ctz32 (uint32_t x) |
115 | { |
117 | { |
116 | int r = 0; |
118 | int r = 0; |
117 | |
119 | |
118 | x &= -x; /* this isolates the lowest bit */ |
120 | x &= -x; /* this isolates the lowest bit */ |
… | |
… | |
125 | |
127 | |
126 | return r; |
128 | return r; |
127 | } |
129 | } |
128 | |
130 | |
129 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const; |
131 | ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) ecb_const; |
130 | ECB_HEADER_INLINE |
132 | ECB_HEADER_INLINE int |
131 | ecb_popcount32 (uint32_t x) |
133 | ecb_popcount32 (uint32_t x) |
132 | { |
134 | { |
133 | x -= (x >> 1) & 0x55555555; |
135 | x -= (x >> 1) & 0x55555555; |
134 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
136 | x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
135 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
137 | x = ((x >> 4) + x) & 0x0f0f0f0f; |
… | |
… | |
182 | ECB_HEADER_INLINE ecb_bool ecb_big_endian (void) ecb_const; |
184 | ECB_HEADER_INLINE ecb_bool ecb_big_endian (void) ecb_const; |
183 | ECB_HEADER_INLINE ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }; |
185 | ECB_HEADER_INLINE ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }; |
184 | ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) ecb_const; |
186 | ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) ecb_const; |
185 | ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }; |
187 | ECB_HEADER_INLINE ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }; |
186 | |
188 | |
|
|
189 | #if ECB_GCC_VERSION(3,0) |
|
|
190 | # define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
|
|
191 | #else |
187 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - (-(m) % (n))) : ((m) % (n))) |
192 | # define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
|
|
193 | #endif |
188 | |
194 | |
189 | #if ecb_cplusplus_does_not_suck |
195 | #if ecb_cplusplus_does_not_suck |
190 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
196 | // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) |
191 | template<typename T, int N> |
197 | template<typename T, int N> |
192 | static inline int ecb_array_length (const T (&arr)[N]) |
198 | static inline int ecb_array_length (const T (&arr)[N]) |