2005-04-16 15:20:36 -07: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 .
* /
2005-08-08 13:24:38 +10:00
# include " p p c _ a s m . h "
2005-04-16 15:20:36 -07:00
.globl __div64_32
__div64_32 :
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 ,
2010-11-18 14:39:24 +11: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-04-16 15:20:36 -07: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
2010-11-18 14:39:24 +11:00
addze r9 ,r9
2005-04-16 15:20:36 -07: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
2011-11-30 21:39:21 +00:00
/ *
* Extended p r e c i s i o n s h i f t s .
*
* Updated t o b e v a l i d f o r s h i f t c o u n t s f r o m 0 t o 6 3 i n c l u s i v e .
* - - Gabriel
*
* R3 / R 4 h a s 6 4 b i t v a l u e
* R5 h a s s h i f t c o u n t
* result i n R 3 / R 4
*
* ashrdi3 : arithmetic r i g h t s h i f t ( s i g n p r o p a g a t i o n )
* lshrdi3 : logical r i g h t s h i f t
* ashldi3 : left s h i f t
* /
.globl __ashrdi3
__ashrdi3 :
subfic r6 ,r5 ,3 2
srw r4 ,r4 ,r5 # L S W = c o u n t > 31 ? 0 : L S W > > c o u n t
addi r7 ,r5 ,3 2 # c o u l d b e x o r i , o r a d d i w i t h - 32
slw r6 ,r3 ,r6 # t 1 = c o u n t > 3 1 ? 0 : M S W < < ( 3 2 - c o u n t )
rlwinm r8 ,r7 ,0 ,3 2 # t 3 = ( c o u n t < 3 2 ) ? 3 2 : 0
sraw r7 ,r3 ,r7 # t 2 = M S W > > ( c o u n t - 3 2 )
or r4 ,r4 ,r6 # L S W | = t 1
slw r7 ,r7 ,r8 # t 2 = ( c o u n t < 3 2 ) ? 0 : t 2
sraw r3 ,r3 ,r5 # M S W = M S W > > c o u n t
or r4 ,r4 ,r7 # L S W | = t 2
blr
.globl __ashldi3
__ashldi3 :
subfic r6 ,r5 ,3 2
slw r3 ,r3 ,r5 # M S W = c o u n t > 31 ? 0 : M S W < < c o u n t
addi r7 ,r5 ,3 2 # c o u l d b e x o r i , o r a d d i w i t h - 32
srw r6 ,r4 ,r6 # t 1 = c o u n t > 3 1 ? 0 : L S W > > ( 3 2 - c o u n t )
slw r7 ,r4 ,r7 # t 2 = c o u n t < 3 2 ? 0 : L S W < < ( c o u n t - 3 2 )
or r3 ,r3 ,r6 # M S W | = t 1
slw r4 ,r4 ,r5 # L S W = L S W < < c o u n t
or r3 ,r3 ,r7 # M S W | = t 2
blr
.globl __lshrdi3
__lshrdi3 :
subfic r6 ,r5 ,3 2
srw r4 ,r4 ,r5 # L S W = c o u n t > 31 ? 0 : L S W > > c o u n t
addi r7 ,r5 ,3 2 # c o u l d b e x o r i , o r a d d i w i t h - 32
slw r6 ,r3 ,r6 # t 1 = c o u n t > 3 1 ? 0 : M S W < < ( 3 2 - c o u n t )
srw r7 ,r3 ,r7 # t 2 = c o u n t < 3 2 ? 0 : M S W > > ( c o u n t - 3 2 )
or r4 ,r4 ,r6 # L S W | = t 1
srw r3 ,r3 ,r5 # M S W = M S W > > c o u n t
or r4 ,r4 ,r7 # L S W | = t 2
blr