udivmodti4.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* ===-- udivmodti4.c - Implement __udivmodti4 -----------------------------===
  2. *
  3. * The LLVM Compiler Infrastructure
  4. *
  5. * This file is dual licensed under the MIT and the University of Illinois Open
  6. * Source Licenses. See LICENSE.TXT for details.
  7. *
  8. * ===----------------------------------------------------------------------===
  9. *
  10. * This file implements __udivmodti4 for the compiler_rt library.
  11. *
  12. * ===----------------------------------------------------------------------===
  13. */
  14. #include <limits.h>
  15. typedef long long di_int;
  16. typedef unsigned long long du_int;
  17. typedef int si_int;
  18. typedef unsigned int su_int;
  19. typedef int ti_int __attribute__((mode (TI)));
  20. typedef unsigned int tu_int __attribute__((mode (TI)));
  21. typedef union
  22. {
  23. ti_int all;
  24. struct
  25. {
  26. du_int low;
  27. di_int high;
  28. }s;
  29. } twords;
  30. typedef union
  31. {
  32. tu_int all;
  33. struct
  34. {
  35. du_int low;
  36. du_int high;
  37. }s;
  38. } utwords;
  39. /* Effects: if rem != 0, *rem = a % b
  40. * Returns: a / b
  41. */
  42. /* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */
  43. tu_int
  44. __udivmodti4(tu_int a, tu_int b, tu_int* rem)
  45. {
  46. const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT;
  47. const unsigned n_utword_bits = sizeof(tu_int) * CHAR_BIT;
  48. utwords n;
  49. n.all = a;
  50. utwords d;
  51. d.all = b;
  52. utwords q;
  53. utwords r;
  54. unsigned sr;
  55. /* special cases, X is unknown, K != 0 */
  56. if (n.s.high == 0)
  57. {
  58. if (d.s.high == 0)
  59. {
  60. /* 0 X
  61. * ---
  62. * 0 X
  63. */
  64. if (rem)
  65. *rem = n.s.low % d.s.low;
  66. return n.s.low / d.s.low;
  67. }
  68. /* 0 X
  69. * ---
  70. * K X
  71. */
  72. if (rem)
  73. *rem = n.s.low;
  74. return 0;
  75. }
  76. /* n.s.high != 0 */
  77. if (d.s.low == 0)
  78. {
  79. if (d.s.high == 0)
  80. {
  81. /* K X
  82. * ---
  83. * 0 0
  84. */
  85. if (rem)
  86. *rem = n.s.high % d.s.low;
  87. return n.s.high / d.s.low;
  88. }
  89. /* d.s.high != 0 */
  90. if (n.s.low == 0)
  91. {
  92. /* K 0
  93. * ---
  94. * K 0
  95. */
  96. if (rem)
  97. {
  98. r.s.high = n.s.high % d.s.high;
  99. r.s.low = 0;
  100. *rem = r.all;
  101. }
  102. return n.s.high / d.s.high;
  103. }
  104. /* K K
  105. * ---
  106. * K 0
  107. */
  108. if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */
  109. {
  110. if (rem)
  111. {
  112. r.s.low = n.s.low;
  113. r.s.high = n.s.high & (d.s.high - 1);
  114. *rem = r.all;
  115. }
  116. return n.s.high >> __builtin_ctzll(d.s.high);
  117. }
  118. /* K K
  119. * ---
  120. * K 0
  121. */
  122. sr = __builtin_clzll(d.s.high) - __builtin_clzll(n.s.high);
  123. /* 0 <= sr <= n_udword_bits - 2 or sr large */
  124. if (sr > n_udword_bits - 2)
  125. {
  126. if (rem)
  127. *rem = n.all;
  128. return 0;
  129. }
  130. ++sr;
  131. /* 1 <= sr <= n_udword_bits - 1 */
  132. /* q.all = n.all << (n_utword_bits - sr); */
  133. q.s.low = 0;
  134. q.s.high = n.s.low << (n_udword_bits - sr);
  135. /* r.all = n.all >> sr; */
  136. r.s.high = n.s.high >> sr;
  137. r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
  138. }
  139. else /* d.s.low != 0 */
  140. {
  141. if (d.s.high == 0)
  142. {
  143. /* K X
  144. * ---
  145. * 0 K
  146. */
  147. if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */
  148. {
  149. if (rem)
  150. *rem = n.s.low & (d.s.low - 1);
  151. if (d.s.low == 1)
  152. return n.all;
  153. sr = __builtin_ctzll(d.s.low);
  154. q.s.high = n.s.high >> sr;
  155. q.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
  156. return q.all;
  157. }
  158. /* K X
  159. * ---
  160. * 0 K
  161. */
  162. sr = 1 + n_udword_bits + __builtin_clzll(d.s.low)
  163. - __builtin_clzll(n.s.high);
  164. /* 2 <= sr <= n_utword_bits - 1
  165. * q.all = n.all << (n_utword_bits - sr);
  166. * r.all = n.all >> sr;
  167. * if (sr == n_udword_bits)
  168. * {
  169. * q.s.low = 0;
  170. * q.s.high = n.s.low;
  171. * r.s.high = 0;
  172. * r.s.low = n.s.high;
  173. * }
  174. * else if (sr < n_udword_bits) // 2 <= sr <= n_udword_bits - 1
  175. * {
  176. * q.s.low = 0;
  177. * q.s.high = n.s.low << (n_udword_bits - sr);
  178. * r.s.high = n.s.high >> sr;
  179. * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
  180. * }
  181. * else // n_udword_bits + 1 <= sr <= n_utword_bits - 1
  182. * {
  183. * q.s.low = n.s.low << (n_utword_bits - sr);
  184. * q.s.high = (n.s.high << (n_utword_bits - sr)) |
  185. * (n.s.low >> (sr - n_udword_bits));
  186. * r.s.high = 0;
  187. * r.s.low = n.s.high >> (sr - n_udword_bits);
  188. * }
  189. */
  190. q.s.low = (n.s.low << (n_utword_bits - sr)) &
  191. ((di_int)(int)(n_udword_bits - sr) >> (n_udword_bits-1));
  192. q.s.high = ((n.s.low << ( n_udword_bits - sr)) &
  193. ((di_int)(int)(sr - n_udword_bits - 1) >> (n_udword_bits-1))) |
  194. (((n.s.high << (n_utword_bits - sr)) |
  195. (n.s.low >> (sr - n_udword_bits))) &
  196. ((di_int)(int)(n_udword_bits - sr) >> (n_udword_bits-1)));
  197. r.s.high = (n.s.high >> sr) &
  198. ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1));
  199. r.s.low = ((n.s.high >> (sr - n_udword_bits)) &
  200. ((di_int)(int)(n_udword_bits - sr - 1) >> (n_udword_bits-1))) |
  201. (((n.s.high << (n_udword_bits - sr)) |
  202. (n.s.low >> sr)) &
  203. ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1)));
  204. }
  205. else
  206. {
  207. /* K X
  208. * ---
  209. * K K
  210. */
  211. sr = __builtin_clzll(d.s.high) - __builtin_clzll(n.s.high);
  212. /*0 <= sr <= n_udword_bits - 1 or sr large */
  213. if (sr > n_udword_bits - 1)
  214. {
  215. if (rem)
  216. *rem = n.all;
  217. return 0;
  218. }
  219. ++sr;
  220. /* 1 <= sr <= n_udword_bits */
  221. /* q.all = n.all << (n_utword_bits - sr); */
  222. q.s.low = 0;
  223. q.s.high = n.s.low << (n_udword_bits - sr);
  224. /* r.all = n.all >> sr;
  225. * if (sr < n_udword_bits)
  226. * {
  227. * r.s.high = n.s.high >> sr;
  228. * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
  229. * }
  230. * else
  231. * {
  232. * r.s.high = 0;
  233. * r.s.low = n.s.high;
  234. * }
  235. */
  236. r.s.high = (n.s.high >> sr) &
  237. ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1));
  238. r.s.low = (n.s.high << (n_udword_bits - sr)) |
  239. ((n.s.low >> sr) &
  240. ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1)));
  241. }
  242. }
  243. /* Not a special case
  244. * q and r are initialized with:
  245. * q.all = n.all << (n_utword_bits - sr);
  246. * r.all = n.all >> sr;
  247. * 1 <= sr <= n_utword_bits - 1
  248. */
  249. su_int carry = 0;
  250. for (; sr > 0; --sr)
  251. {
  252. /* r:q = ((r:q) << 1) | carry */
  253. r.s.high = (r.s.high << 1) | (r.s.low >> (n_udword_bits - 1));
  254. r.s.low = (r.s.low << 1) | (q.s.high >> (n_udword_bits - 1));
  255. q.s.high = (q.s.high << 1) | (q.s.low >> (n_udword_bits - 1));
  256. q.s.low = (q.s.low << 1) | carry;
  257. /* carry = 0;
  258. * if (r.all >= d.all)
  259. * {
  260. * r.all -= d.all;
  261. * carry = 1;
  262. * }
  263. */
  264. const ti_int s = (ti_int)(d.all - r.all - 1) >> (n_utword_bits - 1);
  265. carry = s & 1;
  266. r.all -= d.all & s;
  267. }
  268. q.all = (q.all << 1) | carry;
  269. if (rem)
  270. *rem = r.all;
  271. return q.all;
  272. }
  273. /* Returns: a / b */
  274. tu_int
  275. __udivti3(tu_int a, tu_int b)
  276. {
  277. return __udivmodti4(a, b, 0);
  278. }