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