1129202Scognet/*      $NetBSD: memcmp.S,v 1.3 2003/10/14 07:51:45 scw Exp $ */
2129202Scognet
3129202Scognet/*
4129202Scognet * Copyright 2003 Wasabi Systems, Inc.
5129202Scognet * All rights reserved.
6129202Scognet *
7129202Scognet * Written by Steve C. Woodford for Wasabi Systems, Inc.
8129202Scognet *
9129202Scognet * Redistribution and use in source and binary forms, with or without
10129202Scognet * modification, are permitted provided that the following conditions
11129202Scognet * are met:
12129202Scognet * 1. Redistributions of source code must retain the above copyright
13129202Scognet *    notice, this list of conditions and the following disclaimer.
14129202Scognet * 2. Redistributions in binary form must reproduce the above copyright
15129202Scognet *    notice, this list of conditions and the following disclaimer in the
16129202Scognet *    documentation and/or other materials provided with the distribution.
17129202Scognet * 3. All advertising materials mentioning features or use of this software
18129202Scognet *    must display the following acknowledgement:
19129202Scognet *      This product includes software developed for the NetBSD Project by
20129202Scognet *      Wasabi Systems, Inc.
21129202Scognet * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22129202Scognet *    or promote products derived from this software without specific prior
23129202Scognet *    written permission.
24129202Scognet *
25129202Scognet * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26129202Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27129202Scognet * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28129202Scognet * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29129202Scognet * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30129202Scognet * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31129202Scognet * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32129202Scognet * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33129202Scognet * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34129202Scognet * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35129202Scognet * POSSIBILITY OF SUCH DAMAGE.
36129202Scognet */
37129202Scognet/*
38129202Scognet * Copyright (c) 2002 ARM Ltd
39129202Scognet * All rights reserved.
40129202Scognet *
41129202Scognet * Redistribution and use in source and binary forms, with or without
42129202Scognet * modification, are permitted provided that the following conditions
43129202Scognet * are met:
44129202Scognet * 1. Redistributions of source code must retain the above copyright
45129202Scognet *    notice, this list of conditions and the following disclaimer.
46129202Scognet * 2. Redistributions in binary form must reproduce the above copyright
47129202Scognet *    notice, this list of conditions and the following disclaimer in the
48129202Scognet *    documentation and/or other materials provided with the distribution.
49129202Scognet * 3. The name of the company may not be used to endorse or promote
50129202Scognet *    products derived from this software without specific prior written
51129202Scognet *    permission.
52129202Scognet *
53129202Scognet * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
54129202Scognet * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55129202Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56129202Scognet * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57129202Scognet * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
58129202Scognet * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
59129202Scognet * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
60129202Scognet * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
61129202Scognet * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62129202Scognet * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63129202Scognet */
64129202Scognet
65129202Scognet#include <machine/asm.h>
66129202Scognet
67129202Scognet__FBSDID("$FreeBSD$");
68129202Scognet
69275256Sandrew.syntax	unified
70275256Sandrew
71129202ScognetENTRY(memcmp)
72129202Scognet	mov	ip, r0
73129202Scognet#if defined(_KERNEL) && !defined(_STANDALONE)
74129202Scognet	cmp	r2, #0x06
75129202Scognet	beq	.Lmemcmp_6bytes
76129202Scognet#endif
77129202Scognet	mov	r0, #0x00
78129202Scognet
79129202Scognet	/* Are both addresses aligned the same way? */
80129202Scognet	cmp	r2, #0x00
81275256Sandrew	eorsne	r3, ip, r1
82137464Scognet	RETeq			/* len == 0, or same addresses! */
83129202Scognet	tst	r3, #0x03
84129202Scognet	subne	r2, r2, #0x01
85129202Scognet	bne	.Lmemcmp_bytewise2	/* Badly aligned. Do it the slow way */
86129202Scognet
87129202Scognet	/* Word-align the addresses, if necessary */
88135682Scognet	sub	r3, r1, #0x05
89135682Scognet	ands	r3, r3, #0x03
90129202Scognet	add	r3, r3, r3, lsl #1
91129202Scognet	addne	pc, pc, r3, lsl #3
92129202Scognet	nop
93129202Scognet
94129202Scognet	/* Compare up to 3 bytes */
95129202Scognet	ldrb	r0, [ip], #0x01
96129202Scognet	ldrb	r3, [r1], #0x01
97129202Scognet	subs	r0, r0, r3
98137464Scognet	RETne
99129202Scognet	subs	r2, r2, #0x01
100137464Scognet	RETeq
101129202Scognet
102129202Scognet	/* Compare up to 2 bytes */
103129202Scognet	ldrb	r0, [ip], #0x01
104129202Scognet	ldrb	r3, [r1], #0x01
105129202Scognet	subs	r0, r0, r3
106137464Scognet	RETne
107129202Scognet	subs	r2, r2, #0x01
108137464Scognet	RETeq
109129202Scognet
110129202Scognet	/* Compare 1 byte */
111129202Scognet	ldrb	r0, [ip], #0x01
112129202Scognet	ldrb	r3, [r1], #0x01
113129202Scognet	subs	r0, r0, r3
114137464Scognet	RETne
115129202Scognet	subs	r2, r2, #0x01
116137464Scognet	RETeq
117129202Scognet
118129202Scognet	/* Compare 4 bytes at a time, if possible */
119129202Scognet	subs	r2, r2, #0x04
120129202Scognet	bcc	.Lmemcmp_bytewise
121129202Scognet.Lmemcmp_word_aligned:
122129202Scognet	ldr	r0, [ip], #0x04
123129202Scognet	ldr	r3, [r1], #0x04
124129202Scognet	subs	r2, r2, #0x04
125129202Scognet	cmpcs	r0, r3
126129202Scognet	beq	.Lmemcmp_word_aligned
127129202Scognet	sub	r0, r0, r3
128129202Scognet
129129202Scognet	/* Correct for extra subtraction, and check if done */
130129202Scognet	adds	r2, r2, #0x04
131129202Scognet	cmpeq	r0, #0x00		/* If done, did all bytes match? */
132137464Scognet	RETeq			/* Yup. Just return */
133129202Scognet
134129202Scognet	/* Re-do the final word byte-wise */
135129202Scognet	sub	ip, ip, #0x04
136129202Scognet	sub	r1, r1, #0x04
137129202Scognet
138129202Scognet.Lmemcmp_bytewise:
139129202Scognet	add	r2, r2, #0x03
140129202Scognet.Lmemcmp_bytewise2:
141129202Scognet	ldrb	r0, [ip], #0x01
142129202Scognet	ldrb	r3, [r1], #0x01
143129202Scognet	subs	r2, r2, #0x01
144129202Scognet	cmpcs	r0, r3
145129202Scognet	beq	.Lmemcmp_bytewise2
146129202Scognet	sub	r0, r0, r3
147137464Scognet	RET
148129202Scognet
149129202Scognet#if defined(_KERNEL) && !defined(_STANDALONE)
150129202Scognet	/*
151129202Scognet	 * 6 byte compares are very common, thanks to the network stack.
152129202Scognet	 * This code is hand-scheduled to reduce the number of stalls for
153129202Scognet	 * load results. Everything else being equal, this will be ~32%
154129202Scognet	 * faster than a byte-wise memcmp.
155129202Scognet	 */
156129202Scognet	.align	5
157129202Scognet.Lmemcmp_6bytes:
158129202Scognet	ldrb	r3, [r1, #0x00]		/* r3 = b2#0 */
159129202Scognet	ldrb	r0, [ip, #0x00]		/* r0 = b1#0 */
160129202Scognet	ldrb	r2, [r1, #0x01]		/* r2 = b2#1 */
161129202Scognet	subs	r0, r0, r3		/* r0 = b1#0 - b2#0 */
162129202Scognet	ldreqb	r3, [ip, #0x01]		/* r3 = b1#1 */
163137464Scognet	RETne			/* Return if mismatch on #0 */
164129202Scognet	subs	r0, r3, r2		/* r0 = b1#1 - b2#1 */
165129202Scognet	ldreqb	r3, [r1, #0x02]		/* r3 = b2#2 */
166129202Scognet	ldreqb	r0, [ip, #0x02]		/* r0 = b1#2 */
167137464Scognet	RETne			/* Return if mismatch on #1 */
168129202Scognet	ldrb	r2, [r1, #0x03]		/* r2 = b2#3 */
169129202Scognet	subs	r0, r0, r3		/* r0 = b1#2 - b2#2 */
170129202Scognet	ldreqb	r3, [ip, #0x03]		/* r3 = b1#3 */
171137464Scognet	RETne			/* Return if mismatch on #2 */
172129202Scognet	subs	r0, r3, r2		/* r0 = b1#3 - b2#3 */
173129202Scognet	ldreqb	r3, [r1, #0x04]		/* r3 = b2#4 */
174129202Scognet	ldreqb	r0, [ip, #0x04]		/* r0 = b1#4 */
175137464Scognet	RETne			/* Return if mismatch on #3 */
176129202Scognet	ldrb	r2, [r1, #0x05]		/* r2 = b2#5 */
177129202Scognet	subs	r0, r0, r3		/* r0 = b1#4 - b2#4 */
178129202Scognet	ldreqb	r3, [ip, #0x05]		/* r3 = b1#5 */
179137464Scognet	RETne			/* Return if mismatch on #4 */
180129202Scognet	sub	r0, r3, r2		/* r0 = b1#5 - b2#5 */
181137464Scognet	RET
182129202Scognet#endif
183270882SianEND(memcmp)
184288373Skib
185288373Skib	.section .note.GNU-stack,"",%progbits
186