div64.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #ifndef __ASM_ARM_DIV64
  2. #define __ASM_ARM_DIV64
  3. #include <asm/system.h>
  4. /*
  5. * The semantics of do_div() are:
  6. *
  7. * uint32_t do_div(uint64_t *n, uint32_t base)
  8. * {
  9. * uint32_t remainder = *n % base;
  10. * *n = *n / base;
  11. * return remainder;
  12. * }
  13. *
  14. * In other words, a 64-bit dividend with a 32-bit divisor producing
  15. * a 64-bit result and a 32-bit remainder. To accomplish this optimally
  16. * we call a special __do_div64 helper with completely non standard
  17. * calling convention for arguments and results (beware).
  18. */
  19. #ifdef __ARMEB__
  20. #define __xh "r0"
  21. #define __xl "r1"
  22. #else
  23. #define __xl "r0"
  24. #define __xh "r1"
  25. #endif
  26. #define __do_div_asm(n, base) \
  27. ({ \
  28. register unsigned int __base asm("r4") = base; \
  29. register unsigned long long __n asm("r0") = n; \
  30. register unsigned long long __res asm("r2"); \
  31. register unsigned int __rem asm(__xh); \
  32. asm( __asmeq("%0", __xh) \
  33. __asmeq("%1", "r2") \
  34. __asmeq("%2", "r0") \
  35. __asmeq("%3", "r4") \
  36. "bl __do_div64" \
  37. : "=r" (__rem), "=r" (__res) \
  38. : "r" (__n), "r" (__base) \
  39. : "ip", "lr", "cc"); \
  40. n = __res; \
  41. __rem; \
  42. })
  43. #if __GNUC__ < 4
  44. /*
  45. * gcc versions earlier than 4.0 are simply too problematic for the
  46. * optimized implementation below. First there is gcc PR 15089 that
  47. * tend to trig on more complex constructs, spurious .global __udivsi3
  48. * are inserted even if none of those symbols are referenced in the
  49. * generated code, and those gcc versions are not able to do constant
  50. * propagation on long long values anyway.
  51. */
  52. #define do_div(n, base) __do_div_asm(n, base)
  53. #elif __GNUC__ >= 4
  54. #include <asm/bug.h>
  55. /*
  56. * If the divisor happens to be constant, we determine the appropriate
  57. * inverse at compile time to turn the division into a few inline
  58. * multiplications instead which is much faster. And yet only if compiling
  59. * for ARMv4 or higher (we need umull/umlal) and if the gcc version is
  60. * sufficiently recent to perform proper long long constant propagation.
  61. * (It is unfortunate that gcc doesn't perform all this internally.)
  62. */
  63. #define do_div(n, base) \
  64. ({ \
  65. unsigned int __r, __b = (base); \
  66. if (!__builtin_constant_p(__b) || __b == 0 || \
  67. (__LINUX_ARM_ARCH__ < 4 && (__b & (__b - 1)) != 0)) { \
  68. /* non-constant divisor (or zero): slow path */ \
  69. __r = __do_div_asm(n, __b); \
  70. } else if ((__b & (__b - 1)) == 0) { \
  71. /* Trivial: __b is constant and a power of 2 */ \
  72. /* gcc does the right thing with this code. */ \
  73. __r = n; \
  74. __r &= (__b - 1); \
  75. n /= __b; \
  76. } else { \
  77. /* Multiply by inverse of __b: n/b = n*(p/b)/p */ \
  78. /* We rely on the fact that most of this code gets */ \
  79. /* optimized away at compile time due to constant */ \
  80. /* propagation and only a couple inline assembly */ \
  81. /* instructions should remain. Better avoid any */ \
  82. /* code construct that might prevent that. */ \
  83. unsigned long long __res, __x, __t, __m, __n = n; \
  84. unsigned int __c, __p, __z = 0; \
  85. /* preserve low part of n for reminder computation */ \
  86. __r = __n; \
  87. /* determine number of bits to represent __b */ \
  88. __p = 1 << __div64_fls(__b); \
  89. /* compute __m = ((__p << 64) + __b - 1) / __b */ \
  90. __m = (~0ULL / __b) * __p; \
  91. __m += (((~0ULL % __b + 1) * __p) + __b - 1) / __b; \
  92. /* compute __res = __m*(~0ULL/__b*__b-1)/(__p << 64) */ \
  93. __x = ~0ULL / __b * __b - 1; \
  94. __res = (__m & 0xffffffff) * (__x & 0xffffffff); \
  95. __res >>= 32; \
  96. __res += (__m & 0xffffffff) * (__x >> 32); \
  97. __t = __res; \
  98. __res += (__x & 0xffffffff) * (__m >> 32); \
  99. __t = (__res < __t) ? (1ULL << 32) : 0; \
  100. __res = (__res >> 32) + __t; \
  101. __res += (__m >> 32) * (__x >> 32); \
  102. __res /= __p; \
  103. /* Now sanitize and optimize what we've got. */ \
  104. if (~0ULL % (__b / (__b & -__b)) == 0) { \
  105. /* those cases can be simplified with: */ \
  106. __n /= (__b & -__b); \
  107. __m = ~0ULL / (__b / (__b & -__b)); \
  108. __p = 1; \
  109. __c = 1; \
  110. } else if (__res != __x / __b) { \
  111. /* We can't get away without a correction */ \
  112. /* to compensate for bit truncation errors. */ \
  113. /* To avoid it we'd need an additional bit */ \
  114. /* to represent __m which would overflow it. */ \
  115. /* Instead we do m=p/b and n/b=(n*m+m)/p. */ \
  116. __c = 1; \
  117. /* Compute __m = (__p << 64) / __b */ \
  118. __m = (~0ULL / __b) * __p; \
  119. __m += ((~0ULL % __b + 1) * __p) / __b; \
  120. } else { \
  121. /* Reduce __m/__p, and try to clear bit 31 */ \
  122. /* of __m when possible otherwise that'll */ \
  123. /* need extra overflow handling later. */ \
  124. unsigned int __bits = -(__m & -__m); \
  125. __bits |= __m >> 32; \
  126. __bits = (~__bits) << 1; \
  127. /* If __bits == 0 then setting bit 31 is */ \
  128. /* unavoidable. Simply apply the maximum */ \
  129. /* possible reduction in that case. */ \
  130. /* Otherwise the MSB of __bits indicates the */ \
  131. /* best reduction we should apply. */ \
  132. if (!__bits) { \
  133. __p /= (__m & -__m); \
  134. __m /= (__m & -__m); \
  135. } else { \
  136. __p >>= __div64_fls(__bits); \
  137. __m >>= __div64_fls(__bits); \
  138. } \
  139. /* No correction needed. */ \
  140. __c = 0; \
  141. } \
  142. /* Now we have a combination of 2 conditions: */ \
  143. /* 1) whether or not we need a correction (__c), and */ \
  144. /* 2) whether or not there might be an overflow in */ \
  145. /* the cross product (__m & ((1<<63) | (1<<31))) */ \
  146. /* Select the best insn combination to perform the */ \
  147. /* actual __m * __n / (__p << 64) operation. */ \
  148. if (!__c) { \
  149. asm ( "umull %Q0, %R0, %1, %Q2\n\t" \
  150. "mov %Q0, #0" \
  151. : "=&r" (__res) \
  152. : "r" (__m), "r" (__n) \
  153. : "cc" ); \
  154. } else if (!(__m & ((1ULL << 63) | (1ULL << 31)))) { \
  155. __res = __m; \
  156. asm ( "umlal %Q0, %R0, %Q1, %Q2\n\t" \
  157. "mov %Q0, #0" \
  158. : "+r" (__res) \
  159. : "r" (__m), "r" (__n) \
  160. : "cc" ); \
  161. } else { \
  162. asm ( "umull %Q0, %R0, %Q1, %Q2\n\t" \
  163. "cmn %Q0, %Q1\n\t" \
  164. "adcs %R0, %R0, %R1\n\t" \
  165. "adc %Q0, %3, #0" \
  166. : "=&r" (__res) \
  167. : "r" (__m), "r" (__n), "r" (__z) \
  168. : "cc" ); \
  169. } \
  170. if (!(__m & ((1ULL << 63) | (1ULL << 31)))) { \
  171. asm ( "umlal %R0, %Q0, %R1, %Q2\n\t" \
  172. "umlal %R0, %Q0, %Q1, %R2\n\t" \
  173. "mov %R0, #0\n\t" \
  174. "umlal %Q0, %R0, %R1, %R2" \
  175. : "+r" (__res) \
  176. : "r" (__m), "r" (__n) \
  177. : "cc" ); \
  178. } else { \
  179. asm ( "umlal %R0, %Q0, %R2, %Q3\n\t" \
  180. "umlal %R0, %1, %Q2, %R3\n\t" \
  181. "mov %R0, #0\n\t" \
  182. "adds %Q0, %1, %Q0\n\t" \
  183. "adc %R0, %R0, #0\n\t" \
  184. "umlal %Q0, %R0, %R2, %R3" \
  185. : "+r" (__res), "+r" (__z) \
  186. : "r" (__m), "r" (__n) \
  187. : "cc" ); \
  188. } \
  189. __res /= __p; \
  190. /* The reminder can be computed with 32-bit regs */ \
  191. /* only, and gcc is good at that. */ \
  192. { \
  193. unsigned int __res0 = __res; \
  194. unsigned int __b0 = __b; \
  195. __r -= __res0 * __b0; \
  196. } \
  197. /* BUG_ON(__r >= __b || __res * __b + __r != n); */ \
  198. n = __res; \
  199. } \
  200. __r; \
  201. })
  202. /* our own fls implementation to make sure constant propagation is fine */
  203. #define __div64_fls(bits) \
  204. ({ \
  205. unsigned int __left = (bits), __nr = 0; \
  206. if (__left & 0xffff0000) __nr += 16, __left >>= 16; \
  207. if (__left & 0x0000ff00) __nr += 8, __left >>= 8; \
  208. if (__left & 0x000000f0) __nr += 4, __left >>= 4; \
  209. if (__left & 0x0000000c) __nr += 2, __left >>= 2; \
  210. if (__left & 0x00000002) __nr += 1; \
  211. __nr; \
  212. })
  213. #endif
  214. #endif