math64.h 7.9 KB

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