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 (__ARC600__) || !defined (__ARC_NORM__)) && !defined (__ARC601__) \
41    && defined (__ARC_BARREL_SHIFTER__)
42/* This code is optimized for the ARC600 pipeline.  */
43
44ENTRY (strlen)
45	or	r3,r0,7
46	ld	r2,[r3,-7]
47	ld.a	r6,[r3,-3]
48	mov	r4,0x01010101
49	; uses long immediate
50#ifdef __LITTLE_ENDIAN__
51	asl_s	r1,r0,3
52	btst_s	r0,2
53	asl	r7,r4,r1
54	ror	r5,r4
55	sub	r1,r2,r7
56	bic_l	r1,r1,r2
57	mov.eq	r7,r4
58	sub	r12,r6,r7
59	bic	r12,r12,r6
60	or.eq	r12,r12,r1
61	and	r12,r12,r5
62	brne	r12,0,.Learly_end
63#else /* BIG ENDIAN */
64	ror	r5,r4
65	btst_s	r0,2
66	mov_s	r1,31
67	sub3	r7,r1,r0
68	sub	r1,r2,r4
69	bic_l	r1,r1,r2
70	bmsk	r1,r1,r7
71	sub	r12,r6,r4
72	bic	r12,r12,r6
73	bmsk.ne	r12,r12,r7
74	or.eq	r12,r12,r1
75	and	r12,r12,r5
76	brne	r12,0,.Learly_end
77#endif /* ENDIAN */
78
79.Loop:
80	ld_s	r2,[r3,4]
81	ld.a	r6,[r3,8]
82	; stall for load result
83	sub	r1,r2,r4
84	bic_s	r1,r1,r2
85	sub	r12,r6,r4
86	bic	r12,r12,r6
87	or_s	r12,r12,r1
88	and	r12,r12,r5
89	breq_s	r12,0,.Loop
90.Lend:
91	and.f	r1,r1,r5
92	sub.ne	r3,r3,4
93#ifdef __LITTLE_ENDIAN__
94	mov.eq	r1,r12
95	asr.f	0,r1,8
96	bmsk.f	0,r1,15
97	sub	r0,r3,r0
98	add.cc	r0,r0,1
99	jne.d	[blink]
100	asl.f	0,r1,9
101	j_s.d	[blink]
102	sbc	r0,r0,-2
103#else /* BIG ENDIAN */
104	mov.eq	r2,r6
105	asl_s	r2,r2,7
106	mov.eq	r1,r12
107	bic_s	r1,r1,r2
108	asr.f	0,r1,16
109	sub	r0,r3,r0
110	add.pl	r0,r0,1
111	jne.d	[blink]
112	add.eq	r0,r0,1
113	j_s.d	[blink]
114	add.cc	r0,r0,1
115#endif /* ENDIAN */
116
117	.balign	4
118.Learly_end:
119	b.d	.Lend
120	sub_s.ne r1,r1,r1
121ENDFUNC (strlen)
122#endif /* (__ARC600__ || !__ARC_NORM__) && !__ARC601__ && __ARC_BARREL_SHIFTER__ */
123
124#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */
125