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#ifdef __ARCHS__
39
40#define USE_PREFETCH
41
42#ifdef USE_PREFETCH
43#define PREWRITE(A,B)	prefetchw [(A),(B)]
44#else
45#define PREWRITE(A,B)	prealloc [(A),(B)]
46#endif
47
48ENTRY (memset)
49	prefetchw [r0]		; Prefetch the write location
50	mov.f	0, r2
51; if size is zero
52	jz.d	[blink]
53	mov	r3, r0		; don't clobber ret val
54
55; if length < 8
56	brls.d.nt	r2, 8, .Lsmallchunk
57	mov.f	lp_count,r2
58
59	and.f	r4, r0, 0x03
60	rsub	lp_count, r4, 4
61	lpnz	@.Laligndestination
62	; LOOP BEGIN
63	stb.ab	r1, [r3,1]
64	sub	r2, r2, 1
65.Laligndestination:
66
67; Destination is aligned
68	and	r1, r1, 0xFF
69	asl	r4, r1, 8
70	or	r4, r4, r1
71	asl	r5, r4, 16
72	or	r5, r5, r4
73	mov	r4, r5
74
75	sub3	lp_count, r2, 8
76	cmp     r2, 64
77	bmsk.hi	r2, r2, 5
78	mov.ls	lp_count, 0
79	add3.hi	r2, r2, 8
80
81; Convert len to Dwords, unfold x8
82	lsr.f	lp_count, lp_count, 6
83	lpnz	@.Lset64bytes
84	; LOOP START
85	PREWRITE (r3, 64)	;Prefetch the next write location
86#ifdef __ARC_LL64__
87	std.ab	r4, [r3, 8]
88	std.ab	r4, [r3, 8]
89	std.ab	r4, [r3, 8]
90	std.ab	r4, [r3, 8]
91	std.ab	r4, [r3, 8]
92	std.ab	r4, [r3, 8]
93	std.ab	r4, [r3, 8]
94	std.ab	r4, [r3, 8]
95#else
96	st.ab	r4, [r3, 4]
97	st.ab	r4, [r3, 4]
98	st.ab	r4, [r3, 4]
99	st.ab	r4, [r3, 4]
100	st.ab	r4, [r3, 4]
101	st.ab	r4, [r3, 4]
102	st.ab	r4, [r3, 4]
103	st.ab	r4, [r3, 4]
104	st.ab	r4, [r3, 4]
105	st.ab	r4, [r3, 4]
106	st.ab	r4, [r3, 4]
107	st.ab	r4, [r3, 4]
108	st.ab	r4, [r3, 4]
109	st.ab	r4, [r3, 4]
110	st.ab	r4, [r3, 4]
111	st.ab	r4, [r3, 4]
112#endif
113.Lset64bytes:
114
115	lsr.f	lp_count, r2, 5 ;Last remaining  max 124 bytes
116	lpnz	.Lset32bytes
117	; LOOP START
118	prefetchw [r3, 32]	;Prefetch the next write location
119#ifdef __ARC_LL64__
120	std.ab	r4, [r3, 8]
121	std.ab	r4, [r3, 8]
122	std.ab	r4, [r3, 8]
123	std.ab	r4, [r3, 8]
124#else
125	st.ab	r4, [r3, 4]
126	st.ab	r4, [r3, 4]
127	st.ab	r4, [r3, 4]
128	st.ab	r4, [r3, 4]
129	st.ab	r4, [r3, 4]
130	st.ab	r4, [r3, 4]
131	st.ab	r4, [r3, 4]
132	st.ab	r4, [r3, 4]
133#endif
134.Lset32bytes:
135
136	and.f	lp_count, r2, 0x1F ;Last remaining 31 bytes
137.Lsmallchunk:
138	lpnz	.Lcopy3bytes
139	; LOOP START
140	stb.ab	r1, [r3, 1]
141.Lcopy3bytes:
142
143	j	[blink]
144
145ENDFUNC (memset)
146#endif /* __ARCHS__ */
147
148#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */
149