div64.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef __I386_DIV64
  2. #define __I386_DIV64
  3. /*
  4. * do_div() is NOT a C function. It wants to return
  5. * two values (the quotient and the remainder), but
  6. * since that doesn't work very well in C, what it
  7. * does is:
  8. *
  9. * - modifies the 64-bit dividend _in_place_
  10. * - returns the 32-bit remainder
  11. *
  12. * This ends up being the most efficient "calling
  13. * convention" on x86.
  14. */
  15. #define do_div(n,base) ({ \
  16. unsigned long __upper, __low, __high, __mod, __base; \
  17. __base = (base); \
  18. asm("":"=a" (__low), "=d" (__high):"A" (n)); \
  19. __upper = __high; \
  20. if (__high) { \
  21. __upper = __high % (__base); \
  22. __high = __high / (__base); \
  23. } \
  24. asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (__base), "0" (__low), "1" (__upper)); \
  25. asm("":"=A" (n):"a" (__low),"d" (__high)); \
  26. __mod; \
  27. })
  28. /*
  29. * (long)X = ((long long)divs) / (long)div
  30. * (long)rem = ((long long)divs) % (long)div
  31. *
  32. * Warning, this will do an exception if X overflows.
  33. */
  34. #define div_long_long_rem(a,b,c) div_ll_X_l_rem(a,b,c)
  35. static inline long
  36. div_ll_X_l_rem(long long divs, long div, long *rem)
  37. {
  38. long dum2;
  39. __asm__("divl %2":"=a"(dum2), "=d"(*rem)
  40. : "rm"(div), "A"(divs));
  41. return dum2;
  42. }
  43. #endif