udiv.S 6.8 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. ENTRY(.udiv)
  37. ! Ready to divide. Compute size of quotient; scale comparand.
  38. orcc %o1, %g0, %o5
  39. bne 1f
  40. mov %o0, %o3
  41. ! Divide by zero trap. If it returns, return 0 (about as
  42. ! wrong as possible, but that is what SunOS does...).
  43. ta ST_DIV0
  44. retl
  45. clr %o0
  46. 1:
  47. cmp %o3, %o5 ! if %o1 exceeds %o0, done
  48. blu LOC(got_result) ! (and algorithm fails otherwise)
  49. clr %o2
  50. sethi %hi(1 << (32 - 4 - 1)), %g1
  51. cmp %o3, %g1
  52. blu LOC(not_really_big)
  53. clr %o4
  54. ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
  55. ! as our usual N-at-a-shot divide step will cause overflow and havoc.
  56. ! The number of bits in the result here is N*ITER+SC, where SC <= N.
  57. ! Compute ITER in an unorthodox manner: know we need to shift V into
  58. ! the top decade: so do not even bother to compare to R.
  59. 1:
  60. cmp %o5, %g1
  61. bgeu 3f
  62. mov 1, %g2
  63. sll %o5, 4, %o5
  64. b 1b
  65. add %o4, 1, %o4
  66. ! Now compute %g2.
  67. 2: addcc %o5, %o5, %o5
  68. bcc LOC(not_too_big)
  69. add %g2, 1, %g2
  70. ! We get here if the %o1 overflowed while shifting.
  71. ! This means that %o3 has the high-order bit set.
  72. ! Restore %o5 and subtract from %o3.
  73. sll %g1, 4, %g1 ! high order bit
  74. srl %o5, 1, %o5 ! rest of %o5
  75. add %o5, %g1, %o5
  76. b LOC(do_single_div)
  77. sub %g2, 1, %g2
  78. LOC(not_too_big):
  79. 3: cmp %o5, %o3
  80. blu 2b
  81. nop
  82. be LOC(do_single_div)
  83. nop
  84. /* NB: these are commented out in the V8-Sparc manual as well */
  85. /* (I do not understand this) */
  86. ! %o5 > %o3: went too far: back up 1 step
  87. ! srl %o5, 1, %o5
  88. ! dec %g2
  89. ! do single-bit divide steps
  90. !
  91. ! We have to be careful here. We know that %o3 >= %o5, so we can do the
  92. ! first divide step without thinking. BUT, the others are conditional,
  93. ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
  94. ! order bit set in the first step, just falling into the regular
  95. ! division loop will mess up the first time around.
  96. ! So we unroll slightly...
  97. LOC(do_single_div):
  98. subcc %g2, 1, %g2
  99. bl LOC(end_regular_divide)
  100. nop
  101. sub %o3, %o5, %o3
  102. mov 1, %o2
  103. b LOC(end_single_divloop)
  104. nop
  105. LOC(single_divloop):
  106. sll %o2, 1, %o2
  107. bl 1f
  108. srl %o5, 1, %o5
  109. ! %o3 >= 0
  110. sub %o3, %o5, %o3
  111. b 2f
  112. add %o2, 1, %o2
  113. 1: ! %o3 < 0
  114. add %o3, %o5, %o3
  115. sub %o2, 1, %o2
  116. 2:
  117. LOC(end_single_divloop):
  118. subcc %g2, 1, %g2
  119. bge LOC(single_divloop)
  120. tst %o3
  121. b,a LOC(end_regular_divide)
  122. LOC(not_really_big):
  123. 1:
  124. sll %o5, 4, %o5
  125. cmp %o5, %o3
  126. bleu 1b
  127. addcc %o4, 1, %o4
  128. be LOC(got_result)
  129. sub %o4, 1, %o4
  130. tst %o3 ! set up for initial iteration
  131. LOC(divloop):
  132. sll %o2, 4, %o2
  133. ! depth 1, accumulated bits 0
  134. bl LOC(1.16)
  135. srl %o5,1,%o5
  136. ! remainder is positive
  137. subcc %o3,%o5,%o3
  138. ! depth 2, accumulated bits 1
  139. bl LOC(2.17)
  140. srl %o5,1,%o5
  141. ! remainder is positive
  142. subcc %o3,%o5,%o3
  143. ! depth 3, accumulated bits 3
  144. bl LOC(3.19)
  145. srl %o5,1,%o5
  146. ! remainder is positive
  147. subcc %o3,%o5,%o3
  148. ! depth 4, accumulated bits 7
  149. bl LOC(4.23)
  150. srl %o5,1,%o5
  151. ! remainder is positive
  152. subcc %o3,%o5,%o3
  153. b 9f
  154. add %o2, (7*2+1), %o2
  155. LOC(4.23):
  156. ! remainder is negative
  157. addcc %o3,%o5,%o3
  158. b 9f
  159. add %o2, (7*2-1), %o2
  160. LOC(3.19):
  161. ! remainder is negative
  162. addcc %o3,%o5,%o3
  163. ! depth 4, accumulated bits 5
  164. bl LOC(4.21)
  165. srl %o5,1,%o5
  166. ! remainder is positive
  167. subcc %o3,%o5,%o3
  168. b 9f
  169. add %o2, (5*2+1), %o2
  170. LOC(4.21):
  171. ! remainder is negative
  172. addcc %o3,%o5,%o3
  173. b 9f
  174. add %o2, (5*2-1), %o2
  175. LOC(2.17):
  176. ! remainder is negative
  177. addcc %o3,%o5,%o3
  178. ! depth 3, accumulated bits 1
  179. bl LOC(3.17)
  180. srl %o5,1,%o5
  181. ! remainder is positive
  182. subcc %o3,%o5,%o3
  183. ! depth 4, accumulated bits 3
  184. bl LOC(4.19)
  185. srl %o5,1,%o5
  186. ! remainder is positive
  187. subcc %o3,%o5,%o3
  188. b 9f
  189. add %o2, (3*2+1), %o2
  190. LOC(4.19):
  191. ! remainder is negative
  192. addcc %o3,%o5,%o3
  193. b 9f
  194. add %o2, (3*2-1), %o2
  195. LOC(3.17):
  196. ! remainder is negative
  197. addcc %o3,%o5,%o3
  198. ! depth 4, accumulated bits 1
  199. bl LOC(4.17)
  200. srl %o5,1,%o5
  201. ! remainder is positive
  202. subcc %o3,%o5,%o3
  203. b 9f
  204. add %o2, (1*2+1), %o2
  205. LOC(4.17):
  206. ! remainder is negative
  207. addcc %o3,%o5,%o3
  208. b 9f
  209. add %o2, (1*2-1), %o2
  210. LOC(1.16):
  211. ! remainder is negative
  212. addcc %o3,%o5,%o3
  213. ! depth 2, accumulated bits -1
  214. bl LOC(2.15)
  215. srl %o5,1,%o5
  216. ! remainder is positive
  217. subcc %o3,%o5,%o3
  218. ! depth 3, accumulated bits -1
  219. bl LOC(3.15)
  220. srl %o5,1,%o5
  221. ! remainder is positive
  222. subcc %o3,%o5,%o3
  223. ! depth 4, accumulated bits -1
  224. bl LOC(4.15)
  225. srl %o5,1,%o5
  226. ! remainder is positive
  227. subcc %o3,%o5,%o3
  228. b 9f
  229. add %o2, (-1*2+1), %o2
  230. LOC(4.15):
  231. ! remainder is negative
  232. addcc %o3,%o5,%o3
  233. b 9f
  234. add %o2, (-1*2-1), %o2
  235. LOC(3.15):
  236. ! remainder is negative
  237. addcc %o3,%o5,%o3
  238. ! depth 4, accumulated bits -3
  239. bl LOC(4.13)
  240. srl %o5,1,%o5
  241. ! remainder is positive
  242. subcc %o3,%o5,%o3
  243. b 9f
  244. add %o2, (-3*2+1), %o2
  245. LOC(4.13):
  246. ! remainder is negative
  247. addcc %o3,%o5,%o3
  248. b 9f
  249. add %o2, (-3*2-1), %o2
  250. LOC(2.15):
  251. ! remainder is negative
  252. addcc %o3,%o5,%o3
  253. ! depth 3, accumulated bits -3
  254. bl LOC(3.13)
  255. srl %o5,1,%o5
  256. ! remainder is positive
  257. subcc %o3,%o5,%o3
  258. ! depth 4, accumulated bits -5
  259. bl LOC(4.11)
  260. srl %o5,1,%o5
  261. ! remainder is positive
  262. subcc %o3,%o5,%o3
  263. b 9f
  264. add %o2, (-5*2+1), %o2
  265. LOC(4.11):
  266. ! remainder is negative
  267. addcc %o3,%o5,%o3
  268. b 9f
  269. add %o2, (-5*2-1), %o2
  270. LOC(3.13):
  271. ! remainder is negative
  272. addcc %o3,%o5,%o3
  273. ! depth 4, accumulated bits -7
  274. bl LOC(4.9)
  275. srl %o5,1,%o5
  276. ! remainder is positive
  277. subcc %o3,%o5,%o3
  278. b 9f
  279. add %o2, (-7*2+1), %o2
  280. LOC(4.9):
  281. ! remainder is negative
  282. addcc %o3,%o5,%o3
  283. b 9f
  284. add %o2, (-7*2-1), %o2
  285. 9:
  286. LOC(end_regular_divide):
  287. subcc %o4, 1, %o4
  288. bge LOC(divloop)
  289. tst %o3
  290. bl,a LOC(got_result)
  291. ! non-restoring fixup here (one instruction only!)
  292. sub %o2, 1, %o2
  293. LOC(got_result):
  294. retl
  295. mov %o2, %o0
  296. END(.udiv)