urem.S 7.0 KB

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