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#if defined (__ARC64_ARCH32__)
36
37; 64 bit version has the same working principles, with slightly different
38; instructions, so it is more commented
39
40ENTRY (strcmp)
41	xor	r12, r12, r12
42
43	mov	r8, NULL_32DT_1
44
45	asl	r9, r8, 7
46
47.L_3_4B_comparison:
48
49	ld.ab	r6, [r0, +4]
50
51	ld.ab	r7, [r1, +4]
52
53#if defined (__ARC64_LL64__)
54
55	ldd.ab	r2r3, [r0, +8]
56
57	ldd.ab	r4r5, [r1, +8]
58
59#else
60
61	ld.ab	r2, [r0, +4]
62	ld.ab	r3, [r0, +4]
63
64	ld.ab	r4, [r1, +4]
65	ld.ab	r5, [r1, +4]
66
67#endif
68
69	sub	r13, r6, r8
70	sub	r10, r2, r8
71	sub	r11, r3, r8
72
73	bic	r13, r13, r6
74	bic	r10, r10, r2
75	bic	r11, r11, r3
76
77	; Look for difference
78	sub.f	0, r6, r7
79	bset.ne r12, r12, 3
80
81	sub.f	0, r2, r4
82	bset.ne r12, r12, 2
83
84	sub.f	0, r3, r5
85	bset.ne r12, r12, 1
86
87
88	; Look for NULL byte
89	and.f	r13, r13, r9
90	bset.ne	r12, r12, 3
91
92	and.f	r10, r10, r9
93	bset.ne	r12, r12, 2
94
95	and.f	r11, r11, r9
96	bset.ne	r12, r12, 1
97
98	breq	r12, 0, @.L_3_4B_comparison
99
100; Setup r0, r3 and r5 with the relevant loaded and intermediate values
101	mov r0, r11
102	mov	r3, r3
103	mov	r5, r5
104
105	asr.f	r12, r12, 3
106
107	mov.c	r0, r10
108	mov.c	r3, r2
109	mov.c	r5, r4
110
111	asr.f	r12, r12, 1
112
113	mov.c	r0, r13
114	mov.c	r3, r6
115	mov.c	r5, r7
116
117
118	ffs.f	r10, r0
119	xor	r12, r3, r5
120
121	mov.z	r10, 32
122	ffs r12, r12
123
124	xbfu 	r10, r10, 0b0111000011
125	xbfu 	r12, r12, 0b0111000011
126
127
128	sub.f	0, r10, r12
129
130	asl.ge	r12, r12, 3
131
132; Difference is first
133	lsr.ge	r3, r3, r12
134	lsr.ge	r5, r5, r12
135
136	bmsk	r3, r3, 7
137	bmsk	r5, r5, 7
138
139	j_s.d	[blink]
140	sub	r0, r3, r5
141
142
143ENDFUNC(strcmp)
144
145#else
146
147ENTRY (strcmp)
148
149	xorl	r12, r12, r12
150
151; Setup byte detector (more information bellow) [1]
152	vpack2wl	r8, NULL_32DT_1, NULL_32DT_1
153; Set r9 as a copy of r8 for vectorized sub
154	asll	r9, r8, 7
155
156.L_3_8B_comparison:
157
158	ldl.ab	r6, [r0, +8]
159
160	ldl.ab	r7, [r1, +8]
161
162; Using 128-bit memory operations
163#if defined (__ARC64_M128__)
164
165	lddl.ab	r2r3, [r0, +16]
166
167	lddl.ab	r4r5, [r1, +16]
168
169; The 64-bit crunching implementation.
170#elif defined (__ARC64_ARCH64__)
171
172	ldl.ab	r2, [r0, +8]
173	ldl.ab	r3, [r0, +8]
174
175	ldl.ab	r4, [r1, +8]
176	ldl.ab	r5, [r1, +8]
177
178#else
179	# error Unknown configuration
180#endif
181
182	subl	r13, r6, r8
183	subl	r10, r2, r8
184	subl	r11, r3, r8
185
186	bicl	r13, r13, r6
187	bicl	r10, r10, r2
188	bicl	r11, r11, r3
189
190; Look for difference
191	subl.f	0, r6, r7
192	bset.ne r12, r12, 3
193
194	subl.f	0, r2, r4
195	bset.ne r12, r12, 2
196
197	subl.f	0, r3, r5
198	bset.ne r12, r12, 1
199
200; Look for NULL byte
201	andl.f	r13, r13, r9
202	bset.ne	r12, r12, 3
203
204	andl.f	r10, r10, r9
205	bset.ne	r12, r12, 2
206
207	andl.f	r11, r11, r9
208	bset.ne	r12, r12, 1
209
210	breq	r12, 0, @.L_3_8B_comparison
211
212; Setup r0, r3 and r5 with the relevant loaded and intermediate values [2]
213	; [3]
214	movl	r0, r11
215	movl	r3, r3
216	movl	r5, r5
217
218	asr.f	r12, r12, 3
219
220	movl.c	r0, r10
221	movl.c	r3, r2
222	movl.c	r5, r4
223
224	asr.f	r12, r12, 1
225
226	movl.c	r0, r13
227	movl.c	r3, r6
228	movl.c	r5, r7
229
230	ffsl.f	r10, r0		; [5]
231	xorl	r12, r3, r5
232
233	movl.z	r10, 64		; [6]
234	ffsl	r12, r12	; [8]
235
236	xbful 	r10, r10, 0b0111000011	; [7]
237	xbful 	r12, r12, 0b0111000011
238
239; r12 contains position of difference and r10 the position of a NULL byte
240; r3 and r5 contain the differing 8 bytes
241
242; Is there a difference?
243	subl.f	0, r10, r12
244; Multiply the byte position by 8 to get bit shift
245	asll.ge	r12, r12, 3
246
247	lsrl.ge	r3, r3, r12
248	lsrl.ge	r5, r5, r12
249
250; There is no difference. Up until the NULL byte which must be
251
252	bmskl	r3, r3, 7
253	bmskl	r5, r5, 7
254
255	j_s.d	[blink]
256	subl	r0, r3, r5
257
258
259ENDFUNC (strcmp)
260
261#endif
262
263;; One important thing to note, is that we look for the first byte difference on
264;; both strings but we only look for the NULL byte in one string.
265;; This is because if a NULL byte appears first, it will be the first different
266;; byte. If it doesnt, the difference is what matters either way. If there is no
267;; difference, the NULL bytes will coincide!
268;
269;
270;; This code uses a common technique for NULL byte detection inside a word.
271;; Details on this technique can be found in:
272;; (https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord)
273;
274; In sum, this technique allows for detecting a NULL byte inside any given
275; amount of bits by performing the following operation
276; 		DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) [0]
277;
278; The code above implements this by setting r8 to a 0x01010101... sequence and
279; r1 to a 0x80808080... sequence of appropriate length
280; As LIMM are 32 bit only, we need to perform MOVHL and ORL [1] operations to
281; have the appropriate 64 bit values in place
282;
283;; Comparison is done 24 bytes at a time, either with 3 64 bit loads or 1 128 bit
284;; load and 1 64 bit.
285;; If either a NULL byte or a difference between the strings is found, r12 is
286;; used to know in which word the NULL/difference is found
287;
288; With the carry bit from r12, we can use mov.c to only move the appropriate
289; registers into the ones we will operate on [2]. We can safely directly move
290; the last set of registers without looking at r12, because if they aren't the
291; appropriate ones, they will be rewritten afterwards. [3]
292;
293;; Knowing the registers that contain the relevant information, we only need to
294;; look into where the difference and one of the zeros is.
295;; This is because, if the zeros are in different places, the difference will
296;; either be an earlier difference, or the first zero, so the actual zeros are
297;; irrelevant.
298;; Zero position is only relevant if there is no difference. And if there is no
299;; difference, the zeros have the same position.
300;
301; So now comes the tricky part. In order to obtain the position of a "first
302; NULL byte", we need to understand the NULL byte detection operation.
303; It is explained in depth in the link above but in short, it works by first
304; setting the highest bit of each byte to 1, if the corresponding byte is either
305; 0 or more than 0x80
306; Then, it makes the highest bit of each byte 1, if the byte is less than 0x80.
307; The last step is to AND these two values (this operation is simplified with
308; the SUB, BIC and TST instructions).
309;
310; This means that the evaluated equation result value has zeros for all non
311; zero bytes, except for the NULL bytes. Therefore, we can simply find the
312; first non zero bit (counting from bit 0) which will be inside the position of
313; the first NULL byte. [5]
314;
315; One thing to note, is that ffs oddly returns 31/63 if no bit is found, setting
316; the zero flag. As there can be that no NULL byte is present on one or both
317; strings at this point, we must set r10 and r11 to 32/64 when appropriate. [6]
318;
319; We can then convert the bit position into the last byte position by looking
320; into bits 3 to 5, and shifting 3 bits to the right. This can be combined into
321; a single xbful operation. The bottom 000011 represent shift by 3 and the top
322; 0111 represents the mask (3 to 5 shifted by 3 is 0 to 2). [7]
323;
324; To obtain the position of the difference, all we need to do is xor the two
325; registers. This way, every equal byte cancels out and all we are left with
326; is gibberish in the differing bytes. We can use the same ffs and xbuf
327; operations to get the differing byte position.
328;
329; Note that the order of the operations isnt the same as in this explanation,
330; to reduce register dependency between instructions
331;
332;
333; Unlike with r10, we dont need to check the zero flag for r12s' ffs because if
334; it is 0, it means there is no difference in the loaded data so any subtraction
335; operation will return 0 [8]
336;
337; There is one optimization that is being overlooked, which is returning 0 if
338; there is no difference, but there are NULL bytes anywhere, right after the
339; main loop. The reason for this is because the only way this can happen is if
340; the strings have the same length AND either are a multiple of 16/8 bytes, or
341; the bytes that follow the NULL bytes also match. As this is extremely
342; unlikely, it isnt worth it to perform this optimization since it would require
343; an extra branch in all runs
344;
345