ilsp.S 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP
  3. M68000 Hi-Performance Microprocessor Division
  4. M68060 Software Package
  5. Production Release P1.00 -- October 10, 1994
  6. M68060 Software Package Copyright © 1993, 1994 Motorola Inc. All rights reserved.
  7. THE SOFTWARE is provided on an "AS IS" basis and without warranty.
  8. To the maximum extent permitted by applicable law,
  9. MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
  10. INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  11. and any warranty against infringement with regard to the SOFTWARE
  12. (INCLUDING ANY MODIFIED VERSIONS THEREOF) and any accompanying written materials.
  13. To the maximum extent permitted by applicable law,
  14. IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER
  15. (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
  16. BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS)
  17. ARISING OF THE USE OR INABILITY TO USE THE SOFTWARE.
  18. Motorola assumes no responsibility for the maintenance and support of the SOFTWARE.
  19. You are hereby granted a copyright license to use, modify, and distribute the SOFTWARE
  20. so long as this entire notice is retained without alteration in any modified and/or
  21. redistributed versions, and that such modified versions are clearly identified as such.
  22. No licenses are granted by implication, estoppel or otherwise under any patents
  23. or trademarks of Motorola, Inc.
  24. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. # litop.s:
  26. # This file is appended to the top of the 060FPLSP package
  27. # and contains the entry points into the package. The user, in
  28. # effect, branches to one of the branch table entries located here.
  29. #
  30. bra.l _060LSP__idivs64_
  31. short 0x0000
  32. bra.l _060LSP__idivu64_
  33. short 0x0000
  34. bra.l _060LSP__imuls64_
  35. short 0x0000
  36. bra.l _060LSP__imulu64_
  37. short 0x0000
  38. bra.l _060LSP__cmp2_Ab_
  39. short 0x0000
  40. bra.l _060LSP__cmp2_Aw_
  41. short 0x0000
  42. bra.l _060LSP__cmp2_Al_
  43. short 0x0000
  44. bra.l _060LSP__cmp2_Db_
  45. short 0x0000
  46. bra.l _060LSP__cmp2_Dw_
  47. short 0x0000
  48. bra.l _060LSP__cmp2_Dl_
  49. short 0x0000
  50. # leave room for future possible aditions.
  51. align 0x200
  52. #########################################################################
  53. # XDEF **************************************************************** #
  54. # _060LSP__idivu64_(): Emulate 64-bit unsigned div instruction. #
  55. # _060LSP__idivs64_(): Emulate 64-bit signed div instruction. #
  56. # #
  57. # This is the library version which is accessed as a subroutine #
  58. # and therefore does not work exactly like the 680X0 div{s,u}.l #
  59. # 64-bit divide instruction. #
  60. # #
  61. # XREF **************************************************************** #
  62. # None. #
  63. # #
  64. # INPUT *************************************************************** #
  65. # 0x4(sp) = divisor #
  66. # 0x8(sp) = hi(dividend) #
  67. # 0xc(sp) = lo(dividend) #
  68. # 0x10(sp) = pointer to location to place quotient/remainder #
  69. # #
  70. # OUTPUT ************************************************************** #
  71. # 0x10(sp) = points to location of remainder/quotient. #
  72. # remainder is in first longword, quotient is in 2nd. #
  73. # #
  74. # ALGORITHM *********************************************************** #
  75. # If the operands are signed, make them unsigned and save the #
  76. # sign info for later. Separate out special cases like divide-by-zero #
  77. # or 32-bit divides if possible. Else, use a special math algorithm #
  78. # to calculate the result. #
  79. # Restore sign info if signed instruction. Set the condition #
  80. # codes before performing the final "rts". If the divisor was equal to #
  81. # zero, then perform a divide-by-zero using a 16-bit implemented #
  82. # divide instruction. This way, the operating system can record that #
  83. # the event occurred even though it may not point to the correct place. #
  84. # #
  85. #########################################################################
  86. set POSNEG, -1
  87. set NDIVISOR, -2
  88. set NDIVIDEND, -3
  89. set DDSECOND, -4
  90. set DDNORMAL, -8
  91. set DDQUOTIENT, -12
  92. set DIV64_CC, -16
  93. ##########
  94. # divs.l #
  95. ##########
  96. global _060LSP__idivs64_
  97. _060LSP__idivs64_:
  98. # PROLOGUE BEGIN ########################################################
  99. link.w %a6,&-16
  100. movm.l &0x3f00,-(%sp) # save d2-d7
  101. # fmovm.l &0x0,-(%sp) # save no fpregs
  102. # PROLOGUE END ##########################################################
  103. mov.w %cc,DIV64_CC(%a6)
  104. st POSNEG(%a6) # signed operation
  105. bra.b ldiv64_cont
  106. ##########
  107. # divu.l #
  108. ##########
  109. global _060LSP__idivu64_
  110. _060LSP__idivu64_:
  111. # PROLOGUE BEGIN ########################################################
  112. link.w %a6,&-16
  113. movm.l &0x3f00,-(%sp) # save d2-d7
  114. # fmovm.l &0x0,-(%sp) # save no fpregs
  115. # PROLOGUE END ##########################################################
  116. mov.w %cc,DIV64_CC(%a6)
  117. sf POSNEG(%a6) # unsigned operation
  118. ldiv64_cont:
  119. mov.l 0x8(%a6),%d7 # fetch divisor
  120. beq.w ldiv64eq0 # divisor is = 0!!!
  121. mov.l 0xc(%a6), %d5 # get dividend hi
  122. mov.l 0x10(%a6), %d6 # get dividend lo
  123. # separate signed and unsigned divide
  124. tst.b POSNEG(%a6) # signed or unsigned?
  125. beq.b ldspecialcases # use positive divide
  126. # save the sign of the divisor
  127. # make divisor unsigned if it's negative
  128. tst.l %d7 # chk sign of divisor
  129. slt NDIVISOR(%a6) # save sign of divisor
  130. bpl.b ldsgndividend
  131. neg.l %d7 # complement negative divisor
  132. # save the sign of the dividend
  133. # make dividend unsigned if it's negative
  134. ldsgndividend:
  135. tst.l %d5 # chk sign of hi(dividend)
  136. slt NDIVIDEND(%a6) # save sign of dividend
  137. bpl.b ldspecialcases
  138. mov.w &0x0, %cc # clear 'X' cc bit
  139. negx.l %d6 # complement signed dividend
  140. negx.l %d5
  141. # extract some special cases:
  142. # - is (dividend == 0) ?
  143. # - is (hi(dividend) == 0 && (divisor <= lo(dividend))) ? (32-bit div)
  144. ldspecialcases:
  145. tst.l %d5 # is (hi(dividend) == 0)
  146. bne.b ldnormaldivide # no, so try it the long way
  147. tst.l %d6 # is (lo(dividend) == 0), too
  148. beq.w lddone # yes, so (dividend == 0)
  149. cmp.l %d7,%d6 # is (divisor <= lo(dividend))
  150. bls.b ld32bitdivide # yes, so use 32 bit divide
  151. exg %d5,%d6 # q = 0, r = dividend
  152. bra.w ldivfinish # can't divide, we're done.
  153. ld32bitdivide:
  154. tdivu.l %d7, %d5:%d6 # it's only a 32/32 bit div!
  155. bra.b ldivfinish
  156. ldnormaldivide:
  157. # last special case:
  158. # - is hi(dividend) >= divisor ? if yes, then overflow
  159. cmp.l %d7,%d5
  160. bls.b lddovf # answer won't fit in 32 bits
  161. # perform the divide algorithm:
  162. bsr.l ldclassical # do int divide
  163. # separate into signed and unsigned finishes.
  164. ldivfinish:
  165. tst.b POSNEG(%a6) # do divs, divu separately
  166. beq.b lddone # divu has no processing!!!
  167. # it was a divs.l, so ccode setting is a little more complicated...
  168. tst.b NDIVIDEND(%a6) # remainder has same sign
  169. beq.b ldcc # as dividend.
  170. neg.l %d5 # sgn(rem) = sgn(dividend)
  171. ldcc:
  172. mov.b NDIVISOR(%a6), %d0
  173. eor.b %d0, NDIVIDEND(%a6) # chk if quotient is negative
  174. beq.b ldqpos # branch to quot positive
  175. # 0x80000000 is the largest number representable as a 32-bit negative
  176. # number. the negative of 0x80000000 is 0x80000000.
  177. cmpi.l %d6, &0x80000000 # will (-quot) fit in 32 bits?
  178. bhi.b lddovf
  179. neg.l %d6 # make (-quot) 2's comp
  180. bra.b lddone
  181. ldqpos:
  182. btst &0x1f, %d6 # will (+quot) fit in 32 bits?
  183. bne.b lddovf
  184. lddone:
  185. # if the register numbers are the same, only the quotient gets saved.
  186. # so, if we always save the quotient second, we save ourselves a cmp&beq
  187. andi.w &0x10,DIV64_CC(%a6)
  188. mov.w DIV64_CC(%a6),%cc
  189. tst.l %d6 # may set 'N' ccode bit
  190. # here, the result is in d1 and d0. the current strategy is to save
  191. # the values at the location pointed to by a0.
  192. # use movm here to not disturb the condition codes.
  193. ldexit:
  194. movm.l &0x0060,([0x14,%a6]) # save result
  195. # EPILOGUE BEGIN ########################################################
  196. # fmovm.l (%sp)+,&0x0 # restore no fpregs
  197. movm.l (%sp)+,&0x00fc # restore d2-d7
  198. unlk %a6
  199. # EPILOGUE END ##########################################################
  200. rts
  201. # the result should be the unchanged dividend
  202. lddovf:
  203. mov.l 0xc(%a6), %d5 # get dividend hi
  204. mov.l 0x10(%a6), %d6 # get dividend lo
  205. andi.w &0x1c,DIV64_CC(%a6)
  206. ori.w &0x02,DIV64_CC(%a6) # set 'V' ccode bit
  207. mov.w DIV64_CC(%a6),%cc
  208. bra.b ldexit
  209. ldiv64eq0:
  210. mov.l 0xc(%a6),([0x14,%a6])
  211. mov.l 0x10(%a6),([0x14,%a6],0x4)
  212. mov.w DIV64_CC(%a6),%cc
  213. # EPILOGUE BEGIN ########################################################
  214. # fmovm.l (%sp)+,&0x0 # restore no fpregs
  215. movm.l (%sp)+,&0x00fc # restore d2-d7
  216. unlk %a6
  217. # EPILOGUE END ##########################################################
  218. divu.w &0x0,%d0 # force a divbyzero exception
  219. rts
  220. ###########################################################################
  221. #########################################################################
  222. # This routine uses the 'classical' Algorithm D from Donald Knuth's #
  223. # Art of Computer Programming, vol II, Seminumerical Algorithms. #
  224. # For this implementation b=2**16, and the target is U1U2U3U4/V1V2, #
  225. # where U,V are words of the quadword dividend and longword divisor, #
  226. # and U1, V1 are the most significant words. #
  227. # #
  228. # The most sig. longword of the 64 bit dividend must be in %d5, least #
  229. # in %d6. The divisor must be in the variable ddivisor, and the #
  230. # signed/unsigned flag ddusign must be set (0=unsigned,1=signed). #
  231. # The quotient is returned in %d6, remainder in %d5, unless the #
  232. # v (overflow) bit is set in the saved %ccr. If overflow, the dividend #
  233. # is unchanged. #
  234. #########################################################################
  235. ldclassical:
  236. # if the divisor msw is 0, use simpler algorithm then the full blown
  237. # one at ddknuth:
  238. cmpi.l %d7, &0xffff
  239. bhi.b lddknuth # go use D. Knuth algorithm
  240. # Since the divisor is only a word (and larger than the mslw of the dividend),
  241. # a simpler algorithm may be used :
  242. # In the general case, four quotient words would be created by
  243. # dividing the divisor word into each dividend word. In this case,
  244. # the first two quotient words must be zero, or overflow would occur.
  245. # Since we already checked this case above, we can treat the most significant
  246. # longword of the dividend as (0) remainder (see Knuth) and merely complete
  247. # the last two divisions to get a quotient longword and word remainder:
  248. clr.l %d1
  249. swap %d5 # same as r*b if previous step rqd
  250. swap %d6 # get u3 to lsw position
  251. mov.w %d6, %d5 # rb + u3
  252. divu.w %d7, %d5
  253. mov.w %d5, %d1 # first quotient word
  254. swap %d6 # get u4
  255. mov.w %d6, %d5 # rb + u4
  256. divu.w %d7, %d5
  257. swap %d1
  258. mov.w %d5, %d1 # 2nd quotient 'digit'
  259. clr.w %d5
  260. swap %d5 # now remainder
  261. mov.l %d1, %d6 # and quotient
  262. rts
  263. lddknuth:
  264. # In this algorithm, the divisor is treated as a 2 digit (word) number
  265. # which is divided into a 3 digit (word) dividend to get one quotient
  266. # digit (word). After subtraction, the dividend is shifted and the
  267. # process repeated. Before beginning, the divisor and quotient are
  268. # 'normalized' so that the process of estimating the quotient digit
  269. # will yield verifiably correct results..
  270. clr.l DDNORMAL(%a6) # count of shifts for normalization
  271. clr.b DDSECOND(%a6) # clear flag for quotient digits
  272. clr.l %d1 # %d1 will hold trial quotient
  273. lddnchk:
  274. btst &31, %d7 # must we normalize? first word of
  275. bne.b lddnormalized # divisor (V1) must be >= 65536/2
  276. addq.l &0x1, DDNORMAL(%a6) # count normalization shifts
  277. lsl.l &0x1, %d7 # shift the divisor
  278. lsl.l &0x1, %d6 # shift u4,u3 with overflow to u2
  279. roxl.l &0x1, %d5 # shift u1,u2
  280. bra.w lddnchk
  281. lddnormalized:
  282. # Now calculate an estimate of the quotient words (msw first, then lsw).
  283. # The comments use subscripts for the first quotient digit determination.
  284. mov.l %d7, %d3 # divisor
  285. mov.l %d5, %d2 # dividend mslw
  286. swap %d2
  287. swap %d3
  288. cmp.w %d2, %d3 # V1 = U1 ?
  289. bne.b lddqcalc1
  290. mov.w &0xffff, %d1 # use max trial quotient word
  291. bra.b lddadj0
  292. lddqcalc1:
  293. mov.l %d5, %d1
  294. divu.w %d3, %d1 # use quotient of mslw/msw
  295. andi.l &0x0000ffff, %d1 # zero any remainder
  296. lddadj0:
  297. # now test the trial quotient and adjust. This step plus the
  298. # normalization assures (according to Knuth) that the trial
  299. # quotient will be at worst 1 too large.
  300. mov.l %d6, -(%sp)
  301. clr.w %d6 # word u3 left
  302. swap %d6 # in lsw position
  303. lddadj1: mov.l %d7, %d3
  304. mov.l %d1, %d2
  305. mulu.w %d7, %d2 # V2q
  306. swap %d3
  307. mulu.w %d1, %d3 # V1q
  308. mov.l %d5, %d4 # U1U2
  309. sub.l %d3, %d4 # U1U2 - V1q
  310. swap %d4
  311. mov.w %d4,%d0
  312. mov.w %d6,%d4 # insert lower word (U3)
  313. tst.w %d0 # is upper word set?
  314. bne.w lddadjd1
  315. # add.l %d6, %d4 # (U1U2 - V1q) + U3
  316. cmp.l %d2, %d4
  317. bls.b lddadjd1 # is V2q > (U1U2-V1q) + U3 ?
  318. subq.l &0x1, %d1 # yes, decrement and recheck
  319. bra.b lddadj1
  320. lddadjd1:
  321. # now test the word by multiplying it by the divisor (V1V2) and comparing
  322. # the 3 digit (word) result with the current dividend words
  323. mov.l %d5, -(%sp) # save %d5 (%d6 already saved)
  324. mov.l %d1, %d6
  325. swap %d6 # shift answer to ms 3 words
  326. mov.l %d7, %d5
  327. bsr.l ldmm2
  328. mov.l %d5, %d2 # now %d2,%d3 are trial*divisor
  329. mov.l %d6, %d3
  330. mov.l (%sp)+, %d5 # restore dividend
  331. mov.l (%sp)+, %d6
  332. sub.l %d3, %d6
  333. subx.l %d2, %d5 # subtract double precision
  334. bcc ldd2nd # no carry, do next quotient digit
  335. subq.l &0x1, %d1 # q is one too large
  336. # need to add back divisor longword to current ms 3 digits of dividend
  337. # - according to Knuth, this is done only 2 out of 65536 times for random
  338. # divisor, dividend selection.
  339. clr.l %d2
  340. mov.l %d7, %d3
  341. swap %d3
  342. clr.w %d3 # %d3 now ls word of divisor
  343. add.l %d3, %d6 # aligned with 3rd word of dividend
  344. addx.l %d2, %d5
  345. mov.l %d7, %d3
  346. clr.w %d3 # %d3 now ms word of divisor
  347. swap %d3 # aligned with 2nd word of dividend
  348. add.l %d3, %d5
  349. ldd2nd:
  350. tst.b DDSECOND(%a6) # both q words done?
  351. bne.b lddremain
  352. # first quotient digit now correct. store digit and shift the
  353. # (subtracted) dividend
  354. mov.w %d1, DDQUOTIENT(%a6)
  355. clr.l %d1
  356. swap %d5
  357. swap %d6
  358. mov.w %d6, %d5
  359. clr.w %d6
  360. st DDSECOND(%a6) # second digit
  361. bra.w lddnormalized
  362. lddremain:
  363. # add 2nd word to quotient, get the remainder.
  364. mov.w %d1, DDQUOTIENT+2(%a6)
  365. # shift down one word/digit to renormalize remainder.
  366. mov.w %d5, %d6
  367. swap %d6
  368. swap %d5
  369. mov.l DDNORMAL(%a6), %d7 # get norm shift count
  370. beq.b lddrn
  371. subq.l &0x1, %d7 # set for loop count
  372. lddnlp:
  373. lsr.l &0x1, %d5 # shift into %d6
  374. roxr.l &0x1, %d6
  375. dbf %d7, lddnlp
  376. lddrn:
  377. mov.l %d6, %d5 # remainder
  378. mov.l DDQUOTIENT(%a6), %d6 # quotient
  379. rts
  380. ldmm2:
  381. # factors for the 32X32->64 multiplication are in %d5 and %d6.
  382. # returns 64 bit result in %d5 (hi) %d6(lo).
  383. # destroys %d2,%d3,%d4.
  384. # multiply hi,lo words of each factor to get 4 intermediate products
  385. mov.l %d6, %d2
  386. mov.l %d6, %d3
  387. mov.l %d5, %d4
  388. swap %d3
  389. swap %d4
  390. mulu.w %d5, %d6 # %d6 <- lsw*lsw
  391. mulu.w %d3, %d5 # %d5 <- msw-dest*lsw-source
  392. mulu.w %d4, %d2 # %d2 <- msw-source*lsw-dest
  393. mulu.w %d4, %d3 # %d3 <- msw*msw
  394. # now use swap and addx to consolidate to two longwords
  395. clr.l %d4
  396. swap %d6
  397. add.w %d5, %d6 # add msw of l*l to lsw of m*l product
  398. addx.w %d4, %d3 # add any carry to m*m product
  399. add.w %d2, %d6 # add in lsw of other m*l product
  400. addx.w %d4, %d3 # add any carry to m*m product
  401. swap %d6 # %d6 is low 32 bits of final product
  402. clr.w %d5
  403. clr.w %d2 # lsw of two mixed products used,
  404. swap %d5 # now use msws of longwords
  405. swap %d2
  406. add.l %d2, %d5
  407. add.l %d3, %d5 # %d5 now ms 32 bits of final product
  408. rts
  409. #########################################################################
  410. # XDEF **************************************************************** #
  411. # _060LSP__imulu64_(): Emulate 64-bit unsigned mul instruction #
  412. # _060LSP__imuls64_(): Emulate 64-bit signed mul instruction. #
  413. # #
  414. # This is the library version which is accessed as a subroutine #
  415. # and therefore does not work exactly like the 680X0 mul{s,u}.l #
  416. # 64-bit multiply instruction. #
  417. # #
  418. # XREF **************************************************************** #
  419. # None #
  420. # #
  421. # INPUT *************************************************************** #
  422. # 0x4(sp) = multiplier #
  423. # 0x8(sp) = multiplicand #
  424. # 0xc(sp) = pointer to location to place 64-bit result #
  425. # #
  426. # OUTPUT ************************************************************** #
  427. # 0xc(sp) = points to location of 64-bit result #
  428. # #
  429. # ALGORITHM *********************************************************** #
  430. # Perform the multiply in pieces using 16x16->32 unsigned #
  431. # multiplies and "add" instructions. #
  432. # Set the condition codes as appropriate before performing an #
  433. # "rts". #
  434. # #
  435. #########################################################################
  436. set MUL64_CC, -4
  437. global _060LSP__imulu64_
  438. _060LSP__imulu64_:
  439. # PROLOGUE BEGIN ########################################################
  440. link.w %a6,&-4
  441. movm.l &0x3800,-(%sp) # save d2-d4
  442. # fmovm.l &0x0,-(%sp) # save no fpregs
  443. # PROLOGUE END ##########################################################
  444. mov.w %cc,MUL64_CC(%a6) # save incoming ccodes
  445. mov.l 0x8(%a6),%d0 # store multiplier in d0
  446. beq.w mulu64_zero # handle zero separately
  447. mov.l 0xc(%a6),%d1 # get multiplicand in d1
  448. beq.w mulu64_zero # handle zero separately
  449. #########################################################################
  450. # 63 32 0 #
  451. # ---------------------------- #
  452. # | hi(mplier) * hi(mplicand)| #
  453. # ---------------------------- #
  454. # ----------------------------- #
  455. # | hi(mplier) * lo(mplicand) | #
  456. # ----------------------------- #
  457. # ----------------------------- #
  458. # | lo(mplier) * hi(mplicand) | #
  459. # ----------------------------- #
  460. # | ----------------------------- #
  461. # --|-- | lo(mplier) * lo(mplicand) | #
  462. # | ----------------------------- #
  463. # ======================================================== #
  464. # -------------------------------------------------------- #
  465. # | hi(result) | lo(result) | #
  466. # -------------------------------------------------------- #
  467. #########################################################################
  468. mulu64_alg:
  469. # load temp registers with operands
  470. mov.l %d0,%d2 # mr in d2
  471. mov.l %d0,%d3 # mr in d3
  472. mov.l %d1,%d4 # md in d4
  473. swap %d3 # hi(mr) in lo d3
  474. swap %d4 # hi(md) in lo d4
  475. # complete necessary multiplies:
  476. mulu.w %d1,%d0 # [1] lo(mr) * lo(md)
  477. mulu.w %d3,%d1 # [2] hi(mr) * lo(md)
  478. mulu.w %d4,%d2 # [3] lo(mr) * hi(md)
  479. mulu.w %d4,%d3 # [4] hi(mr) * hi(md)
  480. # add lo portions of [2],[3] to hi portion of [1].
  481. # add carries produced from these adds to [4].
  482. # lo([1]) is the final lo 16 bits of the result.
  483. clr.l %d4 # load d4 w/ zero value
  484. swap %d0 # hi([1]) <==> lo([1])
  485. add.w %d1,%d0 # hi([1]) + lo([2])
  486. addx.l %d4,%d3 # [4] + carry
  487. add.w %d2,%d0 # hi([1]) + lo([3])
  488. addx.l %d4,%d3 # [4] + carry
  489. swap %d0 # lo([1]) <==> hi([1])
  490. # lo portions of [2],[3] have been added in to final result.
  491. # now, clear lo, put hi in lo reg, and add to [4]
  492. clr.w %d1 # clear lo([2])
  493. clr.w %d2 # clear hi([3])
  494. swap %d1 # hi([2]) in lo d1
  495. swap %d2 # hi([3]) in lo d2
  496. add.l %d2,%d1 # [4] + hi([2])
  497. add.l %d3,%d1 # [4] + hi([3])
  498. # now, grab the condition codes. only one that can be set is 'N'.
  499. # 'N' CAN be set if the operation is unsigned if bit 63 is set.
  500. mov.w MUL64_CC(%a6),%d4
  501. andi.b &0x10,%d4 # keep old 'X' bit
  502. tst.l %d1 # may set 'N' bit
  503. bpl.b mulu64_ddone
  504. ori.b &0x8,%d4 # set 'N' bit
  505. mulu64_ddone:
  506. mov.w %d4,%cc
  507. # here, the result is in d1 and d0. the current strategy is to save
  508. # the values at the location pointed to by a0.
  509. # use movm here to not disturb the condition codes.
  510. mulu64_end:
  511. exg %d1,%d0
  512. movm.l &0x0003,([0x10,%a6]) # save result
  513. # EPILOGUE BEGIN ########################################################
  514. # fmovm.l (%sp)+,&0x0 # restore no fpregs
  515. movm.l (%sp)+,&0x001c # restore d2-d4
  516. unlk %a6
  517. # EPILOGUE END ##########################################################
  518. rts
  519. # one or both of the operands is zero so the result is also zero.
  520. # save the zero result to the register file and set the 'Z' ccode bit.
  521. mulu64_zero:
  522. clr.l %d0
  523. clr.l %d1
  524. mov.w MUL64_CC(%a6),%d4
  525. andi.b &0x10,%d4
  526. ori.b &0x4,%d4
  527. mov.w %d4,%cc # set 'Z' ccode bit
  528. bra.b mulu64_end
  529. ##########
  530. # muls.l #
  531. ##########
  532. global _060LSP__imuls64_
  533. _060LSP__imuls64_:
  534. # PROLOGUE BEGIN ########################################################
  535. link.w %a6,&-4
  536. movm.l &0x3c00,-(%sp) # save d2-d5
  537. # fmovm.l &0x0,-(%sp) # save no fpregs
  538. # PROLOGUE END ##########################################################
  539. mov.w %cc,MUL64_CC(%a6) # save incoming ccodes
  540. mov.l 0x8(%a6),%d0 # store multiplier in d0
  541. beq.b mulu64_zero # handle zero separately
  542. mov.l 0xc(%a6),%d1 # get multiplicand in d1
  543. beq.b mulu64_zero # handle zero separately
  544. clr.b %d5 # clear sign tag
  545. tst.l %d0 # is multiplier negative?
  546. bge.b muls64_chk_md_sgn # no
  547. neg.l %d0 # make multiplier positive
  548. ori.b &0x1,%d5 # save multiplier sgn
  549. # the result sign is the exclusive or of the operand sign bits.
  550. muls64_chk_md_sgn:
  551. tst.l %d1 # is multiplicand negative?
  552. bge.b muls64_alg # no
  553. neg.l %d1 # make multiplicand positive
  554. eori.b &0x1,%d5 # calculate correct sign
  555. #########################################################################
  556. # 63 32 0 #
  557. # ---------------------------- #
  558. # | hi(mplier) * hi(mplicand)| #
  559. # ---------------------------- #
  560. # ----------------------------- #
  561. # | hi(mplier) * lo(mplicand) | #
  562. # ----------------------------- #
  563. # ----------------------------- #
  564. # | lo(mplier) * hi(mplicand) | #
  565. # ----------------------------- #
  566. # | ----------------------------- #
  567. # --|-- | lo(mplier) * lo(mplicand) | #
  568. # | ----------------------------- #
  569. # ======================================================== #
  570. # -------------------------------------------------------- #
  571. # | hi(result) | lo(result) | #
  572. # -------------------------------------------------------- #
  573. #########################################################################
  574. muls64_alg:
  575. # load temp registers with operands
  576. mov.l %d0,%d2 # mr in d2
  577. mov.l %d0,%d3 # mr in d3
  578. mov.l %d1,%d4 # md in d4
  579. swap %d3 # hi(mr) in lo d3
  580. swap %d4 # hi(md) in lo d4
  581. # complete necessary multiplies:
  582. mulu.w %d1,%d0 # [1] lo(mr) * lo(md)
  583. mulu.w %d3,%d1 # [2] hi(mr) * lo(md)
  584. mulu.w %d4,%d2 # [3] lo(mr) * hi(md)
  585. mulu.w %d4,%d3 # [4] hi(mr) * hi(md)
  586. # add lo portions of [2],[3] to hi portion of [1].
  587. # add carries produced from these adds to [4].
  588. # lo([1]) is the final lo 16 bits of the result.
  589. clr.l %d4 # load d4 w/ zero value
  590. swap %d0 # hi([1]) <==> lo([1])
  591. add.w %d1,%d0 # hi([1]) + lo([2])
  592. addx.l %d4,%d3 # [4] + carry
  593. add.w %d2,%d0 # hi([1]) + lo([3])
  594. addx.l %d4,%d3 # [4] + carry
  595. swap %d0 # lo([1]) <==> hi([1])
  596. # lo portions of [2],[3] have been added in to final result.
  597. # now, clear lo, put hi in lo reg, and add to [4]
  598. clr.w %d1 # clear lo([2])
  599. clr.w %d2 # clear hi([3])
  600. swap %d1 # hi([2]) in lo d1
  601. swap %d2 # hi([3]) in lo d2
  602. add.l %d2,%d1 # [4] + hi([2])
  603. add.l %d3,%d1 # [4] + hi([3])
  604. tst.b %d5 # should result be signed?
  605. beq.b muls64_done # no
  606. # result should be a signed negative number.
  607. # compute 2's complement of the unsigned number:
  608. # -negate all bits and add 1
  609. muls64_neg:
  610. not.l %d0 # negate lo(result) bits
  611. not.l %d1 # negate hi(result) bits
  612. addq.l &1,%d0 # add 1 to lo(result)
  613. addx.l %d4,%d1 # add carry to hi(result)
  614. muls64_done:
  615. mov.w MUL64_CC(%a6),%d4
  616. andi.b &0x10,%d4 # keep old 'X' bit
  617. tst.l %d1 # may set 'N' bit
  618. bpl.b muls64_ddone
  619. ori.b &0x8,%d4 # set 'N' bit
  620. muls64_ddone:
  621. mov.w %d4,%cc
  622. # here, the result is in d1 and d0. the current strategy is to save
  623. # the values at the location pointed to by a0.
  624. # use movm here to not disturb the condition codes.
  625. muls64_end:
  626. exg %d1,%d0
  627. movm.l &0x0003,([0x10,%a6]) # save result at (a0)
  628. # EPILOGUE BEGIN ########################################################
  629. # fmovm.l (%sp)+,&0x0 # restore no fpregs
  630. movm.l (%sp)+,&0x003c # restore d2-d5
  631. unlk %a6
  632. # EPILOGUE END ##########################################################
  633. rts
  634. # one or both of the operands is zero so the result is also zero.
  635. # save the zero result to the register file and set the 'Z' ccode bit.
  636. muls64_zero:
  637. clr.l %d0
  638. clr.l %d1
  639. mov.w MUL64_CC(%a6),%d4
  640. andi.b &0x10,%d4
  641. ori.b &0x4,%d4
  642. mov.w %d4,%cc # set 'Z' ccode bit
  643. bra.b muls64_end
  644. #########################################################################
  645. # XDEF **************************************************************** #
  646. # _060LSP__cmp2_Ab_(): Emulate "cmp2.b An,<ea>". #
  647. # _060LSP__cmp2_Aw_(): Emulate "cmp2.w An,<ea>". #
  648. # _060LSP__cmp2_Al_(): Emulate "cmp2.l An,<ea>". #
  649. # _060LSP__cmp2_Db_(): Emulate "cmp2.b Dn,<ea>". #
  650. # _060LSP__cmp2_Dw_(): Emulate "cmp2.w Dn,<ea>". #
  651. # _060LSP__cmp2_Dl_(): Emulate "cmp2.l Dn,<ea>". #
  652. # #
  653. # This is the library version which is accessed as a subroutine #
  654. # and therefore does not work exactly like the 680X0 "cmp2" #
  655. # instruction. #
  656. # #
  657. # XREF **************************************************************** #
  658. # None #
  659. # #
  660. # INPUT *************************************************************** #
  661. # 0x4(sp) = Rn #
  662. # 0x8(sp) = pointer to boundary pair #
  663. # #
  664. # OUTPUT ************************************************************** #
  665. # cc = condition codes are set correctly #
  666. # #
  667. # ALGORITHM *********************************************************** #
  668. # In the interest of simplicity, all operands are converted to #
  669. # longword size whether the operation is byte, word, or long. The #
  670. # bounds are sign extended accordingly. If Rn is a data regsiter, Rn is #
  671. # also sign extended. If Rn is an address register, it need not be sign #
  672. # extended since the full register is always used. #
  673. # The condition codes are set correctly before the final "rts". #
  674. # #
  675. #########################################################################
  676. set CMP2_CC, -4
  677. global _060LSP__cmp2_Ab_
  678. _060LSP__cmp2_Ab_:
  679. # PROLOGUE BEGIN ########################################################
  680. link.w %a6,&-4
  681. movm.l &0x3800,-(%sp) # save d2-d4
  682. # fmovm.l &0x0,-(%sp) # save no fpregs
  683. # PROLOGUE END ##########################################################
  684. mov.w %cc,CMP2_CC(%a6)
  685. mov.l 0x8(%a6), %d2 # get regval
  686. mov.b ([0xc,%a6],0x0),%d0
  687. mov.b ([0xc,%a6],0x1),%d1
  688. extb.l %d0 # sign extend lo bnd
  689. extb.l %d1 # sign extend hi bnd
  690. bra.w l_cmp2_cmp # go do the compare emulation
  691. global _060LSP__cmp2_Aw_
  692. _060LSP__cmp2_Aw_:
  693. # PROLOGUE BEGIN ########################################################
  694. link.w %a6,&-4
  695. movm.l &0x3800,-(%sp) # save d2-d4
  696. # fmovm.l &0x0,-(%sp) # save no fpregs
  697. # PROLOGUE END ##########################################################
  698. mov.w %cc,CMP2_CC(%a6)
  699. mov.l 0x8(%a6), %d2 # get regval
  700. mov.w ([0xc,%a6],0x0),%d0
  701. mov.w ([0xc,%a6],0x2),%d1
  702. ext.l %d0 # sign extend lo bnd
  703. ext.l %d1 # sign extend hi bnd
  704. bra.w l_cmp2_cmp # go do the compare emulation
  705. global _060LSP__cmp2_Al_
  706. _060LSP__cmp2_Al_:
  707. # PROLOGUE BEGIN ########################################################
  708. link.w %a6,&-4
  709. movm.l &0x3800,-(%sp) # save d2-d4
  710. # fmovm.l &0x0,-(%sp) # save no fpregs
  711. # PROLOGUE END ##########################################################
  712. mov.w %cc,CMP2_CC(%a6)
  713. mov.l 0x8(%a6), %d2 # get regval
  714. mov.l ([0xc,%a6],0x0),%d0
  715. mov.l ([0xc,%a6],0x4),%d1
  716. bra.w l_cmp2_cmp # go do the compare emulation
  717. global _060LSP__cmp2_Db_
  718. _060LSP__cmp2_Db_:
  719. # PROLOGUE BEGIN ########################################################
  720. link.w %a6,&-4
  721. movm.l &0x3800,-(%sp) # save d2-d4
  722. # fmovm.l &0x0,-(%sp) # save no fpregs
  723. # PROLOGUE END ##########################################################
  724. mov.w %cc,CMP2_CC(%a6)
  725. mov.l 0x8(%a6), %d2 # get regval
  726. mov.b ([0xc,%a6],0x0),%d0
  727. mov.b ([0xc,%a6],0x1),%d1
  728. extb.l %d0 # sign extend lo bnd
  729. extb.l %d1 # sign extend hi bnd
  730. # operation is a data register compare.
  731. # sign extend byte to long so we can do simple longword compares.
  732. extb.l %d2 # sign extend data byte
  733. bra.w l_cmp2_cmp # go do the compare emulation
  734. global _060LSP__cmp2_Dw_
  735. _060LSP__cmp2_Dw_:
  736. # PROLOGUE BEGIN ########################################################
  737. link.w %a6,&-4
  738. movm.l &0x3800,-(%sp) # save d2-d4
  739. # fmovm.l &0x0,-(%sp) # save no fpregs
  740. # PROLOGUE END ##########################################################
  741. mov.w %cc,CMP2_CC(%a6)
  742. mov.l 0x8(%a6), %d2 # get regval
  743. mov.w ([0xc,%a6],0x0),%d0
  744. mov.w ([0xc,%a6],0x2),%d1
  745. ext.l %d0 # sign extend lo bnd
  746. ext.l %d1 # sign extend hi bnd
  747. # operation is a data register compare.
  748. # sign extend word to long so we can do simple longword compares.
  749. ext.l %d2 # sign extend data word
  750. bra.w l_cmp2_cmp # go emulate compare
  751. global _060LSP__cmp2_Dl_
  752. _060LSP__cmp2_Dl_:
  753. # PROLOGUE BEGIN ########################################################
  754. link.w %a6,&-4
  755. movm.l &0x3800,-(%sp) # save d2-d4
  756. # fmovm.l &0x0,-(%sp) # save no fpregs
  757. # PROLOGUE END ##########################################################
  758. mov.w %cc,CMP2_CC(%a6)
  759. mov.l 0x8(%a6), %d2 # get regval
  760. mov.l ([0xc,%a6],0x0),%d0
  761. mov.l ([0xc,%a6],0x4),%d1
  762. #
  763. # To set the ccodes correctly:
  764. # (1) save 'Z' bit from (Rn - lo)
  765. # (2) save 'Z' and 'N' bits from ((hi - lo) - (Rn - hi))
  766. # (3) keep 'X', 'N', and 'V' from before instruction
  767. # (4) combine ccodes
  768. #
  769. l_cmp2_cmp:
  770. sub.l %d0, %d2 # (Rn - lo)
  771. mov.w %cc, %d3 # fetch resulting ccodes
  772. andi.b &0x4, %d3 # keep 'Z' bit
  773. sub.l %d0, %d1 # (hi - lo)
  774. cmp.l %d1,%d2 # ((hi - lo) - (Rn - hi))
  775. mov.w %cc, %d4 # fetch resulting ccodes
  776. or.b %d4, %d3 # combine w/ earlier ccodes
  777. andi.b &0x5, %d3 # keep 'Z' and 'N'
  778. mov.w CMP2_CC(%a6), %d4 # fetch old ccodes
  779. andi.b &0x1a, %d4 # keep 'X','N','V' bits
  780. or.b %d3, %d4 # insert new ccodes
  781. mov.w %d4,%cc # save new ccodes
  782. # EPILOGUE BEGIN ########################################################
  783. # fmovm.l (%sp)+,&0x0 # restore no fpregs
  784. movm.l (%sp)+,&0x001c # restore d2-d4
  785. unlk %a6
  786. # EPILOGUE END ##########################################################
  787. rts