Rev 4292 | Rev 5056 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4292 | Rev 4559 | ||
---|---|---|---|
Line 34... | Line 34... | ||
34 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) |
34 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) |
35 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
35 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
36 | #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) |
36 | #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) |
37 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) |
37 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) |
Line -... | Line 38... | ||
- | 38 | ||
Line -... | Line 39... | ||
- | 39 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) |
|
- | 40 | ||
- | 41 | /* |
|
- | 42 | * This looks more complex than it should be. But we need to |
|
- | 43 | * get the type for the ~ right in round_down (it needs to be |
|
- | 44 | * as wide as the result!), and we want to evaluate the macro |
|
38 | 45 | * arguments just once each. |
|
39 | 46 | */ |
|
- | 47 | #define __round_mask(x, y) ((__typeof__(x))((y)-1)) |
|
- | 48 | #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) |
|
- | 49 | #define round_down(x, y) ((x) & ~__round_mask(x, y)) |
|
- | 50 | ||
- | 51 | #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) |
|
- | 52 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
|
- | 53 | #define DIV_ROUND_UP_ULL(ll,d) \ |
|
- | 54 | ({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; }) |
|
- | 55 | ||
- | 56 | #if BITS_PER_LONG == 32 |
|
- | 57 | # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) |
|
- | 58 | #else |
|
Line 40... | Line 59... | ||
40 | #define __round_mask(x, y) ((__typeof__(x))((y)-1)) |
59 | # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) |
41 | #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) |
60 | #endif |
42 | 61 | ||
43 | /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */ |
62 | /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */ |
44 | #define roundup(x, y) ( \ |
63 | #define roundup(x, y) ( \ |
45 | { \ |
64 | { \ |
46 | const typeof(y) __y = y; \ |
65 | const typeof(y) __y = y; \ |
- | 66 | (((x) + (__y - 1)) / __y) * __y; \ |
|
- | 67 | } \ |
|
- | 68 | ) |
|
- | 69 | #define rounddown(x, y) ( \ |
|
- | 70 | { \ |
|
- | 71 | typeof(x) __x = (x); \ |
|
Line -... | Line 72... | ||
- | 72 | __x - (__x % (y)); \ |
|
47 | (((x) + (__y - 1)) / __y) * __y; \ |
73 | } \ |
48 | } \ |
74 | ) |
49 | ) |
75 | |
- | 76 | /* |
|
50 | 77 | * Divide positive or negative dividend by positive divisor and round |
|
51 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
78 | * to closest integer. Result is undefined for negative divisors and |
- | 79 | * for negative dividends if the divisor variable type is unsigned. |
|
52 | #define DIV_ROUND_UP_ULL(ll,d) \ |
80 | */ |
- | 81 | #define DIV_ROUND_CLOSEST(x, divisor)( \ |
|
- | 82 | { \ |
|
53 | ({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; }) |
83 | typeof(x) __x = x; \ |
- | 84 | typeof(divisor) __d = divisor; \ |
|
54 | #define DIV_ROUND_CLOSEST(x, divisor)( \ |
85 | (((typeof(x))-1) > 0 || \ |
55 | { \ |
86 | ((typeof(divisor))-1) > 0 || (__x) > 0) ? \ |
Line -... | Line 87... | ||
- | 87 | (((__x) + ((__d) / 2)) / (__d)) : \ |
|
- | 88 | (((__x) - ((__d) / 2)) / (__d)); \ |
|
- | 89 | } \ |
|
- | 90 | ) |
|
- | 91 | ||
- | 92 | /* |
|
- | 93 | * Multiplies an integer by a fraction, while avoiding unnecessary |
|
- | 94 | * overflow or loss of precision. |
|
- | 95 | */ |
|
- | 96 | #define mult_frac(x, numer, denom)( \ |
|
- | 97 | { \ |
|
Line 56... | Line 98... | ||
56 | typeof(divisor) __divisor = divisor; \ |
98 | typeof(x) quot = (x) / (denom); \ |
57 | (((x) + ((__divisor) / 2)) / (__divisor)); \ |
99 | typeof(x) rem = (x) % (denom); \ |
58 | } \ |
100 | (quot * (numer)) + ((rem * (numer)) / (denom)); \ |
59 | ) |
101 | } \ |