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/strcmp.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#ifdef __ARCHS__
42ENTRY (strcmp)
43	or	r2, r0, r1
44	bmsk_s	r2, r2, 1
45	brne	r2, 0, .Lcharloop
46
47; s1 and s2 are word aligned
48	ld.ab	r2, [r0, 4]
49
50	mov_s	r12, 0x01010101
51	ror	r11, r12
52	.align  4
53.LwordLoop:
54	ld.ab	r3, [r1, 4]
55	; Detect NULL char in str1
56	sub	r4, r2, r12
57	ld.ab	r5, [r0, 4]
58	bic	r4, r4, r2
59	and	r4, r4, r11
60	brne.d.nt	r4, 0, .LfoundNULL
61	; Check if the read locations are the same
62	cmp	r2, r3
63	beq.d	.LwordLoop
64	mov.eq	r2, r5
65
66	; A match is found, spot it out
67#ifdef __LITTLE_ENDIAN__
68	swape	r3, r3
69	mov_s	r0, 1
70	swape	r2, r2
71#else
72	mov_s	r0, 1
73#endif
74	cmp_s	r2, r3
75	j_s.d	[blink]
76	bset.lo	r0, r0, 31
77
78	.align 4
79.LfoundNULL:
80#ifdef __BIG_ENDIAN__
81	swape	r4, r4
82	swape	r2, r2
83	swape	r3, r3
84#endif
85	; Find null byte
86	ffs	r0, r4
87	bmsk	r2, r2, r0
88	bmsk	r3, r3, r0
89	swape	r2, r2
90	swape	r3, r3
91	; make the return value
92	sub.f	r0, r2, r3
93	mov.hi	r0, 1
94	j_s.d	[blink]
95	bset.lo	r0, r0, 31
96
97	.align 4
98.Lcharloop:
99	ldb.ab	r2, [r0, 1]
100	ldb.ab	r3, [r1, 1]
101	nop
102	breq	r2, 0, .Lcmpend
103	breq	r2, r3, .Lcharloop
104
105	.align 4
106.Lcmpend:
107	j_s.d	[blink]
108	sub	r0, r2, r3
109ENDFUNC (strcmp)
110#endif /* __ARCHS__ */
111
112#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */
113