memcmp.S revision 135682
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: head/lib/libc/arm/string/memcmp.S 135682 2004-09-23 23:10:56Z cognet $");
68129202Scognet
69129202ScognetENTRY(memcmp)
70129202Scognet	mov	ip, r0
71129202Scognet#if defined(_KERNEL) && !defined(_STANDALONE)
72129202Scognet	cmp	r2, #0x06
73129202Scognet	beq	.Lmemcmp_6bytes
74129202Scognet#endif
75129202Scognet	mov	r0, #0x00
76129202Scognet
77129202Scognet	/* Are both addresses aligned the same way? */
78129202Scognet	cmp	r2, #0x00
79129202Scognet	eornes	r3, ip, r1
80129202Scognet	moveq	pc, lr			/* len == 0, or same addresses! */
81129202Scognet	tst	r3, #0x03
82129202Scognet	subne	r2, r2, #0x01
83129202Scognet	bne	.Lmemcmp_bytewise2	/* Badly aligned. Do it the slow way */
84129202Scognet
85129202Scognet	/* Word-align the addresses, if necessary */
86135682Scognet	sub	r3, r1, #0x05
87135682Scognet	ands	r3, r3, #0x03
88129202Scognet	add	r3, r3, r3, lsl #1
89129202Scognet	addne	pc, pc, r3, lsl #3
90129202Scognet	nop
91129202Scognet
92129202Scognet	/* Compare up to 3 bytes */
93129202Scognet	ldrb	r0, [ip], #0x01
94129202Scognet	ldrb	r3, [r1], #0x01
95129202Scognet	subs	r0, r0, r3
96129202Scognet	movne	pc, lr
97129202Scognet	subs	r2, r2, #0x01
98129202Scognet	moveq	pc, lr
99129202Scognet
100129202Scognet	/* Compare up to 2 bytes */
101129202Scognet	ldrb	r0, [ip], #0x01
102129202Scognet	ldrb	r3, [r1], #0x01
103129202Scognet	subs	r0, r0, r3
104129202Scognet	movne	pc, lr
105129202Scognet	subs	r2, r2, #0x01
106129202Scognet	moveq	pc, lr
107129202Scognet
108129202Scognet	/* Compare 1 byte */
109129202Scognet	ldrb	r0, [ip], #0x01
110129202Scognet	ldrb	r3, [r1], #0x01
111129202Scognet	subs	r0, r0, r3
112129202Scognet	movne	pc, lr
113129202Scognet	subs	r2, r2, #0x01
114129202Scognet	moveq	pc, lr
115129202Scognet
116129202Scognet	/* Compare 4 bytes at a time, if possible */
117129202Scognet	subs	r2, r2, #0x04
118129202Scognet	bcc	.Lmemcmp_bytewise
119129202Scognet.Lmemcmp_word_aligned:
120129202Scognet	ldr	r0, [ip], #0x04
121129202Scognet	ldr	r3, [r1], #0x04
122129202Scognet	subs	r2, r2, #0x04
123129202Scognet	cmpcs	r0, r3
124129202Scognet	beq	.Lmemcmp_word_aligned
125129202Scognet	sub	r0, r0, r3
126129202Scognet
127129202Scognet	/* Correct for extra subtraction, and check if done */
128129202Scognet	adds	r2, r2, #0x04
129129202Scognet	cmpeq	r0, #0x00		/* If done, did all bytes match? */
130129202Scognet	moveq	pc, lr			/* Yup. Just return */
131129202Scognet
132129202Scognet	/* Re-do the final word byte-wise */
133129202Scognet	sub	ip, ip, #0x04
134129202Scognet	sub	r1, r1, #0x04
135129202Scognet
136129202Scognet.Lmemcmp_bytewise:
137129202Scognet	add	r2, r2, #0x03
138129202Scognet.Lmemcmp_bytewise2:
139129202Scognet	ldrb	r0, [ip], #0x01
140129202Scognet	ldrb	r3, [r1], #0x01
141129202Scognet	subs	r2, r2, #0x01
142129202Scognet	cmpcs	r0, r3
143129202Scognet	beq	.Lmemcmp_bytewise2
144129202Scognet	sub	r0, r0, r3
145129202Scognet	mov	pc, lr
146129202Scognet
147129202Scognet#if defined(_KERNEL) && !defined(_STANDALONE)
148129202Scognet	/*
149129202Scognet	 * 6 byte compares are very common, thanks to the network stack.
150129202Scognet	 * This code is hand-scheduled to reduce the number of stalls for
151129202Scognet	 * load results. Everything else being equal, this will be ~32%
152129202Scognet	 * faster than a byte-wise memcmp.
153129202Scognet	 */
154129202Scognet	.align	5
155129202Scognet.Lmemcmp_6bytes:
156129202Scognet	ldrb	r3, [r1, #0x00]		/* r3 = b2#0 */
157129202Scognet	ldrb	r0, [ip, #0x00]		/* r0 = b1#0 */
158129202Scognet	ldrb	r2, [r1, #0x01]		/* r2 = b2#1 */
159129202Scognet	subs	r0, r0, r3		/* r0 = b1#0 - b2#0 */
160129202Scognet	ldreqb	r3, [ip, #0x01]		/* r3 = b1#1 */
161129202Scognet	movne	pc, lr			/* Return if mismatch on #0 */
162129202Scognet	subs	r0, r3, r2		/* r0 = b1#1 - b2#1 */
163129202Scognet	ldreqb	r3, [r1, #0x02]		/* r3 = b2#2 */
164129202Scognet	ldreqb	r0, [ip, #0x02]		/* r0 = b1#2 */
165129202Scognet	movne	pc, lr			/* Return if mismatch on #1 */
166129202Scognet	ldrb	r2, [r1, #0x03]		/* r2 = b2#3 */
167129202Scognet	subs	r0, r0, r3		/* r0 = b1#2 - b2#2 */
168129202Scognet	ldreqb	r3, [ip, #0x03]		/* r3 = b1#3 */
169129202Scognet	movne	pc, lr			/* Return if mismatch on #2 */
170129202Scognet	subs	r0, r3, r2		/* r0 = b1#3 - b2#3 */
171129202Scognet	ldreqb	r3, [r1, #0x04]		/* r3 = b2#4 */
172129202Scognet	ldreqb	r0, [ip, #0x04]		/* r0 = b1#4 */
173129202Scognet	movne	pc, lr			/* Return if mismatch on #3 */
174129202Scognet	ldrb	r2, [r1, #0x05]		/* r2 = b2#5 */
175129202Scognet	subs	r0, r0, r3		/* r0 = b1#4 - b2#4 */
176129202Scognet	ldreqb	r3, [ip, #0x05]		/* r3 = b1#5 */
177129202Scognet	movne	pc, lr			/* Return if mismatch on #4 */
178129202Scognet	sub	r0, r3, r2		/* r0 = b1#5 - b2#5 */
179129202Scognet	mov	pc, lr
180129202Scognet#endif
181