1/* Optimized version of the standard strchr() function.
2   This file is part of the GNU C Library.
3   Copyright (C) 2000-2022 Free Software Foundation, Inc.
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 first occurence of chr in str or NULL
20
21   Inputs:
22        in0:    str
23        in1:    chr
24
25   A modified version of memchr.S, the search ends when the character is
26   found or the terminating null character is encountered.
27
28   This implementation assumes little endian mode.  For big endian mode,
29   the instruction czx1.r should be replaced by czx1.l.  */
30
31#include <sysdep.h>
32#undef ret
33
34#define saved_lc	r18
35#define poschr		r19
36#define pos0		r20
37#define val1		r21
38#define val2		r22
39#define tmp		r24
40#define chrx8		r25
41#define loopcnt		r30
42
43#define str		in0
44#define chr		in1
45
46ENTRY(strchr)
47	.prologue
48	alloc r2 = ar.pfs, 2, 0, 0, 0
49	.save ar.lc, saved_lc
50        mov 	saved_lc = ar.lc 	// save the loop counter
51	.body
52	mov 	ret0 = str
53	and 	tmp = 7, str		// tmp = str % 8
54	mux1	chrx8 = chr, @brcst
55	extr.u	chr = chr, 0, 8		// retain only the last byte
56	cmp.ne	p8, p0 = r0, r0		// clear p8
57	;;
58	sub	loopcnt = 8, tmp	// loopcnt = 8 - tmp
59	cmp.eq	p6, p0 = tmp, r0
60(p6)	br.cond.sptk	.str_aligned;;
61	adds	loopcnt = -1, loopcnt;;
62	mov	ar.lc = loopcnt
63.l1:
64	ld1	val2 = [ret0], 1
65	;;
66	cmp.eq	p6, p0 = val2, chr
67	cmp.eq	p7, p0 = val2, r0
68(p6)	br.cond.spnt	.restore_and_exit
69(p7)	br.cond.spnt	.notfound
70	br.cloop.sptk	.l1
71.str_aligned:
72	ld8	val1 = [ret0], 8;;
73	nop.b	0
74	nop.b 	0
75.l2:
76	ld8.s	val2 = [ret0], 8	// don't bomb out here
77	czx1.r	pos0 = val1
78	xor	tmp = val1, chrx8	// if val1 contains chr, tmp will
79	;;				// contain a zero in its position
80	czx1.r	poschr = tmp
81	cmp.ne	p6, p0 = 8, pos0
82	;;
83	cmp.ne	p7, p0 = 8, poschr
84(p7)	br.cond.spnt .foundit
85(p6)	br.cond.spnt .notfound
86	chk.s	val2, .recovery
87.back:
88	mov	val1 = val2
89	br.cond.dptk .l2
90.foundit:
91(p6)	cmp.lt	p8, p0 = pos0, poschr	// we found chr and null in the word
92(p8)	br.cond.spnt .notfound		// null was found before chr
93	add	ret0 = ret0, poschr ;;
94	adds	ret0 = -15, ret0 ;;	// should be -16, but we decrement
95.restore_and_exit:			// ret0 in the next instruction
96	adds	ret0 = -1, ret0		// ret0 was pointing 1 char too far
97	mov 	ar.lc = saved_lc	// restore the loop counter
98	br.ret.sptk.many b0
99.notfound:
100	mov	ret0 = r0		// return NULL if null was found
101	mov 	ar.lc = saved_lc
102	br.ret.sptk.many b0
103.recovery:
104	adds	ret0 = -8, ret0;;
105	ld8	val2 = [ret0], 8	// bomb out here
106	br.cond.sptk	.back
107END(strchr)
108
109weak_alias (strchr, index)
110libc_hidden_builtin_def (strchr)
111