2005-09-26 10:04:21 +04:00
/ *
* Divide a 6 4 - b i t u n s i g n e d n u m b e r b y a 3 2 - b i t u n s i g n e d n u m b e r .
* This r o u t i n e a s s u m e s t h a t t h e t o p 3 2 b i t s o f t h e d i v i d e n d a r e
* non- z e r o t o s t a r t w i t h .
* On e n t r y , r3 p o i n t s t o t h e d i v i d e n d , w h i c h g e t o v e r w r i t t e n w i t h
* the 6 4 - b i t q u o t i e n t , a n d r4 c o n t a i n s t h e d i v i s o r .
* On e x i t , r3 c o n t a i n s t h e r e m a i n d e r .
*
* Copyright ( C ) 2 0 0 2 P a u l M a c k e r r a s , I B M C o r p .
*
* This p r o g r a m i s f r e e s o f t w a r e ; you can redistribute it and/or
* modify i t u n d e r t h e t e r m s o f t h e G N U G e n e r a l P u b l i c L i c e n s e
* as p u b l i s h e d b y t h e F r e e S o f t w a r e F o u n d a t i o n ; either version
* 2 of t h e L i c e n s e , o r ( a t y o u r o p t i o n ) a n y l a t e r v e r s i o n .
* /
# include < a s m / p p c _ a s m . h >
# include < a s m / p r o c e s s o r . h >
_ GLOBAL( _ _ d i v64 _ 3 2 )
lwz r5 ,0 ( r3 ) # g e t t h e d i v i d e n d i n t o r 5 / r6
lwz r6 ,4 ( r3 )
cmplw r5 ,r4
li r7 ,0
li r8 ,0
blt 1 f
divwu r7 ,r5 ,r4 # i f d i v i d e n d . h i > = d i v i s o r ,
mullw r0 ,r7 ,r4 # q u o t i e n t . h i = d i v i d e n d . h i / d i v i s o r
subf. r5 ,r0 ,r5 # d i v i d e n d . h i % = d i v i s o r
beq 3 f
1 : mr r11 ,r5 # h e r e d i v i d e n d . h i ! = 0
andis. r0 ,r5 ,0 x c00 0
bne 2 f
cntlzw r0 ,r5 # w e a r e s h i f t i n g t h e d i v i d e n d r i g h t
li r10 ,- 1 # t o m a k e i t < 2 ^ 3 2 , a n d s h i f t i n g
srw r10 ,r10 ,r0 # t h e d i v i s o r r i g h t t h e s a m e a m o u n t ,
2005-10-20 03:37:02 +04:00
addc r9 ,r4 ,r10 # r o u n d i n g u p ( s o t h e e s t i m a t e c a n n o t
2005-09-26 10:04:21 +04:00
andc r11 ,r6 ,r10 # e v e r b e t o o l a r g e , o n l y t o o s m a l l )
andc r9 ,r9 ,r10
2005-10-20 03:37:02 +04:00
addze r9 ,r9
2005-09-26 10:04:21 +04:00
or r11 ,r5 ,r11
rotlw r9 ,r9 ,r0
rotlw r11 ,r11 ,r0
divwu r11 ,r11 ,r9 # t h e n w e d i v i d e t h e s h i f t e d q u a n t i t i e s
2 : mullw r10 ,r11 ,r4 # t o g e t a n e s t i m a t e o f t h e q u o t i e n t ,
mulhwu r9 ,r11 ,r4 # m u l t i p l y t h e e s t i m a t e b y t h e d i v i s o r ,
subfc r6 ,r10 ,r6 # t a k e t h e p r o d u c t f r o m t h e d i v i s o r ,
add r8 ,r8 ,r11 # a n d a d d t h e e s t i m a t e t o t h e a c c u m u l a t e d
subfe. r5 ,r9 ,r5 # q u o t i e n t
bne 1 b
3 : cmplw r6 ,r4
blt 4 f
divwu r0 ,r6 ,r4 # p e r f o r m t h e r e m a i n i n g 32 - b i t d i v i s i o n
mullw r10 ,r0 ,r4 # a n d g e t t h e r e m a i n d e r
add r8 ,r8 ,r0
subf r6 ,r10 ,r6
4 : stw r7 ,0 ( r3 ) # r e t u r n t h e q u o t i e n t i n * r 3
stw r8 ,4 ( r3 )
mr r3 ,r6 # r e t u r n t h e r e m a i n d e r i n r 3
blr