urem.S 7.0 KB

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