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. /*
  33. * (long)X = ((long long)divs) / (long)div
  34. * (long)rem = ((long long)divs) % (long)div
  35. *
  36. * Warning, this will do an exception if X overflows.
  37. */
  38. #define div_long_long_rem(a, b, c) div_ll_X_l_rem(a, b, c)
  39. static inline long div_ll_X_l_rem(long long divs, long div, long *rem)
  40. {
  41. long dum2;
  42. asm("divl %2":"=a"(dum2), "=d"(*rem)
  43. : "rm"(div), "A"(divs));
  44. return dum2;
  45. }
  46. extern uint64_t div64_64(uint64_t dividend, uint64_t divisor);
  47. #else
  48. # include <asm-generic/div64.h>
  49. #endif /* CONFIG_X86_32 */
  50. #endif /* _ASM_X86_DIV64_H */