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/memset.c will be
33   used.  */
34#if !defined (__OPTIMIZE_SIZE__) && !defined (PREFER_SIZE_OVER_SPEED)
35
36#include "asm.h"
37
38/* ARC HS has it's own implementation of memset, yet we want this function
39   still to be compiled under "__dummy_memset" disguise, because strncpy
40   function uses __strncpy_bzero as a second entry point into memset.  Would be
41   better to add __strncpy_bzero label to memset for ARC HS though, and even
42   better would be to avoid a second entry point into function.  ARC HS always
43   has barrel-shifter, so this implementation will be always used for this
44   purpose.  */
45#if !defined (__ARC601__) && defined (__ARC_BARREL_SHIFTER__)
46
47/* To deal with alignment/loop issues, SMALL must be at least 2.  */
48#define SMALL	7
49
50	.global	__strncpy_bzero
51	.hidden __strncpy_bzero
52/* __strncpy_bzero provides the following interface to strncpy:
53   r0: return value
54   r2: zeroing length
55   r3: zeroing start address
56   No attempt is made here for __strncpy_memset to speed up aligned
57   cases, because the copying of a string presumably leaves start address
58   and length alignment for the zeroing randomly distributed.  */
59
60#ifdef __ARCHS__
61ENTRY (__dummy_memset)
62#else
63ENTRY (memset)
64#endif
65#if !defined (__ARC700__) && !defined (__ARCEM__)
66#undef SMALL
67#define SMALL 8 /* Even faster if aligned.  */
68	brls.d	r2,SMALL,.Ltiny
69#endif
70	mov_s	r3,r0
71	or	r12,r0,r2
72	bmsk.f	r12,r12,1
73	extb_s	r1,r1
74	asl	r12,r1,8
75	beq.d	.Laligned
76	or_s	r1,r1,r12
77#if defined (__ARC700__) || defined (__ARCEM__)
78	brls	r2,SMALL,.Ltiny
79#endif
80.Lnot_tiny:
81	add_s	r12,r2,r0
82	stb	r1,[r12,-1]
83	bclr_l	r12,r12,0
84	stw	r1,[r12,-2]
85	bmsk.f	r12,r3,1
86	add_s	r2,r2,r12
87	sub.ne	r2,r2,4
88	stb.ab	r1,[r3,1]
89	bclr_s	r3,r3,0
90	stw.ab	r1,[r3,2]
91	bclr_s	r3,r3,1
92.Laligned:	; This code address should be aligned for speed.
93#if defined (__ARC700__) || defined (__ARCEM__)
94	asl	r12,r1,16
95	lsr.f	lp_count,r2,2
96	or_s	r1,r1,r12
97	lpne	.Loop_end
98	st.ab	r1,[r3,4]
99.Loop_end:
100	j_s	[blink]
101#else /* !__ARC700 */
102	lsr.f	lp_count,r2,3
103	asl	r12,r1,16
104	or_s	r1,r1,r12
105	lpne	.Loop_end
106	st.ab	r1,[r3,4]
107	st.ab	r1,[r3,4]
108.Loop_end:
109	jcc	[blink]
110	j_s.d	[blink]
111	st_s	r1,[r3]
112#endif /* !__ARC700 */
113
114#if defined (__ARC700__) || defined (__ARCEM__)
115	.balign	4
116__strncpy_bzero:
117	brhi.d	r2,17,.Lnot_tiny
118	mov_l	r1,0
119.Ltiny:
120	mov.f	lp_count,r2
121	lpne	.Ltiny_end
122	stb.ab	r1,[r3,1]
123.Ltiny_end:
124	j_s	[blink]
125#else /* !__ARC700__ */
126#if SMALL > 8
127FIXME
128#endif
129	.balign	4
130__strncpy_bzero:
131	brhi.d	r2,8,.Lnot_tiny
132	mov_s	r1,0
133.Ltiny:
134	sub_s	r2,r2,11
135	sub1	r12,pcl,r2
136	j_s	[r12]
137	stb_s	r1,[r3,7]
138	stb_s	r1,[r3,6]
139	stb_s	r1,[r3,5]
140	stb_s	r1,[r3,4]
141	stb_s	r1,[r3,3]
142	stb_s	r1,[r3,2]
143	stb_s	r1,[r3,1]
144	stb_s	r1,[r3]
145	j_s	[blink]
146#endif /* !__ARC700 */
147#ifdef __ARCHS__
148ENDFUNC (__dummy_memset)
149#else
150ENDFUNC (memset)
151#endif
152#endif /* !__ARC601__ && __ARC_BARREL_SHIFTER__ */
153
154#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */
155