/*1* arch/alpha/lib/ev6-memchr.S2*3* 21264 version contributed by Rick Gorton <[email protected]>4*5* Finds characters in a memory area. Optimized for the Alpha:6*7* - memory accessed as aligned quadwords only8* - uses cmpbge to compare 8 bytes in parallel9* - does binary search to find 0 byte in last10* quadword (HAKMEM needed 12 instructions to11* do this instead of the 9 instructions that12* binary search needs).13*14* For correctness consider that:15*16* - only minimum number of quadwords may be accessed17* - the third argument is an unsigned long18*19* Much of the information about 21264 scheduling/coding comes from:20* Compiler Writer's Guide for the Alpha 2126421* abbreviated as 'CWG' in other comments here22* ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html23* Scheduling notation:24* E - either cluster25* U - upper subcluster; U0 - subcluster U0; U1 - subcluster U126* L - lower subcluster; L0 - subcluster L0; L1 - subcluster L127* Try not to change the actual algorithm if possible for consistency.28*/2930.set noreorder31.set noat3233.align 434.globl memchr35.ent memchr36memchr:37.frame $30,0,$26,038.prologue 03940# Hack -- if someone passes in (size_t)-1, hoping to just41# search til the end of the address space, we will overflow42# below when we find the address of the last byte. Given43# that we will never have a 56-bit address space, cropping44# the length is the easiest way to avoid trouble.45zap $18, 0x80, $5 # U : Bound length46beq $18, $not_found # U :47ldq_u $1, 0($16) # L : load first quadword Latency=348and $17, 0xff, $17 # E : L L U U : 00000000000000ch4950insbl $17, 1, $2 # U : 000000000000ch0051cmpult $18, 9, $4 # E : small (< 1 quad) string?52or $2, $17, $17 # E : 000000000000chch53lda $3, -1($31) # E : U L L U5455sll $17, 16, $2 # U : 00000000chch000056addq $16, $5, $5 # E : Max search address57or $2, $17, $17 # E : 00000000chchchch58sll $17, 32, $2 # U : U L L U : chchchch000000005960or $2, $17, $17 # E : chchchchchchchch61extql $1, $16, $7 # U : $7 is upper bits62beq $4, $first_quad # U :63ldq_u $6, -1($5) # L : L U U L : eight or less bytes to search Latency=36465extqh $6, $16, $6 # U : 2 cycle stall for $666mov $16, $0 # E :67nop # E :68or $7, $6, $1 # E : L U L U $1 = quadword starting at $166970# Deal with the case where at most 8 bytes remain to be searched71# in $1. E.g.:72# $18 = 673# $1 = ????c6c5c4c3c2c174$last_quad:75negq $18, $6 # E :76xor $17, $1, $1 # E :77srl $3, $6, $6 # U : $6 = mask of $18 bits set78cmpbge $31, $1, $2 # E : L U L U7980nop81nop82and $2, $6, $2 # E :83beq $2, $not_found # U : U L U L8485$found_it:86#ifdef CONFIG_ALPHA_EV6787/*88* Since we are guaranteed to have set one of the bits, we don't89* have to worry about coming back with a 0x40 out of cttz...90*/91cttz $2, $3 # U0 :92addq $0, $3, $0 # E : All done93nop # E :94ret # L0 : L U L U95#else96/*97* Slow and clunky. It can probably be improved.98* An exercise left for others.99*/100negq $2, $3 # E :101and $2, $3, $2 # E :102and $2, 0x0f, $1 # E :103addq $0, 4, $3 # E :104105cmoveq $1, $3, $0 # E : Latency 2, extra map cycle106nop # E : keep with cmov107and $2, 0x33, $1 # E :108addq $0, 2, $3 # E : U L U L : 2 cycle stall on $0109110cmoveq $1, $3, $0 # E : Latency 2, extra map cycle111nop # E : keep with cmov112and $2, 0x55, $1 # E :113addq $0, 1, $3 # E : U L U L : 2 cycle stall on $0114115cmoveq $1, $3, $0 # E : Latency 2, extra map cycle116nop117nop118ret # L0 : L U L U119#endif120121# Deal with the case where $18 > 8 bytes remain to be122# searched. $16 may not be aligned.123.align 4124$first_quad:125andnot $16, 0x7, $0 # E :126insqh $3, $16, $2 # U : $2 = 0000ffffffffffff ($16<0:2> ff)127xor $1, $17, $1 # E :128or $1, $2, $1 # E : U L U L $1 = ====ffffffffffff129130cmpbge $31, $1, $2 # E :131bne $2, $found_it # U :132# At least one byte left to process.133ldq $1, 8($0) # L :134subq $5, 1, $18 # E : U L U L135136addq $0, 8, $0 # E :137# Make $18 point to last quad to be accessed (the138# last quad may or may not be partial).139andnot $18, 0x7, $18 # E :140cmpult $0, $18, $2 # E :141beq $2, $final # U : U L U L142143# At least two quads remain to be accessed.144145subq $18, $0, $4 # E : $4 <- nr quads to be processed146and $4, 8, $4 # E : odd number of quads?147bne $4, $odd_quad_count # U :148# At least three quads remain to be accessed149mov $1, $4 # E : L U L U : move prefetched value to correct reg150151.align 4152$unrolled_loop:153ldq $1, 8($0) # L : prefetch $1154xor $17, $4, $2 # E :155cmpbge $31, $2, $2 # E :156bne $2, $found_it # U : U L U L157158addq $0, 8, $0 # E :159nop # E :160nop # E :161nop # E :162163$odd_quad_count:164xor $17, $1, $2 # E :165ldq $4, 8($0) # L : prefetch $4166cmpbge $31, $2, $2 # E :167addq $0, 8, $6 # E :168169bne $2, $found_it # U :170cmpult $6, $18, $6 # E :171addq $0, 8, $0 # E :172nop # E :173174bne $6, $unrolled_loop # U :175mov $4, $1 # E : move prefetched value into $1176nop # E :177nop # E :178179$final: subq $5, $0, $18 # E : $18 <- number of bytes left to do180nop # E :181nop # E :182bne $18, $last_quad # U :183184$not_found:185mov $31, $0 # E :186nop # E :187nop # E :188ret # L0 :189190.end memchr191192193