sdiv.S 7.5 KB

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