1/*
2   Copyright (c) 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#include <picolibc.h>
32
33#include <sys/asm.h>
34
35; This file contains variants of the same function with different
36; instructions.  The generic one, the implementation that comes the
37; last after the #else macro, is the most commented.
38
39; Using 128-bit memory operations
40#if defined (__ARC64_M128__)
41
42ENTRY (memset)
43	;; Assemble 128b token
44	bmsk_s	r1, r1, 7
45	lsl8	r3, r1
46	or_s	r1, r1, r3
47	lsl16	r3, r1
48	or	r6, r1, r3
49	addhl	r6, r6, r6
50	movl	r7, r6
51
52	lsrl.f	r5, r2, 6
53	beq.d	@.L_write_63_bytes
54	movl	r4, r0
55.L_write_64_bytes:
56	stdl.ab	r6r7, [r4, +16]
57	stdl.ab	r6r7, [r4, +16]
58	stdl.ab	r6r7, [r4, +16]
59	dbnz.d	r5, @.L_write_64_bytes
60	stdl.ab	r6r7, [r4, +16]
61	bmsk_s	r2, r2, 5
62
63.L_write_63_bytes:
64	bbit0.d	r2, 3, @1f
65	lsr	r3, r2, 4
66	stl.ab	r6, [r4, 8]
671:
68	bbit0.d	r2, 2, @1f
69	xor	r3, r3, 3
70	st.ab	r6, [r4, 4]
711:
72	bbit0	r2, 1, @1f
73	sth.ab	r6, [r4, 2]
741:
75	bbit0	r2, 0, @1f
76	stb.ab	r6, [r4, 1]
771:
78	bi	[r3]
79	stdl.ab	r6r7,[r4, 16]
80	stdl.ab	r6r7,[r4, 16]
81	stdl.ab	r6r7,[r4, 16]
82
83	j_s	[blink]
84
85.L_write_1_bytes:
86	breq	r2, 0, @.L_return
87	dbnz.d	r2, @.
88	stb.ab	r1, [r4, +1]
89.L_return:
90	j_s	[blink]
91ENDFUNC (memset)
92
93; The generic 64-bit implementation without any frills.
94#elif defined (__ARC64_ARCH64__) || defined (__ARC64_LL64__)
95
96#if defined (__ARC64_ARCH32__)
97# define MOVH  mov	r7,r6
98#elif defined (__ARC64_ARCH64__)
99# define MOVH  addhl	r6,r6,r6
100#else
101# error Please use either 32-bit or 64-bit version of arc64 compiler
102#endif
103
104; R0: dest
105; R1: ch
106; R2: count
107; ret (R0): dest
108ENTRY (memset)
109	;; Assemble the bytes to 64bit words
110	bmsk_s	r1, r1, 7		; treat it like unsigned char
111	lsl8	r3, r1
112	or_s	r1, r1, r3
113	lsl16	r3, r1
114	or	r6, r1, r3
115	MOVH
116
117	LSRP.f	r5, r2, 5		; counter for 32-byte chunks
118	beq.d	@.L_write_31_bytes
119	MOVP	r4, r0			; work on a copy of "r0"
120.L_write_32_bytes:
121	ST64.ab	r6, [r4, +8]
122	ST64.ab	r6, [r4, +8]
123	ST64.ab	r6, [r4, +8]
124	dbnz.d	r5, @.L_write_32_bytes
125	ST64.ab	r6, [r4, +8]
126	bmsk_s	r2, r2, 4
127
128.L_write_31_bytes:
129	bbit0.d	r2, 2, @1f
130	lsr	r3, r2, 3
131	st.ab	r6, [r4, 4]
1321:
133	bbit0.d	r2, 1, @1f
134	xor	r3, r3, 3
135	sth.ab	r6, [r4, 2]
1361:
137	bbit0	r2, 0, @1f
138	stb.ab	r6, [r4, 1]
1391:
140	bi	[r3]
141	ST64.ab	r6,[r4, 8]
142	ST64.ab	r6,[r4, 8]
143	ST64.ab	r6,[r4, 8]
144
145	j_s	[blink]
146ENDFUNC (memset)
147
148#elif defined (__ARC64_ARCH32__)
149ENTRY (memset)
150	;; Assemble the bytes to 32bit words
151	bmsk_s	r1, r1, 7		; treat it like unsigned char
152	lsl8	r3, r1
153	or_s	r1, r1, r3
154	lsl16	r3, r1
155	or	r6, r1, r3
156
157	lsr.f	r5, r2, 4		; counter for 16-byte chunks
158	beq.d	@.L_write_15_bytes
159	mov	r4, r0			; work on a copy of "r0"
160.L_write_16_bytes:
161	st.ab	r6, [r4, 4]
162	st.ab	r6, [r4, 4]
163	st.ab	r6, [r4, 4]
164	dbnz.d	r5, @.L_write_16_bytes
165	st.ab	r6, [r4, 4]
166	bmsk_s	r2, r2, 3
167
168.L_write_15_bytes:
169	bbit0.d	r2, 1, @1f
170	lsr	r3, r2, 2
171	sth.ab	r6, [r4, 2]
1721:
173	bbit0.d	r2, 0, @1f
174	xor	r3, r3, 3
175	stb.ab	r6, [r4, 1]
1761:
177	bi	[r3]
178	st.ab	r6,[r4, 4]
179	st.ab	r6,[r4, 4]
180	st.ab	r6,[r4, 4]
181
182	j_s	[blink]
183ENDFUNC (memset)
184#else
185# error Unknown configuration
186#endif
187