2 | srem_mod.sa 3.1 12/10/90
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.
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.
18 | FREM(X,Y) or FMOD(X,Y), depending on entry point.
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
28 | Step 2. Set L := expo(X)-expo(Y), k := 0, Q := 0.
30 | R := X, go to Step 4.
32 | R := 2^(-L)X, j := L.
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
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.
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
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 }.
54 | Step 6. R := signX*R.
56 | Step 7. If Last_Subtract = true, R := R - Y.
58 | Step 8. Return signQ, last 7 bits of Q, and R as required.
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.
66 | Copyright (C) Motorola, Inc. 1990
69 | THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF MOTOROLA
70 | The copyright notice above does not evidence any
71 | actual or intended publication of such source code.
73 SREM_MOD: |idnt 2,1 | Motorola 040 Floating Point Software Package
94 Scale: .long 0x00010000,0x80000000,0x00000000,0x00000000
101 movel #0,Mod_Flag(%a6)
107 movel #1,Mod_Flag(%a6)
110 |..Save sign of X and Y
111 moveml %d2-%d7,-(%a7) | ...save data registers
114 andil #0x00007FFF,%d3 | ...Y := |Y|
118 movel 8(%a0),%d5 | ...(D3,D4,D5) is |Y|
123 movel #0x00003FFE,%d3 | ...$3FFD + 1
132 bfffo %d4{#0:#32},%d6
134 subl %d6,%d3 | ...(D3,D4,D5) is normalized
140 bfffo %d4{#0:#32},%d6
143 movel %d5,%d7 | ...a copy of D5
148 orl %d7,%d4 | ...(D3,D4,D5) normalized
153 addil #0x00003FFE,%d3 | ...(D3,D4,D5) normalized
161 andil #0x00008000,%d1
162 movew %d1,SignQ(%a6) | ...sign(Q) obtained
163 andil #0x00007FFF,%d0
165 movel -4(%a0),%d2 | ...(D0,D1,D2) is |X|
168 movel #0x00003FFE,%d0
177 bfffo %d1{#0:#32},%d6
179 subl %d6,%d0 | ...(D0,D1,D2) is normalized
185 bfffo %d1{#0:#32},%d6
188 movel %d2,%d7 | ...a copy of D2
193 orl %d7,%d1 | ...(D0,D1,D2) normalized
198 addil #0x00003FFE,%d0 | ...(D0,D1,D2) normalized
203 movel %d3,L_SCR1(%a6) | ...save biased expo(Y)
204 movel %d0,L_SCR2(%a6) |save d0
205 subl %d3,%d0 | ...L := expo(X)-expo(Y)
206 | Move.L D0,L ...D0 is j
207 clrl %d6 | ...D6 := carry <- 0
208 clrl %d3 | ...D3 is Q
209 moveal #0,%a1 | ...A1 is k; j+k=L, Q=0
211 |..(Carry,D1,D2) is R
215 |..expo(X) < expo(Y). Thus X = mod(X,Y)
217 movel L_SCR2(%a6),%d0 |restore d0
220 |..At this point R = 2^(-L)X; Q = 0; k = 0; and k+j = L
224 tstl %d6 | ...test carry bit
227 |..At this point carry = 0, R = (D1,D2), Y = (D4,D5)
228 cmpl %d4,%d1 | ...compare hi(R) and hi(Y)
230 cmpl %d5,%d2 | ...compare lo(R) and lo(Y)
233 |..At this point, R = Y
237 |..use the borrow of the previous compare
238 bcss R_LT_Y | ...borrow is set iff R < Y
241 |..If Carry is set, then Y < (Carry,D1,D2) < 2Y. Otherwise, Carry = 0
242 |..and Y < (D1,D2) < 2Y. Either way, perform R - Y
243 subl %d5,%d2 | ...lo(R) - lo(Y)
244 subxl %d4,%d1 | ...hi(R) - hi(Y)
245 clrl %d6 | ...clear carry
246 addql #1,%d3 | ...Q := Q + 1
249 |..At this point, Carry=0, R < Y. R = 2^(k-L)X - QY; k+j = L; j >= 0.
250 tstl %d0 | ...see if j = 0.
253 addl %d3,%d3 | ...Q := 2Q
254 addl %d2,%d2 | ...lo(R) = 2lo(R)
255 roxll #1,%d1 | ...hi(R) = 2hi(R) + carry
256 scs %d6 | ...set Carry if 2(R) overflows
257 addql #1,%a1 | ...k := k+1
258 subql #1,%d0 | ...j := j - 1
259 |..At this point, R=(Carry,D1,D2) = 2^(k-L)X - QY, j+k=L, j >= 0, R < 2Y.
264 |..k = L, j = 0, Carry = 0, R = (D1,D2) = X - QY, R < Y.
267 movel L_SCR1(%a6),%d0 | ...new biased expo of R
276 bfffo %d1{#0:#32},%d6
278 subl %d6,%d0 | ...(D0,D1,D2) is normalized
284 bfffo %d1{#0:#32},%d6
285 bmis Get_Mod | ...already normalized
288 movel %d2,%d7 | ...a copy of D2
293 orl %d7,%d1 | ...(D0,D1,D2) normalized
297 cmpil #0x000041FE,%d0
304 movel L_SCR1(%a6),%d6
309 fmovex R(%a6),%fp0 | ...no exception
310 movel #1,Sc_Flag(%a6)
318 movel L_SCR1(%a6),%d6
320 movel %d6,L_SCR1(%a6)
325 movel #0,Sc_Flag(%a6)
331 movel Mod_Flag(%a6),%d6
334 movel L_SCR1(%a6),%d6 | ...new biased expo(Y)
335 subql #1,%d6 | ...biased expo(Y/2)
351 fsubx Y(%a6),%fp0 | ...no exceptions
352 addql #1,%d3 | ...Q := Q + 1
366 movew SignQ(%a6),%d6 | ...D6 is sign(Q)
369 andil #0x0000007F,%d3 | ...7 bits of Q
370 orl %d6,%d3 | ...sign and bits of Q
373 andil #0xFF00FFFF,%d6
375 fmovel %d6,%fpsr | ...put Q in fpsr
379 moveml (%a7)+,%d2-%d7
380 fmovel USER_FPCR(%a6),%fpcr
381 movel Sc_Flag(%a6),%d0
383 fmulx Scale(%pc),%fp0 | ...may cause underflow
384 bra t_avoid_unsupp |check for denorm as a
385 | ;result of the scaling
388 fmovex %fp0,%fp0 |capture exceptions & round
392 |..R = 2^(-j)X - Q Y = Y, thus R = 0 and quotient = 2^j (Q+1)
394 cmpil #8,%d0 | ...D0 is j
404 fmoves #0x00000000,%fp0
405 movel #0,Sc_Flag(%a6)
411 andil #0x00000001,%d6
413 beq Fix_Sign | ...Q is even
415 |..Q is odd, Q := Q + 1, signX := -signX
418 eoril #0x00008000,%d6