srem_mod.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. | For details on the license for this file, please see the
  69. | file, README, in this same directory.
  70. SREM_MOD: |idnt 2,1 | Motorola 040 Floating Point Software Package
  71. |section 8
  72. #include "fpsp.h"
  73. .set Mod_Flag,L_SCR3
  74. .set SignY,FP_SCR3+4
  75. .set SignX,FP_SCR3+8
  76. .set SignQ,FP_SCR3+12
  77. .set Sc_Flag,FP_SCR4
  78. .set Y,FP_SCR1
  79. .set Y_Hi,Y+4
  80. .set Y_Lo,Y+8
  81. .set R,FP_SCR2
  82. .set R_Hi,R+4
  83. .set R_Lo,R+8
  84. Scale: .long 0x00010000,0x80000000,0x00000000,0x00000000
  85. |xref t_avoid_unsupp
  86. .global smod
  87. smod:
  88. movel #0,Mod_Flag(%a6)
  89. bras Mod_Rem
  90. .global srem
  91. srem:
  92. movel #1,Mod_Flag(%a6)
  93. Mod_Rem:
  94. |..Save sign of X and Y
  95. moveml %d2-%d7,-(%a7) | ...save data registers
  96. movew (%a0),%d3
  97. movew %d3,SignY(%a6)
  98. andil #0x00007FFF,%d3 | ...Y := |Y|
  99. |
  100. movel 4(%a0),%d4
  101. movel 8(%a0),%d5 | ...(D3,D4,D5) is |Y|
  102. tstl %d3
  103. bnes Y_Normal
  104. movel #0x00003FFE,%d3 | ...$3FFD + 1
  105. tstl %d4
  106. bnes HiY_not0
  107. HiY_0:
  108. movel %d5,%d4
  109. clrl %d5
  110. subil #32,%d3
  111. clrl %d6
  112. bfffo %d4{#0:#32},%d6
  113. lsll %d6,%d4
  114. subl %d6,%d3 | ...(D3,D4,D5) is normalized
  115. | ...with bias $7FFD
  116. bras Chk_X
  117. HiY_not0:
  118. clrl %d6
  119. bfffo %d4{#0:#32},%d6
  120. subl %d6,%d3
  121. lsll %d6,%d4
  122. movel %d5,%d7 | ...a copy of D5
  123. lsll %d6,%d5
  124. negl %d6
  125. addil #32,%d6
  126. lsrl %d6,%d7
  127. orl %d7,%d4 | ...(D3,D4,D5) normalized
  128. | ...with bias $7FFD
  129. bras Chk_X
  130. Y_Normal:
  131. addil #0x00003FFE,%d3 | ...(D3,D4,D5) normalized
  132. | ...with bias $7FFD
  133. Chk_X:
  134. movew -12(%a0),%d0
  135. movew %d0,SignX(%a6)
  136. movew SignY(%a6),%d1
  137. eorl %d0,%d1
  138. andil #0x00008000,%d1
  139. movew %d1,SignQ(%a6) | ...sign(Q) obtained
  140. andil #0x00007FFF,%d0
  141. movel -8(%a0),%d1
  142. movel -4(%a0),%d2 | ...(D0,D1,D2) is |X|
  143. tstl %d0
  144. bnes X_Normal
  145. movel #0x00003FFE,%d0
  146. tstl %d1
  147. bnes HiX_not0
  148. HiX_0:
  149. movel %d2,%d1
  150. clrl %d2
  151. subil #32,%d0
  152. clrl %d6
  153. bfffo %d1{#0:#32},%d6
  154. lsll %d6,%d1
  155. subl %d6,%d0 | ...(D0,D1,D2) is normalized
  156. | ...with bias $7FFD
  157. bras Init
  158. HiX_not0:
  159. clrl %d6
  160. bfffo %d1{#0:#32},%d6
  161. subl %d6,%d0
  162. lsll %d6,%d1
  163. movel %d2,%d7 | ...a copy of D2
  164. lsll %d6,%d2
  165. negl %d6
  166. addil #32,%d6
  167. lsrl %d6,%d7
  168. orl %d7,%d1 | ...(D0,D1,D2) normalized
  169. | ...with bias $7FFD
  170. bras Init
  171. X_Normal:
  172. addil #0x00003FFE,%d0 | ...(D0,D1,D2) normalized
  173. | ...with bias $7FFD
  174. Init:
  175. |
  176. movel %d3,L_SCR1(%a6) | ...save biased expo(Y)
  177. movel %d0,L_SCR2(%a6) |save d0
  178. subl %d3,%d0 | ...L := expo(X)-expo(Y)
  179. | Move.L D0,L ...D0 is j
  180. clrl %d6 | ...D6 := carry <- 0
  181. clrl %d3 | ...D3 is Q
  182. moveal #0,%a1 | ...A1 is k; j+k=L, Q=0
  183. |..(Carry,D1,D2) is R
  184. tstl %d0
  185. bges Mod_Loop
  186. |..expo(X) < expo(Y). Thus X = mod(X,Y)
  187. |
  188. movel L_SCR2(%a6),%d0 |restore d0
  189. bra Get_Mod
  190. |..At this point R = 2^(-L)X; Q = 0; k = 0; and k+j = L
  191. Mod_Loop:
  192. tstl %d6 | ...test carry bit
  193. bgts R_GT_Y
  194. |..At this point carry = 0, R = (D1,D2), Y = (D4,D5)
  195. cmpl %d4,%d1 | ...compare hi(R) and hi(Y)
  196. bnes R_NE_Y
  197. cmpl %d5,%d2 | ...compare lo(R) and lo(Y)
  198. bnes R_NE_Y
  199. |..At this point, R = Y
  200. bra Rem_is_0
  201. R_NE_Y:
  202. |..use the borrow of the previous compare
  203. bcss R_LT_Y | ...borrow is set iff R < Y
  204. R_GT_Y:
  205. |..If Carry is set, then Y < (Carry,D1,D2) < 2Y. Otherwise, Carry = 0
  206. |..and Y < (D1,D2) < 2Y. Either way, perform R - Y
  207. subl %d5,%d2 | ...lo(R) - lo(Y)
  208. subxl %d4,%d1 | ...hi(R) - hi(Y)
  209. clrl %d6 | ...clear carry
  210. addql #1,%d3 | ...Q := Q + 1
  211. R_LT_Y:
  212. |..At this point, Carry=0, R < Y. R = 2^(k-L)X - QY; k+j = L; j >= 0.
  213. tstl %d0 | ...see if j = 0.
  214. beqs PostLoop
  215. addl %d3,%d3 | ...Q := 2Q
  216. addl %d2,%d2 | ...lo(R) = 2lo(R)
  217. roxll #1,%d1 | ...hi(R) = 2hi(R) + carry
  218. scs %d6 | ...set Carry if 2(R) overflows
  219. addql #1,%a1 | ...k := k+1
  220. subql #1,%d0 | ...j := j - 1
  221. |..At this point, R=(Carry,D1,D2) = 2^(k-L)X - QY, j+k=L, j >= 0, R < 2Y.
  222. bras Mod_Loop
  223. PostLoop:
  224. |..k = L, j = 0, Carry = 0, R = (D1,D2) = X - QY, R < Y.
  225. |..normalize R.
  226. movel L_SCR1(%a6),%d0 | ...new biased expo of R
  227. tstl %d1
  228. bnes HiR_not0
  229. HiR_0:
  230. movel %d2,%d1
  231. clrl %d2
  232. subil #32,%d0
  233. clrl %d6
  234. bfffo %d1{#0:#32},%d6
  235. lsll %d6,%d1
  236. subl %d6,%d0 | ...(D0,D1,D2) is normalized
  237. | ...with bias $7FFD
  238. bras Get_Mod
  239. HiR_not0:
  240. clrl %d6
  241. bfffo %d1{#0:#32},%d6
  242. bmis Get_Mod | ...already normalized
  243. subl %d6,%d0
  244. lsll %d6,%d1
  245. movel %d2,%d7 | ...a copy of D2
  246. lsll %d6,%d2
  247. negl %d6
  248. addil #32,%d6
  249. lsrl %d6,%d7
  250. orl %d7,%d1 | ...(D0,D1,D2) normalized
  251. |
  252. Get_Mod:
  253. cmpil #0x000041FE,%d0
  254. bges No_Scale
  255. Do_Scale:
  256. movew %d0,R(%a6)
  257. clrw R+2(%a6)
  258. movel %d1,R_Hi(%a6)
  259. movel %d2,R_Lo(%a6)
  260. movel L_SCR1(%a6),%d6
  261. movew %d6,Y(%a6)
  262. clrw Y+2(%a6)
  263. movel %d4,Y_Hi(%a6)
  264. movel %d5,Y_Lo(%a6)
  265. fmovex R(%a6),%fp0 | ...no exception
  266. movel #1,Sc_Flag(%a6)
  267. bras ModOrRem
  268. No_Scale:
  269. movel %d1,R_Hi(%a6)
  270. movel %d2,R_Lo(%a6)
  271. subil #0x3FFE,%d0
  272. movew %d0,R(%a6)
  273. clrw R+2(%a6)
  274. movel L_SCR1(%a6),%d6
  275. subil #0x3FFE,%d6
  276. movel %d6,L_SCR1(%a6)
  277. fmovex R(%a6),%fp0
  278. movew %d6,Y(%a6)
  279. movel %d4,Y_Hi(%a6)
  280. movel %d5,Y_Lo(%a6)
  281. movel #0,Sc_Flag(%a6)
  282. |
  283. ModOrRem:
  284. movel Mod_Flag(%a6),%d6
  285. beqs Fix_Sign
  286. movel L_SCR1(%a6),%d6 | ...new biased expo(Y)
  287. subql #1,%d6 | ...biased expo(Y/2)
  288. cmpl %d6,%d0
  289. blts Fix_Sign
  290. bgts Last_Sub
  291. cmpl %d4,%d1
  292. bnes Not_EQ
  293. cmpl %d5,%d2
  294. bnes Not_EQ
  295. bra Tie_Case
  296. Not_EQ:
  297. bcss Fix_Sign
  298. Last_Sub:
  299. |
  300. fsubx Y(%a6),%fp0 | ...no exceptions
  301. addql #1,%d3 | ...Q := Q + 1
  302. |
  303. Fix_Sign:
  304. |..Get sign of X
  305. movew SignX(%a6),%d6
  306. bges Get_Q
  307. fnegx %fp0
  308. |..Get Q
  309. |
  310. Get_Q:
  311. clrl %d6
  312. movew SignQ(%a6),%d6 | ...D6 is sign(Q)
  313. movel #8,%d7
  314. lsrl %d7,%d6
  315. andil #0x0000007F,%d3 | ...7 bits of Q
  316. orl %d6,%d3 | ...sign and bits of Q
  317. swap %d3
  318. fmovel %fpsr,%d6
  319. andil #0xFF00FFFF,%d6
  320. orl %d3,%d6
  321. fmovel %d6,%fpsr | ...put Q in fpsr
  322. |
  323. Restore:
  324. moveml (%a7)+,%d2-%d7
  325. fmovel USER_FPCR(%a6),%fpcr
  326. movel Sc_Flag(%a6),%d0
  327. beqs Finish
  328. fmulx Scale(%pc),%fp0 | ...may cause underflow
  329. bra t_avoid_unsupp |check for denorm as a
  330. | ;result of the scaling
  331. Finish:
  332. fmovex %fp0,%fp0 |capture exceptions & round
  333. rts
  334. Rem_is_0:
  335. |..R = 2^(-j)X - Q Y = Y, thus R = 0 and quotient = 2^j (Q+1)
  336. addql #1,%d3
  337. cmpil #8,%d0 | ...D0 is j
  338. bges Q_Big
  339. lsll %d0,%d3
  340. bras Set_R_0
  341. Q_Big:
  342. clrl %d3
  343. Set_R_0:
  344. fmoves #0x00000000,%fp0
  345. movel #0,Sc_Flag(%a6)
  346. bra Fix_Sign
  347. Tie_Case:
  348. |..Check parity of Q
  349. movel %d3,%d6
  350. andil #0x00000001,%d6
  351. tstl %d6
  352. beq Fix_Sign | ...Q is even
  353. |..Q is odd, Q := Q + 1, signX := -signX
  354. addql #1,%d3
  355. movew SignX(%a6),%d6
  356. eoril #0x00008000,%d6
  357. movew %d6,SignX(%a6)
  358. bra Fix_Sign
  359. |end