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/strlen.c will be
33   used.  */
34#if !defined (__OPTIMIZE_SIZE__) && !defined (PREFER_SIZE_OVER_SPEED)
35
36#include "asm.h"
37
38#if defined(__ARC601__) || !defined (__ARC_BARREL_SHIFTER__)
39/* This code is optimized for the ARC601 pipeline without barrel shifter.  */
40
41ENTRY (strlen)
42	or	r3,r0,7
43	ld	r2,[r3,-7]
44	ld.a	r6,[r3,-3]
45	mov	r4,0x01010101
46	; uses long immediate
47#ifdef __LITTLE_ENDIAN__
48	bmsk.f	0,r0,1
49	mov_s	r1,31
50	add3_s	r1,r1,r0
51	bmsk	r7,r4,r1
52	xor.ne	r7,r7,r4
53	btst_s	r0,2
54	ror	r5,r4
55	sub	r1,r2,r7
56	bic_s	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	add.f	r1,r4,30 ; r1 mod 31 := -1; clear carry
65	ror	r5,r4
66	sub3	r7,r1,r0
67	btst_s	r0,2
68	sub	r1,r2,r4
69	bic_s	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	btst_s	r1,7
96	sub	r0,r3,r0
97	add.eq	r0,r0,1
98	bmsk.f	0,r1,15
99	add.eq	r0,r0,1
100	bmsk.f	0,r1,23
101	j_s.d	[blink]
102	add.eq	r0,r0,1
103#else /* BIG ENDIAN */
104#ifdef __OPTIMIZE_SIZE__
1051:	ldb_s	r1,[r3]
106	breq_s	r1,0,0f
107	ldb.a	r1,[r3,1]
108	breq_s	r1,0,0f
109	ldb.a	r1,[r3,1]
110	breq_s	r1,0,0f
111	add_s	r3,r3,1
1120:	j_s.d	[blink]
113	sub	r0,r3,r0
114
115#define SPECIAL_EARLY_END
116.Learly_end:
117	mov_s	r3,r0
118	b_s	1b
119#elif 0 /* Need more information about pipeline to assess if this is faster.  */
120	mov.eq  r2,r6
121	and	r2,r2,r5
122	sub1	r2,r4,r2
123	mov.eq  r1,r12
124	bic.f   r1,r1,r2
125	sub     r0,r3,r0
126	add.pl  r0,r0,1
127	btst.pl	r1,23
128	add.eq	r0,r0,1
129	btst.eq	r1,15
130	j_s.d	[blink]
131	add.eq	r0,r0,1
132#else /* !__OPTIMIZE_SIZE__ */
133	/* Need carry clear here.  */
134	mov.eq	r2,r6
1351:	bmsk	r1,r2,23
136	breq	r1,r2,0f
137	bmsk	r2,r1,15
138	breq.d	r1,r2,0f
139	add_s	r3,r3,1
140	cmp	r2,0x100
141	add_s	r3,r3,2
1420:	j_s.d	[blink]
143	sbc	r0,r3,r0
144
145#define SPECIAL_EARLY_END
146.Learly_end:
147	sub_s.ne r1,r1,r1
148	mov_s	r12,0
149	bset	r12,r12,r7
150	sub1	r2,r2,r12
151	b.d	.Lend
152	sub1.ne	r6,r6,r12
153#endif /* !__OPTIMIZE_SIZE__ */
154#endif /* ENDIAN */
155
156#ifndef SPECIAL_EARLY_END
157	.balign	4
158.Learly_end:
159	b.d	.Lend
160	sub_s.ne r1,r1,r1
161#endif /* !SPECIAL_EARLY_END */
162ENDFUNC (strlen)
163#endif /* __ARC601__ || !__ARC_BARREL_SHIFTER__*/
164
165#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */
166