Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 1 | #ifndef _LINUX_MATH64_H |
| 2 | #define _LINUX_MATH64_H |
| 3 | |
Peng Fan | d218320 | 2017-04-10 13:39:48 +0800 | [diff] [blame] | 4 | #include <div64.h> |
| 5 | #include <linux/bitops.h> |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 6 | #include <linux/types.h> |
| 7 | |
| 8 | #if BITS_PER_LONG == 64 |
| 9 | |
Peng Fan | d218320 | 2017-04-10 13:39:48 +0800 | [diff] [blame] | 10 | #define div64_long(x, y) div64_s64((x), (y)) |
| 11 | #define div64_ul(x, y) div64_u64((x), (y)) |
| 12 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 13 | /** |
| 14 | * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder |
| 15 | * |
| 16 | * This is commonly provided by 32bit archs to provide an optimized 64bit |
| 17 | * divide. |
| 18 | */ |
| 19 | static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder) |
| 20 | { |
| 21 | *remainder = dividend % divisor; |
| 22 | return dividend / divisor; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * div_s64_rem - signed 64bit divide with 32bit divisor with remainder |
| 27 | */ |
| 28 | static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) |
| 29 | { |
| 30 | *remainder = dividend % divisor; |
| 31 | return dividend / divisor; |
| 32 | } |
| 33 | |
| 34 | /** |
Peng Fan | d218320 | 2017-04-10 13:39:48 +0800 | [diff] [blame] | 35 | * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder |
| 36 | */ |
| 37 | static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) |
| 38 | { |
| 39 | *remainder = dividend % divisor; |
| 40 | return dividend / divisor; |
| 41 | } |
| 42 | |
| 43 | /** |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 44 | * div64_u64 - unsigned 64bit divide with 64bit divisor |
| 45 | */ |
| 46 | static inline u64 div64_u64(u64 dividend, u64 divisor) |
| 47 | { |
| 48 | return dividend / divisor; |
| 49 | } |
| 50 | |
Ashok Reddy Soma | 982686c | 2022-02-23 15:36:04 +0100 | [diff] [blame] | 51 | #define DIV64_U64_ROUND_UP(ll, d) \ |
| 52 | ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); }) |
| 53 | |
Peng Fan | d218320 | 2017-04-10 13:39:48 +0800 | [diff] [blame] | 54 | /** |
| 55 | * div64_s64 - signed 64bit divide with 64bit divisor |
| 56 | */ |
| 57 | static inline s64 div64_s64(s64 dividend, s64 divisor) |
| 58 | { |
| 59 | return dividend / divisor; |
| 60 | } |
| 61 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 62 | #elif BITS_PER_LONG == 32 |
| 63 | |
Peng Fan | d218320 | 2017-04-10 13:39:48 +0800 | [diff] [blame] | 64 | #define div64_long(x, y) div_s64((x), (y)) |
| 65 | #define div64_ul(x, y) div_u64((x), (y)) |
| 66 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 67 | #ifndef div_u64_rem |
| 68 | static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder) |
| 69 | { |
| 70 | *remainder = do_div(dividend, divisor); |
| 71 | return dividend; |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | #ifndef div_s64_rem |
| 76 | extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder); |
| 77 | #endif |
| 78 | |
Peng Fan | d218320 | 2017-04-10 13:39:48 +0800 | [diff] [blame] | 79 | #ifndef div64_u64_rem |
| 80 | extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder); |
| 81 | #endif |
| 82 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 83 | #ifndef div64_u64 |
| 84 | extern u64 div64_u64(u64 dividend, u64 divisor); |
| 85 | #endif |
| 86 | |
Peng Fan | d218320 | 2017-04-10 13:39:48 +0800 | [diff] [blame] | 87 | #ifndef div64_s64 |
| 88 | extern s64 div64_s64(s64 dividend, s64 divisor); |
| 89 | #endif |
| 90 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 91 | #endif /* BITS_PER_LONG */ |
| 92 | |
| 93 | /** |
| 94 | * div_u64 - unsigned 64bit divide with 32bit divisor |
| 95 | * |
| 96 | * This is the most common 64bit divide and should be used if possible, |
| 97 | * as many 32bit archs can optimize this variant better than a full 64bit |
| 98 | * divide. |
| 99 | */ |
| 100 | #ifndef div_u64 |
| 101 | static inline u64 div_u64(u64 dividend, u32 divisor) |
| 102 | { |
| 103 | u32 remainder; |
| 104 | return div_u64_rem(dividend, divisor, &remainder); |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | /** |
| 109 | * div_s64 - signed 64bit divide with 32bit divisor |
| 110 | */ |
| 111 | #ifndef div_s64 |
| 112 | static inline s64 div_s64(s64 dividend, s32 divisor) |
| 113 | { |
| 114 | s32 remainder; |
| 115 | return div_s64_rem(dividend, divisor, &remainder); |
| 116 | } |
| 117 | #endif |
| 118 | |
| 119 | u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder); |
| 120 | |
Peng Fan | d218320 | 2017-04-10 13:39:48 +0800 | [diff] [blame] | 121 | static __always_inline u32 |
| 122 | __iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder) |
| 123 | { |
| 124 | u32 ret = 0; |
| 125 | |
| 126 | while (dividend >= divisor) { |
| 127 | /* The following asm() prevents the compiler from |
| 128 | optimising this loop into a modulo operation. */ |
| 129 | asm("" : "+rm"(dividend)); |
| 130 | |
| 131 | dividend -= divisor; |
| 132 | ret++; |
| 133 | } |
| 134 | |
| 135 | *remainder = dividend; |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | #ifndef mul_u32_u32 |
| 141 | /* |
| 142 | * Many a GCC version messes this up and generates a 64x64 mult :-( |
| 143 | */ |
| 144 | static inline u64 mul_u32_u32(u32 a, u32 b) |
| 145 | { |
| 146 | return (u64)a * b; |
| 147 | } |
| 148 | #endif |
| 149 | |
| 150 | #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__) |
| 151 | |
| 152 | #ifndef mul_u64_u32_shr |
| 153 | static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift) |
| 154 | { |
| 155 | return (u64)(((unsigned __int128)a * mul) >> shift); |
| 156 | } |
| 157 | #endif /* mul_u64_u32_shr */ |
| 158 | |
| 159 | #ifndef mul_u64_u64_shr |
| 160 | static inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift) |
| 161 | { |
| 162 | return (u64)(((unsigned __int128)a * mul) >> shift); |
| 163 | } |
| 164 | #endif /* mul_u64_u64_shr */ |
| 165 | |
| 166 | #else |
| 167 | |
| 168 | #ifndef mul_u64_u32_shr |
| 169 | static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift) |
| 170 | { |
| 171 | u32 ah, al; |
| 172 | u64 ret; |
| 173 | |
| 174 | al = a; |
| 175 | ah = a >> 32; |
| 176 | |
| 177 | ret = mul_u32_u32(al, mul) >> shift; |
| 178 | if (ah) |
| 179 | ret += mul_u32_u32(ah, mul) << (32 - shift); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | #endif /* mul_u64_u32_shr */ |
| 184 | |
| 185 | #ifndef mul_u64_u64_shr |
| 186 | static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift) |
| 187 | { |
| 188 | union { |
| 189 | u64 ll; |
| 190 | struct { |
| 191 | #ifdef __BIG_ENDIAN |
| 192 | u32 high, low; |
| 193 | #else |
| 194 | u32 low, high; |
| 195 | #endif |
| 196 | } l; |
| 197 | } rl, rm, rn, rh, a0, b0; |
| 198 | u64 c; |
| 199 | |
| 200 | a0.ll = a; |
| 201 | b0.ll = b; |
| 202 | |
| 203 | rl.ll = mul_u32_u32(a0.l.low, b0.l.low); |
| 204 | rm.ll = mul_u32_u32(a0.l.low, b0.l.high); |
| 205 | rn.ll = mul_u32_u32(a0.l.high, b0.l.low); |
| 206 | rh.ll = mul_u32_u32(a0.l.high, b0.l.high); |
| 207 | |
| 208 | /* |
| 209 | * Each of these lines computes a 64-bit intermediate result into "c", |
| 210 | * starting at bits 32-95. The low 32-bits go into the result of the |
| 211 | * multiplication, the high 32-bits are carried into the next step. |
| 212 | */ |
| 213 | rl.l.high = c = (u64)rl.l.high + rm.l.low + rn.l.low; |
| 214 | rh.l.low = c = (c >> 32) + rm.l.high + rn.l.high + rh.l.low; |
| 215 | rh.l.high = (c >> 32) + rh.l.high; |
| 216 | |
| 217 | /* |
| 218 | * The 128-bit result of the multiplication is in rl.ll and rh.ll, |
| 219 | * shift it right and throw away the high part of the result. |
| 220 | */ |
| 221 | if (shift == 0) |
| 222 | return rl.ll; |
| 223 | if (shift < 64) |
| 224 | return (rl.ll >> shift) | (rh.ll << (64 - shift)); |
| 225 | return rh.ll >> (shift & 63); |
| 226 | } |
| 227 | #endif /* mul_u64_u64_shr */ |
| 228 | |
| 229 | #endif |
| 230 | |
| 231 | #ifndef mul_u64_u32_div |
| 232 | static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor) |
| 233 | { |
| 234 | union { |
| 235 | u64 ll; |
| 236 | struct { |
| 237 | #ifdef __BIG_ENDIAN |
| 238 | u32 high, low; |
| 239 | #else |
| 240 | u32 low, high; |
| 241 | #endif |
| 242 | } l; |
| 243 | } u, rl, rh; |
| 244 | |
| 245 | u.ll = a; |
| 246 | rl.ll = mul_u32_u32(u.l.low, mul); |
| 247 | rh.ll = mul_u32_u32(u.l.high, mul) + rl.l.high; |
| 248 | |
| 249 | /* Bits 32-63 of the result will be in rh.l.low. */ |
| 250 | rl.l.high = do_div(rh.ll, divisor); |
| 251 | |
| 252 | /* Bits 0-31 of the result will be in rl.l.low. */ |
| 253 | do_div(rl.ll, divisor); |
| 254 | |
| 255 | rl.l.high = rh.l.low; |
| 256 | return rl.ll; |
| 257 | } |
| 258 | #endif /* mul_u64_u32_div */ |
| 259 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 260 | #endif /* _LINUX_MATH64_H */ |