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/strncpy.c will be
33   used.  */
34#if !defined (__OPTIMIZE_SIZE__) && !defined (PREFER_SIZE_OVER_SPEED)
35
36#include "asm.h"
37
38/* If dst and src are 4 byte aligned, copy 8 bytes at a time.
39   If the src is 4, but not 8 byte aligned, we first read 4 bytes to get
40   it 8 byte aligned.  Thus, we can do a little read-ahead, without
41   dereferencing a cache line that we should not touch.
42   Note that short and long instructions have been scheduled to avoid
43   branch stalls.
44   The beq_s to r3z could be made unaligned & long to avoid a stall
45   there, but the it is not likely to be taken often, and it
46   would also be likey to cost an unaligned mispredict at the next call.  */
47
48#if !defined (__ARC601__) && defined (__ARC_BARREL_SHIFTER__)
49
50#if defined (__ARC700___) || defined (__ARCEM__) || defined (__ARCHS__)
51#define BRand(a,b,l)	tst	a,b ` bne_l l
52#else
53#define BRand(a,b,l)	and	a,a,b ` brne_s a,0,l
54#endif
55
56ENTRY (strncpy)
57	cmp_s	r2,8
58	or	r12,r0,r1
59	bmsk.cc.f r12,r12,1
60	brne.d	r12,0,.Lbytewise
61	mov_s	r10,r0
62	ld_s	r3,[r1,0]
63	mov	r8,0x01010101
64	sub	lp_count,r2,1
65	bbit0.d	r1,2,.Loop_start
66	ror	r11,r8
67	sub	r12,r3,r8
68	bic_l	r12,r12,r3
69	BRand	(r12,r11,.Lr3z)
70	mov_s	r4,r3
71	ld.a	r3,[r1,4]
72	sub	lp_count,lp_count,4
73	st.ab	r4,[r10,4]
74	.balign 4
75.Loop_start:
76	lsr.f	lp_count,lp_count,3
77	lpne	.Loop_end
78	ld.a	r4,[r1,4]
79	sub	r12,r3,r8
80	bic_s	r12,r12,r3
81	BRand	(r12,r11,.Lr3z)
82	st.ab	r3,[r10,4]
83	sub	r12,r4,r8
84	bic	r12,r12,r4
85	BRand	(r12,r11,.Lr4z)
86	ld.a	r3,[r1,4]
87	st.ab	r4,[r10,4]
88.Loop_end:
89	bcc_s	.Lastword
90	ld.a	r4,[r1,4]
91	sub	r12,r3,r8
92	bic_s	r12,r12,r3
93	BRand	(r12,r11,.Lr3z)
94	st.ab	r3,[r10,4]
95	mov_s	r3,r4
96.Lastword:
97	and.f	lp_count,r2,3
98	mov.eq	lp_count,4
99	lp	.Last_byte_end
100#ifdef __LITTLE_ENDIAN__
101	bmsk.f	r1,r3,7
102	lsr.ne	r3,r3,8
103#else
104	lsr.f	r1,r3,24
105	asl.ne	r3,r3,8
106#endif
107	stb.ab	r1,[r10,1]
108.Last_byte_end:
109	j_s	[blink]
110
111	.balign	4
112.Lr4z:
113	mov_l	r3,r4
114.Lr3z:
115#if defined (__ARC700__) || defined (__ARCEM__) || defined (__ARCHS__)
116#ifdef __LITTLE_ENDIAN__
117	bmsk.f	r1,r3,7
118	lsr_s	r3,r3,8
119#else
120	lsr.f	r1,r3,24
121	asl_s	r3,r3,8
122#endif
123	bne.d	.Lr3z
124	stb.ab	r1,[r10,1]
125#else /* ! __ARC700__ */
126#ifdef __LITTLE_ENDIAN__
127	bmsk.f	r1,r3,7
128.Lr3z_loop:
129	lsr_s	r3,r3,8
130	stb.ab	r1,[r10,1]
131	bne.d	.Lr3z_loop
132	bmsk.f	r1,r3,7
133#else
134	lsr.f	r1,r3,24
135.Lr3z_loop:
136	asl_s	r3,r3,8
137	stb.ab	r1,[r10,1]
138	bne.d	.Lr3z_loop
139	lsr.f	r1,r3,24
140#endif /* ENDIAN */
141#endif /* ! __ARC700__ */
142.Lzero_rest:
143	; __strncpy_bzero requires:
144	; return value in r0
145	; zeroing length in r2
146	; zeroing start address in r3
147	mov_s	r3,r10
148	add_s	r2,r2,r0
149	b.d	__strncpy_bzero
150	sub_s	r2,r2,r3
151
152	.balign	4
153.Lbytewise:
154	sub.f	r2,r2,1
155	mov_l	r3,r0
156	jcs	[blink]
157.Lcharloop:
158	ldb.ab	r12,[r1,1]
159	beq.d	.Last_byte
160	sub.f	r2,r2,1
161	brne.d	r12,0,.Lcharloop
162	stb.ab	r12,[r3,1]
163	b.d	__strncpy_bzero
164	stb.ab	r12,[r3,1]
165.Last_byte:
166	j_s.d	[blink]
167	stb_l	r12,[r3]
168ENDFUNC (strncpy)
169#endif /* !__ARC601__ && __ARC_BARREL_SHIFTER__ */
170
171#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */
172