div64.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_DIV64_H
  3. #define _ASM_X86_DIV64_H
  4. #ifdef CONFIG_X86_32
  5. #include <linux/types.h>
  6. #include <linux/log2.h>
  7. /*
  8. * do_div() is NOT a C function. It wants to return
  9. * two values (the quotient and the remainder), but
  10. * since that doesn't work very well in C, what it
  11. * does is:
  12. *
  13. * - modifies the 64-bit dividend _in_place_
  14. * - returns the 32-bit remainder
  15. *
  16. * This ends up being the most efficient "calling
  17. * convention" on x86.
  18. */
  19. #define do_div(n, base) \
  20. ({ \
  21. unsigned long __upper, __low, __high, __mod, __base; \
  22. __base = (base); \
  23. if (__builtin_constant_p(__base) && is_power_of_2(__base)) { \
  24. __mod = n & (__base - 1); \
  25. n >>= ilog2(__base); \
  26. } else { \
  27. asm("" : "=a" (__low), "=d" (__high) : "A" (n));\
  28. __upper = __high; \
  29. if (__high) { \
  30. __upper = __high % (__base); \
  31. __high = __high / (__base); \
  32. } \
  33. asm("divl %2" : "=a" (__low), "=d" (__mod) \
  34. : "rm" (__base), "0" (__low), "1" (__upper)); \
  35. asm("" : "=A" (n) : "a" (__low), "d" (__high)); \
  36. } \
  37. __mod; \
  38. })
  39. static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
  40. {
  41. union {
  42. u64 v64;
  43. u32 v32[2];
  44. } d = { dividend };
  45. u32 upper;
  46. upper = d.v32[1];
  47. d.v32[1] = 0;
  48. if (upper >= divisor) {
  49. d.v32[1] = upper / divisor;
  50. upper %= divisor;
  51. }
  52. asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
  53. "rm" (divisor), "0" (d.v32[0]), "1" (upper));
  54. return d.v64;
  55. }
  56. #define div_u64_rem div_u64_rem
  57. static inline u64 mul_u32_u32(u32 a, u32 b)
  58. {
  59. u32 high, low;
  60. asm ("mull %[b]" : "=a" (low), "=d" (high)
  61. : [a] "a" (a), [b] "rm" (b) );
  62. return low | ((u64)high) << 32;
  63. }
  64. #define mul_u32_u32 mul_u32_u32
  65. #else
  66. # include <asm-generic/div64.h>
  67. /*
  68. * Will generate an #DE when the result doesn't fit u64, could fix with an
  69. * __ex_table[] entry when it becomes an issue.
  70. */
  71. static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
  72. {
  73. u64 q;
  74. asm ("mulq %2; divq %3" : "=a" (q)
  75. : "a" (a), "rm" (mul), "rm" (div)
  76. : "rdx");
  77. return q;
  78. }
  79. #define mul_u64_u64_div_u64 mul_u64_u64_div_u64
  80. static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 div)
  81. {
  82. return mul_u64_u64_div_u64(a, mul, div);
  83. }
  84. #define mul_u64_u32_div mul_u64_u32_div
  85. #endif /* CONFIG_X86_32 */
  86. #endif /* _ASM_X86_DIV64_H */