div64.h 7.5 KB

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