sdiv.S 7.3 KB

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