xref: /minix3/common/lib/libc/arch/or1k/string/memcmp.S (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc/* $NetBSD: memcmp.S,v 1.1 2014/09/03 19:34:25 matt Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc/*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc * by Matt Thomas of 3am Software Foundry.
9*0a6a1f1dSLionel Sambuc *
10*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc * are met:
13*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc *    notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc *    notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc *    documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc *
19*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc */
31*0a6a1f1dSLionel Sambuc
32*0a6a1f1dSLionel Sambuc/*
33*0a6a1f1dSLionel Sambuc *
34*0a6a1f1dSLionel Sambuc *
35*0a6a1f1dSLionel Sambuc * int memcmp(const char *s1, const char *s2, size_t n);
36*0a6a1f1dSLionel Sambuc *
37*0a6a1f1dSLionel Sambuc *	for (; n-- != 0; s1++, s2++) {
38*0a6a1f1dSLionel Sambuc *		if (*s1 < *s2)
39*0a6a1f1dSLionel Sambuc *			return -1;
40*0a6a1f1dSLionel Sambuc *		if (*s1 > *s2)
41*0a6a1f1dSLionel Sambuc *			return 1;
42*0a6a1f1dSLionel Sambuc *	}
43*0a6a1f1dSLionel Sambuc *	return 0;
44*0a6a1f1dSLionel Sambuc *
45*0a6a1f1dSLionel Sambuc * Return:	((s1 > s2) ? 1 : (s1 < s2) ? -1 : 0)
46*0a6a1f1dSLionel Sambuc *
47*0a6a1f1dSLionel Sambuc * ==========================================================================
48*0a6a1f1dSLionel Sambuc */
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel Sambuc#include <machine/asm.h>
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc        .text
53*0a6a1f1dSLionel Sambuc        .align 4
54*0a6a1f1dSLionel Sambuc/* LINTSTUB: Func: void *memcmp(const void *, const void *, size_t) */
55*0a6a1f1dSLionel SambucENTRY(memcmp)
56*0a6a1f1dSLionel Sambuc
57*0a6a1f1dSLionel Sambuc	/*
58*0a6a1f1dSLionel Sambuc	 * Check count passed in R5. If zero, return 0; otherwise continue.
59*0a6a1f1dSLionel Sambuc	 */
60*0a6a1f1dSLionel Sambuc	l.sfeqi	r5, 0			/* nothing to compare?		*/
61*0a6a1f1dSLionel Sambuc	l.bf	.Lret_0			/*   yes, return equality	*/
62*0a6a1f1dSLionel Sambuc	l.nop
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc#ifdef _KERNEL
65*0a6a1f1dSLionel Sambuc	l.sfeqi	r5, 6			/* less than two words?		*/
66*0a6a1f1dSLionel Sambuc	l.bnf	.Lsixbyte_compare	/*   yes, just compare by bytes */
67*0a6a1f1dSLionel Sambuc	l.nop
68*0a6a1f1dSLionel Sambuc#endif
69*0a6a1f1dSLionel Sambuc
70*0a6a1f1dSLionel Sambuc	l.sfgesi r5, 7			/* less than two words?		*/
71*0a6a1f1dSLionel Sambuc	l.bnf	.Lbyte_compare		/*   yes, just compare by bytes */
72*0a6a1f1dSLionel Sambuc	l.nop
73*0a6a1f1dSLionel Sambuc
74*0a6a1f1dSLionel Sambuc	l.xor	r6, r3, r4		/* check alignment compatibility */
75*0a6a1f1dSLionel Sambuc	l.andi	r6, r6, 3		/* only care about the two bits */
76*0a6a1f1dSLionel Sambuc	l.sfeqi	r6, 0			/* same alignment? */
77*0a6a1f1dSLionel Sambuc	l.bnf	.Lmisaligned		/*   no, avoid alignment errors */
78*0a6a1f1dSLionel Sambuc	l.nop
79*0a6a1f1dSLionel Sambuc
80*0a6a1f1dSLionel Sambuc	/*
81*0a6a1f1dSLionel Sambuc	 * At this point, we know we read the data via word accesses.
82*0a6a1f1dSLionel Sambuc	 */
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc	l.andi	r7, r3, 3		/* check alignment		*/
85*0a6a1f1dSLionel Sambuc	l.sfeqi	r7, 0			/* word aligned?		*/
86*0a6a1f1dSLionel Sambuc	l.bf	.Lword_compare		/*   yes, it is.		*/
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc	l.sub	r3, r3, r7		/* align string 1		*/
89*0a6a1f1dSLionel Sambuc	l.sub	r4, r4, r7		/* align string 2		*/
90*0a6a1f1dSLionel Sambuc	l.add	r5, r5, r7		/* pad length			*/
91*0a6a1f1dSLionel Sambuc
92*0a6a1f1dSLionel Sambuc	l.lwz	r15, 0(r3)		/* load word from s1		*/
93*0a6a1f1dSLionel Sambuc	l.lwz	r17, 0(r4)		/* load word from s2		*/
94*0a6a1f1dSLionel Sambuc
95*0a6a1f1dSLionel Sambuc	l.slli	r7, r7, 3		/* bytes to bits		*/
96*0a6a1f1dSLionel Sambuc	l.sll	r15, r15, r7		/* shift away leading bytes	*/
97*0a6a1f1dSLionel Sambuc	l.sll	r17, r17, r7		/* shift away leading bytes	*/
98*0a6a1f1dSLionel Sambuc	l.j	.Lword_compare		/* now we can compare them	*/
99*0a6a1f1dSLionel Sambuc	l.nop
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc.Lword_loop:
102*0a6a1f1dSLionel Sambuc	l.lwz	r15, 0(r3)		/* load s1 word			*/
103*0a6a1f1dSLionel Sambuc	l.lwz	r17, 0(r4)		/* load s2 word			*/
104*0a6a1f1dSLionel Sambuc.Lword_compare:
105*0a6a1f1dSLionel Sambuc	l.sfeq	r15, r17		/* compare s1 and s2 words	*/
106*0a6a1f1dSLionel Sambuc	l.bnf	.Lall_done		/*   different? we're done	*/
107*0a6a1f1dSLionel Sambuc
108*0a6a1f1dSLionel Sambuc	l.addi	r3, r3, 4		/* advance s1 one word		*/
109*0a6a1f1dSLionel Sambuc	l.addi	r4, r4, 4		/* advance s2 one word		*/
110*0a6a1f1dSLionel Sambuc	l.addi	r5, r5, -4		/* decrement one word		*/
111*0a6a1f1dSLionel Sambuc	l.sfgtsi r5, 4			/* at least more than a word?	*/
112*0a6a1f1dSLionel Sambuc	l.bf	.Lword_loop		/*    yes, loop around		*/
113*0a6a1f1dSLionel Sambuc	l.nop
114*0a6a1f1dSLionel Sambuc	l.sfeqi	r5, 0			/* nothing left?		*/
115*0a6a1f1dSLionel Sambuc	l.bf	.Lret_0			/*   yes, return equality	*/
116*0a6a1f1dSLionel Sambuc	l.nop
117*0a6a1f1dSLionel Sambuc
118*0a6a1f1dSLionel Sambuc	/*
119*0a6a1f1dSLionel Sambuc	 * Fall through to handle the last word
120*0a6a1f1dSLionel Sambuc	 */
121*0a6a1f1dSLionel Sambuc
122*0a6a1f1dSLionel Sambuc	l.sub	r3, r0, r5		/* If count <= 4, handle 	*/
123*0a6a1f1dSLionel Sambuc	l.andi	r3, r3, 3		/* mask off low 2 bits		*/
124*0a6a1f1dSLionel Sambuc	l.slli	r3, r3, 3		/* count *= 8			*/
125*0a6a1f1dSLionel Sambuc	l.srl	r15, r15, r3		/* discard extra s1 bytes	*/
126*0a6a1f1dSLionel Sambuc	l.srl	r17, r17, r3		/* discard extra s2 bytes	*/
127*0a6a1f1dSLionel Sambuc
128*0a6a1f1dSLionel Sambuc	l.sfeq	r17, r15		/* compare result		*/
129*0a6a1f1dSLionel Sambuc	l.bnf	.Lall_done
130*0a6a1f1dSLionel Sambuc.Lret_0:
131*0a6a1f1dSLionel Sambuc	l.addi	r11, r0, 0
132*0a6a1f1dSLionel Sambuc	l.jr	lr
133*0a6a1f1dSLionel Sambuc	l.nop
134*0a6a1f1dSLionel Sambuc
135*0a6a1f1dSLionel Sambuc/*
136*0a6a1f1dSLionel Sambuc * The two string don't have the same word alignment.
137*0a6a1f1dSLionel Sambuc */
138*0a6a1f1dSLionel Sambuc.Lmisaligned:
139*0a6a1f1dSLionel Sambuc	l.sfeqi	r6, 2			/* check for halfword alignment */
140*0a6a1f1dSLionel Sambuc	l.bnf	.Lbyte_compare
141*0a6a1f1dSLionel Sambuc	l.nop
142*0a6a1f1dSLionel Sambuc	l.andi	r7, r3, 1
143*0a6a1f1dSLionel Sambuc	l.sfeqi	r7, 0
144*0a6a1f1dSLionel Sambuc	l.bf	.Lhalfword_loop
145*0a6a1f1dSLionel Sambuc	l.nop
146*0a6a1f1dSLionel Sambuc	l.addi	r5, r5, 1
147*0a6a1f1dSLionel Sambuc	l.addi	r3, r3, -1
148*0a6a1f1dSLionel Sambuc	l.addi	r4, r4, -1
149*0a6a1f1dSLionel Sambuc	l.lbz	r15, 1(r3)
150*0a6a1f1dSLionel Sambuc	l.lbz	r17, 1(r4)
151*0a6a1f1dSLionel Sambuc	l.j	.Lhalfword_compare
152*0a6a1f1dSLionel Sambuc	l.nop
153*0a6a1f1dSLionel Sambuc.Lhalfword_loop:
154*0a6a1f1dSLionel Sambuc	l.lhz	r15, 0(r3)
155*0a6a1f1dSLionel Sambuc	l.lhz	r17, 0(r4)
156*0a6a1f1dSLionel Sambuc.Lhalfword_compare:
157*0a6a1f1dSLionel Sambuc	l.sfeq	r15, r17
158*0a6a1f1dSLionel Sambuc	l.bnf	.Lall_done
159*0a6a1f1dSLionel Sambuc	l.nop
160*0a6a1f1dSLionel Sambuc	l.addi	r3, r3, 2
161*0a6a1f1dSLionel Sambuc	l.addi	r4, r4, 2
162*0a6a1f1dSLionel Sambuc	l.addi	r5, r5, -2
163*0a6a1f1dSLionel Sambuc	l.sfgesi r5, 2
164*0a6a1f1dSLionel Sambuc	l.bf	.Lhalfword_loop
165*0a6a1f1dSLionel Sambuc	l.nop
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc.Lbyte_compare:
168*0a6a1f1dSLionel Sambuc	l.addi	r5, r5, -1
169*0a6a1f1dSLionel Sambuc	l.sfgesi r5, 0
170*0a6a1f1dSLionel Sambuc	l.bnf	.Lret_0
171*0a6a1f1dSLionel Sambuc	l.nop
172*0a6a1f1dSLionel Sambuc	l.lbz	r15, 0(r3)
173*0a6a1f1dSLionel Sambuc	l.lbz	r17, 0(r4)
174*0a6a1f1dSLionel Sambuc	l.addi	r3, r3, 1
175*0a6a1f1dSLionel Sambuc	l.addi	r4, r4, 1
176*0a6a1f1dSLionel Sambuc	l.sfeq	r15, r17
177*0a6a1f1dSLionel Sambuc	l.bf	.Lbyte_compare
178*0a6a1f1dSLionel Sambuc	l.nop
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc.Lall_done:
181*0a6a1f1dSLionel Sambuc	l.sub	r11, r15, r17		/* subtract s2 from s1		*/
182*0a6a1f1dSLionel Sambuc	l.srai	r11, r11, 30		/* replicate sign bit thru bit 1 */
183*0a6a1f1dSLionel Sambuc	l.ori	r11, r11, 1		/* make sure bit 0 is set	*/
184*0a6a1f1dSLionel Sambuc	l.jr	lr
185*0a6a1f1dSLionel Sambuc	l.nop
186*0a6a1f1dSLionel Sambuc
187*0a6a1f1dSLionel Sambuc#ifdef _KERNEL
188*0a6a1f1dSLionel Sambuc.Lsixbyte_compare:
189*0a6a1f1dSLionel Sambuc	l.or	r7, r3, r4
190*0a6a1f1dSLionel Sambuc	l.andi	r7, r7, 1
191*0a6a1f1dSLionel Sambuc	l.sfeqi	r7, 0
192*0a6a1f1dSLionel Sambuc	l.bnf	.Lbyte_compare
193*0a6a1f1dSLionel Sambuc	l.nop
194*0a6a1f1dSLionel Sambuc	l.lhz	r15, 0(r3)
195*0a6a1f1dSLionel Sambuc	l.lhz	r17, 0(r4)
196*0a6a1f1dSLionel Sambuc	l.sfeq	r15, r17
197*0a6a1f1dSLionel Sambuc	l.bnf	.Lall_done
198*0a6a1f1dSLionel Sambuc	l.nop
199*0a6a1f1dSLionel Sambuc	l.lhz	r15, 2(r3)
200*0a6a1f1dSLionel Sambuc	l.lhz	r17, 2(r4)
201*0a6a1f1dSLionel Sambuc	l.sfeq	r15, r17
202*0a6a1f1dSLionel Sambuc	l.bnf	.Lall_done
203*0a6a1f1dSLionel Sambuc	l.nop
204*0a6a1f1dSLionel Sambuc	l.lhz	r15, 4(r3)
205*0a6a1f1dSLionel Sambuc	l.lhz	r17, 4(r4)
206*0a6a1f1dSLionel Sambuc	l.sfeq	r15, r17
207*0a6a1f1dSLionel Sambuc	l.bnf	.Lall_done
208*0a6a1f1dSLionel Sambuc	l.nop
209*0a6a1f1dSLionel Sambuc	l.addi	r11, r0, 0
210*0a6a1f1dSLionel Sambuc	l.jr	lr
211*0a6a1f1dSLionel Sambuc	l.nop
212*0a6a1f1dSLionel Sambuc#endif
213*0a6a1f1dSLionel SambucEND(memcmp)
214