div64.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef _ASM_GENERIC_DIV64_H
  2. #define _ASM_GENERIC_DIV64_H
  3. /*
  4. * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
  5. * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
  6. *
  7. * The semantics of do_div() are:
  8. *
  9. * uint32_t do_div(uint64_t *n, uint32_t base)
  10. * {
  11. * uint32_t remainder = *n % base;
  12. * *n = *n / base;
  13. * return remainder;
  14. * }
  15. *
  16. * NOTE: macro parameter n is evaluated multiple times,
  17. * beware of side effects!
  18. */
  19. #include <linux/types.h>
  20. extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
  21. /* The unnecessary pointer compare is there
  22. * to check for type safety (n must be 64bit)
  23. */
  24. # define do_div(n,base) ({ \
  25. uint32_t __base = (base); \
  26. uint32_t __rem; \
  27. (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \
  28. if (((n) >> 32) == 0) { \
  29. __rem = (uint32_t)(n) % __base; \
  30. (n) = (uint32_t)(n) / __base; \
  31. } else \
  32. __rem = __div64_32(&(n), __base); \
  33. __rem; \
  34. })
  35. /* Wrapper for do_div(). Doesn't modify dividend and returns
  36. * the result, not reminder.
  37. */
  38. static inline uint64_t lldiv(uint64_t dividend, uint32_t divisor)
  39. {
  40. uint64_t __res = dividend;
  41. do_div(__res, divisor);
  42. return(__res);
  43. }
  44. #endif /* _ASM_GENERIC_DIV64_H */