div64.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
  47. {
  48. union {
  49. u64 v64;
  50. u32 v32[2];
  51. } d = { dividend };
  52. u32 upper;
  53. upper = d.v32[1];
  54. d.v32[1] = 0;
  55. if (upper >= divisor) {
  56. d.v32[1] = upper / divisor;
  57. upper %= divisor;
  58. }
  59. asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
  60. "rm" (divisor), "0" (d.v32[0]), "1" (upper));
  61. return d.v64;
  62. }
  63. #define div_u64_rem div_u64_rem
  64. #else
  65. # include <asm-generic/div64.h>
  66. #endif /* CONFIG_X86_32 */
  67. #endif /* _ASM_X86_DIV64_H */