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