18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/alpha/lib/ev67-strrchr.S
48c2ecf20Sopenharmony_ci * 21264 version by Rick Gorton <rick.gorton@alpha-processor.com>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Finds length of a 0-terminated string.  Optimized for the
78c2ecf20Sopenharmony_ci * Alpha architecture:
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *	- memory accessed as aligned quadwords only
108c2ecf20Sopenharmony_ci *	- uses bcmpge to compare 8 bytes in parallel
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Much of the information about 21264 scheduling/coding comes from:
138c2ecf20Sopenharmony_ci *	Compiler Writer's Guide for the Alpha 21264
148c2ecf20Sopenharmony_ci *	abbreviated as 'CWG' in other comments here
158c2ecf20Sopenharmony_ci *	ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
168c2ecf20Sopenharmony_ci * Scheduling notation:
178c2ecf20Sopenharmony_ci *	E	- either cluster
188c2ecf20Sopenharmony_ci *	U	- upper subcluster; U0 - subcluster U0; U1 - subcluster U1
198c2ecf20Sopenharmony_ci *	L	- lower subcluster; L0 - subcluster L0; L1 - subcluster L1
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <asm/export.h>
238c2ecf20Sopenharmony_ci#include <asm/regdef.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	.set noreorder
268c2ecf20Sopenharmony_ci	.set noat
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	.align 4
298c2ecf20Sopenharmony_ci	.ent strrchr
308c2ecf20Sopenharmony_ci	.globl strrchr
318c2ecf20Sopenharmony_cistrrchr:
328c2ecf20Sopenharmony_ci	.frame sp, 0, ra
338c2ecf20Sopenharmony_ci	.prologue 0
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	and	a1, 0xff, t2	# E : 00000000000000ch
368c2ecf20Sopenharmony_ci	insbl	a1, 1, t4	# U : 000000000000ch00
378c2ecf20Sopenharmony_ci	insbl	a1, 2, t5	# U : 0000000000ch0000
388c2ecf20Sopenharmony_ci	ldq_u   t0, 0(a0)	# L : load first quadword Latency=3
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	mov	zero, t6	# E : t6 is last match aligned addr
418c2ecf20Sopenharmony_ci	or	t2, t4, a1	# E : 000000000000chch
428c2ecf20Sopenharmony_ci	sll	t5, 8, t3	# U : 00000000ch000000
438c2ecf20Sopenharmony_ci	mov	zero, t8	# E : t8 is last match byte compare mask
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	andnot  a0, 7, v0	# E : align source addr
468c2ecf20Sopenharmony_ci	or	t5, t3, t3	# E : 00000000chch0000
478c2ecf20Sopenharmony_ci	sll	a1, 32, t2	# U : 0000chch00000000
488c2ecf20Sopenharmony_ci	sll	a1, 48, t4	# U : chch000000000000
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	or	t4, a1, a1	# E : chch00000000chch
518c2ecf20Sopenharmony_ci	or	t2, t3, t2	# E : 0000chchchch0000
528c2ecf20Sopenharmony_ci	or	a1, t2, a1	# E : chchchchchchchch
538c2ecf20Sopenharmony_ci	lda	t5, -1		# E : build garbage mask
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	cmpbge  zero, t0, t1	# E : bits set iff byte == zero
568c2ecf20Sopenharmony_ci	mskqh	t5, a0, t4	# E : Complete garbage mask
578c2ecf20Sopenharmony_ci	xor	t0, a1, t2	# E : make bytes == c zero
588c2ecf20Sopenharmony_ci	cmpbge	zero, t4, t4	# E : bits set iff byte is garbage
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	cmpbge  zero, t2, t3	# E : bits set iff byte == c
618c2ecf20Sopenharmony_ci	andnot	t1, t4, t1	# E : clear garbage from null test
628c2ecf20Sopenharmony_ci	andnot	t3, t4, t3	# E : clear garbage from char test
638c2ecf20Sopenharmony_ci	bne	t1, $eos	# U : did we already hit the terminator?
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/* Character search main loop */
668c2ecf20Sopenharmony_ci$loop:
678c2ecf20Sopenharmony_ci	ldq	t0, 8(v0)	# L : load next quadword
688c2ecf20Sopenharmony_ci	cmovne	t3, v0, t6	# E : save previous comparisons match
698c2ecf20Sopenharmony_ci	nop			#   : Latency=2, extra map slot (keep nop with cmov)
708c2ecf20Sopenharmony_ci	nop
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	cmovne	t3, t3, t8	# E : Latency=2, extra map slot
738c2ecf20Sopenharmony_ci	nop			#   : keep with cmovne
748c2ecf20Sopenharmony_ci	addq	v0, 8, v0	# E :
758c2ecf20Sopenharmony_ci	xor	t0, a1, t2	# E :
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	cmpbge	zero, t0, t1	# E : bits set iff byte == zero
788c2ecf20Sopenharmony_ci	cmpbge	zero, t2, t3	# E : bits set iff byte == c
798c2ecf20Sopenharmony_ci	beq	t1, $loop	# U : if we havnt seen a null, loop
808c2ecf20Sopenharmony_ci	nop
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/* Mask out character matches after terminator */
838c2ecf20Sopenharmony_ci$eos:
848c2ecf20Sopenharmony_ci	negq	t1, t4		# E : isolate first null byte match
858c2ecf20Sopenharmony_ci	and	t1, t4, t4	# E :
868c2ecf20Sopenharmony_ci	subq	t4, 1, t5	# E : build a mask of the bytes up to...
878c2ecf20Sopenharmony_ci	or	t4, t5, t4	# E : ... and including the null
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	and	t3, t4, t3	# E : mask out char matches after null
908c2ecf20Sopenharmony_ci	cmovne	t3, t3, t8	# E : save it, if match found Latency=2, extra map slot
918c2ecf20Sopenharmony_ci	nop			#   : Keep with cmovne
928c2ecf20Sopenharmony_ci	nop
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	cmovne	t3, v0, t6	# E :
958c2ecf20Sopenharmony_ci	nop			#   : Keep with cmovne
968c2ecf20Sopenharmony_ci	/* Locate the address of the last matched character */
978c2ecf20Sopenharmony_ci	ctlz	t8, t2		# U0 : Latency=3 (0x40 for t8=0)
988c2ecf20Sopenharmony_ci	nop
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	cmoveq	t8, 0x3f, t2	# E : Compensate for case when no match is seen
1018c2ecf20Sopenharmony_ci	nop			# E : hide the cmov latency (2) behind ctlz latency
1028c2ecf20Sopenharmony_ci	lda	t5, 0x3f($31)	# E :
1038c2ecf20Sopenharmony_ci	subq	t5, t2, t5	# E : Normalize leading zero count
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	addq	t6, t5, v0	# E : and add to quadword address
1068c2ecf20Sopenharmony_ci	ret			# L0 : Latency=3
1078c2ecf20Sopenharmony_ci	nop
1088c2ecf20Sopenharmony_ci	nop
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	.end strrchr
1118c2ecf20Sopenharmony_ci	EXPORT_SYMBOL(strrchr)
112