sdiv.S 7.3 KB

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