--- libecb/ecb.h 2011/05/26 17:27:15 1.5 +++ libecb/ecb.h 2011/05/26 17:43:41 1.8 @@ -2,6 +2,7 @@ * libecb * * Copyright (©) 2009-2011 Marc Alexander Lehmann + * Copyright (©) 2011 Emanuele Giaquinta * All rights reserved. * * Redistribution and use in source and binary forms, with or without modifica- @@ -29,6 +30,8 @@ #ifndef ECB_H #define ECB_H +#include + #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) #ifndef __cplusplus @@ -41,6 +44,11 @@ # endif #endif +#define ECB_CONCAT_(a, b) a ## b +#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) +#define ECB_STRINGIFY_(a) # a +#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) + #define ECB_HEADER_INLINE static ECB_INLINE #if ECB_GCC_VERSION(3,1) @@ -81,19 +89,19 @@ /* put into ifs if you are very sure that the expression */ /* is mostly true or mosty false. note that these return */ /* booleans, not the expression. */ -#define ecb_expect_false(expr) ecb_expect ((expr) ? 1 : 0, 0) -#define ecb_expect_true(expr) ecb_expect ((expr) ? 1 : 0, 1) +#define ecb_unlikely(expr) ecb_expect ((expr) ? 1 : 0, 0) +#define ecb_likely(expr) ecb_expect ((expr) ? 1 : 0, 1) /* try to tell the compiler that some condition is definitely true */ #define ecb_assume(cond) do { if (!(cond)) unreachable (); } while (0) /* count trailing zero bits and count # of one bits */ #if ECB_GCC_VERSION(3,4) -ECB_HEADER_INLINE int ecb_ctz32 (unsigned int x) { return __builtin_ctz (x); } -ECB_HEADER_INLINE int ecb_popcount32 (unsigned int x) { return __builtin_popcount (x); } +ECB_HEADER_INLINE int ecb_ctz32 (uint32_t x) { return __builtin_ctz (x); } +ECB_HEADER_INLINE int ecb_popcount32 (uint32_t x) { return __builtin_popcount (x); } #else ECB_HEADER_INLINE -ecb_ctz32 (unsigned int x) ecb_const +ecb_ctz32 (uint32_t x) ecb_const { int r = 0; @@ -109,7 +117,7 @@ } ECB_HEADER_INLINE -ecb_popcount32 (unsigned int x) ecb_const +ecb_popcount32 (uint32_t x) ecb_const { x -= (x >> 1) & 0x55555555; x = ((x >> 2) & 0x33333333) + (x & 0x33333333); @@ -133,7 +141,7 @@ #if ecb_cplusplus_does_not_suck // does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) template -static inline int array_length (const T (&arr)[N]) +static inline int ecb_array_length (const T (&arr)[N]) { return N; } @@ -141,5 +149,17 @@ #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) #endif +ECB_INLINE uint32_t +ecb_rotr32 (uint32_t c, unsigned int count) +{ + return (c << (32 - count)) | (c >> count); +} + +ECB_INLINE uint32_t +ecb_rotl32 (uint32_t c, unsigned int count) +{ + return (c >> (32 - count)) | (c << count); +} + #endif