calc64.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef _LINUX_CALC64_H
  2. #define _LINUX_CALC64_H
  3. #include <linux/types.h>
  4. #include <asm/div64.h>
  5. /*
  6. * This is a generic macro which is used when the architecture
  7. * specific div64.h does not provide a optimized one.
  8. *
  9. * The 64bit dividend is divided by the divisor (data type long), the
  10. * result is returned and the remainder stored in the variable
  11. * referenced by remainder (data type long *). In contrast to the
  12. * do_div macro the dividend is kept intact.
  13. */
  14. #ifndef div_long_long_rem
  15. #define div_long_long_rem(dividend, divisor, remainder) \
  16. do_div_llr((dividend), divisor, remainder)
  17. static inline unsigned long do_div_llr(const long long dividend,
  18. const long divisor, long *remainder)
  19. {
  20. u64 result = dividend;
  21. *(remainder) = do_div(result, divisor);
  22. return (unsigned long) result;
  23. }
  24. #endif
  25. /*
  26. * Sign aware variation of the above. On some architectures a
  27. * negative dividend leads to an divide overflow exception, which
  28. * is avoided by the sign check.
  29. */
  30. static inline long div_long_long_rem_signed(const long long dividend,
  31. const long divisor, long *remainder)
  32. {
  33. long res;
  34. if (unlikely(dividend < 0)) {
  35. res = -div_long_long_rem(-dividend, divisor, remainder);
  36. *remainder = -(*remainder);
  37. } else
  38. res = div_long_long_rem(dividend, divisor, remainder);
  39. return res;
  40. }
  41. #endif