1/*
2   Copyright (c) 2015-2024, 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/strlen.c will be
33   used.  */
34#include <picolibc.h>
35#if !defined (__OPTIMIZE_SIZE__) && !defined (PREFER_SIZE_OVER_SPEED) \
36    && !defined (__ARC_RF16__)
37
38#include "asm.h"
39
40#if defined(__ARC601__) || !defined (__ARC_BARREL_SHIFTER__)
41/* This code is optimized for the ARC601 pipeline without barrel shifter.  */
42
43ENTRY (strlen)
44	or	r3,r0,7
45	ld	r2,[r3,-7]
46	ld.a	r6,[r3,-3]
47	mov	r4,0x01010101
48	; uses long immediate
49#ifdef __LITTLE_ENDIAN__
50	bmsk.f	0,r0,1
51	mov_s	r1,31
52	add3_s	r1,r1,r0
53	bmsk	r7,r4,r1
54	xor.ne	r7,r7,r4
55	btst_s	r0,2
56	ror	r5,r4
57	sub	r1,r2,r7
58	bic_s	r1,r1,r2
59	mov.eq	r7,r4
60	sub	r12,r6,r7
61	bic	r12,r12,r6
62	or.eq	r12,r12,r1
63	and	r12,r12,r5
64	brne	r12,0,.Learly_end
65#else /* BIG ENDIAN */
66	add.f	r1,r4,30 ; r1 mod 31 := -1; clear carry
67	ror	r5,r4
68	sub3	r7,r1,r0
69	btst_s	r0,2
70	sub	r1,r2,r4
71	bic_s	r1,r1,r2
72	bmsk	r1,r1,r7
73	sub	r12,r6,r4
74	bic	r12,r12,r6
75	bmsk.ne	r12,r12,r7
76	or.eq	r12,r12,r1
77	and	r12,r12,r5
78	brne	r12,0,.Learly_end
79#endif /* ENDIAN */
80
81.Loop:
82	ld_s	r2,[r3,4]
83	ld.a	r6,[r3,8]
84	; stall for load result
85	sub	r1,r2,r4
86	bic_s	r1,r1,r2
87	sub	r12,r6,r4
88	bic	r12,r12,r6
89	or_s	r12,r12,r1
90	and	r12,r12,r5
91	breq_s	r12,0,.Loop
92.Lend:
93	and.f	r1,r1,r5
94	sub.ne	r3,r3,4
95#ifdef __LITTLE_ENDIAN__
96	mov.eq	r1,r12
97	btst_s	r1,7
98	sub	r0,r3,r0
99	add.eq	r0,r0,1
100	bmsk.f	0,r1,15
101	add.eq	r0,r0,1
102	bmsk.f	0,r1,23
103	j_s.d	[blink]
104	add.eq	r0,r0,1
105#else /* BIG ENDIAN */
106#ifdef __OPTIMIZE_SIZE__
1071:	ldb_s	r1,[r3]
108	breq_s	r1,0,0f
109	ldb.a	r1,[r3,1]
110	breq_s	r1,0,0f
111	ldb.a	r1,[r3,1]
112	breq_s	r1,0,0f
113	add_s	r3,r3,1
1140:	j_s.d	[blink]
115	sub	r0,r3,r0
116
117#define SPECIAL_EARLY_END
118.Learly_end:
119	mov_s	r3,r0
120	b_s	1b
121#elif 0 /* Need more information about pipeline to assess if this is faster.  */
122	mov.eq  r2,r6
123	and	r2,r2,r5
124	sub1	r2,r4,r2
125	mov.eq  r1,r12
126	bic.f   r1,r1,r2
127	sub     r0,r3,r0
128	add.pl  r0,r0,1
129	btst.pl	r1,23
130	add.eq	r0,r0,1
131	btst.eq	r1,15
132	j_s.d	[blink]
133	add.eq	r0,r0,1
134#else /* !__OPTIMIZE_SIZE__ */
135	/* Need carry clear here.  */
136	mov.eq	r2,r6
1371:	bmsk	r1,r2,23
138	breq	r1,r2,0f
139	bmsk	r2,r1,15
140	breq.d	r1,r2,0f
141	add_s	r3,r3,1
142	cmp	r2,0x100
143	add_s	r3,r3,2
1440:	j_s.d	[blink]
145	sbc	r0,r3,r0
146
147#define SPECIAL_EARLY_END
148.Learly_end:
149	sub_s.ne r1,r1,r1
150	mov_s	r12,0
151	bset	r12,r12,r7
152	sub1	r2,r2,r12
153	b.d	.Lend
154	sub1.ne	r6,r6,r12
155#endif /* !__OPTIMIZE_SIZE__ */
156#endif /* ENDIAN */
157
158#ifndef SPECIAL_EARLY_END
159	.balign	4
160.Learly_end:
161	b.d	.Lend
162	sub_s.ne r1,r1,r1
163#endif /* !SPECIAL_EARLY_END */
164ENDFUNC (strlen)
165#endif /* __ARC601__ || !__ARC_BARREL_SHIFTER__*/
166
167#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */
168