#ifndef _HELPER_H_ #define _HELPER_H_ #include #include #include #if LONG_MAX == 2147483647L # define BITS_PER_LONG 32 #elif LONG_MAX == 9223372036854775807L # define BITS_PER_LONG 64 #else # error Wacky LONG_MAX! #endif #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) #define __pure __attribute__((pure)) #define __aligned(x) __attribute__((aligned(x))) #define __printf(a,b) __attribute__((format(printf,a,b))) #define noinline __attribute__((noinline)) #ifndef __attribute_const__ #define __attribute_const__ __attribute__((__const__)) #endif #define __maybe_unused __attribute__((unused)) #if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__)) #define STORE32H(x, y) \ asm __volatile__ ( \ "bswapl %0 \n\t" \ "movl %0,(%1)\n\t" \ "bswapl %0 \n\t" \ ::"r"(x), "r"(y)); #define LOAD32H(x, y) \ asm __volatile__ ( \ "movl (%1),%0\n\t" \ "bswapl %0\n\t" \ :"=r"(x): "r"(y)); #else #define STORE32H(x, y) \ { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } #define LOAD32H(x, y) \ { x = ((unsigned long)((y)[0] & 255)<<24) | \ ((unsigned long)((y)[1] & 255)<<16) | \ ((unsigned long)((y)[2] & 255)<<8) | \ ((unsigned long)((y)[3] & 255)); } #endif #if defined(__GNUC__) && defined(__x86_64__) #define STORE64H(x, y) \ asm __volatile__ ( \ "bswapq %0 \n\t" \ "movq %0,(%1)\n\t" \ "bswapq %0 \n\t" \ ::"r"(x), "r"(y)); #define LOAD64H(x, y) \ asm __volatile__ ( \ "movq (%1),%0\n\t" \ "bswapq %0\n\t" \ :"=r"(x): "r"(y)); #else #define STORE64H(x, y) \ { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } #define LOAD64H(x, y) \ { x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \ (((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \ (((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \ (((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); } #endif #define ROTL8(v, n) \ ((uint8_t)(((v) << (n)) | ((v) >> (8 - (n))))) #define ROTL16(v, n) \ ((uint16_t)(((v) << (n)) | ((v) >> (16 - (n))))) #define ROTL32(v, n) \ ((uint32_t)(((v) << (n)) | ((v) >> (32 - (n))))) #define ROTL64(v, n) \ ((uint64_t)(((v) << (n)) | ((v) >> (64 - (n))))) #define ROTR8(v, n) ROTL8(v, 8 - (n)) #define ROTR16(v, n) ROTL16(v, 16 - (n)) #define ROTR32(v, n) ROTL32(v, 32 - (n)) #define ROTR64(v, n) ROTL64(v, 64 - (n)) /* To avoid that a compiler optimizes certain memset calls away, these * macros may be used instead. */ #define wipememory_set(_ptr,_set,_len) do { \ volatile char *_vptr=(volatile char *)(_ptr); \ size_t _vlen=(_len); \ while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \ } while(0) #define wipememory(_ptr,_len) wipememory_set(_ptr,0,_len) /* * min()/max() macros that also do * strict type-checking.. See the * "unnecessary" pointer comparison. */ #define min(x,y) ({ \ typeof(x) _x = (x); \ typeof(y) _y = (y); \ (void) (&_x == &_y); \ _x < _y ? _x : _y; }) #define max(x,y) ({ \ typeof(x) _x = (x); \ typeof(y) _y = (y); \ (void) (&_x == &_y); \ _x > _y ? _x : _y; }) /* Data needs not stay in cache */ static inline void prefetch0(const void *ptr) { __builtin_prefetch(ptr, 0, 0); } static inline void prefetch1(const void *ptr) { __builtin_prefetch(ptr, 0, 1); } static inline void prefetch2(const void *ptr) { __builtin_prefetch(ptr, 0, 2); } /* High degree of locality */ static inline void prefetch3(const void *ptr) { __builtin_prefetch(ptr, 0, 3); } typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef int8_t s8; typedef int16_t s16; typedef int32_t s32; typedef int64_t s64; /* * The next routines deal with comparing 16/32/64 bit unsigned ints * and worry about wraparound (automatic with unsigned arithmetic). */ static inline int before16(u16 seq1, u16 seq2) { return (s16)(seq1-seq2) < 0; } #define after16(seq2, seq1) before16(seq1, seq2) /* is s2<=s1<=s3 ? */ static inline int between16(u16 seq1, u16 seq2, u16 seq3) { return seq3 - seq2 >= seq1 - seq2; } static inline int before32(u32 seq1, u32 seq2) { return (s32)(seq1-seq2) < 0; } #define after32(seq2, seq1) before32(seq1, seq2) static inline int between32(u32 seq1, u32 seq2, u32 seq3) { return seq3 - seq2 >= seq1 - seq2; } static inline int before64(u64 seq1, u64 seq2) { return (s64)(seq1-seq2) < 0; } #define after64(seq2, seq1) before64(seq1, seq2) /* is s2<=s1<=s3 ? */ static inline int between64(u64 seq1, u64 seq2, u64 seq3) { return seq3 - seq2 >= seq1 - seq2; } /* * ..and if you can't take the strict * types, you can specify one yourself. * * Or not use min/max at all, of course. */ #define min_t(type,x,y) \ ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; }) #define max_t(type,x,y) \ ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; }) //#define offsetof(___TYPE, ___MEMBER) __builtin_offsetof(___TYPE, ___MEMBER) /** * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. * @type: the type of the container struct this is embedded in. * @member: the name of the member within the struct. * */ #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) /* * Check at compile time that something is of a particular type. * Always evaluates to 1 so you may use it easily in comparisons. */ #define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \ }) /* * Check at compile time that 'function' is a certain type, or is a pointer * to that type (needs to use typedef for the function type.) */ #define typecheck_fn(type,function) \ ({ typeof(type) __tmp = function; \ (void)__tmp; \ }) #endif