udiv.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* This file is generated from divrem.m4; DO NOT EDIT! */
  2. /*
  3. * Division and remainder, from Appendix E of the Sparc Version 8
  4. * Architecture Manual, with fixes from Gordon Irlam.
  5. */
  6. /*
  7. * Input: dividend and divisor in %o0 and %o1 respectively.
  8. *
  9. * m4 parameters:
  10. * .udiv name of function to generate
  11. * div div=div => %o0 / %o1; div=rem => %o0 % %o1
  12. * false false=true => signed; false=false => unsigned
  13. *
  14. * Algorithm parameters:
  15. * N how many bits per iteration we try to get (4)
  16. * WORDSIZE total number of bits (32)
  17. *
  18. * Derived constants:
  19. * TOPBITS number of bits in the top decade of a number
  20. *
  21. * Important variables:
  22. * Q the partial quotient under development (initially 0)
  23. * R the remainder so far, initially the dividend
  24. * ITER number of main division loop iterations required;
  25. * equal to ceil(log2(quotient) / N). Note that this
  26. * is the log base (2^N) of the quotient.
  27. * V the current comparand, initially divisor*2^(ITER*N-1)
  28. *
  29. * Cost:
  30. * Current estimate for non-large dividend is
  31. * ceil(log2(quotient) / N) * (10 + 7N/2) + C
  32. * A large dividend is one greater than 2^(31-TOPBITS) and takes a
  33. * different path, as the upper bits of the quotient must be developed
  34. * one bit at a time.
  35. */
  36. #include "DEFS.h"
  37. #include <asm/traps.h>
  38. FUNC(_dl_udiv)
  39. ! Ready to divide. Compute size of quotient; scale comparand.
  40. orcc %o1, %g0, %o5
  41. bne 1f
  42. mov %o0, %o3
  43. ! Divide by zero trap. If it returns, return 0 (about as
  44. ! wrong as possible, but that is what SunOS does...).
  45. ta ST_DIV0
  46. retl
  47. clr %o0
  48. 1:
  49. cmp %o3, %o5 ! if %o1 exceeds %o0, done
  50. blu Lgot_result ! (and algorithm fails otherwise)
  51. clr %o2
  52. sethi %hi(1 << (32 - 4 - 1)), %g1
  53. cmp %o3, %g1
  54. blu Lnot_really_big
  55. clr %o4
  56. ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
  57. ! as our usual N-at-a-shot divide step will cause overflow and havoc.
  58. ! The number of bits in the result here is N*ITER+SC, where SC <= N.
  59. ! Compute ITER in an unorthodox manner: know we need to shift V into
  60. ! the top decade: so do not even bother to compare to R.
  61. 1:
  62. cmp %o5, %g1
  63. bgeu 3f
  64. mov 1, %g7
  65. sll %o5, 4, %o5
  66. b 1b
  67. add %o4, 1, %o4
  68. ! Now compute %g7.
  69. 2: addcc %o5, %o5, %o5
  70. bcc Lnot_too_big
  71. add %g7, 1, %g7
  72. ! We get here if the %o1 overflowed while shifting.
  73. ! This means that %o3 has the high-order bit set.
  74. ! Restore %o5 and subtract from %o3.
  75. sll %g1, 4, %g1 ! high order bit
  76. srl %o5, 1, %o5 ! rest of %o5
  77. add %o5, %g1, %o5
  78. b Ldo_single_div
  79. sub %g7, 1, %g7
  80. Lnot_too_big:
  81. 3: cmp %o5, %o3
  82. blu 2b
  83. nop
  84. be Ldo_single_div
  85. nop
  86. /* NB: these are commented out in the V8-Sparc manual as well */
  87. /* (I do not understand this) */
  88. ! %o5 > %o3: went too far: back up 1 step
  89. ! srl %o5, 1, %o5
  90. ! dec %g7
  91. ! do single-bit divide steps
  92. !
  93. ! We have to be careful here. We know that %o3 >= %o5, so we can do the
  94. ! first divide step without thinking. BUT, the others are conditional,
  95. ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
  96. ! order bit set in the first step, just falling into the regular
  97. ! division loop will mess up the first time around.
  98. ! So we unroll slightly...
  99. Ldo_single_div:
  100. subcc %g7, 1, %g7
  101. bl Lend_regular_divide
  102. nop
  103. sub %o3, %o5, %o3
  104. mov 1, %o2
  105. b Lend_single_divloop
  106. nop
  107. Lsingle_divloop:
  108. sll %o2, 1, %o2
  109. bl 1f
  110. srl %o5, 1, %o5
  111. ! %o3 >= 0
  112. sub %o3, %o5, %o3
  113. b 2f
  114. add %o2, 1, %o2
  115. 1: ! %o3 < 0
  116. add %o3, %o5, %o3
  117. sub %o2, 1, %o2
  118. 2:
  119. Lend_single_divloop:
  120. subcc %g7, 1, %g7
  121. bge Lsingle_divloop
  122. tst %o3
  123. b,a Lend_regular_divide
  124. Lnot_really_big:
  125. 1:
  126. sll %o5, 4, %o5
  127. cmp %o5, %o3
  128. bleu 1b
  129. addcc %o4, 1, %o4
  130. be Lgot_result
  131. sub %o4, 1, %o4
  132. tst %o3 ! set up for initial iteration
  133. Ldivloop:
  134. sll %o2, 4, %o2
  135. ! depth 1, accumulated bits 0
  136. bl L.1.16
  137. srl %o5,1,%o5
  138. ! remainder is positive
  139. subcc %o3,%o5,%o3
  140. ! depth 2, accumulated bits 1
  141. bl L.2.17
  142. srl %o5,1,%o5
  143. ! remainder is positive
  144. subcc %o3,%o5,%o3
  145. ! depth 3, accumulated bits 3
  146. bl L.3.19
  147. srl %o5,1,%o5
  148. ! remainder is positive
  149. subcc %o3,%o5,%o3
  150. ! depth 4, accumulated bits 7
  151. bl L.4.23
  152. srl %o5,1,%o5
  153. ! remainder is positive
  154. subcc %o3,%o5,%o3
  155. b 9f
  156. add %o2, (7*2+1), %o2
  157. L.4.23:
  158. ! remainder is negative
  159. addcc %o3,%o5,%o3
  160. b 9f
  161. add %o2, (7*2-1), %o2
  162. L.3.19:
  163. ! remainder is negative
  164. addcc %o3,%o5,%o3
  165. ! depth 4, accumulated bits 5
  166. bl L.4.21
  167. srl %o5,1,%o5
  168. ! remainder is positive
  169. subcc %o3,%o5,%o3
  170. b 9f
  171. add %o2, (5*2+1), %o2
  172. L.4.21:
  173. ! remainder is negative
  174. addcc %o3,%o5,%o3
  175. b 9f
  176. add %o2, (5*2-1), %o2
  177. L.2.17:
  178. ! remainder is negative
  179. addcc %o3,%o5,%o3
  180. ! depth 3, accumulated bits 1
  181. bl L.3.17
  182. srl %o5,1,%o5
  183. ! remainder is positive
  184. subcc %o3,%o5,%o3
  185. ! depth 4, accumulated bits 3
  186. bl L.4.19
  187. srl %o5,1,%o5
  188. ! remainder is positive
  189. subcc %o3,%o5,%o3
  190. b 9f
  191. add %o2, (3*2+1), %o2
  192. L.4.19:
  193. ! remainder is negative
  194. addcc %o3,%o5,%o3
  195. b 9f
  196. add %o2, (3*2-1), %o2
  197. L.3.17:
  198. ! remainder is negative
  199. addcc %o3,%o5,%o3
  200. ! depth 4, accumulated bits 1
  201. bl L.4.17
  202. srl %o5,1,%o5
  203. ! remainder is positive
  204. subcc %o3,%o5,%o3
  205. b 9f
  206. add %o2, (1*2+1), %o2
  207. L.4.17:
  208. ! remainder is negative
  209. addcc %o3,%o5,%o3
  210. b 9f
  211. add %o2, (1*2-1), %o2
  212. L.1.16:
  213. ! remainder is negative
  214. addcc %o3,%o5,%o3
  215. ! depth 2, accumulated bits -1
  216. bl L.2.15
  217. srl %o5,1,%o5
  218. ! remainder is positive
  219. subcc %o3,%o5,%o3
  220. ! depth 3, accumulated bits -1
  221. bl L.3.15
  222. srl %o5,1,%o5
  223. ! remainder is positive
  224. subcc %o3,%o5,%o3
  225. ! depth 4, accumulated bits -1
  226. bl L.4.15
  227. srl %o5,1,%o5
  228. ! remainder is positive
  229. subcc %o3,%o5,%o3
  230. b 9f
  231. add %o2, (-1*2+1), %o2
  232. L.4.15:
  233. ! remainder is negative
  234. addcc %o3,%o5,%o3
  235. b 9f
  236. add %o2, (-1*2-1), %o2
  237. L.3.15:
  238. ! remainder is negative
  239. addcc %o3,%o5,%o3
  240. ! depth 4, accumulated bits -3
  241. bl L.4.13
  242. srl %o5,1,%o5
  243. ! remainder is positive
  244. subcc %o3,%o5,%o3
  245. b 9f
  246. add %o2, (-3*2+1), %o2
  247. L.4.13:
  248. ! remainder is negative
  249. addcc %o3,%o5,%o3
  250. b 9f
  251. add %o2, (-3*2-1), %o2
  252. L.2.15:
  253. ! remainder is negative
  254. addcc %o3,%o5,%o3
  255. ! depth 3, accumulated bits -3
  256. bl L.3.13
  257. srl %o5,1,%o5
  258. ! remainder is positive
  259. subcc %o3,%o5,%o3
  260. ! depth 4, accumulated bits -5
  261. bl L.4.11
  262. srl %o5,1,%o5
  263. ! remainder is positive
  264. subcc %o3,%o5,%o3
  265. b 9f
  266. add %o2, (-5*2+1), %o2
  267. L.4.11:
  268. ! remainder is negative
  269. addcc %o3,%o5,%o3
  270. b 9f
  271. add %o2, (-5*2-1), %o2
  272. L.3.13:
  273. ! remainder is negative
  274. addcc %o3,%o5,%o3
  275. ! depth 4, accumulated bits -7
  276. bl L.4.9
  277. srl %o5,1,%o5
  278. ! remainder is positive
  279. subcc %o3,%o5,%o3
  280. b 9f
  281. add %o2, (-7*2+1), %o2
  282. L.4.9:
  283. ! remainder is negative
  284. addcc %o3,%o5,%o3
  285. b 9f
  286. add %o2, (-7*2-1), %o2
  287. 9:
  288. Lend_regular_divide:
  289. subcc %o4, 1, %o4
  290. bge Ldivloop
  291. tst %o3
  292. bl,a Lgot_result
  293. ! non-restoring fixup here (one instruction only!)
  294. sub %o2, 1, %o2
  295. Lgot_result:
  296. retl
  297. mov %o2, %o0