div64.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. unsigned long __upper, __low, __high, __mod, __base; \
  19. __base = (base); \
  20. asm("":"=a" (__low), "=d" (__high):"A" (n)); \
  21. __upper = __high; \
  22. if (__high) { \
  23. __upper = __high % (__base); \
  24. __high = __high / (__base); \
  25. } \
  26. asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (__base), "0" (__low), "1" (__upper)); \
  27. asm("":"=A" (n):"a" (__low),"d" (__high)); \
  28. __mod; \
  29. })
  30. /*
  31. * (long)X = ((long long)divs) / (long)div
  32. * (long)rem = ((long long)divs) % (long)div
  33. *
  34. * Warning, this will do an exception if X overflows.
  35. */
  36. #define div_long_long_rem(a,b,c) div_ll_X_l_rem(a,b,c)
  37. static inline long
  38. div_ll_X_l_rem(long long divs, long div, long *rem)
  39. {
  40. long dum2;
  41. __asm__("divl %2":"=a"(dum2), "=d"(*rem)
  42. : "rm"(div), "A"(divs));
  43. return dum2;
  44. }
  45. extern uint64_t div64_64(uint64_t dividend, uint64_t divisor);
  46. #else
  47. # include <asm-generic/div64.h>
  48. #endif /* CONFIG_X86_32 */
  49. #endif /* _ASM_X86_DIV64_H */