1/* Copyright (C) 2000-2021 Free Software Foundation, Inc.
2   EV67 optimized by Rick Gorton <rick.gorton@alpha-processor.com>.
3   This file is part of the GNU C Library.
4
5   The GNU C Library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   The GNU C Library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library.  If not, see
17   <https://www.gnu.org/licenses/>.  */
18
19/* Return the address of the last occurrence of a given character
20   within a null-terminated string, or null if it is not found.  */
21
22#include <sysdep.h>
23
24	.arch ev6
25	.set noreorder
26	.set noat
27
28ENTRY(strrchr)
29#ifdef PROF
30	ldgp	gp, 0(pv)
31	lda	AT, _mcount
32	jsr	AT, (AT), _mcount
33	.prologue 1
34#else
35	.prologue 0
36#endif
37
38	and	a1, 0xff, t2	# E : 00000000000000ch
39	insbl	a1, 1, t4	# U : 000000000000ch00
40	insbl	a1, 2, t5	# U : 0000000000ch0000
41	ldq_u   t0, 0(a0)	# L : load first quadword Latency=3
42
43	mov	zero, t6	# E : t6 is last match aligned addr
44	or	t2, t4, a1	# E : 000000000000chch
45	sll	t5, 8, t3	# U : 00000000ch000000
46	mov	zero, t8	# E : t8 is last match byte compare mask
47
48	andnot  a0, 7, v0	# E : align source addr
49	or	t5, t3, t3	# E : 00000000chch0000
50	sll	a1, 32, t2	# U : 0000chch00000000
51	sll	a1, 48, t4	# U : chch000000000000
52
53	or	t4, a1, a1	# E : chch00000000chch
54	or	t2, t3, t2	# E : 0000chchchch0000
55	or	a1, t2, a1	# E : chchchchchchchch
56	lda	t5, -1		# E : build garbage mask
57
58	cmpbge  zero, t0, t1	# E : bits set iff byte == zero
59	mskqh	t5, a0, t4	# E : Complete garbage mask
60	xor	t0, a1, t2	# E : make bytes == c zero
61	cmpbge	zero, t4, t4	# E : bits set iff byte is garbage
62
63	cmpbge  zero, t2, t3	# E : bits set iff byte == c
64	andnot	t1, t4, t1	# E : clear garbage from null test
65	andnot	t3, t4, t3	# E : clear garbage from char test
66	bne	t1, $eos	# U : did we already hit the terminator?
67
68	/* Character search main loop */
69$loop:
70	ldq	t0, 8(v0)	# L : load next quadword
71	cmovne	t3, v0, t6	# E : save previous comparisons match
72	nop			#   : Latency=2, extra map slot (keep nop with cmov)
73	nop
74
75	cmovne	t3, t3, t8	# E : Latency=2, extra map slot
76	nop			#   : keep with cmovne
77	addq	v0, 8, v0	# E :
78	xor	t0, a1, t2	# E :
79
80	cmpbge	zero, t0, t1	# E : bits set iff byte == zero
81	cmpbge	zero, t2, t3	# E : bits set iff byte == c
82	beq	t1, $loop	# U : if we havnt seen a null, loop
83	nop
84
85	/* Mask out character matches after terminator */
86$eos:
87	negq	t1, t4		# E : isolate first null byte match
88	and	t1, t4, t4	# E :
89	subq	t4, 1, t5	# E : build a mask of the bytes upto...
90	or	t4, t5, t4	# E : ... and including the null
91
92	and	t3, t4, t3	# E : mask out char matches after null
93	cmovne	t3, t3, t8	# E : save it, if match found Latency=2, extra map slot
94	nop			#   : Keep with cmovne
95	nop
96
97	cmovne	t3, v0, t6	# E :
98	nop			#   : Keep with cmovne
99	/* Locate the address of the last matched character */
100	ctlz	t8, t2		# U0 : Latency=3 (0x40 for t8=0)
101	nop
102
103	cmoveq	t8, 0x3f, t2	# E : Compensate for case when no match is seen
104	nop			# E : hide the cmov latency (2) behind ctlz latency
105	lda	t5, 0x3f($31)	# E :
106	subq	t5, t2, t5	# E : Normalize leading zero count
107
108	addq	t6, t5, v0	# E : and add to quadword address
109	ret			# L0 : Latency=3
110	nop
111	nop
112
113END(strrchr)
114
115weak_alias (strrchr, rindex)
116libc_hidden_builtin_def (strrchr)
117