1/*
2Copyright (c) 2013 Andes Technology Corporation.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
8    Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10
11    Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14
15    The name of the company may not be used to endorse or promote
16    products derived from this software without specific prior written
17    permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED.  IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE FOR ANY
23DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31   Function:
32      strcmp - compare two strings.
33   Syntax:
34      int strcmp(const char *s1, const char *s2);
35   Description:
36      This function compares the two strings s1 and s2. It returns an
37      integer less than, equal to, or greater than zero if s1 is found,
38      respectively, to be less than, to match, or be greater than s2.
39   Return value:
40      strcmp returns an integer less than, equal to, or greater than
41      zero if s1 (or the first n bytes thereof) is found, respectively,
42      to be less than, to match, or be greater than s2.
43*/
44	.text
45	.align	2
46	.globl	strcmp
47	.type 	strcmp, @function
48strcmp:
49	/* If s1 or s2 are unaligned, then compare bytes.  */
50	or	$r5, $r1, $r0
51	andi    $r5, $r5, #3
52	bnez    $r5, .Lbyte_mode
53
54	/* If s1 and s2 are word-aligned, compare them a word at a time.  */
55	lwi	$r5, [$r0+(0)]
56	lwi	$r3, [$r1+(0)]
57	bne	$r5, $r3, .Lbyte_mode	/* A difference was detected, so
58					   search bytewise. */
59
60	/* It's more efficient to set bit mask outside the word_mode loop.  */
61	sethi	$r4, hi20(0xFEFEFEFF)	/* Set $r4 as -0x01010101.  */
62	ori	$r4, $r4, lo12(0xFEFEFEFF)
63	sethi	$r2, hi20(0x80808080)
64	ori	$r2, $r2, lo12(0x80808080)
65	b	.Ldetect_null
66
67.align	2
68.Lword_mode:
69	lmw.aim	$r5, [$r0], $r5
70	lmw.aim $r3, [$r1], $r3
71	bne     $r5, $r3, .Lbyte_mode
72
73.Ldetect_null:
74	/* #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
75	   DETECTNULL returns nonzero if (long)X contains a NULL byte.  */
76	nor	$r3, $r5, $r5		/* r3 = ~(X) */
77	add	$r5, $r5, $r4		/* r2 = ((X) - 0x01010101) */
78	and	$r5, $r5, $r3		/* r2 = ~(X) & ((X) - 0x01010101) */
79	and	$r5, $r5, $r2		/* r2= r2 & 0x80808080	*/
80	beqz	$r5, .Lword_mode	/* No NULL byte, compare next word.  */
81
82	/* To get here, *a1 == *a2, thus if we find a null in *a1,
83	   then the strings must be equal, so return zero.  */
84	movi	$r0, #0
85	ret
86
87.Lbyte_mode:
88	/* Byte-mode compare.  */
89	lbi.bi	$r5, [$r0], #1
90	lbi.bi	$r3, [$r1], #1
91	bne	$r5, $r3, 1f	/* Mismatch, done.  */
92	bnez	$r5, .Lbyte_mode
931:
94	sub $r0, $r5, $r3
95	ret
96	.size	strcmp, .-strcmp
97