2005-04-17 02:20:36 +04:00
/ *
* Optimized v e r s i o n o f t h e s t r l e n _ u s e r ( ) f u n c t i o n
*
* Inputs :
* in0 a d d r e s s o f b u f f e r
*
* Outputs :
* ret0 0 i n c a s e o f f a u l t , s t r l e n ( b u f f e r ) + 1 o t h e r w i s e
*
* Copyright ( C ) 1 9 9 8 , 1 9 9 9 , 2 0 0 1 H e w l e t t - P a c k a r d C o
* David M o s b e r g e r - T a n g < d a v i d m @hpl.hp.com>
* Stephane E r a n i a n < e r a n i a n @hpl.hp.com>
*
* 0 1 / 1 9 / 9 9 S. E r a n i a n h e a v i l y e n h a n c e d v e r s i o n ( s e e d e t a i l s b e l o w )
* 0 9 / 2 4 / 9 9 S. E r a n i a n a d d e d s p e c u l a t i o n r e c o v e r y c o d e
* /
# include < a s m / a s m m a c r o . h >
2016-01-17 09:13:41 +03:00
# include < a s m / e x p o r t . h >
2005-04-17 02:20:36 +04:00
/ /
/ / int s t r l e n _ u s e r ( c h a r * )
/ / - - - - - - - - - - - - - - - - - - - - - - - -
/ / Returns :
/ / - length o f s t r i n g + 1
/ / - 0 in c a s e a n e x c e p t i o n i s r a i s e d
/ /
/ / This i s a n e n h a n c e d v e r s i o n o f t h e b a s i c s t r l e n _ u s e r . i t i n c l u d e s a
/ / combination o f c o m p u t e z e r o i n d e x ( c z x ) , p a r a l l e l c o m p a r i s o n s , s p e c u l a t i v e
/ / loads a n d l o o p u n r o l l u s i n g r o t a t i n g r e g i s t e r s .
/ /
/ / General I d e a s a b o u t t h e a l g o r i t h m :
/ / The g o a l i s t o l o o k a t t h e s t r i n g i n c h u n k s o f 8 b y t e s .
/ / so w e n e e d t o d o a f e w e x t r a c h e c k s a t t h e b e g i n n i n g b e c a u s e t h e
/ / string m a y n o t b e 8 - b y t e a l i g n e d . I n t h i s c a s e w e l o a d t h e 8 b y t e
/ / quantity w h i c h i n c l u d e s t h e s t a r t o f t h e s t r i n g a n d m a s k t h e u n u s e d
/ / bytes w i t h 0 x f f t o a v o i d c o n f u s i n g c z x .
/ / We u s e s p e c u l a t i v e l o a d s a n d s o f t w a r e p i p e l i n i n g t o h i d e m e m o r y
/ / latency a n d d o r e a d a h e a d s a f e l y . T h i s w a y w e d e f e r a n y e x c e p t i o n .
/ /
/ / Because w e d o n ' t w a n t t h e k e r n e l t o b e r e l y i n g o n p a r t i c u l a r
/ / settings o f t h e D C R r e g i s t e r , w e p r o v i d e r e c o v e r y c o d e i n c a s e
/ / speculation f a i l s . T h e r e c o v e r y c o d e i s g o i n g t o " r e d o " t h e w o r k u s i n g
/ / only n o r m a l l o a d s . I f w e s t i l l g e t a f a u l t t h e n w e r e t u r n a n
/ / error ( r e t 0 =0 ) . O t h e r w i s e w e r e t u r n t h e s t r l e n + 1 a s u s u a l .
/ / The f a c t t h a t s p e c u l a t i o n m a y f a i l c a n b e c a u s e d , f o r i n s t a n c e , b y
/ / the D C R . d m b i t b e i n g s e t . I n t h i s c a s e T L B m i s s e s a r e d e f e r r e d , i . e . ,
/ / a N a T b i t w i l l b e s e t i f t h e t r a n s l a t i o n i s n o t p r e s e n t . T h e n o r m a l
/ / load, o n t h e o t h e r h a n d , w i l l c a u s e t h e t r a n s l a t i o n t o b e i n s e r t e d
/ / if t h e m a p p i n g e x i s t s .
/ /
/ / It s h o u l d b e n o t e d t h a t w e e x e c u t e r e c o v e r y c o d e o n l y w h e n w e n e e d
/ / to u s e t h e d a t a t h a t h a s b e e n s p e c u l a t i v e l y l o a d e d : w e d o n ' t e x e c u t e
/ / recovery c o d e o n p u r e r e a d a h e a d d a t a .
/ /
/ / Remarks :
/ / - the c m p r0 ,r0 i s u s e d a s a f a s t w a y t o i n i t i a l i z e a p r e d i c a t e
/ / register t o 1 . T h i s i s r e q u i r e d t o m a k e s u r e t h a t w e g e t t h e p a r a l l e l
/ / compare c o r r e c t .
/ /
/ / - we d o n ' t u s e t h e e p i l o g u e c o u n t e r t o e x i t t h e l o o p b u t w e n e e d t o s e t
/ / it t o z e r o b e f o r e h a n d .
/ /
/ / - after t h e l o o p w e m u s t t e s t f o r N a t v a l u e s b e c a u s e n e i t h e r t h e
/ / czx n o r c m p i n s t r u c t i o n r a i s e a N a T c o n s u m p t i o n f a u l t . W e m u s t b e
/ / careful n o t t o l o o k t o o f a r f o r a N a t f o r w h i c h w e d o n ' t c a r e .
/ / For i n s t a n c e w e d o n ' t n e e d t o l o o k a t a N a T i n v a l 2 i f t h e z e r o b y t e
/ / was i n v a l 1 .
/ /
/ / - Clearly p e r f o r m a n c e t u n i n g i s r e q u i r e d .
/ /
# define s a v e d _ p f s r11
# define t m p r10
# define b a s e r16
# define o r i g r17
# define s a v e d _ p r r18
# define s r c r19
# define m a s k r20
# define v a l r21
# define v a l 1 r22
# define v a l 2 r23
GLOBAL_ E N T R Y ( _ _ s t r l e n _ u s e r )
.prologue
.save ar. p f s , s a v e d _ p f s
alloc s a v e d _ p f s =ar . p f s ,1 1 ,0 ,0 ,8
.rotr v[ 2 ] , w [ 2 ] / / d e c l a r e s o u r 4 a l i a s e s
extr. u t m p =in0 ,0 ,3 / / t m p =least s i g n i f i c a n t 3 b i t s
mov o r i g =in0 / / k e e p t r a c k o f i n i t i a l b y t e a d d r e s s
dep s r c =0 ,i n 0 ,0 ,3 / / s r c =8byte - a l i g n e d i n 0 a d d r e s s
.save pr, s a v e d _ p r
mov s a v e d _ p r =pr / / p r e s e r v e p r e d i c a t e s ( r o t a t i o n )
;;
.body
ld8 . s v [ 1 ] = [ s r c ] ,8 / / l o a d t h e i n i t i a l 8 b y t e s ( m u s t s p e c u l a t e )
shl t m p =tmp ,3 / / m u l t i p l y b y 8 b i t s / b y t e
mov m a s k = - 1 / / o u r m a s k
;;
ld8 . s w [ 1 ] = [ s r c ] ,8 / / l o a d n e x t 8 b y t e s i n 2 n d p i p e l i n e
cmp. e q p6 ,p0 =r0 ,r0 / / s e t s p6 ( r e q u i r e d b e c a u s e o f / / c m p . a n d )
sub t m p =64 ,t m p / / h o w m a n y b i t s t o s h i f t o u r m a s k o n t h e r i g h t
;;
shr. u m a s k =mask ,t m p / / z e r o e n o u g h b i t s t o h o l d v [ 1 ] v a l u a b l e p a r t
mov a r . e c =r0 / / c l e a r e p i l o g u e c o u n t e r ( s a v e d i n a r . p f s )
;;
add b a s e = - 1 6 ,s r c / / k e e p t r a c k o f a l i g n e d b a s e
chk. s v [ 1 ] , . r e c o v e r / / i f a l r e a d y N a T , t h e n d i r e c t l y s k i p t o r e c o v e r
or v [ 1 ] =v [ 1 ] ,m a s k / / n o w w e h a v e a s a f e i n i t i a l b y t e p a t t e r n
;;
1 :
ld8 . s v [ 0 ] = [ s r c ] ,8 / / s p e c u l a t i v e l y l o a d n e x t
czx1 . r v a l 1 =v [ 1 ] / / s e a r c h 0 b y t e f r o m r i g h t
czx1 . r v a l 2 =w [ 1 ] / / s e a r c h 0 b y t e f r o m r i g h t f o l l o w i n g 8 b y t e s
;;
ld8 . s w [ 0 ] = [ s r c ] ,8 / / s p e c u l a t i v e l y l o a d n e x t t o n e x t
cmp. e q . a n d p6 ,p0 =8 ,v a l 1 / / p6 = p6 a n d v a l 1 = =8
cmp. e q . a n d p6 ,p0 =8 ,v a l 2 / / p6 = p6 a n d m a s k = =8
( p6 ) b r . w t o p . d p t k . f e w 1 b / / l o o p u n t i l p6 = = 0
;;
/ /
/ / We m u s t r e t u r n t r y t h e r e c o v e r y c o d e i f f
/ / val1 _ i s _ n a t | | ( v a l 1 = =8 & & v a l 2 _ i s _ n a t )
/ /
/ / XXX F i x m e
/ / - there m u s t b e a b e t t e r w a y o f d o i n g t h e t e s t
/ /
cmp. e q p8 ,p9 =8 ,v a l 1 / / p6 = v a l 1 h a d z e r o ( d i s a m b i g u a t e )
tnat. n z p6 ,p7 =val1 / / t e s t N a T o n v a l 1
( p6 ) b r . c o n d . s p n t . r e c o v e r / / j u m p t o r e c o v e r y i f v a l 1 i s N a T
;;
/ /
/ / if w e c o m e h e r e p7 i s t r u e , i . e . , i n i t i a l i z e d f o r / / c m p
/ /
cmp. e q . a n d p7 ,p0 =8 ,v a l 1 / / v a l 1 = =8 ?
tnat. n z . a n d p7 ,p0 =val2 / / t e s t N a T i f v a l 2
( p7 ) b r . c o n d . s p n t . r e c o v e r / / j u m p t o r e c o v e r y i f v a l 2 i s N a T
;;
( p8 ) m o v v a l 1 =val2 / / v a l 2 c o n t a i n s t h e v a l u e
( p8 ) a d d s s r c = - 1 6 ,s r c / / c o r r e c t p o s i t i o n w h e n 3 a h e a d
( p9 ) a d d s s r c = - 2 4 ,s r c / / c o r r e c t p o s i t i o n w h e n 4 a h e a d
;;
sub r e t 0 =src ,o r i g / / d i s t a n c e f r o m o r i g i n
sub t m p =7 ,v a l 1 / / 7 =8 - 1 b e c a u s e t h i s s t r l e n r e t u r n s s t r l e n + 1
mov p r =saved_pr ,0 x f f f f f f f f f f f f00 0 0
;;
sub r e t 0 =ret0 ,t m p / / l e n g t h =now - b a c k - 1
mov a r . p f s =saved_pfs / / b e c a u s e o f a r . e c , r e s t o r e n o m a t t e r w h a t
br. r e t . s p t k . m a n y r p / / e n d o f n o r m a l e x e c u t i o n
/ /
/ / Outlined r e c o v e r y c o d e w h e n s p e c u l a t i o n f a i l e d
/ /
/ / This t i m e w e d o n ' t u s e s p e c u l a t i o n a n d r e l y o n t h e n o r m a l e x c e p t i o n
/ / mechanism. t h a t ' s w h y t h e l o o p i s n o t a s g o o d a s t h e p r e v i o u s o n e
/ / because r e a d a h e a d i s n o t p o s s i b l e
/ /
/ / XXX F i x m e
/ / - today w e r e s t a r t f r o m t h e b e g i n n i n g o f t h e s t r i n g i n s t e a d
/ / of t r y i n g t o c o n t i n u e w h e r e w e l e f t o f f .
/ /
.recover :
EX( . L e x i t 1 , l d8 v a l = [ b a s e ] ,8 ) / / l o a d t h e i n i t i a l b y t e s
;;
or v a l =val ,m a s k / / r e m a s k f i r s t b y t e s
cmp. e q p0 ,p6 =r0 ,r0 / / n u l l i f y f i r s t l d8 i n l o o p
;;
/ /
/ / ar. e c i s s t i l l z e r o h e r e
/ /
2 :
EX( . L e x i t 1 , ( p6 ) l d8 v a l = [ b a s e ] ,8 )
;;
czx1 . r v a l 1 =val / / s e a r c h 0 b y t e f r o m r i g h t
;;
cmp. e q p6 ,p0 =8 ,v a l 1 / / v a l 1 = =8 ?
( p6 ) b r . w t o p . d p t k . f e w 2 b / / l o o p u n t i l p6 = = 0
;;
sub r e t 0 =base ,o r i g / / d i s t a n c e f r o m b a s e
sub t m p =7 ,v a l 1 / / 7 =8 - 1 b e c a u s e t h i s s t r l e n r e t u r n s s t r l e n + 1
mov p r =saved_pr ,0 x f f f f f f f f f f f f00 0 0
;;
sub r e t 0 =ret0 ,t m p / / l e n g t h =now - b a c k - 1
mov a r . p f s =saved_pfs / / b e c a u s e o f a r . e c , r e s t o r e n o m a t t e r w h a t
br. r e t . s p t k . m a n y r p / / e n d o f s u c c e s s f u l r e c o v e r y c o d e
/ /
/ / We f a i l e d e v e n o n t h e n o r m a l l o a d ( c a l l e d f r o m e x c e p t i o n h a n d l e r )
/ /
.Lexit1 :
mov r e t 0 =0
mov p r =saved_pr ,0 x f f f f f f f f f f f f00 0 0
mov a r . p f s =saved_pfs / / b e c a u s e o f a r . e c , r e s t o r e n o m a t t e r w h a t
br. r e t . s p t k . m a n y r p
END( _ _ s t r l e n _ u s e r )
2016-01-17 09:13:41 +03:00
EXPORT_ S Y M B O L ( _ _ s t r l e n _ u s e r )