/* SPDX-License-Identifier: GPL-2.0 */1/*2* arch/alpha/lib/strrchr.S3* Contributed by Richard Henderson ([email protected])4*5* Return the address of the last occurrence of a given character6* within a null-terminated string, or null if it is not found.7*/8#include <linux/export.h>9#include <asm/regdef.h>1011.set noreorder12.set noat1314.align 315.ent strrchr16.globl strrchr17strrchr:18.frame sp, 0, ra19.prologue 02021zapnot a1, 1, a1 # e0 : zero extend our test character22mov zero, t6 # .. e1 : t6 is last match aligned addr23sll a1, 8, t5 # e0 : replicate our test character24mov zero, t8 # .. e1 : t8 is last match byte compare mask25or t5, a1, a1 # e0 :26ldq_u t0, 0(a0) # .. e1 : load first quadword27sll a1, 16, t5 # e0 :28andnot a0, 7, v0 # .. e1 : align source addr29or t5, a1, a1 # e0 :30lda t4, -1 # .. e1 : build garbage mask31sll a1, 32, t5 # e0 :32cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero33mskqh t4, a0, t4 # e0 :34or t5, a1, a1 # .. e1 : character replication complete35xor t0, a1, t2 # e0 : make bytes == c zero36cmpbge zero, t4, t4 # .. e1 : bits set iff byte is garbage37cmpbge zero, t2, t3 # e0 : bits set iff byte == c38andnot t1, t4, t1 # .. e1 : clear garbage from null test39andnot t3, t4, t3 # e0 : clear garbage from char test40bne t1, $eos # .. e1 : did we already hit the terminator?4142/* Character search main loop */43$loop:44ldq t0, 8(v0) # e0 : load next quadword45cmovne t3, v0, t6 # .. e1 : save previous comparisons match46cmovne t3, t3, t8 # e0 :47addq v0, 8, v0 # .. e1 :48xor t0, a1, t2 # e0 :49cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero50cmpbge zero, t2, t3 # e0 : bits set iff byte == c51beq t1, $loop # .. e1 : if we havnt seen a null, loop5253/* Mask out character matches after terminator */54$eos:55negq t1, t4 # e0 : isolate first null byte match56and t1, t4, t4 # e1 :57subq t4, 1, t5 # e0 : build a mask of the bytes up to...58or t4, t5, t4 # e1 : ... and including the null5960and t3, t4, t3 # e0 : mask out char matches after null61cmovne t3, t3, t8 # .. e1 : save it, if match found62cmovne t3, v0, t6 # e0 :6364/* Locate the address of the last matched character */6566/* Retain the early exit for the ev4 -- the ev5 mispredict penalty67is 5 cycles -- the same as just falling through. */68beq t8, $retnull # .. e1 :6970and t8, 0xf0, t2 # e0 : binary search for the high bit set71cmovne t2, t2, t8 # .. e1 (zdb)72cmovne t2, 4, t2 # e0 :73and t8, 0xcc, t1 # .. e1 :74cmovne t1, t1, t8 # e0 :75cmovne t1, 2, t1 # .. e1 :76and t8, 0xaa, t0 # e0 :77cmovne t0, 1, t0 # .. e1 (zdb)78addq t2, t1, t1 # e0 :79addq t6, t0, v0 # .. e1 : add our aligned base ptr to the mix80addq v0, t1, v0 # e0 :81ret # .. e1 :8283$retnull:84mov zero, v0 # e0 :85ret # .. e1 :8687.end strrchr88EXPORT_SYMBOL(strrchr)899091