div64.h 1.4 KB

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