1 #pragma once 2 3 #include "../types.h" 4 #include "div64.h" 5 6 /* 7 * This looks more complex than it should be. But we need to 8 * get the type for the ~ right in round_down (it needs to be 9 * as wide as the result!), and we want to evaluate the macro 10 * arguments just once each. 11 */ 12 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 13 14 /** 15 * round_up - round up to next specified power of 2 16 * @x: the value to round 17 * @y: multiple to round up to (must be a power of 2) 18 * 19 * Rounds @x up to next multiple of @y (which must be a power of 2). 20 * To perform arbitrary rounding up, use roundup() below. 21 */ 22 #define round_up(x, y) ((((x)-1) | __round_mask(x, y)) + 1) 23 24 /** 25 * round_down - round down to next specified power of 2 26 * @x: the value to round 27 * @y: multiple to round down to (must be a power of 2) 28 * 29 * Rounds @x down to next multiple of @y (which must be a power of 2). 30 * To perform arbitrary rounding down, use rounddown() below. 31 */ 32 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 33 34 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP 35 36 #define DIV_ROUND_DOWN_ULL(ll, d) \ 37 ({ \ 38 unsigned long long _tmp = (ll); \ 39 do_div(_tmp, d); \ 40 _tmp; \ 41 }) 42 43 #define DIV_ROUND_UP_ULL(ll, d) \ 44 DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d)-1, (d)) 45 46 #if BITS_PER_LONG == 32 47 #define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP_ULL(ll, d) 48 #else 49 #define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP(ll, d) 50 #endif 51 52 /** 53 * roundup - round up to the next specified multiple 54 * @x: the value to up 55 * @y: multiple to round up to 56 * 57 * Rounds @x up to next multiple of @y. If @y will always be a power 58 * of 2, consider using the faster round_up(). 59 */ 60 #define roundup(x, y) \ 61 ({ \ 62 typeof(y) __y = y; \ 63 (((x) + (__y - 1)) / __y) * __y; \ 64 }) 65 /** 66 * rounddown - round down to next specified multiple 67 * @x: the value to round 68 * @y: multiple to round down to 69 * 70 * Rounds @x down to next multiple of @y. If @y will always be a power 71 * of 2, consider using the faster round_down(). 72 */ 73 #define rounddown(x, y) \ 74 ({ \ 75 typeof(x) __x = (x); \ 76 __x - (__x % (y)); \ 77 }) 78 79 /* 80 * Divide positive or negative dividend by positive or negative divisor 81 * and round to closest integer. Result is undefined for negative 82 * divisors if the dividend variable type is unsigned and for negative 83 * dividends if the divisor variable type is unsigned. 84 */ 85 #define DIV_ROUND_CLOSEST(x, divisor) \ 86 ({ \ 87 typeof(x) __x = x; \ 88 typeof(divisor) __d = divisor; \ 89 (((typeof(x))-1) > 0 || ((typeof(divisor))-1) > 0 || \ 90 (((__x) > 0) == ((__d) > 0))) ? \ 91 (((__x) + ((__d) / 2)) / (__d)) : \ 92 (((__x) - ((__d) / 2)) / (__d)); \ 93 }) 94 /* 95 * Same as above but for u64 dividends. divisor must be a 32-bit 96 * number. 97 */ 98 #define DIV_ROUND_CLOSEST_ULL(x, divisor) \ 99 ({ \ 100 typeof(divisor) __d = divisor; \ 101 unsigned long long _tmp = (x) + (__d) / 2; \ 102 do_div(_tmp, __d); \ 103 _tmp; \ 104 }) 105