udiv.S 6.8 KB

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