1 #ifndef _LINUX_KERNEL_H 2 #define _LINUX_KERNEL_H 3 4 5 #include <linux/types.h> 6 7 #define INT_MAX ((int)(~0U>>1)) 8 #define INT_MIN (-INT_MAX - 1) 9 #define LLONG_MAX ((long long)(~0ULL>>1)) 10 11 #define U8_MAX ((u8)~0U) 12 #define U32_MAX ((u32)~0U) 13 #define U64_MAX ((u64)~0ULL) 14 15 #define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1) 16 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 17 18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 19 20 /* 21 * This looks more complex than it should be. But we need to 22 * get the type for the ~ right in round_down (it needs to be 23 * as wide as the result!), and we want to evaluate the macro 24 * arguments just once each. 25 */ 26 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 27 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 28 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 29 30 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 31 32 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 33 34 /* 35 * Divide positive or negative dividend by positive divisor and round 36 * to closest integer. Result is undefined for negative divisors and 37 * for negative dividends if the divisor variable type is unsigned. 38 */ 39 #define DIV_ROUND_CLOSEST(x, divisor)( \ 40 { \ 41 typeof(x) __x = x; \ 42 typeof(divisor) __d = divisor; \ 43 (((typeof(x))-1) > 0 || \ 44 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \ 45 (((__x) + ((__d) / 2)) / (__d)) : \ 46 (((__x) - ((__d) / 2)) / (__d)); \ 47 } \ 48 ) 49 50 /* 51 * Multiplies an integer by a fraction, while avoiding unnecessary 52 * overflow or loss of precision. 53 */ 54 #define mult_frac(x, numer, denom)( \ 55 { \ 56 typeof(x) quot = (x) / (denom); \ 57 typeof(x) rem = (x) % (denom); \ 58 (quot * (numer)) + ((rem * (numer)) / (denom)); \ 59 } \ 60 ) 61 62 /** 63 * upper_32_bits - return bits 32-63 of a number 64 * @n: the number we're accessing 65 * 66 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 67 * the "right shift count >= width of type" warning when that quantity is 68 * 32-bits. 69 */ 70 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 71 72 /** 73 * lower_32_bits - return bits 0-31 of a number 74 * @n: the number we're accessing 75 */ 76 #define lower_32_bits(n) ((u32)(n)) 77 78 /* 79 * abs() handles unsigned and signed longs, ints, shorts and chars. For all 80 * input types abs() returns a signed long. 81 * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64() 82 * for those. 83 */ 84 #define abs(x) ({ \ 85 long ret; \ 86 if (sizeof(x) == sizeof(long)) { \ 87 long __x = (x); \ 88 ret = (__x < 0) ? -__x : __x; \ 89 } else { \ 90 int __x = (x); \ 91 ret = (__x < 0) ? -__x : __x; \ 92 } \ 93 ret; \ 94 }) 95 96 #define abs64(x) ({ \ 97 s64 __x = (x); \ 98 (__x < 0) ? -__x : __x; \ 99 }) 100 101 /* 102 * min()/max()/clamp() macros that also do 103 * strict type-checking.. See the 104 * "unnecessary" pointer comparison. 105 */ 106 #define min(x, y) ({ \ 107 typeof(x) _min1 = (x); \ 108 typeof(y) _min2 = (y); \ 109 _min1 < _min2 ? _min1 : _min2; }) 110 111 #define max(x, y) ({ \ 112 typeof(x) _max1 = (x); \ 113 typeof(y) _max2 = (y); \ 114 _max1 > _max2 ? _max1 : _max2; }) 115 116 #define min3(x, y, z) ({ \ 117 typeof(x) _min1 = (x); \ 118 typeof(y) _min2 = (y); \ 119 typeof(z) _min3 = (z); \ 120 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \ 121 (_min2 < _min3 ? _min2 : _min3); }) 122 123 #define max3(x, y, z) ({ \ 124 typeof(x) _max1 = (x); \ 125 typeof(y) _max2 = (y); \ 126 typeof(z) _max3 = (z); \ 127 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \ 128 (_max2 > _max3 ? _max2 : _max3); }) 129 130 /* 131 * ..and if you can't take the strict 132 * types, you can specify one yourself. 133 * 134 * Or not use min/max/clamp at all, of course. 135 */ 136 #define min_t(type, x, y) ({ \ 137 type __min1 = (x); \ 138 type __min2 = (y); \ 139 __min1 < __min2 ? __min1: __min2; }) 140 141 #define max_t(type, x, y) ({ \ 142 type __max1 = (x); \ 143 type __max2 = (y); \ 144 __max1 > __max2 ? __max1: __max2; }) 145 146 /** 147 * container_of - cast a member of a structure out to the containing structure 148 * @ptr: the pointer to the member. 149 * @type: the type of the container struct this is embedded in. 150 * @member: the name of the member within the struct. 151 * 152 */ 153 #define container_of(ptr, type, member) ({ \ 154 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 155 (type *)( (char *)__mptr - offsetof(type,member) );}) 156 157 #endif 158