2005-04-16 15:20:36 -07:00
/ * Copyright ( C ) 1 9 9 6 F r e e S o f t w a r e F o u n d a t i o n , I n c .
This f i l e i s p a r t o f t h e G N U C L i b r a r y .
Contributed b y D a v i d M o s b e r g e r ( d a v i d m @cs.arizona.edu).
The G N U C L i b r a r y 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 L i b r a r y G e n e r a l P u b l i c L i c e n s e a s
published 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 the
License, 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 .
The G N U C L i b r a r y i s d i s t r i b u t e d i n t h e h o p e t h a t i t w i l l b e u s e f u l ,
but W I T H O U T A N Y W A R R A N T Y ; without even the implied warranty of
MERCHANTABILITY o r F I T N E S S F O R A P A R T I C U L A R P U R P O S E . S e e t h e G N U
Library G e n e r a l P u b l i c L i c e n s e f o r m o r e d e t a i l s .
You s h o u l d h a v e r e c e i v e d a c o p y o f t h e G N U L i b r a r y G e n e r a l P u b l i c
License a l o n g w i t h t h e G N U C L i b r a r y ; see the file COPYING.LIB. If not,
write t o 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 , I n c . , 5 9 T e m p l e P l a c e - S u i t e 3 3 0 ,
Boston, M A 0 2 1 1 1 - 1 3 0 7 , U S A . * /
/ * Finds c h a r a c t e r s i n a m e m o r y a r e a . O p t i m i z e d f o r t h e A l p h a :
- memory a c c e s s e d a s a l i g n e d q u a d w o r d s o n l y
- uses c m p b g e t o c o m p a r e 8 b y t e s i n p a r a l l e l
- does b i n a r y s e a r c h t o f i n d 0 b y t e i n l a s t
quadword ( H A K M E M n e e d e d 1 2 i n s t r u c t i o n s t o
do t h i s i n s t e a d o f t h e 9 i n s t r u c t i o n s t h a t
binary s e a r c h n e e d s ) .
For c o r r e c t n e s s c o n s i d e r t h a t :
- only m i n i m u m n u m b e r o f q u a d w o r d s m a y b e a c c e s s e d
- the t h i r d a r g u m e n t i s a n u n s i g n e d l o n g
* /
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
.set noreorder
.set noat
.globl memchr
.ent memchr
memchr :
.frame $ 3 0 , 0 , $ 2 6 , 0
.prologue 0
# Hack - - i f s o m e o n e p a s s e s i n ( s i z e _ t ) - 1 , h o p i n g t o j u s t
# search t i l t h e e n d o f t h e a d d r e s s s p a c e , w e w i l l o v e r f l o w
# below w h e n w e f i n d t h e a d d r e s s o f t h e l a s t b y t e . G i v e n
# that w e w i l l n e v e r h a v e a 5 6 - b i t a d d r e s s s p a c e , c r o p p i n g
# the l e n g t h i s t h e e a s i e s t w a y t o a v o i d t r o u b l e .
zap $ 1 8 , 0 x80 , $ 5 #- e 0 :
beq $ 1 8 , $ n o t _ f o u n d # . . e 1 :
ldq_ u $ 1 , 0 ( $ 1 6 ) # e 1 : l o a d f i r s t q u a d w o r d
insbl $ 1 7 , 1 , $ 2 # . . e 0 : $ 2 = 0 0 0 0 0 0 0 0 0 0 0 0 c h00
and $ 1 7 , 0 x f f , $ 1 7 #- e 0 : $ 1 7 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 c h
cmpult $ 1 8 , 9 , $ 4 # . . e 1 :
or $ 2 , $ 1 7 , $ 1 7 # e 0 : $ 1 7 = 0 0 0 0 0 0 0 0 0 0 0 0 c h c h
lda $ 3 , - 1 ( $ 3 1 ) # . . e 1 :
sll $ 1 7 , 1 6 , $ 2 #- e 0 : $ 2 = 0 0 0 0 0 0 0 0 c h c h00 0 0
addq $ 1 6 , $ 5 , $ 5 # . . e 1 :
or $ 2 , $ 1 7 , $ 1 7 # e 1 : $ 1 7 = 0 0 0 0 0 0 0 0 c h c h c h c h
unop # :
sll $ 1 7 , 3 2 , $ 2 #- e 0 : $ 2 = c h c h c h c h00 0 0 0 0 0 0
or $ 2 , $ 1 7 , $ 1 7 # e 1 : $ 1 7 = c h c h c h c h c h c h c h c h
extql $ 1 , $ 1 6 , $ 7 # e 0 :
beq $ 4 , $ f i r s t _ q u a d # . . e 1 :
ldq_ u $ 6 , - 1 ( $ 5 ) #- e 1 : e i g h t o r l e s s b y t e s t o s e a r c h
extqh $ 6 , $ 1 6 , $ 6 # . . e 0 :
mov $ 1 6 , $ 0 # e 0 :
or $ 7 , $ 6 , $ 1 # . . e 1 : $ 1 = q u a d w o r d s t a r t i n g a t $ 1 6
# Deal w i t h t h e c a s e w h e r e a t m o s t 8 b y t e s r e m a i n t o b e s e a r c h e d
# in $ 1 . E . g . :
# $ 1 8 = 6
# $ 1 = ? ? ? ? c6 c5 c4 c3 c2 c1
$ last_quad :
negq $ 1 8 , $ 6 #- e 0 :
xor $ 1 7 , $ 1 , $ 1 # . . e 1 :
srl $ 3 , $ 6 , $ 6 # e 0 : $ 6 = m a s k o f $ 1 8 b i t s s e t
cmpbge $ 3 1 , $ 1 , $ 2 # . . e 1 :
and $ 2 , $ 6 , $ 2 #- e 0 :
beq $ 2 , $ n o t _ f o u n d # . . e 1 :
$ found_it :
# Now, d e t e r m i n e w h i c h b y t e m a t c h e d :
negq $ 2 , $ 3 # e 0 :
and $ 2 , $ 3 , $ 2 # e 1 :
and $ 2 , 0 x0 f , $ 1 #- e 0 :
addq $ 0 , 4 , $ 3 # . . e 1 :
cmoveq $ 1 , $ 3 , $ 0 # e 0 :
addq $ 0 , 2 , $ 3 # . . e 1 :
and $ 2 , 0 x33 , $ 1 #- e 0 :
cmoveq $ 1 , $ 3 , $ 0 # . . e 1 :
and $ 2 , 0 x55 , $ 1 # e 0 :
addq $ 0 , 1 , $ 3 # . . e 1 :
cmoveq $ 1 , $ 3 , $ 0 #- e 0 :
$ done : ret # . . e 1 :
# Deal w i t h t h e c a s e w h e r e $ 1 8 > 8 b y t e s r e m a i n t o b e
# searched. $ 1 6 m a y n o t b e a l i g n e d .
.align 4
$ first_quad :
andnot $ 1 6 , 0 x7 , $ 0 #- e 1 :
insqh $ 3 , $ 1 6 , $ 2 # . . e 0 : $ 2 = 0 0 0 0 f f f f f f f f f f f f ( $ 1 6 < 0 : 2 > f f )
xor $ 1 , $ 1 7 , $ 1 # e 0 :
or $ 1 , $ 2 , $ 1 # e 1 : $ 1 = = = = =ffffffffffff
cmpbge $ 3 1 , $ 1 , $ 2 #- e 0 :
bne $ 2 , $ f o u n d _ i t # . . e 1 :
# At l e a s t o n e b y t e l e f t t o p r o c e s s .
ldq $ 1 , 8 ( $ 0 ) # e 0 :
subq $ 5 , 1 , $ 1 8 # . . e 1 :
addq $ 0 , 8 , $ 0 #- e 0 :
# Make $ 1 8 p o i n t t o l a s t q u a d t o b e a c c e s s e d ( t h e
# last q u a d m a y o r m a y n o t b e p a r t i a l ) .
andnot $ 1 8 , 0 x7 , $ 1 8 # . . e 1 :
cmpult $ 0 , $ 1 8 , $ 2 # e 0 :
beq $ 2 , $ f i n a l # . . e 1 :
# At l e a s t t w o q u a d s r e m a i n t o b e a c c e s s e d .
subq $ 1 8 , $ 0 , $ 4 #- e 0 : $ 4 < - n r q u a d s t o b e p r o c e s s e d
and $ 4 , 8 , $ 4 # e 1 : o d d n u m b e r o f q u a d s ?
bne $ 4 , $ o d d _ q u a d _ c o u n t # e 1 :
# At l e a s t t h r e e q u a d s r e m a i n t o b e a c c e s s e d
mov $ 1 , $ 4 # e 0 : m o v e p r e f e t c h e d v a l u e t o c o r r e c t r e g
.align 4
$ unrolled_loop :
ldq $ 1 , 8 ( $ 0 ) #- e 0 : p r e f e t c h $ 1
xor $ 1 7 , $ 4 , $ 2 # . . e 1 :
cmpbge $ 3 1 , $ 2 , $ 2 # e 0 :
bne $ 2 , $ f o u n d _ i t # . . e 1 :
addq $ 0 , 8 , $ 0 #- e 0 :
$ odd_quad_count :
xor $ 1 7 , $ 1 , $ 2 # . . e 1 :
ldq $ 4 , 8 ( $ 0 ) # e 0 : p r e f e t c h $ 4
cmpbge $ 3 1 , $ 2 , $ 2 # . . e 1 :
addq $ 0 , 8 , $ 6 #- e 0 :
bne $ 2 , $ f o u n d _ i t # . . e 1 :
cmpult $ 6 , $ 1 8 , $ 6 # e 0 :
addq $ 0 , 8 , $ 0 # . . e 1 :
bne $ 6 , $ u n r o l l e d _ l o o p #- e 1 :
mov $ 4 , $ 1 # e 0 : m o v e p r e f e t c h e d v a l u e i n t o $ 1
$ final : subq $ 5 , $ 0 , $ 1 8 # . . e 1 : $ 1 8 < - n u m b e r o f b y t e s l e f t t o d o
bne $ 1 8 , $ l a s t _ q u a d # e 1 :
$ not_found :
mov $ 3 1 , $ 0 #- e 0 :
ret # . . e 1 :
.end memchr
2016-01-11 09:51:29 -05:00
EXPORT_ S Y M B O L ( m e m c h r )