1 * $NetBSD: srem_mod.sa,v 1.3 1994/10/26 07:49:58 cgd Exp $
3 * MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP
4 * M68000 Hi-Performance Microprocessor Division
5 * M68040 Software Package
7 * M68040 Software Package Copyright (c) 1993, 1994 Motorola Inc.
10 * THE SOFTWARE is provided on an "AS IS" basis and without warranty.
11 * To the maximum extent permitted by applicable law,
12 * MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
13 * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
14 * PARTICULAR PURPOSE and any warranty against infringement with
15 * regard to the SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF)
16 * and any accompanying written materials.
18 * To the maximum extent permitted by applicable law,
19 * IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER
20 * (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS
21 * PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR
22 * OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE
23 * SOFTWARE. Motorola assumes no responsibility for the maintenance
24 * and support of the SOFTWARE.
26 * You are hereby granted a copyright license to use, modify, and
27 * distribute the SOFTWARE so long as this entire notice is retained
28 * without alteration in any modified and/or redistributed versions,
29 * and that such modified versions are clearly identified as such.
30 * No licenses are granted by implication, estoppel or otherwise
31 * under any patents or trademarks of Motorola, Inc.
34 * srem_mod.sa 3.1 12/10/90
36 * The entry point sMOD computes the floating point MOD of the
37 * input values X and Y. The entry point sREM computes the floating
38 * point (IEEE) REM of the input values X and Y.
42 * Double-extended value Y is pointed to by address in register
43 * A0. Double-extended value X is located in -12(A0). The values
44 * of X and Y are both nonzero and finite; although either or both
45 * of them can be denormalized. The special cases of zeros, NaNs,
46 * and infinities are handled elsewhere.
50 * FREM(X,Y) or FMOD(X,Y), depending on entry point.
55 * Step 1. Save and strip signs of X and Y: signX := sign(X),
56 * signY := sign(Y), X := |X|, Y := |Y|,
57 * signQ := signX EOR signY. Record whether MOD or REM
60 * Step 2. Set L := expo(X)-expo(Y), k := 0, Q := 0.
62 * R := X, go to Step 4.
64 * R := 2^(-L)X, j := L.
67 * Step 3. Perform MOD(X,Y)
68 * 3.1 If R = Y, go to Step 9.
69 * 3.2 If R > Y, then { R := R - Y, Q := Q + 1}
70 * 3.3 If j = 0, go to Step 4.
71 * 3.4 k := k + 1, j := j - 1, Q := 2Q, R := 2R. Go to
74 * Step 4. At this point, R = X - QY = MOD(X,Y). Set
75 * Last_Subtract := false (used in Step 7 below). If
76 * MOD is requested, go to Step 6.
78 * Step 5. R = MOD(X,Y), but REM(X,Y) is requested.
79 * 5.1 If R < Y/2, then R = MOD(X,Y) = REM(X,Y). Go to
81 * 5.2 If R > Y/2, then { set Last_Subtract := true,
82 * Q := Q + 1, Y := signY*Y }. Go to Step 6.
83 * 5.3 This is the tricky case of R = Y/2. If Q is odd,
84 * then { Q := Q + 1, signX := -signX }.
86 * Step 6. R := signX*R.
88 * Step 7. If Last_Subtract = true, R := R - Y.
90 * Step 8. Return signQ, last 7 bits of Q, and R as required.
92 * Step 9. At this point, R = 2^(-j)*X - Q Y = Y. Thus,
93 * X = 2^(j)*(Q+1)Y. set Q := 2^(j)*(Q+1),
94 * R := 0. Return signQ, last 7 bits of Q, and R.
97 SREM_MOD IDNT 2,1 Motorola 040 Floating Point Software Package
118 Scale DC.L $00010000,$80000000,$00000000,$00000000
131 Move.L #1,Mod_Flag(a6)
134 *..Save sign of X and Y
135 MoveM.L D2-D7,-(A7) ...save data registers
138 AndI.L #$00007FFF,D3 ...Y := |Y|
142 Move.L 8(A0),D5 ...(D3,D4,D5) is |Y|
147 Move.L #$00003FFE,D3 ...$3FFD + 1
158 Sub.L D6,D3 ...(D3,D4,D5) is normalized
167 Move.L D5,D7 ...a copy of D5
172 Or.L D7,D4 ...(D3,D4,D5) normalized
177 AddI.L #$00003FFE,D3 ...(D3,D4,D5) normalized
186 Move.W D1,SignQ(a6) ...sign(Q) obtained
189 Move.L -4(A0),D2 ...(D0,D1,D2) is |X|
203 Sub.L D6,D0 ...(D0,D1,D2) is normalized
212 Move.L D2,D7 ...a copy of D2
217 Or.L D7,D1 ...(D0,D1,D2) normalized
222 AddI.L #$00003FFE,D0 ...(D0,D1,D2) normalized
227 Move.L D3,L_SCR1(a6) ...save biased expo(Y)
228 move.l d0,L_SCR2(a6) ;save d0
229 Sub.L D3,D0 ...L := expo(X)-expo(Y)
230 * Move.L D0,L ...D0 is j
231 CLR.L D6 ...D6 := carry <- 0
233 MoveA.L #0,A1 ...A1 is k; j+k=L, Q=0
235 *..(Carry,D1,D2) is R
239 *..expo(X) < expo(Y). Thus X = mod(X,Y)
241 move.l L_SCR2(a6),d0 ;restore d0
244 *..At this point R = 2^(-L)X; Q = 0; k = 0; and k+j = L
248 Tst.L D6 ...test carry bit
251 *..At this point carry = 0, R = (D1,D2), Y = (D4,D5)
252 Cmp.L D4,D1 ...compare hi(R) and hi(Y)
254 Cmp.L D5,D2 ...compare lo(R) and lo(Y)
257 *..At this point, R = Y
261 *..use the borrow of the previous compare
262 BCS.B R_LT_Y ...borrow is set iff R < Y
265 *..If Carry is set, then Y < (Carry,D1,D2) < 2Y. Otherwise, Carry = 0
266 *..and Y < (D1,D2) < 2Y. Either way, perform R - Y
267 Sub.L D5,D2 ...lo(R) - lo(Y)
268 SubX.L D4,D1 ...hi(R) - hi(Y)
269 CLR.L D6 ...clear carry
270 AddQ.L #1,D3 ...Q := Q + 1
273 *..At this point, Carry=0, R < Y. R = 2^(k-L)X - QY; k+j = L; j >= 0.
274 Tst.L D0 ...see if j = 0.
277 Add.L D3,D3 ...Q := 2Q
278 Add.L D2,D2 ...lo(R) = 2lo(R)
279 AddX.L D1,D1 ...hi(R) = 2hi(R) + carry
280 SCS D6 ...set Carry if 2(R) overflows
281 AddQ.L #1,A1 ...k := k+1
282 SubQ.L #1,D0 ...j := j - 1
283 *..At this point, R=(Carry,D1,D2) = 2^(k-L)X - QY, j+k=L, j >= 0, R < 2Y.
288 *..k = L, j = 0, Carry = 0, R = (D1,D2) = X - QY, R < Y.
291 Move.L L_SCR1(a6),D0 ...new biased expo of R
302 Sub.L D6,D0 ...(D0,D1,D2) is normalized
309 BMI.B Get_Mod ...already normalized
312 Move.L D2,D7 ...a copy of D2
317 Or.L D7,D1 ...(D0,D1,D2) normalized
333 FMove.X R(a6),fp0 ...no exception
334 Move.L #1,Sc_Flag(a6)
355 Move.L Mod_Flag(a6),D6
358 Move.L L_SCR1(a6),D6 ...new biased expo(Y)
359 SubQ.L #1,D6 ...biased expo(Y/2)
375 FSub.X Y(a6),fp0 ...no exceptions
376 AddQ.L #1,D3 ...Q := Q + 1
390 Move.W SignQ(a6),D6 ...D6 is sign(Q)
393 AndI.L #$0000007F,D3 ...7 bits of Q
394 Or.L D6,D3 ...sign and bits of Q
399 FMove.L D6,fpsr ...put Q in fpsr
404 FMove.L USER_FPCR(a6),fpcr
405 Move.L Sc_Flag(a6),D0
407 FMul.X Scale(pc),fp0 ...may cause underflow
408 bra t_avoid_unsupp ;check for denorm as a
409 * ;result of the scaling
412 fmove.x fp0,fp0 ;capture exceptions & round
416 *..R = 2^(-j)X - Q Y = Y, thus R = 0 and quotient = 2^j (Q+1)
418 CmpI.L #8,D0 ...D0 is j
428 FMove.S #:00000000,fp0
437 BEq.W Fix_Sign ...Q is even
439 *..Q is odd, Q := Q + 1, signX := -signX