/*1* arch/alpha/lib/strrchr.S2* Contributed by Richard Henderson ([email protected])3*4* Return the address of the last occurrence of a given character5* within a null-terminated string, or null if it is not found.6*/78#include <asm/regdef.h>910.set noreorder11.set noat1213.align 314.ent strrchr15.globl strrchr16strrchr:17.frame sp, 0, ra18.prologue 01920zapnot a1, 1, a1 # e0 : zero extend our test character21mov zero, t6 # .. e1 : t6 is last match aligned addr22sll a1, 8, t5 # e0 : replicate our test character23mov zero, t8 # .. e1 : t8 is last match byte compare mask24or t5, a1, a1 # e0 :25ldq_u t0, 0(a0) # .. e1 : load first quadword26sll a1, 16, t5 # e0 :27andnot a0, 7, v0 # .. e1 : align source addr28or t5, a1, a1 # e0 :29lda t4, -1 # .. e1 : build garbage mask30sll a1, 32, t5 # e0 :31cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero32mskqh t4, a0, t4 # e0 :33or t5, a1, a1 # .. e1 : character replication complete34xor t0, a1, t2 # e0 : make bytes == c zero35cmpbge zero, t4, t4 # .. e1 : bits set iff byte is garbage36cmpbge zero, t2, t3 # e0 : bits set iff byte == c37andnot t1, t4, t1 # .. e1 : clear garbage from null test38andnot t3, t4, t3 # e0 : clear garbage from char test39bne t1, $eos # .. e1 : did we already hit the terminator?4041/* Character search main loop */42$loop:43ldq t0, 8(v0) # e0 : load next quadword44cmovne t3, v0, t6 # .. e1 : save previous comparisons match45cmovne t3, t3, t8 # e0 :46addq v0, 8, v0 # .. e1 :47xor t0, a1, t2 # e0 :48cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero49cmpbge zero, t2, t3 # e0 : bits set iff byte == c50beq t1, $loop # .. e1 : if we havnt seen a null, loop5152/* Mask out character matches after terminator */53$eos:54negq t1, t4 # e0 : isolate first null byte match55and t1, t4, t4 # e1 :56subq t4, 1, t5 # e0 : build a mask of the bytes up to...57or t4, t5, t4 # e1 : ... and including the null5859and t3, t4, t3 # e0 : mask out char matches after null60cmovne t3, t3, t8 # .. e1 : save it, if match found61cmovne t3, v0, t6 # e0 :6263/* Locate the address of the last matched character */6465/* Retain the early exit for the ev4 -- the ev5 mispredict penalty66is 5 cycles -- the same as just falling through. */67beq t8, $retnull # .. e1 :6869and t8, 0xf0, t2 # e0 : binary search for the high bit set70cmovne t2, t2, t8 # .. e1 (zdb)71cmovne t2, 4, t2 # e0 :72and t8, 0xcc, t1 # .. e1 :73cmovne t1, t1, t8 # e0 :74cmovne t1, 2, t1 # .. e1 :75and t8, 0xaa, t0 # e0 :76cmovne t0, 1, t0 # .. e1 (zdb)77addq t2, t1, t1 # e0 :78addq t6, t0, v0 # .. e1 : add our aligned base ptr to the mix79addq v0, t1, v0 # e0 :80ret # .. e1 :8182$retnull:83mov zero, v0 # e0 :84ret # .. e1 :8586.end strrchr878889