div64.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef _ASM_X86_DIV64_H
  2. #define _ASM_X86_DIV64_H
  3. #ifdef CONFIG_X86_32
  4. #include <linux/types.h>
  5. #include <linux/log2.h>
  6. /*
  7. * do_div() is NOT a C function. It wants to return
  8. * two values (the quotient and the remainder), but
  9. * since that doesn't work very well in C, what it
  10. * does is:
  11. *
  12. * - modifies the 64-bit dividend _in_place_
  13. * - returns the 32-bit remainder
  14. *
  15. * This ends up being the most efficient "calling
  16. * convention" on x86.
  17. */
  18. #define do_div(n, base) \
  19. ({ \
  20. unsigned long __upper, __low, __high, __mod, __base; \
  21. __base = (base); \
  22. if (__builtin_constant_p(__base) && is_power_of_2(__base)) { \
  23. __mod = n & (__base - 1); \
  24. n >>= ilog2(__base); \
  25. } else { \
  26. asm("" : "=a" (__low), "=d" (__high) : "A" (n));\
  27. __upper = __high; \
  28. if (__high) { \
  29. __upper = __high % (__base); \
  30. __high = __high / (__base); \
  31. } \
  32. asm("divl %2" : "=a" (__low), "=d" (__mod) \
  33. : "rm" (__base), "0" (__low), "1" (__upper)); \
  34. asm("" : "=A" (n) : "a" (__low), "d" (__high)); \
  35. } \
  36. __mod; \
  37. })
  38. static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
  39. {
  40. union {
  41. u64 v64;
  42. u32 v32[2];
  43. } d = { dividend };
  44. u32 upper;
  45. upper = d.v32[1];
  46. d.v32[1] = 0;
  47. if (upper >= divisor) {
  48. d.v32[1] = upper / divisor;
  49. upper %= divisor;
  50. }
  51. asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
  52. "rm" (divisor), "0" (d.v32[0]), "1" (upper));
  53. return d.v64;
  54. }
  55. #define div_u64_rem div_u64_rem
  56. #else
  57. # include <asm-generic/div64.h>
  58. #endif /* CONFIG_X86_32 */
  59. #endif /* _ASM_X86_DIV64_H */