2005-04-17 02:20:36 +04:00
|
| srem_ m o d . s a 3 . 1 1 2 / 1 0 / 9 0
|
| The e n t r y p o i n t s M O D c o m p u t e s t h e f l o a t i n g p o i n t M O D o f t h e
| input v a l u e s X a n d Y . T h e e n t r y p o i n t s R E M c o m p u t e s t h e f l o a t i n g
| point ( I E E E ) R E M o f t h e i n p u t v a l u e s X a n d Y .
|
| INPUT
| - - - - -
| Double- e x t e n d e d v a l u e Y i s p o i n t e d t o b y a d d r e s s i n r e g i s t e r
| A0 . D o u b l e - e x t e n d e d v a l u e X i s l o c a t e d i n - 1 2 ( A 0 ) . T h e v a l u e s
| of X a n d Y a r e b o t h n o n z e r o a n d f i n i t e ; although either or both
| of t h e m c a n b e d e n o r m a l i z e d . T h e s p e c i a l c a s e s o f z e r o s , N a N s ,
| and i n f i n i t i e s a r e h a n d l e d e l s e w h e r e .
|
| OUTPUT
| - - - - - -
| FREM( X ,Y ) o r F M O D ( X ,Y ) , d e p e n d i n g o n e n t r y p o i n t .
|
| ALGORITHM
| - - - - - - - - -
|
| Step 1 . S a v e a n d s t r i p s i g n s o f X a n d Y : s i g n X : = s i g n ( X ) ,
| signY : = s i g n ( Y ) , X : = | X | , Y : = | Y | ,
| signQ : = s i g n X E O R s i g n Y . R e c o r d w h e t h e r M O D o r R E M
| is r e q u e s t e d .
|
| Step 2 . S e t L : = e x p o ( X ) - e x p o ( Y ) , k : = 0 , Q : = 0 .
| If ( L < 0 ) t h e n
| R : = X , g o t o S t e p 4 .
| else
| R : = 2 ^ ( - L ) X , j : = L .
| endif
|
| Step 3 . P e r f o r m M O D ( X ,Y )
| 3 .1 If R = Y , g o t o S t e p 9 .
| 3 .2 If R > Y , t h e n { R : = R - Y , Q : = Q + 1 }
| 3 .3 If j = 0 , g o t o S t e p 4 .
| 3 .4 k : = k + 1 , j : = j - 1 , Q : = 2 Q , R : = 2 R . G o t o
| Step 3 . 1 .
|
| Step 4 . A t t h i s p o i n t , R = X - Q Y = M O D ( X ,Y ) . S e t
| Last_ S u b t r a c t : = f a l s e ( u s e d i n S t e p 7 b e l o w ) . I f
| MOD i s r e q u e s t e d , g o t o S t e p 6 .
|
| Step 5 . R = M O D ( X ,Y ) , b u t R E M ( X ,Y ) i s r e q u e s t e d .
| 5 .1 If R < Y / 2 , t h e n R = M O D ( X ,Y ) = R E M ( X ,Y ) . G o t o
| Step 6 .
| 5 .2 If R > Y / 2 , t h e n { s e t L a s t _ S u b t r a c t : = t r u e ,
| Q : = Q + 1 , Y : = s i g n Y * Y } . G o t o S t e p 6 .
| 5 .3 This is t h e t r i c k y c a s e o f R = Y / 2 . I f Q i s o d d ,
| then { Q : = Q + 1 , s i g n X : = - s i g n X } .
|
| Step 6 . R : = s i g n X * R .
|
| Step 7 . I f L a s t _ S u b t r a c t = t r u e , R : = R - Y .
|
| Step 8 . R e t u r n s i g n Q , l a s t 7 b i t s o f Q , a n d R a s r e q u i r e d .
|
| Step 9 . A t t h i s p o i n t , R = 2 ^ ( - j ) * X - Q Y = Y . T h u s ,
| X = 2 ^ ( j ) * ( Q + 1 ) Y . s e t Q : = 2 ^ ( j ) * ( Q + 1 ) ,
| R : = 0 . R e t u r n s i g n Q , l a s t 7 b i t s o f Q , a n d R .
|
|
| Copyright ( C ) M o t o r o l a , I n c . 1 9 9 0
| All R i g h t s R e s e r v e d
|
2006-02-12 04:55:48 +03:00
| For d e t a i l s o n t h e l i c e n s e f o r t h i s f i l e , p l e a s e s e e t h e
| file, R E A D M E , i n t h i s s a m e d i r e c t o r y .
2005-04-17 02:20:36 +04:00
SREM_MOD : | idnt 2 ,1 | M o t o r o l a 0 4 0 F l o a t i n g P o i n t S o f t w a r e P a c k a g e
| section 8
# include " f p s p . h "
.set Mod_ F l a g ,L _ S C R 3
.set SignY,F P _ S C R 3 + 4
.set SignX,F P _ S C R 3 + 8
.set SignQ,F P _ S C R 3 + 1 2
.set Sc_ F l a g ,F P _ S C R 4
.set Y,F P _ S C R 1
.set Y_ H i ,Y + 4
.set Y_ L o ,Y + 8
.set R,F P _ S C R 2
.set R_ H i ,R + 4
.set R_ L o ,R + 8
Scale : .long 0x00010000 , 0 x8 0 0 0 0 0 0 0 ,0 x00 0 0 0 0 0 0 ,0 x00 0 0 0 0 0 0
| xref t _ a v o i d _ u n s u p p
.global smod
smod :
movel #0 ,M o d _ F l a g ( % a6 )
bras M o d _ R e m
.global srem
srem :
movel #1 ,M o d _ F l a g ( % a6 )
Mod_Rem :
| . .Save sign of X a n d Y
moveml % d2 - % d7 ,- ( % a7 ) | . . . s a v e d a t a r e g i s t e r s
movew ( % a0 ) ,% d3
movew % d3 ,S i g n Y ( % a6 )
andil #0x00007FFF ,% d3 | . . . Y : = | Y |
|
movel 4 ( % a0 ) ,% d4
movel 8 ( % a0 ) ,% d5 | . . . ( D 3 ,D 4 ,D 5 ) i s | Y |
tstl % d3
bnes Y _ N o r m a l
movel #0x00003FFE ,% d3 | . . . $ 3 F F D + 1
tstl % d4
bnes H i Y _ n o t 0
HiY_0 :
movel % d5 ,% d4
clrl % d5
subil #32 ,% d3
clrl % d6
bfffo % d4 { #0 : #32 } ,% d6
lsll % d6 ,% d4
subl % d6 ,% d3 | . . . ( D 3 ,D 4 ,D 5 ) i s n o r m a l i z e d
| . . .with bias $ 7 FFD
bras C h k _ X
HiY_not0 :
clrl % d6
bfffo % d4 { #0 : #32 } ,% d6
subl % d6 ,% d3
lsll % d6 ,% d4
movel % d5 ,% d7 | . . . a c o p y o f D 5
lsll % d6 ,% d5
negl % d6
addil #32 ,% d6
lsrl % d6 ,% d7
orl % d7 ,% d4 | . . . ( D 3 ,D 4 ,D 5 ) n o r m a l i z e d
| . . .with bias $ 7 FFD
bras C h k _ X
Y_Normal :
addil #0x00003FFE ,% d3 | . . . ( D 3 ,D 4 ,D 5 ) n o r m a l i z e d
| . . .with bias $ 7 FFD
Chk_X :
movew - 1 2 ( % a0 ) ,% d0
movew % d0 ,S i g n X ( % a6 )
movew S i g n Y ( % a6 ) ,% d1
eorl % d0 ,% d1
andil #0x00008000 ,% d1
movew % d1 ,S i g n Q ( % a6 ) | . . . s i g n ( Q ) o b t a i n e d
andil #0x00007FFF ,% d0
movel - 8 ( % a0 ) ,% d1
movel - 4 ( % a0 ) ,% d2 | . . . ( D 0 ,D 1 ,D 2 ) i s | X |
tstl % d0
bnes X _ N o r m a l
movel #0x00003FFE ,% d0
tstl % d1
bnes H i X _ n o t 0
HiX_0 :
movel % d2 ,% d1
clrl % d2
subil #32 ,% d0
clrl % d6
bfffo % d1 { #0 : #32 } ,% d6
lsll % d6 ,% d1
subl % d6 ,% d0 | . . . ( D 0 ,D 1 ,D 2 ) i s n o r m a l i z e d
| . . .with bias $ 7 FFD
bras I n i t
HiX_not0 :
clrl % d6
bfffo % d1 { #0 : #32 } ,% d6
subl % d6 ,% d0
lsll % d6 ,% d1
movel % d2 ,% d7 | . . . a c o p y o f D 2
lsll % d6 ,% d2
negl % d6
addil #32 ,% d6
lsrl % d6 ,% d7
orl % d7 ,% d1 | . . . ( D 0 ,D 1 ,D 2 ) n o r m a l i z e d
| . . .with bias $ 7 FFD
bras I n i t
X_Normal :
addil #0x00003FFE ,% d0 | . . . ( D 0 ,D 1 ,D 2 ) n o r m a l i z e d
| . . .with bias $ 7 FFD
Init :
|
movel % d3 ,L _ S C R 1 ( % a6 ) | . . . s a v e b i a s e d e x p o ( Y )
movel % d0 ,L _ S C R 2 ( % a6 ) | s a v e d0
subl % d3 ,% d0 | . . . L : = e x p o ( X ) - e x p o ( Y )
| Move. L D 0 ,L . . . D 0 i s j
clrl % d6 | . . . D 6 : = c a r r y < - 0
clrl % d3 | . . . D 3 i s Q
moveal #0 ,% a1 | . . . A 1 i s k ; j+k=L, Q=0
| . . ( Carry,D 1 ,D 2 ) i s R
tstl % d0
bges M o d _ L o o p
| . .expo ( X) < e x p o ( Y ) . T h u s X = m o d ( X ,Y )
|
movel L _ S C R 2 ( % a6 ) ,% d0 | r e s t o r e d0
bra G e t _ M o d
| . .At this point R = 2 ^ ( - L ) X ; Q = 0; k = 0; and k+j = L
Mod_Loop :
tstl % d6 | . . . t e s t c a r r y b i t
bgts R _ G T _ Y
| . .At this point c a r r y = 0 , R = ( D 1 ,D 2 ) , Y = ( D 4 ,D 5 )
cmpl % d4 ,% d1 | . . . c o m p a r e h i ( R ) a n d h i ( Y )
bnes R _ N E _ Y
cmpl % d5 ,% d2 | . . . c o m p a r e l o ( R ) a n d l o ( Y )
bnes R _ N E _ Y
| . .At this point, R = Y
bra R e m _ i s _ 0
R_NE_Y :
| . .use the borrow o f t h e p r e v i o u s c o m p a r e
bcss R _ L T _ Y | . . . b o r r o w i s s e t i f f R < Y
R_GT_Y :
| . .If Carry is s e t , t h e n Y < ( C a r r y ,D 1 ,D 2 ) < 2 Y . O t h e r w i s e , C a r r y = 0
| . .and Y < ( D1 ,D 2 ) < 2 Y . E i t h e r w a y , p e r f o r m R - Y
subl % d5 ,% d2 | . . . l o ( R ) - l o ( Y )
subxl % d4 ,% d1 | . . . h i ( R ) - h i ( Y )
clrl % d6 | . . . c l e a r c a r r y
addql #1 ,% d3 | . . . Q : = Q + 1
R_LT_Y :
| . .At this point, C a r r y =0 , R < Y . R = 2 ^ ( k - L ) X - Q Y ; k+j = L; j >= 0.
tstl % d0 | . . . s e e i f j = 0 .
beqs P o s t L o o p
addl % d3 ,% d3 | . . . Q : = 2 Q
addl % d2 ,% d2 | . . . l o ( R ) = 2 l o ( R )
roxll #1 ,% d1 | . . . h i ( R ) = 2 h i ( R ) + c a r r y
scs % d6 | . . . s e t C a r r y i f 2 ( R ) o v e r f l o w s
addql #1 ,% a1 | . . . k : = k + 1
subql #1 ,% d0 | . . . j : = j - 1
| . .At this point, R = ( C a r r y ,D 1 ,D 2 ) = 2 ^ ( k - L ) X - Q Y , j + k =L , j > = 0 , R < 2 Y .
bras M o d _ L o o p
PostLoop :
| . .k = L, j = 0 , C a r r y = 0 , R = ( D 1 ,D 2 ) = X - Q Y , R < Y .
| . .normalize R.
movel L _ S C R 1 ( % a6 ) ,% d0 | . . . n e w b i a s e d e x p o o f R
tstl % d1
bnes H i R _ n o t 0
HiR_0 :
movel % d2 ,% d1
clrl % d2
subil #32 ,% d0
clrl % d6
bfffo % d1 { #0 : #32 } ,% d6
lsll % d6 ,% d1
subl % d6 ,% d0 | . . . ( D 0 ,D 1 ,D 2 ) i s n o r m a l i z e d
| . . .with bias $ 7 FFD
bras G e t _ M o d
HiR_not0 :
clrl % d6
bfffo % d1 { #0 : #32 } ,% d6
bmis G e t _ M o d | . . . a l r e a d y n o r m a l i z e d
subl % d6 ,% d0
lsll % d6 ,% d1
movel % d2 ,% d7 | . . . a c o p y o f D 2
lsll % d6 ,% d2
negl % d6
addil #32 ,% d6
lsrl % d6 ,% d7
orl % d7 ,% d1 | . . . ( D 0 ,D 1 ,D 2 ) n o r m a l i z e d
|
Get_Mod :
cmpil #0x000041FE ,% d0
bges N o _ S c a l e
Do_Scale :
movew % d0 ,R ( % a6 )
clrw R + 2 ( % a6 )
movel % d1 ,R _ H i ( % a6 )
movel % d2 ,R _ L o ( % a6 )
movel L _ S C R 1 ( % a6 ) ,% d6
movew % d6 ,Y ( % a6 )
clrw Y + 2 ( % a6 )
movel % d4 ,Y _ H i ( % a6 )
movel % d5 ,Y _ L o ( % a6 )
fmovex R ( % a6 ) ,% f p0 | . . . n o e x c e p t i o n
movel #1 ,S c _ F l a g ( % a6 )
bras M o d O r R e m
No_Scale :
movel % d1 ,R _ H i ( % a6 )
movel % d2 ,R _ L o ( % a6 )
subil #0x3FFE ,% d0
movew % d0 ,R ( % a6 )
clrw R + 2 ( % a6 )
movel L _ S C R 1 ( % a6 ) ,% d6
subil #0x3FFE ,% d6
movel % d6 ,L _ S C R 1 ( % a6 )
fmovex R ( % a6 ) ,% f p0
movew % d6 ,Y ( % a6 )
movel % d4 ,Y _ H i ( % a6 )
movel % d5 ,Y _ L o ( % a6 )
movel #0 ,S c _ F l a g ( % a6 )
|
ModOrRem :
movel M o d _ F l a g ( % a6 ) ,% d6
beqs F i x _ S i g n
movel L _ S C R 1 ( % a6 ) ,% d6 | . . . n e w b i a s e d e x p o ( Y )
subql #1 ,% d6 | . . . b i a s e d e x p o ( Y / 2 )
cmpl % d6 ,% d0
blts F i x _ S i g n
bgts L a s t _ S u b
cmpl % d4 ,% d1
bnes N o t _ E Q
cmpl % d5 ,% d2
bnes N o t _ E Q
bra T i e _ C a s e
Not_EQ :
bcss F i x _ S i g n
Last_Sub :
|
fsubx Y ( % a6 ) ,% f p0 | . . . n o e x c e p t i o n s
addql #1 ,% d3 | . . . Q : = Q + 1
|
Fix_Sign :
| . .Get sign of X
movew S i g n X ( % a6 ) ,% d6
bges G e t _ Q
fnegx % f p0
| . .Get Q
|
Get_Q :
clrl % d6
movew S i g n Q ( % a6 ) ,% d6 | . . . D 6 i s s i g n ( Q )
movel #8 ,% d7
lsrl % d7 ,% d6
andil #0x0000007F ,% d3 | . . . 7 b i t s o f Q
orl % d6 ,% d3 | . . . s i g n a n d b i t s o f Q
swap % d3
fmovel % f p s r ,% d6
andil #0xFF00FFFF ,% d6
orl % d3 ,% d6
fmovel % d6 ,% f p s r | . . . p u t Q i n f p s r
|
Restore :
moveml ( % a7 ) + ,% d2 - % d7
fmovel U S E R _ F P C R ( % a6 ) ,% f p c r
movel S c _ F l a g ( % a6 ) ,% d0
beqs F i n i s h
fmulx S c a l e ( % p c ) ,% f p0 | . . . m a y c a u s e u n d e r f l o w
bra t _ a v o i d _ u n s u p p | c h e c k f o r d e n o r m a s a
| ;result of the scaling
Finish :
fmovex % f p0 ,% f p0 | c a p t u r e e x c e p t i o n s & r o u n d
rts
Rem_is_0 :
| . .R = 2 ^ ( - j) X - Q Y = Y , t h u s R = 0 a n d q u o t i e n t = 2 ^ j ( Q + 1 )
addql #1 ,% d3
cmpil #8 ,% d0 | . . . D 0 i s j
bges Q _ B i g
lsll % d0 ,% d3
bras S e t _ R _ 0
Q_Big :
clrl % d3
Set_R_0 :
fmoves #0x00000000 ,% f p0
movel #0 ,S c _ F l a g ( % a6 )
bra F i x _ S i g n
Tie_Case :
| . .Check parity of Q
movel % d3 ,% d6
andil #0x00000001 ,% d6
tstl % d6
beq F i x _ S i g n | . . . Q i s e v e n
| . .Q is odd, Q : = Q + 1 , s i g n X : = - s i g n X
addql #1 ,% d3
movew S i g n X ( % a6 ) ,% d6
eoril #0x00008000 ,% d6
movew % d6 ,S i g n X ( % a6 )
bra F i x _ S i g n
| end