xref: /freebsd/lib/libc/arm/string/memcmp.S (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1/*      $NetBSD: memcmp.S,v 1.3 2003/10/14 07:51:45 scw Exp $ */
2
3/*
4 * Copyright 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *      This product includes software developed for the NetBSD Project by
20 *      Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 *    or promote products derived from this software without specific prior
23 *    written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37/*
38 * Copyright (c) 2002 ARM Ltd
39 * All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 *    notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 *    notice, this list of conditions and the following disclaimer in the
48 *    documentation and/or other materials provided with the distribution.
49 * 3. The name of the company may not be used to endorse or promote
50 *    products derived from this software without specific prior written
51 *    permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
54 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
58 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
59 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
60 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
61 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 */
64
65#include <machine/asm.h>
66.syntax	unified
67
68ENTRY(memcmp)
69	mov	ip, r0
70#if defined(_KERNEL) && !defined(_STANDALONE)
71	cmp	r2, #0x06
72	beq	.Lmemcmp_6bytes
73#endif
74	mov	r0, #0x00
75
76	/* Are both addresses aligned the same way? */
77	cmp	r2, #0x00
78	eorsne	r3, ip, r1
79	RETeq			/* len == 0, or same addresses! */
80	tst	r3, #0x03
81	subne	r2, r2, #0x01
82	bne	.Lmemcmp_bytewise2	/* Badly aligned. Do it the slow way */
83
84	/* Word-align the addresses, if necessary */
85	sub	r3, r1, #0x05
86	ands	r3, r3, #0x03
87	add	r3, r3, r3, lsl #1
88	addne	pc, pc, r3, lsl #3
89	nop
90
91	/* Compare up to 3 bytes */
92	ldrb	r0, [ip], #0x01
93	ldrb	r3, [r1], #0x01
94	subs	r0, r0, r3
95	RETne
96	subs	r2, r2, #0x01
97	RETeq
98
99	/* Compare up to 2 bytes */
100	ldrb	r0, [ip], #0x01
101	ldrb	r3, [r1], #0x01
102	subs	r0, r0, r3
103	RETne
104	subs	r2, r2, #0x01
105	RETeq
106
107	/* Compare 1 byte */
108	ldrb	r0, [ip], #0x01
109	ldrb	r3, [r1], #0x01
110	subs	r0, r0, r3
111	RETne
112	subs	r2, r2, #0x01
113	RETeq
114
115	/* Compare 4 bytes at a time, if possible */
116	subs	r2, r2, #0x04
117	bcc	.Lmemcmp_bytewise
118.Lmemcmp_word_aligned:
119	ldr	r0, [ip], #0x04
120	ldr	r3, [r1], #0x04
121	subs	r2, r2, #0x04
122	cmpcs	r0, r3
123	beq	.Lmemcmp_word_aligned
124	sub	r0, r0, r3
125
126	/* Correct for extra subtraction, and check if done */
127	adds	r2, r2, #0x04
128	cmpeq	r0, #0x00		/* If done, did all bytes match? */
129	RETeq			/* Yup. Just return */
130
131	/* Re-do the final word byte-wise */
132	sub	ip, ip, #0x04
133	sub	r1, r1, #0x04
134
135.Lmemcmp_bytewise:
136	add	r2, r2, #0x03
137.Lmemcmp_bytewise2:
138	ldrb	r0, [ip], #0x01
139	ldrb	r3, [r1], #0x01
140	subs	r2, r2, #0x01
141	cmpcs	r0, r3
142	beq	.Lmemcmp_bytewise2
143	sub	r0, r0, r3
144	RET
145
146#if defined(_KERNEL) && !defined(_STANDALONE)
147	/*
148	 * 6 byte compares are very common, thanks to the network stack.
149	 * This code is hand-scheduled to reduce the number of stalls for
150	 * load results. Everything else being equal, this will be ~32%
151	 * faster than a byte-wise memcmp.
152	 */
153	.align	5
154.Lmemcmp_6bytes:
155	ldrb	r3, [r1, #0x00]		/* r3 = b2#0 */
156	ldrb	r0, [ip, #0x00]		/* r0 = b1#0 */
157	ldrb	r2, [r1, #0x01]		/* r2 = b2#1 */
158	subs	r0, r0, r3		/* r0 = b1#0 - b2#0 */
159	ldreqb	r3, [ip, #0x01]		/* r3 = b1#1 */
160	RETne			/* Return if mismatch on #0 */
161	subs	r0, r3, r2		/* r0 = b1#1 - b2#1 */
162	ldreqb	r3, [r1, #0x02]		/* r3 = b2#2 */
163	ldreqb	r0, [ip, #0x02]		/* r0 = b1#2 */
164	RETne			/* Return if mismatch on #1 */
165	ldrb	r2, [r1, #0x03]		/* r2 = b2#3 */
166	subs	r0, r0, r3		/* r0 = b1#2 - b2#2 */
167	ldreqb	r3, [ip, #0x03]		/* r3 = b1#3 */
168	RETne			/* Return if mismatch on #2 */
169	subs	r0, r3, r2		/* r0 = b1#3 - b2#3 */
170	ldreqb	r3, [r1, #0x04]		/* r3 = b2#4 */
171	ldreqb	r0, [ip, #0x04]		/* r0 = b1#4 */
172	RETne			/* Return if mismatch on #3 */
173	ldrb	r2, [r1, #0x05]		/* r2 = b2#5 */
174	subs	r0, r0, r3		/* r0 = b1#4 - b2#4 */
175	ldreqb	r3, [ip, #0x05]		/* r3 = b1#5 */
176	RETne			/* Return if mismatch on #4 */
177	sub	r0, r3, r2		/* r0 = b1#5 - b2#5 */
178	RET
179#endif
180END(memcmp)
181
182	.section .note.GNU-stack,"",%progbits
183