softdivide.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* Division and remainder routines for Tile.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by Walter Lee (walt@tilera.com)
  4. This file is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. This file is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. typedef int int32_t;
  20. typedef unsigned uint32_t;
  21. typedef long long int64_t;
  22. typedef unsigned long long uint64_t;
  23. /* Raise signal 8 (SIGFPE) with code 1 (FPE_INTDIV). */
  24. static inline void
  25. raise_intdiv (void)
  26. {
  27. asm ("{ raise; moveli zero, 8 + (1 << 6) }");
  28. }
  29. #ifndef __tilegx__
  30. /*__udivsi3 - 32 bit integer unsigned divide */
  31. static inline uint32_t __attribute__ ((always_inline))
  32. __udivsi3_inline (uint32_t dividend, uint32_t divisor)
  33. {
  34. /* Divide out any power of two factor from dividend and divisor.
  35. Note that when dividing by zero the divisor will remain zero,
  36. which is all we need to detect that case below. */
  37. const int power_of_two_factor = __insn_ctz (divisor);
  38. divisor >>= power_of_two_factor;
  39. dividend >>= power_of_two_factor;
  40. /* Checks for division by power of two or division by zero. */
  41. if (divisor <= 1)
  42. {
  43. if (divisor == 0)
  44. {
  45. raise_intdiv ();
  46. return 0;
  47. }
  48. return dividend;
  49. }
  50. /* Compute (a / b) by repeatedly finding the largest N
  51. such that (b << N) <= a. For each such N, set bit N in the
  52. quotient, subtract (b << N) from a, and keep going. Think of this as
  53. the reverse of the "shift-and-add" that a multiply does. The values
  54. of N are precisely those shift counts.
  55. Finding N is easy. First, use clz(b) - clz(a) to find the N
  56. that lines up the high bit of (b << N) with the high bit of a.
  57. Any larger value of N would definitely make (b << N) > a,
  58. which is too big.
  59. Then, if (b << N) > a (because it has larger low bits), decrement
  60. N by one. This adjustment will definitely make (b << N) less
  61. than a, because a's high bit is now one higher than b's. */
  62. /* Precomputing the max_ values allows us to avoid a subtract
  63. in the inner loop and just right shift by clz(remainder). */
  64. const int divisor_clz = __insn_clz (divisor);
  65. const uint32_t max_divisor = divisor << divisor_clz;
  66. const uint32_t max_qbit = 1 << divisor_clz;
  67. uint32_t quotient = 0;
  68. uint32_t remainder = dividend;
  69. while (remainder >= divisor)
  70. {
  71. int shift = __insn_clz (remainder);
  72. uint32_t scaled_divisor = max_divisor >> shift;
  73. uint32_t quotient_bit = max_qbit >> shift;
  74. int too_big = (scaled_divisor > remainder);
  75. scaled_divisor >>= too_big;
  76. quotient_bit >>= too_big;
  77. remainder -= scaled_divisor;
  78. quotient |= quotient_bit;
  79. }
  80. return quotient;
  81. }
  82. #endif /* !__tilegx__ */
  83. /* __udivdi3 - 64 bit integer unsigned divide */
  84. static inline uint64_t __attribute__ ((always_inline))
  85. __udivdi3_inline (uint64_t dividend, uint64_t divisor)
  86. {
  87. /* Divide out any power of two factor from dividend and divisor.
  88. Note that when dividing by zero the divisor will remain zero,
  89. which is all we need to detect that case below. */
  90. const int power_of_two_factor = __builtin_ctzll (divisor);
  91. divisor >>= power_of_two_factor;
  92. dividend >>= power_of_two_factor;
  93. /* Checks for division by power of two or division by zero. */
  94. if (divisor <= 1)
  95. {
  96. if (divisor == 0)
  97. {
  98. raise_intdiv ();
  99. return 0;
  100. }
  101. return dividend;
  102. }
  103. #ifndef __tilegx__
  104. if (((uint32_t) (dividend >> 32) | ((uint32_t) (divisor >> 32))) == 0)
  105. {
  106. /* Operands both fit in 32 bits, so use faster 32 bit algorithm. */
  107. return __udivsi3_inline ((uint32_t) dividend, (uint32_t) divisor);
  108. }
  109. #endif /* !__tilegx__ */
  110. /* See algorithm description in __udivsi3 */
  111. const int divisor_clz = __builtin_clzll (divisor);
  112. const uint64_t max_divisor = divisor << divisor_clz;
  113. const uint64_t max_qbit = 1ULL << divisor_clz;
  114. uint64_t quotient = 0;
  115. uint64_t remainder = dividend;
  116. while (remainder >= divisor)
  117. {
  118. int shift = __builtin_clzll (remainder);
  119. uint64_t scaled_divisor = max_divisor >> shift;
  120. uint64_t quotient_bit = max_qbit >> shift;
  121. int too_big = (scaled_divisor > remainder);
  122. scaled_divisor >>= too_big;
  123. quotient_bit >>= too_big;
  124. remainder -= scaled_divisor;
  125. quotient |= quotient_bit;
  126. }
  127. return quotient;
  128. }
  129. #ifndef __tilegx__
  130. /* __umodsi3 - 32 bit integer unsigned modulo */
  131. static inline uint32_t __attribute__ ((always_inline))
  132. __umodsi3_inline (uint32_t dividend, uint32_t divisor)
  133. {
  134. /* Shortcircuit mod by a power of two (and catch mod by zero). */
  135. const uint32_t mask = divisor - 1;
  136. if ((divisor & mask) == 0)
  137. {
  138. if (divisor == 0)
  139. {
  140. raise_intdiv ();
  141. return 0;
  142. }
  143. return dividend & mask;
  144. }
  145. /* We compute the remainder (a % b) by repeatedly subtracting off
  146. multiples of b from a until a < b. The key is that subtracting
  147. off a multiple of b does not affect the result mod b.
  148. To make the algorithm run efficiently, we need to subtract
  149. off a large multiple of b at each step. We subtract the largest
  150. (b << N) that is <= a.
  151. Finding N is easy. First, use clz(b) - clz(a) to find the N
  152. that lines up the high bit of (b << N) with the high bit of a.
  153. Any larger value of N would definitely make (b << N) > a,
  154. which is too big.
  155. Then, if (b << N) > a (because it has larger low bits), decrement
  156. N by one. This adjustment will definitely make (b << N) less
  157. than a, because a's high bit is now one higher than b's. */
  158. const uint32_t max_divisor = divisor << __insn_clz (divisor);
  159. uint32_t remainder = dividend;
  160. while (remainder >= divisor)
  161. {
  162. const int shift = __insn_clz (remainder);
  163. uint32_t scaled_divisor = max_divisor >> shift;
  164. scaled_divisor >>= (scaled_divisor > remainder);
  165. remainder -= scaled_divisor;
  166. }
  167. return remainder;
  168. }
  169. #endif /* !__tilegx__ */
  170. /* __umoddi3 - 64 bit integer unsigned modulo */
  171. static inline uint64_t __attribute__ ((always_inline))
  172. __umoddi3_inline (uint64_t dividend, uint64_t divisor)
  173. {
  174. #ifndef __tilegx__
  175. if (((uint32_t) (dividend >> 32) | ((uint32_t) (divisor >> 32))) == 0)
  176. {
  177. /* Operands both fit in 32 bits, so use faster 32 bit algorithm. */
  178. return __umodsi3_inline ((uint32_t) dividend, (uint32_t) divisor);
  179. }
  180. #endif /* !__tilegx__ */
  181. /* Shortcircuit mod by a power of two (and catch mod by zero). */
  182. const uint64_t mask = divisor - 1;
  183. if ((divisor & mask) == 0)
  184. {
  185. if (divisor == 0)
  186. {
  187. raise_intdiv ();
  188. return 0;
  189. }
  190. return dividend & mask;
  191. }
  192. /* See algorithm description in __umodsi3 */
  193. const uint64_t max_divisor = divisor << __builtin_clzll (divisor);
  194. uint64_t remainder = dividend;
  195. while (remainder >= divisor)
  196. {
  197. const int shift = __builtin_clzll (remainder);
  198. uint64_t scaled_divisor = max_divisor >> shift;
  199. scaled_divisor >>= (scaled_divisor > remainder);
  200. remainder -= scaled_divisor;
  201. }
  202. return remainder;
  203. }
  204. uint32_t __udivsi3 (uint32_t dividend, uint32_t divisor);
  205. #ifdef L_tile_udivsi3
  206. uint32_t
  207. __udivsi3 (uint32_t dividend, uint32_t divisor)
  208. {
  209. #ifndef __tilegx__
  210. return __udivsi3_inline (dividend, divisor);
  211. #else /* !__tilegx__ */
  212. uint64_t n = __udivdi3_inline (((uint64_t) dividend), ((uint64_t) divisor));
  213. return (uint32_t) n;
  214. #endif /* !__tilegx__ */
  215. }
  216. #endif
  217. #define ABS(x) ((x) >= 0 ? (x) : -(x))
  218. int32_t __divsi3 (int32_t dividend, int32_t divisor);
  219. #ifdef L_tile_divsi3
  220. /* __divsi3 - 32 bit integer signed divide */
  221. int32_t
  222. __divsi3 (int32_t dividend, int32_t divisor)
  223. {
  224. #ifndef __tilegx__
  225. uint32_t n = __udivsi3_inline (ABS (dividend), ABS (divisor));
  226. #else /* !__tilegx__ */
  227. uint64_t n =
  228. __udivdi3_inline (ABS ((int64_t) dividend), ABS ((int64_t) divisor));
  229. #endif /* !__tilegx__ */
  230. if ((dividend ^ divisor) < 0)
  231. n = -n;
  232. return (int32_t) n;
  233. }
  234. #endif
  235. uint64_t __udivdi3 (uint64_t dividend, uint64_t divisor);
  236. #ifdef L_tile_udivdi3
  237. uint64_t
  238. __udivdi3 (uint64_t dividend, uint64_t divisor)
  239. {
  240. return __udivdi3_inline (dividend, divisor);
  241. }
  242. #endif
  243. /*__divdi3 - 64 bit integer signed divide */
  244. int64_t __divdi3 (int64_t dividend, int64_t divisor);
  245. #ifdef L_tile_divdi3
  246. int64_t
  247. __divdi3 (int64_t dividend, int64_t divisor)
  248. {
  249. uint64_t n = __udivdi3_inline (ABS (dividend), ABS (divisor));
  250. if ((dividend ^ divisor) < 0)
  251. n = -n;
  252. return (int64_t) n;
  253. }
  254. #endif
  255. uint32_t __umodsi3 (uint32_t dividend, uint32_t divisor);
  256. #ifdef L_tile_umodsi3
  257. uint32_t
  258. __umodsi3 (uint32_t dividend, uint32_t divisor)
  259. {
  260. #ifndef __tilegx__
  261. return __umodsi3_inline (dividend, divisor);
  262. #else /* !__tilegx__ */
  263. return __umoddi3_inline ((uint64_t) dividend, (uint64_t) divisor);
  264. #endif /* !__tilegx__ */
  265. }
  266. #endif
  267. /* __modsi3 - 32 bit integer signed modulo */
  268. int32_t __modsi3 (int32_t dividend, int32_t divisor);
  269. #ifdef L_tile_modsi3
  270. int32_t
  271. __modsi3 (int32_t dividend, int32_t divisor)
  272. {
  273. #ifndef __tilegx__
  274. uint32_t remainder = __umodsi3_inline (ABS (dividend), ABS (divisor));
  275. #else /* !__tilegx__ */
  276. uint64_t remainder =
  277. __umoddi3_inline (ABS ((int64_t) dividend), ABS ((int64_t) divisor));
  278. #endif /* !__tilegx__ */
  279. return (int32_t) ((dividend >= 0) ? remainder : -remainder);
  280. }
  281. #endif
  282. uint64_t __umoddi3 (uint64_t dividend, uint64_t divisor);
  283. #ifdef L_tile_umoddi3
  284. uint64_t
  285. __umoddi3 (uint64_t dividend, uint64_t divisor)
  286. {
  287. return __umoddi3_inline (dividend, divisor);
  288. }
  289. #endif
  290. /* __moddi3 - 64 bit integer signed modulo */
  291. int64_t __moddi3 (int64_t dividend, int64_t divisor);
  292. #ifdef L_tile_moddi3
  293. int64_t
  294. __moddi3 (int64_t dividend, int64_t divisor)
  295. {
  296. uint64_t remainder = __umoddi3_inline (ABS (dividend), ABS (divisor));
  297. return (int64_t) ((dividend >= 0) ? remainder : -remainder);
  298. }
  299. #endif