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