2005-04-16 15:20:36 -07:00
/ *
* arch/ a l p h a / l i b / d i v i d e . S
*
* ( C) 1 9 9 5 L i n u s T o r v a l d s
*
* Alpha d i v i s i o n . .
* /
/ *
* The a l p h a c h i p d o e s n ' t p r o v i d e h a r d w a r e d i v i s i o n , s o w e h a v e t o d o i t
* by h a n d . T h e c o m p i l e r e x p e c t s t h e f u n c t i o n s
*
* __divqu : 6 4 - bit u n s i g n e d l o n g d i v i d e
* __remqu : 6 4 - bit u n s i g n e d l o n g r e m a i n d e r
* _ _ divqs/ _ _ r e m q s : s i g n e d 6 4 - b i t
* _ _ divlu/ _ _ r e m l u : u n s i g n e d 3 2 - b i t
* _ _ divls/ _ _ r e m l s : s i g n e d 3 2 - b i t
*
* These a r e n o t n o r m a l C f u n c t i o n s : i n s t e a d o f t h e n o r m a l
* calling s e q u e n c e , t h e s e e x p e c t t h e i r a r g u m e n t s i n r e g i s t e r s
* $ 2 4 and $ 2 5 , a n d r e t u r n t h e r e s u l t i n $ 2 7 . R e g i s t e r $ 2 8 m a y
* be c l o b b e r e d ( a s s e m b l y t e m p o r a r y ) , a n y t h i n g e l s e m u s t b e s a v e d .
*
* In s h o r t : p a i n f u l .
*
* This i s a r a t h e r s i m p l e b i t - a t - a - t i m e a l g o r i t h m : i t ' s v e r y g o o d
* at d i v i d i n g r a n d o m 6 4 - b i t n u m b e r s , b u t t h e m o r e u s u a l c a s e w h e r e
* the d i v i s o r i s s m a l l i s h a n d l e d b e t t e r b y t h e D E C a l g o r i t h m
* using l o o k u p t a b l e s . T h i s u s e s m u c h l e s s m e m o r y , t h o u g h , a n d i s
* nicer o n t h e c a c h e . . B e s i d e s , I d o n ' t k n o w t h e c o p y r i g h t s t a t u s
* of t h e D E C c o d e .
* /
/ *
* My t e m p o r a r i e s :
* $ 0 - current b i t
* $ 1 - shifted d i v i s o r
* $ 2 - modulus/ q u o t i e n t
*
* $ 2 3 - return a d d r e s s
* $ 2 4 - dividend
* $ 2 5 - divisor
*
* $ 2 7 - quotient/ m o d u l u s
* $ 2 8 - compare s t a t u s
* /
2016-01-11 09:51:29 -05:00
# include < a s m / e x p o r t . h >
2005-04-16 15:20:36 -07:00
# define h a l t . l o n g 0
/ *
* Select f u n c t i o n t y p e a n d r e g i s t e r s
* /
# define m a s k $ 0
# define d i v i s o r $ 1
# define c o m p a r e $ 2 8
# define t m p1 $ 3
# define t m p2 $ 4
# ifdef D I V
# define D I V _ O N L Y ( x ,y . . . ) x ,## y
# define M O D _ O N L Y ( x ,y . . . )
# define f u n c ( x ) _ _ d i v ## x
# define m o d u l u s $ 2
# define q u o t i e n t $ 2 7
# define G E T S I G N ( x ) x o r $ 2 4 ,$ 2 5 ,x
# define S T A C K 4 8
# else
# define D I V _ O N L Y ( x ,y . . . )
# define M O D _ O N L Y ( x ,y . . . ) x ,## y
# define f u n c ( x ) _ _ r e m ## x
# define m o d u l u s $ 2 7
# define q u o t i e n t $ 2
# define G E T S I G N ( x ) b i s $ 2 4 ,$ 2 4 ,x
# define S T A C K 3 2
# endif
/ *
* For 3 2 - b i t o p e r a t i o n s , w e n e e d t o e x t e n d t o 6 4 - b i t
* /
# ifdef I N T S I Z E
# define u f u n c t i o n f u n c ( l u )
# define s f u n c t i o n f u n c ( l )
# define L O N G I F Y ( x ) z a p n o t x ,1 5 ,x
# define S L O N G I F Y ( x ) a d d l x ,0 ,x
# else
# define u f u n c t i o n f u n c ( q u )
# define s f u n c t i o n f u n c ( q )
# define L O N G I F Y ( x )
# define S L O N G I F Y ( x )
# endif
.set noat
.align 3
.globl ufunction
.ent ufunction
ufunction :
subq $ 3 0 ,S T A C K ,$ 3 0
.frame $ 3 0 , STACK,$ 2 3
.prologue 0
7 : stq $ 1 , 0 ( $ 3 0 )
bis $ 2 5 ,$ 2 5 ,d i v i s o r
stq $ 2 , 8 ( $ 3 0 )
bis $ 2 4 ,$ 2 4 ,m o d u l u s
stq $ 0 ,1 6 ( $ 3 0 )
bis $ 3 1 ,$ 3 1 ,q u o t i e n t
LONGIFY( d i v i s o r )
stq t m p1 ,2 4 ( $ 3 0 )
LONGIFY( m o d u l u s )
bis $ 3 1 ,1 ,m a s k
DIV_ O N L Y ( s t q t m p2 ,3 2 ( $ 3 0 ) )
beq d i v i s o r , 9 f / * d i v b y z e r o * /
# ifdef I N T S I Z E
/ *
* shift d i v i s o r l e f t , u s i n g 3 - b i t s h i f t s f o r
* 3 2 - bit d i v i d e s a s w e c a n ' t o v e r f l o w . T h r e e - b i t
* shifts w i l l r e s u l t i n l o o p i n g t h r e e t i m e s l e s s
* here, b u t c a n r e s u l t i n t w o l o o p s m o r e l a t e r .
* Thus u s i n g a l a r g e s h i f t i s n ' t w o r t h i t ( a n d
* s8 a d d p a i r s b e t t e r t h a n a s l l . . )
* /
1 : cmpult d i v i s o r ,m o d u l u s ,c o m p a r e
s8 a d d q d i v i s o r ,$ 3 1 ,d i v i s o r
s8 a d d q m a s k ,$ 3 1 ,m a s k
bne c o m p a r e ,1 b
# else
1 : cmpult d i v i s o r ,m o d u l u s ,c o m p a r e
blt d i v i s o r , 2 f
addq d i v i s o r ,d i v i s o r ,d i v i s o r
addq m a s k ,m a s k ,m a s k
bne c o m p a r e ,1 b
unop
# endif
/* ok, start to go right again.. */
2 : DIV_ O N L Y ( a d d q q u o t i e n t ,m a s k ,t m p2 )
srl m a s k ,1 ,m a s k
cmpule d i v i s o r ,m o d u l u s ,c o m p a r e
subq m o d u l u s ,d i v i s o r ,t m p1
DIV_ O N L Y ( c m o v n e c o m p a r e ,t m p2 ,q u o t i e n t )
srl d i v i s o r ,1 ,d i v i s o r
cmovne c o m p a r e ,t m p1 ,m o d u l u s
bne m a s k ,2 b
9 : ldq $ 1 , 0 ( $ 3 0 )
ldq $ 2 , 8 ( $ 3 0 )
ldq $ 0 ,1 6 ( $ 3 0 )
ldq t m p1 ,2 4 ( $ 3 0 )
DIV_ O N L Y ( l d q t m p2 ,3 2 ( $ 3 0 ) )
addq $ 3 0 ,S T A C K ,$ 3 0
ret $ 3 1 ,( $ 2 3 ) ,1
.end ufunction
2016-01-11 09:51:29 -05:00
EXPORT_ S Y M B O L ( u f u n c t i o n )
2005-04-16 15:20:36 -07:00
/ *
* Uhh. . U g l y s i g n e d d i v i s i o n . I ' d r a t h e r n o t h a v e i t a t a l l , b u t
* it' s n e e d e d i n s o m e c i r c u m s t a n c e s . T h e r e a r e d i f f e r e n t w a y s t o
* handle t h i s , r e a l l y . T h i s d o e s :
* - a / b = a / - b = - ( a / b )
* - a % b = - ( a % b )
* a % - b = a % b
* which i s p r o b a b l y n o t t h e b e s t s o l u t i o n , b u t a t l e a s t s h o u l d
* have t h e p r o p e r t y t h a t ( x / y ) * y + ( x % y ) = x .
* /
.align 3
.globl sfunction
.ent sfunction
sfunction :
subq $ 3 0 ,S T A C K ,$ 3 0
.frame $ 3 0 , STACK,$ 2 3
.prologue 0
bis $ 2 4 ,$ 2 5 ,$ 2 8
SLONGIFY( $ 2 8 )
bge $ 2 8 ,7 b
stq $ 2 4 ,0 ( $ 3 0 )
subq $ 3 1 ,$ 2 4 ,$ 2 8
stq $ 2 5 ,8 ( $ 3 0 )
cmovlt $ 2 4 ,$ 2 8 ,$ 2 4 / * a b s ( $ 2 4 ) * /
stq $ 2 3 ,1 6 ( $ 3 0 )
subq $ 3 1 ,$ 2 5 ,$ 2 8
stq t m p1 ,2 4 ( $ 3 0 )
cmovlt $ 2 5 ,$ 2 8 ,$ 2 5 / * a b s ( $ 2 5 ) * /
unop
bsr $ 2 3 ,u f u n c t i o n
ldq $ 2 4 ,0 ( $ 3 0 )
ldq $ 2 5 ,8 ( $ 3 0 )
GETSIGN( $ 2 8 )
subq $ 3 1 ,$ 2 7 ,t m p1
SLONGIFY( $ 2 8 )
ldq $ 2 3 ,1 6 ( $ 3 0 )
cmovlt $ 2 8 ,t m p1 ,$ 2 7
ldq t m p1 ,2 4 ( $ 3 0 )
addq $ 3 0 ,S T A C K ,$ 3 0
ret $ 3 1 ,( $ 2 3 ) ,1
.end sfunction
2016-01-11 09:51:29 -05:00
EXPORT_ S Y M B O L ( s f u n c t i o n )