udiv.S 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* $OpenBSD: udiv.S,v 1.2 2011/03/12 18:51:31 deraadt Exp $ */
  2. /* $NetBSD: divrem.m4,v 1.3 1995/04/22 09:37:39 pk Exp $ */
  3. /*
  4. * Copyright (c) 1992, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This software was developed by the Computer Systems Engineering group
  8. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  9. * contributed to Berkeley.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS `AS IS' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * Header: divrem.m4,v 1.4 92/06/25 13:23:57 torek Exp
  36. */
  37. /*
  38. * Division and remainder, from Appendix E of the Sparc Version 8
  39. * Architecture Manual, with fixes from Gordon Irlam.
  40. */
  41. /*
  42. * Input: dividend and divisor in %o0 and %o1 respectively.
  43. *
  44. * m4 parameters:
  45. * .udiv name of function to generate
  46. * _C_LABEL(_udiv) secondary name of function to generate
  47. * div div=div => %o0 / %o1; div=rem => %o0 % %o1
  48. * false false=true => signed; false=false => unsigned
  49. *
  50. * Algorithm parameters:
  51. * N how many bits per iteration we try to get (4)
  52. * WORDSIZE total number of bits (32)
  53. *
  54. * Derived constants:
  55. * TWOSUPN 2^N, for label generation (m4 exponentiation currently broken)
  56. * TOPBITS number of bits in the top decade of a number
  57. *
  58. * Important variables:
  59. * Q the partial quotient under development (initially 0)
  60. * R the remainder so far, initially the dividend
  61. * ITER number of main division loop iterations required;
  62. * equal to ceil(log2(quotient) / N). Note that this
  63. * is the log base (2^N) of the quotient.
  64. * V the current comparand, initially divisor*2^(ITER*N-1)
  65. *
  66. * Cost:
  67. * Current estimate for non-large dividend is
  68. * ceil(log2(quotient) / N) * (10 + 7N/2) + C
  69. * A large dividend is one greater than 2^(31-TOPBITS) and takes a
  70. * different path, as the upper bits of the quotient must be developed
  71. * one bit at a time.
  72. */
  73. /* m4 reminder: d => if a is b, then c, else d */
  74. /*
  75. * This is the recursive definition for developing quotient digits.
  76. *
  77. * Parameters:
  78. * $1 the current depth, 1 <= $1 <= 4
  79. * $2 the current accumulation of quotient bits
  80. * 4 max depth
  81. *
  82. * We add a new bit to $2 and either recurse or insert the bits in
  83. * the quotient. %o3, %o2, and %o5 are inputs and outputs as defined above;
  84. * the condition codes are expected to reflect the input %o3, and are
  85. * modified to reflect the output %o3.
  86. */
  87. #include "DEFS.h"
  88. #include <machine/trap.h>
  89. #include <machine/asm.h>
  90. .globl _C_LABEL(_udiv)
  91. _C_LABEL(_udiv):
  92. FUNC(.udiv)
  93. ! Ready to divide. Compute size of quotient; scale comparand.
  94. orcc %o1, %g0, %o5
  95. bnz 1f
  96. mov %o0, %o3
  97. ! Divide by zero trap. If it returns, return 0 (about as
  98. ! wrong as possible, but that is what SunOS does...).
  99. t ST_DIV0
  100. retl
  101. clr %o0
  102. 1:
  103. cmp %o3, %o5 ! if %o1 exceeds %o0, done
  104. blu Lgot_result ! (and algorithm fails otherwise)
  105. clr %o2
  106. sethi %hi(1 << (32 - 4 - 1)), %g1
  107. cmp %o3, %g1
  108. blu Lnot_really_big
  109. clr %o4
  110. ! Here the dividend is >= 2^(31-N) or so. We must be careful here,
  111. ! as our usual N-at-a-shot divide step will cause overflow and havoc.
  112. ! The number of bits in the result here is N*ITER+SC, where SC <= N.
  113. ! Compute ITER in an unorthodox manner: know we need to shift V into
  114. ! the top decade: so do not even bother to compare to R.
  115. 1:
  116. cmp %o5, %g1
  117. bgeu 3f
  118. mov 1, %g5
  119. sll %o5, 4, %o5
  120. b 1b
  121. inc %o4
  122. ! Now compute %g5.
  123. 2: addcc %o5, %o5, %o5
  124. bcc Lnot_too_big
  125. inc %g5
  126. ! We get here if the %o1 overflowed while shifting.
  127. ! This means that %o3 has the high-order bit set.
  128. ! Restore %o5 and subtract from %o3.
  129. sll %g1, 4, %g1 ! high order bit
  130. srl %o5, 1, %o5 ! rest of %o5
  131. add %o5, %g1, %o5
  132. b Ldo_single_div
  133. dec %g5
  134. Lnot_too_big:
  135. 3: cmp %o5, %o3
  136. blu 2b
  137. nop
  138. be Ldo_single_div
  139. nop
  140. /* NB: these are commented out in the V8-Sparc manual as well */
  141. /* (I do not understand this) */
  142. ! %o5 > %o3: went too far: back up 1 step
  143. ! srl %o5, 1, %o5
  144. ! dec %g5
  145. ! do single-bit divide steps
  146. !
  147. ! We have to be careful here. We know that %o3 >= %o5, so we can do the
  148. ! first divide step without thinking. BUT, the others are conditional,
  149. ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
  150. ! order bit set in the first step, just falling into the regular
  151. ! division loop will mess up the first time around.
  152. ! So we unroll slightly...
  153. Ldo_single_div:
  154. deccc %g5
  155. bl Lend_regular_divide
  156. nop
  157. sub %o3, %o5, %o3
  158. mov 1, %o2
  159. b Lend_single_divloop
  160. nop
  161. Lsingle_divloop:
  162. sll %o2, 1, %o2
  163. bl 1f
  164. srl %o5, 1, %o5
  165. ! %o3 >= 0
  166. sub %o3, %o5, %o3
  167. b 2f
  168. inc %o2
  169. 1: ! %o3 < 0
  170. add %o3, %o5, %o3
  171. dec %o2
  172. 2:
  173. Lend_single_divloop:
  174. deccc %g5
  175. bge Lsingle_divloop
  176. tst %o3
  177. b,a Lend_regular_divide
  178. Lnot_really_big:
  179. 1:
  180. sll %o5, 4, %o5
  181. cmp %o5, %o3
  182. bleu 1b
  183. inccc %o4
  184. be Lgot_result
  185. dec %o4
  186. tst %o3 ! set up for initial iteration
  187. Ldivloop:
  188. sll %o2, 4, %o2
  189. ! depth 1, accumulated bits 0
  190. bl L.1.16
  191. srl %o5,1,%o5
  192. ! remainder is positive
  193. subcc %o3,%o5,%o3
  194. ! depth 2, accumulated bits 1
  195. bl L.2.17
  196. srl %o5,1,%o5
  197. ! remainder is positive
  198. subcc %o3,%o5,%o3
  199. ! depth 3, accumulated bits 3
  200. bl L.3.19
  201. srl %o5,1,%o5
  202. ! remainder is positive
  203. subcc %o3,%o5,%o3
  204. ! depth 4, accumulated bits 7
  205. bl L.4.23
  206. srl %o5,1,%o5
  207. ! remainder is positive
  208. subcc %o3,%o5,%o3
  209. b 9f
  210. add %o2, (7*2+1), %o2
  211. L.4.23:
  212. ! remainder is negative
  213. addcc %o3,%o5,%o3
  214. b 9f
  215. add %o2, (7*2-1), %o2
  216. L.3.19:
  217. ! remainder is negative
  218. addcc %o3,%o5,%o3
  219. ! depth 4, accumulated bits 5
  220. bl L.4.21
  221. srl %o5,1,%o5
  222. ! remainder is positive
  223. subcc %o3,%o5,%o3
  224. b 9f
  225. add %o2, (5*2+1), %o2
  226. L.4.21:
  227. ! remainder is negative
  228. addcc %o3,%o5,%o3
  229. b 9f
  230. add %o2, (5*2-1), %o2
  231. L.2.17:
  232. ! remainder is negative
  233. addcc %o3,%o5,%o3
  234. ! depth 3, accumulated bits 1
  235. bl L.3.17
  236. srl %o5,1,%o5
  237. ! remainder is positive
  238. subcc %o3,%o5,%o3
  239. ! depth 4, accumulated bits 3
  240. bl L.4.19
  241. srl %o5,1,%o5
  242. ! remainder is positive
  243. subcc %o3,%o5,%o3
  244. b 9f
  245. add %o2, (3*2+1), %o2
  246. L.4.19:
  247. ! remainder is negative
  248. addcc %o3,%o5,%o3
  249. b 9f
  250. add %o2, (3*2-1), %o2
  251. L.3.17:
  252. ! remainder is negative
  253. addcc %o3,%o5,%o3
  254. ! depth 4, accumulated bits 1
  255. bl L.4.17
  256. srl %o5,1,%o5
  257. ! remainder is positive
  258. subcc %o3,%o5,%o3
  259. b 9f
  260. add %o2, (1*2+1), %o2
  261. L.4.17:
  262. ! remainder is negative
  263. addcc %o3,%o5,%o3
  264. b 9f
  265. add %o2, (1*2-1), %o2
  266. L.1.16:
  267. ! remainder is negative
  268. addcc %o3,%o5,%o3
  269. ! depth 2, accumulated bits -1
  270. bl L.2.15
  271. srl %o5,1,%o5
  272. ! remainder is positive
  273. subcc %o3,%o5,%o3
  274. ! depth 3, accumulated bits -1
  275. bl L.3.15
  276. srl %o5,1,%o5
  277. ! remainder is positive
  278. subcc %o3,%o5,%o3
  279. ! depth 4, accumulated bits -1
  280. bl L.4.15
  281. srl %o5,1,%o5
  282. ! remainder is positive
  283. subcc %o3,%o5,%o3
  284. b 9f
  285. add %o2, (-1*2+1), %o2
  286. L.4.15:
  287. ! remainder is negative
  288. addcc %o3,%o5,%o3
  289. b 9f
  290. add %o2, (-1*2-1), %o2
  291. L.3.15:
  292. ! remainder is negative
  293. addcc %o3,%o5,%o3
  294. ! depth 4, accumulated bits -3
  295. bl L.4.13
  296. srl %o5,1,%o5
  297. ! remainder is positive
  298. subcc %o3,%o5,%o3
  299. b 9f
  300. add %o2, (-3*2+1), %o2
  301. L.4.13:
  302. ! remainder is negative
  303. addcc %o3,%o5,%o3
  304. b 9f
  305. add %o2, (-3*2-1), %o2
  306. L.2.15:
  307. ! remainder is negative
  308. addcc %o3,%o5,%o3
  309. ! depth 3, accumulated bits -3
  310. bl L.3.13
  311. srl %o5,1,%o5
  312. ! remainder is positive
  313. subcc %o3,%o5,%o3
  314. ! depth 4, accumulated bits -5
  315. bl L.4.11
  316. srl %o5,1,%o5
  317. ! remainder is positive
  318. subcc %o3,%o5,%o3
  319. b 9f
  320. add %o2, (-5*2+1), %o2
  321. L.4.11:
  322. ! remainder is negative
  323. addcc %o3,%o5,%o3
  324. b 9f
  325. add %o2, (-5*2-1), %o2
  326. L.3.13:
  327. ! remainder is negative
  328. addcc %o3,%o5,%o3
  329. ! depth 4, accumulated bits -7
  330. bl L.4.9
  331. srl %o5,1,%o5
  332. ! remainder is positive
  333. subcc %o3,%o5,%o3
  334. b 9f
  335. add %o2, (-7*2+1), %o2
  336. L.4.9:
  337. ! remainder is negative
  338. addcc %o3,%o5,%o3
  339. b 9f
  340. add %o2, (-7*2-1), %o2
  341. 9:
  342. Lend_regular_divide:
  343. deccc %o4
  344. bge Ldivloop
  345. tst %o3
  346. bl,a Lgot_result
  347. ! non-restoring fixup here (one instruction only!)
  348. dec %o2
  349. Lgot_result:
  350. retl
  351. mov %o2, %o0