sdiv.S 7.5 KB

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