1/*
2   Copyright (c) 2015, Synopsys, Inc. All rights reserved.
3
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions are met:
6
7   1) Redistributions of source code must retain the above copyright notice,
8   this list of conditions and the following disclaimer.
9
10   2) Redistributions in binary form must reproduce the above copyright notice,
11   this list of conditions and the following disclaimer in the documentation
12   and/or other materials provided with the distribution.
13
14   3) Neither the name of the Synopsys, Inc., nor the names of its contributors
15   may be used to endorse or promote products derived from this software
16   without specific prior written permission.
17
18   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28   POSSIBILITY OF SUCH DAMAGE.
29*/
30
31/* This implementation is optimized for performance.  For code size a generic
32   implementation of this function from newlib/libc/string/strchr.c will be
33   used.  */
34#if !defined (__OPTIMIZE_SIZE__) && !defined (PREFER_SIZE_OVER_SPEED)
35
36/* ARC700 has a relatively long pipeline and branch prediction, so we want
37   to avoid branches that are hard to predict.  On the other hand, the
38   presence of the norm instruction makes it easier to operate on whole
39   words branch-free.  */
40#include "asm.h"
41
42#if (defined (__ARC700__) || defined (__ARCEM__) || defined (__ARCHS__)) \
43    && defined (__ARC_NORM__) && defined (__ARC_BARREL_SHIFTER__)
44
45ENTRY (strchr)
46	extb_s	r1,r1
47	asl	r5,r1,8
48	bmsk	r2,r0,1
49	or	r5,r5,r1
50	mov_s	r3,0x01010101
51	breq.d	r2,r0,.Laligned
52	asl	r4,r5,16
53	sub_s	r0,r0,r2
54	asl	r7,r2,3
55	ld_s	r2,[r0]
56#ifdef __LITTLE_ENDIAN__
57	asl	r7,r3,r7
58#else
59	lsr	r7,r3,r7
60#endif
61	or	r5,r5,r4
62	ror	r4,r3
63	sub	r12,r2,r7
64	bic_s	r12,r12,r2
65	and	r12,r12,r4
66	brne.d	r12,0,.Lfound0_ua
67	xor	r6,r2,r5
68	ld.a	r2,[r0,4]
69	sub	r12,r6,r7
70	bic	r12,r12,r6
71#ifdef __LITTLE_ENDIAN__
72	and	r7,r12,r4
73	breq	r7,0,.Loop ; For speed, we want this branch to be unaligned.
74	b_l	.Lfound_char ; Likewise this one.
75#else
76	and	r12,r12,r4
77	breq_l	r12,0,.Loop ; For speed, we want this branch to be unaligned.
78	lsr_s	r12,r12,7
79	bic	r2,r7,r6
80	b.d	.Lfound_char_b
81	and_s	r2,r2,r12
82#endif
83; /* We require this code address to be unaligned for speed...  */
84.Laligned:
85	ld_s	r2,[r0]
86	or	r5,r5,r4
87	ror	r4,r3
88; /* ... so that this code address is aligned, for itself and ...  */
89.Loop:
90	sub	r12,r2,r3
91	bic_s	r12,r12,r2
92	and	r12,r12,r4
93	brne.d	r12,0,.Lfound0
94	xor	r6,r2,r5
95	ld.a	r2,[r0,4]
96	sub	r12,r6,r3
97	bic	r12,r12,r6
98	and	r7,r12,r4
99	breq	r7,0,.Loop /* ... so that this branch is unaligned.  */
100	; Found searched-for character.  r0 has already advanced to next word.
101#ifdef __LITTLE_ENDIAN__
102/* We only need the information about the first matching byte
103   (i.e. the least significant matching byte) to be exact,
104   hence there is no problem with carry effects.  */
105.Lfound_char:
106	sub	r3,r7,1
107	bic	r3,r3,r7
108	norm	r2,r3
109	sub_s	r0,r0,1
110	asr_s	r2,r2,3
111	j_l.d	[blink]
112	sub_s	r0,r0,r2
113
114	.balign	4
115.Lfound0_ua:
116	mov_l	r3,r7
117.Lfound0:
118	sub	r3,r6,r3
119	bic	r3,r3,r6
120	and	r2,r3,r4
121	or_s	r12,r12,r2
122	sub_s	r3,r12,1
123	bic_s	r3,r3,r12
124	norm	r3,r3
125	add_s	r0,r0,3
126	asr_s	r12,r3,3
127	asl.f	0,r2,r3
128	sub_s	r0,r0,r12
129	j_s.d	[blink]
130	mov.pl	r0,0
131#else /* BIG ENDIAN */
132.Lfound_char:
133	lsr	r7,r7,7
134
135	bic	r2,r7,r6
136.Lfound_char_b:
137	norm	r2,r2
138	sub_s	r0,r0,4
139	asr_s	r2,r2,3
140	j_l.d	[blink]
141	add_s	r0,r0,r2
142
143.Lfound0_ua:
144	mov_s	r3,r7
145.Lfound0:
146	asl_s	r2,r2,7
147	or	r7,r6,r4
148	bic_s	r12,r12,r2
149	sub	r2,r7,r3
150	or	r2,r2,r6
151	bic	r12,r2,r12
152	bic.f	r3,r4,r12
153	norm	r3,r3
154
155	add.pl	r3,r3,1
156	asr_s	r12,r3,3
157	asl.f	0,r2,r3
158	add_s	r0,r0,r12
159	j_s.d	[blink]
160	mov.mi	r0,0
161#endif /* ENDIAN */
162ENDFUNC (strchr)
163#endif /* (__ARC700__ || __ARCEM__ || __ARCHS__) && __ARC_NORM__
164    && __ARC_BARREL_SHIFTER__ */
165
166#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */
167