div64.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2000, 2004, 2021 Maciej W. Rozycki
  3. * Copyright (C) 2003, 07 Ralf Baechle ([email protected])
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. */
  9. #ifndef __ASM_DIV64_H
  10. #define __ASM_DIV64_H
  11. #include <asm/bitsperlong.h>
  12. #if BITS_PER_LONG == 32
  13. /*
  14. * No traps on overflows for any of these...
  15. */
  16. #define do_div64_32(res, high, low, base) ({ \
  17. unsigned long __cf, __tmp, __tmp2, __i; \
  18. unsigned long __quot32, __mod32; \
  19. \
  20. __asm__( \
  21. " .set push \n" \
  22. " .set noat \n" \
  23. " .set noreorder \n" \
  24. " move %2, $0 \n" \
  25. " move %3, $0 \n" \
  26. " b 1f \n" \
  27. " li %4, 0x21 \n" \
  28. "0: \n" \
  29. " sll $1, %0, 0x1 \n" \
  30. " srl %3, %0, 0x1f \n" \
  31. " or %0, $1, %5 \n" \
  32. " sll %1, %1, 0x1 \n" \
  33. " sll %2, %2, 0x1 \n" \
  34. "1: \n" \
  35. " bnez %3, 2f \n" \
  36. " sltu %5, %0, %z6 \n" \
  37. " bnez %5, 3f \n" \
  38. "2: \n" \
  39. " addiu %4, %4, -1 \n" \
  40. " subu %0, %0, %z6 \n" \
  41. " addiu %2, %2, 1 \n" \
  42. "3: \n" \
  43. " bnez %4, 0b \n" \
  44. " srl %5, %1, 0x1f \n" \
  45. " .set pop" \
  46. : "=&r" (__mod32), "=&r" (__tmp), \
  47. "=&r" (__quot32), "=&r" (__cf), \
  48. "=&r" (__i), "=&r" (__tmp2) \
  49. : "Jr" (base), "0" (high), "1" (low)); \
  50. \
  51. (res) = __quot32; \
  52. __mod32; \
  53. })
  54. #define __div64_32(n, base) ({ \
  55. unsigned long __upper, __low, __high, __radix; \
  56. unsigned long long __quot; \
  57. unsigned long long __div; \
  58. unsigned long __mod; \
  59. \
  60. __div = (*n); \
  61. __radix = (base); \
  62. \
  63. __high = __div >> 32; \
  64. __low = __div; \
  65. \
  66. if (__high < __radix) { \
  67. __upper = __high; \
  68. __high = 0; \
  69. } else { \
  70. __upper = __high % __radix; \
  71. __high /= __radix; \
  72. } \
  73. \
  74. __mod = do_div64_32(__low, __upper, __low, __radix); \
  75. \
  76. __quot = __high; \
  77. __quot = __quot << 32 | __low; \
  78. (*n) = __quot; \
  79. __mod; \
  80. })
  81. #endif /* BITS_PER_LONG == 32 */
  82. #include <asm-generic/div64.h>
  83. #endif /* __ASM_DIV64_H */