srem_mod.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. |
  2. | srem_mod.sa 3.1 12/10/90
  3. |
  4. | The entry point sMOD computes the floating point MOD of the
  5. | input values X and Y. The entry point sREM computes the floating
  6. | point (IEEE) REM of the input values X and Y.
  7. |
  8. | INPUT
  9. | -----
  10. | Double-extended value Y is pointed to by address in register
  11. | A0. Double-extended value X is located in -12(A0). The values
  12. | of X and Y are both nonzero and finite; although either or both
  13. | of them can be denormalized. The special cases of zeros, NaNs,
  14. | and infinities are handled elsewhere.
  15. |
  16. | OUTPUT
  17. | ------
  18. | FREM(X,Y) or FMOD(X,Y), depending on entry point.
  19. |
  20. | ALGORITHM
  21. | ---------
  22. |
  23. | Step 1. Save and strip signs of X and Y: signX := sign(X),
  24. | signY := sign(Y), X := |X|, Y := |Y|,
  25. | signQ := signX EOR signY. Record whether MOD or REM
  26. | is requested.
  27. |
  28. | Step 2. Set L := expo(X)-expo(Y), k := 0, Q := 0.
  29. | If (L < 0) then
  30. | R := X, go to Step 4.
  31. | else
  32. | R := 2^(-L)X, j := L.
  33. | endif
  34. |
  35. | Step 3. Perform MOD(X,Y)
  36. | 3.1 If R = Y, go to Step 9.
  37. | 3.2 If R > Y, then { R := R - Y, Q := Q + 1}
  38. | 3.3 If j = 0, go to Step 4.
  39. | 3.4 k := k + 1, j := j - 1, Q := 2Q, R := 2R. Go to
  40. | Step 3.1.
  41. |
  42. | Step 4. At this point, R = X - QY = MOD(X,Y). Set
  43. | Last_Subtract := false (used in Step 7 below). If
  44. | MOD is requested, go to Step 6.
  45. |
  46. | Step 5. R = MOD(X,Y), but REM(X,Y) is requested.
  47. | 5.1 If R < Y/2, then R = MOD(X,Y) = REM(X,Y). Go to
  48. | Step 6.
  49. | 5.2 If R > Y/2, then { set Last_Subtract := true,
  50. | Q := Q + 1, Y := signY*Y }. Go to Step 6.
  51. | 5.3 This is the tricky case of R = Y/2. If Q is odd,
  52. | then { Q := Q + 1, signX := -signX }.
  53. |
  54. | Step 6. R := signX*R.
  55. |
  56. | Step 7. If Last_Subtract = true, R := R - Y.
  57. |
  58. | Step 8. Return signQ, last 7 bits of Q, and R as required.
  59. |
  60. | Step 9. At this point, R = 2^(-j)*X - Q Y = Y. Thus,
  61. | X = 2^(j)*(Q+1)Y. set Q := 2^(j)*(Q+1),
  62. | R := 0. Return signQ, last 7 bits of Q, and R.
  63. |
  64. |
  65. | Copyright (C) Motorola, Inc. 1990
  66. | All Rights Reserved
  67. |
  68. | THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF MOTOROLA
  69. | The copyright notice above does not evidence any
  70. | actual or intended publication of such source code.
  71. SREM_MOD: |idnt 2,1 | Motorola 040 Floating Point Software Package
  72. |section 8
  73. #include "fpsp.h"
  74. .set Mod_Flag,L_SCR3
  75. .set SignY,FP_SCR3+4
  76. .set SignX,FP_SCR3+8
  77. .set SignQ,FP_SCR3+12
  78. .set Sc_Flag,FP_SCR4
  79. .set Y,FP_SCR1
  80. .set Y_Hi,Y+4
  81. .set Y_Lo,Y+8
  82. .set R,FP_SCR2
  83. .set R_Hi,R+4
  84. .set R_Lo,R+8
  85. Scale: .long 0x00010000,0x80000000,0x00000000,0x00000000
  86. |xref t_avoid_unsupp
  87. .global smod
  88. smod:
  89. movel #0,Mod_Flag(%a6)
  90. bras Mod_Rem
  91. .global srem
  92. srem:
  93. movel #1,Mod_Flag(%a6)
  94. Mod_Rem:
  95. |..Save sign of X and Y
  96. moveml %d2-%d7,-(%a7) | ...save data registers
  97. movew (%a0),%d3
  98. movew %d3,SignY(%a6)
  99. andil #0x00007FFF,%d3 | ...Y := |Y|
  100. |
  101. movel 4(%a0),%d4
  102. movel 8(%a0),%d5 | ...(D3,D4,D5) is |Y|
  103. tstl %d3
  104. bnes Y_Normal
  105. movel #0x00003FFE,%d3 | ...$3FFD + 1
  106. tstl %d4
  107. bnes HiY_not0
  108. HiY_0:
  109. movel %d5,%d4
  110. clrl %d5
  111. subil #32,%d3
  112. clrl %d6
  113. bfffo %d4{#0:#32},%d6
  114. lsll %d6,%d4
  115. subl %d6,%d3 | ...(D3,D4,D5) is normalized
  116. | ...with bias $7FFD
  117. bras Chk_X
  118. HiY_not0:
  119. clrl %d6
  120. bfffo %d4{#0:#32},%d6
  121. subl %d6,%d3
  122. lsll %d6,%d4
  123. movel %d5,%d7 | ...a copy of D5
  124. lsll %d6,%d5
  125. negl %d6
  126. addil #32,%d6
  127. lsrl %d6,%d7
  128. orl %d7,%d4 | ...(D3,D4,D5) normalized
  129. | ...with bias $7FFD
  130. bras Chk_X
  131. Y_Normal:
  132. addil #0x00003FFE,%d3 | ...(D3,D4,D5) normalized
  133. | ...with bias $7FFD
  134. Chk_X:
  135. movew -12(%a0),%d0
  136. movew %d0,SignX(%a6)
  137. movew SignY(%a6),%d1
  138. eorl %d0,%d1
  139. andil #0x00008000,%d1
  140. movew %d1,SignQ(%a6) | ...sign(Q) obtained
  141. andil #0x00007FFF,%d0
  142. movel -8(%a0),%d1
  143. movel -4(%a0),%d2 | ...(D0,D1,D2) is |X|
  144. tstl %d0
  145. bnes X_Normal
  146. movel #0x00003FFE,%d0
  147. tstl %d1
  148. bnes HiX_not0
  149. HiX_0:
  150. movel %d2,%d1
  151. clrl %d2
  152. subil #32,%d0
  153. clrl %d6
  154. bfffo %d1{#0:#32},%d6
  155. lsll %d6,%d1
  156. subl %d6,%d0 | ...(D0,D1,D2) is normalized
  157. | ...with bias $7FFD
  158. bras Init
  159. HiX_not0:
  160. clrl %d6
  161. bfffo %d1{#0:#32},%d6
  162. subl %d6,%d0
  163. lsll %d6,%d1
  164. movel %d2,%d7 | ...a copy of D2
  165. lsll %d6,%d2
  166. negl %d6
  167. addil #32,%d6
  168. lsrl %d6,%d7
  169. orl %d7,%d1 | ...(D0,D1,D2) normalized
  170. | ...with bias $7FFD
  171. bras Init
  172. X_Normal:
  173. addil #0x00003FFE,%d0 | ...(D0,D1,D2) normalized
  174. | ...with bias $7FFD
  175. Init:
  176. |
  177. movel %d3,L_SCR1(%a6) | ...save biased expo(Y)
  178. movel %d0,L_SCR2(%a6) |save d0
  179. subl %d3,%d0 | ...L := expo(X)-expo(Y)
  180. | Move.L D0,L ...D0 is j
  181. clrl %d6 | ...D6 := carry <- 0
  182. clrl %d3 | ...D3 is Q
  183. moveal #0,%a1 | ...A1 is k; j+k=L, Q=0
  184. |..(Carry,D1,D2) is R
  185. tstl %d0
  186. bges Mod_Loop
  187. |..expo(X) < expo(Y). Thus X = mod(X,Y)
  188. |
  189. movel L_SCR2(%a6),%d0 |restore d0
  190. bra Get_Mod
  191. |..At this point R = 2^(-L)X; Q = 0; k = 0; and k+j = L
  192. Mod_Loop:
  193. tstl %d6 | ...test carry bit
  194. bgts R_GT_Y
  195. |..At this point carry = 0, R = (D1,D2), Y = (D4,D5)
  196. cmpl %d4,%d1 | ...compare hi(R) and hi(Y)
  197. bnes R_NE_Y
  198. cmpl %d5,%d2 | ...compare lo(R) and lo(Y)
  199. bnes R_NE_Y
  200. |..At this point, R = Y
  201. bra Rem_is_0
  202. R_NE_Y:
  203. |..use the borrow of the previous compare
  204. bcss R_LT_Y | ...borrow is set iff R < Y
  205. R_GT_Y:
  206. |..If Carry is set, then Y < (Carry,D1,D2) < 2Y. Otherwise, Carry = 0
  207. |..and Y < (D1,D2) < 2Y. Either way, perform R - Y
  208. subl %d5,%d2 | ...lo(R) - lo(Y)
  209. subxl %d4,%d1 | ...hi(R) - hi(Y)
  210. clrl %d6 | ...clear carry
  211. addql #1,%d3 | ...Q := Q + 1
  212. R_LT_Y:
  213. |..At this point, Carry=0, R < Y. R = 2^(k-L)X - QY; k+j = L; j >= 0.
  214. tstl %d0 | ...see if j = 0.
  215. beqs PostLoop
  216. addl %d3,%d3 | ...Q := 2Q
  217. addl %d2,%d2 | ...lo(R) = 2lo(R)
  218. roxll #1,%d1 | ...hi(R) = 2hi(R) + carry
  219. scs %d6 | ...set Carry if 2(R) overflows
  220. addql #1,%a1 | ...k := k+1
  221. subql #1,%d0 | ...j := j - 1
  222. |..At this point, R=(Carry,D1,D2) = 2^(k-L)X - QY, j+k=L, j >= 0, R < 2Y.
  223. bras Mod_Loop
  224. PostLoop:
  225. |..k = L, j = 0, Carry = 0, R = (D1,D2) = X - QY, R < Y.
  226. |..normalize R.
  227. movel L_SCR1(%a6),%d0 | ...new biased expo of R
  228. tstl %d1
  229. bnes HiR_not0
  230. HiR_0:
  231. movel %d2,%d1
  232. clrl %d2
  233. subil #32,%d0
  234. clrl %d6
  235. bfffo %d1{#0:#32},%d6
  236. lsll %d6,%d1
  237. subl %d6,%d0 | ...(D0,D1,D2) is normalized
  238. | ...with bias $7FFD
  239. bras Get_Mod
  240. HiR_not0:
  241. clrl %d6
  242. bfffo %d1{#0:#32},%d6
  243. bmis Get_Mod | ...already normalized
  244. subl %d6,%d0
  245. lsll %d6,%d1
  246. movel %d2,%d7 | ...a copy of D2
  247. lsll %d6,%d2
  248. negl %d6
  249. addil #32,%d6
  250. lsrl %d6,%d7
  251. orl %d7,%d1 | ...(D0,D1,D2) normalized
  252. |
  253. Get_Mod:
  254. cmpil #0x000041FE,%d0
  255. bges No_Scale
  256. Do_Scale:
  257. movew %d0,R(%a6)
  258. clrw R+2(%a6)
  259. movel %d1,R_Hi(%a6)
  260. movel %d2,R_Lo(%a6)
  261. movel L_SCR1(%a6),%d6
  262. movew %d6,Y(%a6)
  263. clrw Y+2(%a6)
  264. movel %d4,Y_Hi(%a6)
  265. movel %d5,Y_Lo(%a6)
  266. fmovex R(%a6),%fp0 | ...no exception
  267. movel #1,Sc_Flag(%a6)
  268. bras ModOrRem
  269. No_Scale:
  270. movel %d1,R_Hi(%a6)
  271. movel %d2,R_Lo(%a6)
  272. subil #0x3FFE,%d0
  273. movew %d0,R(%a6)
  274. clrw R+2(%a6)
  275. movel L_SCR1(%a6),%d6
  276. subil #0x3FFE,%d6
  277. movel %d6,L_SCR1(%a6)
  278. fmovex R(%a6),%fp0
  279. movew %d6,Y(%a6)
  280. movel %d4,Y_Hi(%a6)
  281. movel %d5,Y_Lo(%a6)
  282. movel #0,Sc_Flag(%a6)
  283. |
  284. ModOrRem:
  285. movel Mod_Flag(%a6),%d6
  286. beqs Fix_Sign
  287. movel L_SCR1(%a6),%d6 | ...new biased expo(Y)
  288. subql #1,%d6 | ...biased expo(Y/2)
  289. cmpl %d6,%d0
  290. blts Fix_Sign
  291. bgts Last_Sub
  292. cmpl %d4,%d1
  293. bnes Not_EQ
  294. cmpl %d5,%d2
  295. bnes Not_EQ
  296. bra Tie_Case
  297. Not_EQ:
  298. bcss Fix_Sign
  299. Last_Sub:
  300. |
  301. fsubx Y(%a6),%fp0 | ...no exceptions
  302. addql #1,%d3 | ...Q := Q + 1
  303. |
  304. Fix_Sign:
  305. |..Get sign of X
  306. movew SignX(%a6),%d6
  307. bges Get_Q
  308. fnegx %fp0
  309. |..Get Q
  310. |
  311. Get_Q:
  312. clrl %d6
  313. movew SignQ(%a6),%d6 | ...D6 is sign(Q)
  314. movel #8,%d7
  315. lsrl %d7,%d6
  316. andil #0x0000007F,%d3 | ...7 bits of Q
  317. orl %d6,%d3 | ...sign and bits of Q
  318. swap %d3
  319. fmovel %fpsr,%d6
  320. andil #0xFF00FFFF,%d6
  321. orl %d3,%d6
  322. fmovel %d6,%fpsr | ...put Q in fpsr
  323. |
  324. Restore:
  325. moveml (%a7)+,%d2-%d7
  326. fmovel USER_FPCR(%a6),%fpcr
  327. movel Sc_Flag(%a6),%d0
  328. beqs Finish
  329. fmulx Scale(%pc),%fp0 | ...may cause underflow
  330. bra t_avoid_unsupp |check for denorm as a
  331. | ;result of the scaling
  332. Finish:
  333. fmovex %fp0,%fp0 |capture exceptions & round
  334. rts
  335. Rem_is_0:
  336. |..R = 2^(-j)X - Q Y = Y, thus R = 0 and quotient = 2^j (Q+1)
  337. addql #1,%d3
  338. cmpil #8,%d0 | ...D0 is j
  339. bges Q_Big
  340. lsll %d0,%d3
  341. bras Set_R_0
  342. Q_Big:
  343. clrl %d3
  344. Set_R_0:
  345. fmoves #0x00000000,%fp0
  346. movel #0,Sc_Flag(%a6)
  347. bra Fix_Sign
  348. Tie_Case:
  349. |..Check parity of Q
  350. movel %d3,%d6
  351. andil #0x00000001,%d6
  352. tstl %d6
  353. beq Fix_Sign | ...Q is even
  354. |..Q is odd, Q := Q + 1, signX := -signX
  355. addql #1,%d3
  356. movew SignX(%a6),%d6
  357. eoril #0x00008000,%d6
  358. movew %d6,SignX(%a6)
  359. bra Fix_Sign
  360. |end