OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [lib/] [libcpu/] [m68k/] [m68040/] [fpsp/] [srem_mod.S] - Blame information for rev 173

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.