div64.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include <linux/compiler.h>
  21. #if BITS_PER_LONG == 64
  22. # define do_div(n,base) ({ \
  23. uint32_t __base = (base); \
  24. uint32_t __rem; \
  25. __rem = ((uint64_t)(n)) % __base; \
  26. (n) = ((uint64_t)(n)) / __base; \
  27. __rem; \
  28. })
  29. #elif BITS_PER_LONG == 32
  30. extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
  31. /* The unnecessary pointer compare is there
  32. * to check for type safety (n must be 64bit)
  33. */
  34. # define do_div(n,base) ({ \
  35. uint32_t __base = (base); \
  36. uint32_t __rem; \
  37. (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \
  38. if (likely(((n) >> 32) == 0)) { \
  39. __rem = (uint32_t)(n) % __base; \
  40. (n) = (uint32_t)(n) / __base; \
  41. } else \
  42. __rem = __div64_32(&(n), __base); \
  43. __rem; \
  44. })
  45. #else /* BITS_PER_LONG == ?? */
  46. # error do_div() does not yet support the C64
  47. #endif /* BITS_PER_LONG */
  48. #endif /* _ASM_GENERIC_DIV64_H */