rem.S 9.6 KB

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