rem.S 7.2 KB

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