xref: /freebsd/lib/libc/arm/string/memcmp.S (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
12357939bSOlivier Houchard/*      $NetBSD: memcmp.S,v 1.3 2003/10/14 07:51:45 scw Exp $ */
22357939bSOlivier Houchard
32357939bSOlivier Houchard/*
42357939bSOlivier Houchard * Copyright 2003 Wasabi Systems, Inc.
52357939bSOlivier Houchard * All rights reserved.
62357939bSOlivier Houchard *
72357939bSOlivier Houchard * Written by Steve C. Woodford for Wasabi Systems, Inc.
82357939bSOlivier Houchard *
92357939bSOlivier Houchard * Redistribution and use in source and binary forms, with or without
102357939bSOlivier Houchard * modification, are permitted provided that the following conditions
112357939bSOlivier Houchard * are met:
122357939bSOlivier Houchard * 1. Redistributions of source code must retain the above copyright
132357939bSOlivier Houchard *    notice, this list of conditions and the following disclaimer.
142357939bSOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright
152357939bSOlivier Houchard *    notice, this list of conditions and the following disclaimer in the
162357939bSOlivier Houchard *    documentation and/or other materials provided with the distribution.
172357939bSOlivier Houchard * 3. All advertising materials mentioning features or use of this software
182357939bSOlivier Houchard *    must display the following acknowledgement:
192357939bSOlivier Houchard *      This product includes software developed for the NetBSD Project by
202357939bSOlivier Houchard *      Wasabi Systems, Inc.
212357939bSOlivier Houchard * 4. The name of Wasabi Systems, Inc. may not be used to endorse
222357939bSOlivier Houchard *    or promote products derived from this software without specific prior
232357939bSOlivier Houchard *    written permission.
242357939bSOlivier Houchard *
252357939bSOlivier Houchard * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
262357939bSOlivier Houchard * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
272357939bSOlivier Houchard * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
282357939bSOlivier Houchard * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
292357939bSOlivier Houchard * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
302357939bSOlivier Houchard * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
312357939bSOlivier Houchard * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
322357939bSOlivier Houchard * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
332357939bSOlivier Houchard * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
342357939bSOlivier Houchard * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
352357939bSOlivier Houchard * POSSIBILITY OF SUCH DAMAGE.
362357939bSOlivier Houchard */
372357939bSOlivier Houchard/*
382357939bSOlivier Houchard * Copyright (c) 2002 ARM Ltd
392357939bSOlivier Houchard * All rights reserved.
402357939bSOlivier Houchard *
412357939bSOlivier Houchard * Redistribution and use in source and binary forms, with or without
422357939bSOlivier Houchard * modification, are permitted provided that the following conditions
432357939bSOlivier Houchard * are met:
442357939bSOlivier Houchard * 1. Redistributions of source code must retain the above copyright
452357939bSOlivier Houchard *    notice, this list of conditions and the following disclaimer.
462357939bSOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright
472357939bSOlivier Houchard *    notice, this list of conditions and the following disclaimer in the
482357939bSOlivier Houchard *    documentation and/or other materials provided with the distribution.
492357939bSOlivier Houchard * 3. The name of the company may not be used to endorse or promote
502357939bSOlivier Houchard *    products derived from this software without specific prior written
512357939bSOlivier Houchard *    permission.
522357939bSOlivier Houchard *
532357939bSOlivier Houchard * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
542357939bSOlivier Houchard * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
552357939bSOlivier Houchard * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
562357939bSOlivier Houchard * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
572357939bSOlivier Houchard * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
582357939bSOlivier Houchard * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
592357939bSOlivier Houchard * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
602357939bSOlivier Houchard * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
612357939bSOlivier Houchard * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
622357939bSOlivier Houchard * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
632357939bSOlivier Houchard */
642357939bSOlivier Houchard
652357939bSOlivier Houchard#include <machine/asm.h>
66a215cdfdSAndrew Turner.syntax	unified
67a215cdfdSAndrew Turner
682357939bSOlivier HouchardENTRY(memcmp)
692357939bSOlivier Houchard	mov	ip, r0
702357939bSOlivier Houchard#if defined(_KERNEL) && !defined(_STANDALONE)
712357939bSOlivier Houchard	cmp	r2, #0x06
722357939bSOlivier Houchard	beq	.Lmemcmp_6bytes
732357939bSOlivier Houchard#endif
742357939bSOlivier Houchard	mov	r0, #0x00
752357939bSOlivier Houchard
762357939bSOlivier Houchard	/* Are both addresses aligned the same way? */
772357939bSOlivier Houchard	cmp	r2, #0x00
78a215cdfdSAndrew Turner	eorsne	r3, ip, r1
7931489a9aSOlivier Houchard	RETeq			/* len == 0, or same addresses! */
802357939bSOlivier Houchard	tst	r3, #0x03
812357939bSOlivier Houchard	subne	r2, r2, #0x01
822357939bSOlivier Houchard	bne	.Lmemcmp_bytewise2	/* Badly aligned. Do it the slow way */
832357939bSOlivier Houchard
842357939bSOlivier Houchard	/* Word-align the addresses, if necessary */
853e3aced3SOlivier Houchard	sub	r3, r1, #0x05
863e3aced3SOlivier Houchard	ands	r3, r3, #0x03
872357939bSOlivier Houchard	add	r3, r3, r3, lsl #1
882357939bSOlivier Houchard	addne	pc, pc, r3, lsl #3
892357939bSOlivier Houchard	nop
902357939bSOlivier Houchard
912357939bSOlivier Houchard	/* Compare up to 3 bytes */
922357939bSOlivier Houchard	ldrb	r0, [ip], #0x01
932357939bSOlivier Houchard	ldrb	r3, [r1], #0x01
942357939bSOlivier Houchard	subs	r0, r0, r3
9531489a9aSOlivier Houchard	RETne
962357939bSOlivier Houchard	subs	r2, r2, #0x01
9731489a9aSOlivier Houchard	RETeq
982357939bSOlivier Houchard
992357939bSOlivier Houchard	/* Compare up to 2 bytes */
1002357939bSOlivier Houchard	ldrb	r0, [ip], #0x01
1012357939bSOlivier Houchard	ldrb	r3, [r1], #0x01
1022357939bSOlivier Houchard	subs	r0, r0, r3
10331489a9aSOlivier Houchard	RETne
1042357939bSOlivier Houchard	subs	r2, r2, #0x01
10531489a9aSOlivier Houchard	RETeq
1062357939bSOlivier Houchard
1072357939bSOlivier Houchard	/* Compare 1 byte */
1082357939bSOlivier Houchard	ldrb	r0, [ip], #0x01
1092357939bSOlivier Houchard	ldrb	r3, [r1], #0x01
1102357939bSOlivier Houchard	subs	r0, r0, r3
11131489a9aSOlivier Houchard	RETne
1122357939bSOlivier Houchard	subs	r2, r2, #0x01
11331489a9aSOlivier Houchard	RETeq
1142357939bSOlivier Houchard
1152357939bSOlivier Houchard	/* Compare 4 bytes at a time, if possible */
1162357939bSOlivier Houchard	subs	r2, r2, #0x04
1172357939bSOlivier Houchard	bcc	.Lmemcmp_bytewise
1182357939bSOlivier Houchard.Lmemcmp_word_aligned:
1192357939bSOlivier Houchard	ldr	r0, [ip], #0x04
1202357939bSOlivier Houchard	ldr	r3, [r1], #0x04
1212357939bSOlivier Houchard	subs	r2, r2, #0x04
1222357939bSOlivier Houchard	cmpcs	r0, r3
1232357939bSOlivier Houchard	beq	.Lmemcmp_word_aligned
1242357939bSOlivier Houchard	sub	r0, r0, r3
1252357939bSOlivier Houchard
1262357939bSOlivier Houchard	/* Correct for extra subtraction, and check if done */
1272357939bSOlivier Houchard	adds	r2, r2, #0x04
1282357939bSOlivier Houchard	cmpeq	r0, #0x00		/* If done, did all bytes match? */
12931489a9aSOlivier Houchard	RETeq			/* Yup. Just return */
1302357939bSOlivier Houchard
1312357939bSOlivier Houchard	/* Re-do the final word byte-wise */
1322357939bSOlivier Houchard	sub	ip, ip, #0x04
1332357939bSOlivier Houchard	sub	r1, r1, #0x04
1342357939bSOlivier Houchard
1352357939bSOlivier Houchard.Lmemcmp_bytewise:
1362357939bSOlivier Houchard	add	r2, r2, #0x03
1372357939bSOlivier Houchard.Lmemcmp_bytewise2:
1382357939bSOlivier Houchard	ldrb	r0, [ip], #0x01
1392357939bSOlivier Houchard	ldrb	r3, [r1], #0x01
1402357939bSOlivier Houchard	subs	r2, r2, #0x01
1412357939bSOlivier Houchard	cmpcs	r0, r3
1422357939bSOlivier Houchard	beq	.Lmemcmp_bytewise2
1432357939bSOlivier Houchard	sub	r0, r0, r3
14431489a9aSOlivier Houchard	RET
1452357939bSOlivier Houchard
1462357939bSOlivier Houchard#if defined(_KERNEL) && !defined(_STANDALONE)
1472357939bSOlivier Houchard	/*
1482357939bSOlivier Houchard	 * 6 byte compares are very common, thanks to the network stack.
1492357939bSOlivier Houchard	 * This code is hand-scheduled to reduce the number of stalls for
1502357939bSOlivier Houchard	 * load results. Everything else being equal, this will be ~32%
1512357939bSOlivier Houchard	 * faster than a byte-wise memcmp.
1522357939bSOlivier Houchard	 */
1532357939bSOlivier Houchard	.align	5
1542357939bSOlivier Houchard.Lmemcmp_6bytes:
1552357939bSOlivier Houchard	ldrb	r3, [r1, #0x00]		/* r3 = b2#0 */
1562357939bSOlivier Houchard	ldrb	r0, [ip, #0x00]		/* r0 = b1#0 */
1572357939bSOlivier Houchard	ldrb	r2, [r1, #0x01]		/* r2 = b2#1 */
1582357939bSOlivier Houchard	subs	r0, r0, r3		/* r0 = b1#0 - b2#0 */
1592357939bSOlivier Houchard	ldreqb	r3, [ip, #0x01]		/* r3 = b1#1 */
16031489a9aSOlivier Houchard	RETne			/* Return if mismatch on #0 */
1612357939bSOlivier Houchard	subs	r0, r3, r2		/* r0 = b1#1 - b2#1 */
1622357939bSOlivier Houchard	ldreqb	r3, [r1, #0x02]		/* r3 = b2#2 */
1632357939bSOlivier Houchard	ldreqb	r0, [ip, #0x02]		/* r0 = b1#2 */
16431489a9aSOlivier Houchard	RETne			/* Return if mismatch on #1 */
1652357939bSOlivier Houchard	ldrb	r2, [r1, #0x03]		/* r2 = b2#3 */
1662357939bSOlivier Houchard	subs	r0, r0, r3		/* r0 = b1#2 - b2#2 */
1672357939bSOlivier Houchard	ldreqb	r3, [ip, #0x03]		/* r3 = b1#3 */
16831489a9aSOlivier Houchard	RETne			/* Return if mismatch on #2 */
1692357939bSOlivier Houchard	subs	r0, r3, r2		/* r0 = b1#3 - b2#3 */
1702357939bSOlivier Houchard	ldreqb	r3, [r1, #0x04]		/* r3 = b2#4 */
1712357939bSOlivier Houchard	ldreqb	r0, [ip, #0x04]		/* r0 = b1#4 */
17231489a9aSOlivier Houchard	RETne			/* Return if mismatch on #3 */
1732357939bSOlivier Houchard	ldrb	r2, [r1, #0x05]		/* r2 = b2#5 */
1742357939bSOlivier Houchard	subs	r0, r0, r3		/* r0 = b1#4 - b2#4 */
1752357939bSOlivier Houchard	ldreqb	r3, [ip, #0x05]		/* r3 = b1#5 */
17631489a9aSOlivier Houchard	RETne			/* Return if mismatch on #4 */
1772357939bSOlivier Houchard	sub	r0, r3, r2		/* r0 = b1#5 - b2#5 */
17831489a9aSOlivier Houchard	RET
1792357939bSOlivier Houchard#endif
180f2e71517SIan LeporeEND(memcmp)
181*96cdb0abSKonstantin Belousov
182*96cdb0abSKonstantin Belousov	.section .note.GNU-stack,"",%progbits
183