Home | History | Annotate | Line # | Download | only in string
memcmp.S revision 1.1
      1  1.1  matt /* $NetBSD: memcmp.S,v 1.1 2014/08/10 05:47:35 matt Exp $ */
      2  1.1  matt 
      3  1.1  matt /*-
      4  1.1  matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  matt  * All rights reserved.
      6  1.1  matt  *
      7  1.1  matt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  matt  * by Matt Thomas of 3am Software Foundry.
      9  1.1  matt  *
     10  1.1  matt  * Redistribution and use in source and binary forms, with or without
     11  1.1  matt  * modification, are permitted provided that the following conditions
     12  1.1  matt  * are met:
     13  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  matt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  matt  *    documentation and/or other materials provided with the distribution.
     18  1.1  matt  *
     19  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  matt  */
     31  1.1  matt 
     32  1.1  matt #include <machine/asm.h>
     33  1.1  matt 
     34  1.1  matt RCSID("$NetBSD: memcmp.S,v 1.1 2014/08/10 05:47:35 matt Exp $")
     35  1.1  matt 
     36  1.1  matt ENTRY(memcmp)
     37  1.1  matt 	mov	x9, x0
     38  1.1  matt 	mov	x10, x1
     39  1.1  matt 	mov	x0, xzr
     40  1.1  matt 	cbz	x2, .Lmemcmp_ret
     41  1.1  matt #ifdef _KERNEL
     42  1.1  matt 	cmp	x2, #6
     43  1.1  matt 	b.eq	.Lmemcmp_6bytes
     44  1.1  matt #endif
     45  1.1  matt 	cmp	x2, #7
     46  1.1  matt 	b.ls	.Lmemcmp_lessthan8
     47  1.1  matt 
     48  1.1  matt 	ands	x3, x9, #7
     49  1.1  matt 	b.eq	.Lmemcmp_dword_loop
     50  1.1  matt 
     51  1.1  matt /*
     52  1.1  matt  * The two addresses have identical alignment but are not yet dword aligned.
     53  1.1  matt  */
     54  1.1  matt 	add	x2, x2, x3		/* add unalignment to length */
     55  1.1  matt 	sub	x2, x2, #8		/* now subtract a dword */
     56  1.1  matt 
     57  1.1  matt 	sub	x9, x9, x3		/* dword align src1 */
     58  1.1  matt 	sub	x10, x10, x3		/* adjust src2 */
     59  1.1  matt 
     60  1.1  matt 	lsl	x3, x3, #3		/* convert bytes to bits */
     61  1.1  matt 	ldr	x4, [x9], #8		/* load dword from src1 */
     62  1.1  matt 	ldr	x6, [x10], #8		/* load dword from src2 */
     63  1.1  matt #ifdef __AARCH64EB__
     64  1.1  matt 	lsl	x4, x4, x3		/* discard leading bytes from data1 */
     65  1.1  matt 	lsl	x6, x6, x3		/* discard leading bytes from data2 */
     66  1.1  matt #else
     67  1.1  matt 	lsr	x4, x4, x3		/* discard leading bytes from data1 */
     68  1.1  matt 	lsr	x6, x6, x3		/* discard leading bytes from data2 */
     69  1.1  matt #endif
     70  1.1  matt 	subs	x0, x4, x6		/* compare data */
     71  1.1  matt #ifdef __AARCH64EL__
     72  1.1  matt 	b.ne	.Lmemcmp_last_compare	/* difference.  find it */
     73  1.1  matt #else
     74  1.1  matt 	b.eq	.Lmemcmp_dword_loop	/* no difference.  go to loop */
     75  1.1  matt 	rev	x4, x4			/* byte swap data1 */
     76  1.1  matt 	rev	x6, x6			/* byte swap data2 */
     77  1.1  matt 	b	.Lmemcmp_last_compare	/* go find the difference. */
     78  1.1  matt #endif
     79  1.1  matt 
     80  1.1  matt .Lmemcmp_dword_loop:
     81  1.1  matt 	subs	x2, x2, #8
     82  1.1  matt 	b.mi	.Lmemcmp_finish_dword
     83  1.1  matt 	ldr	x4, [x9], #8
     84  1.1  matt 	ldr	x6, [x10], #8
     85  1.1  matt 	subs	x0, x4, x6
     86  1.1  matt 	b.eq	.Lmemcmp_dword_loop	/* no difference.  go to loop */
     87  1.1  matt #ifdef __AARCH64EB__
     88  1.1  matt 	rev	x4, x4			/* byte swap data1 */
     89  1.1  matt 	rev	x6, x6			/* byte swap data2 */
     90  1.1  matt #endif
     91  1.1  matt 	b	.Lmemcmp_last_compare	/* go find the difference. */
     92  1.1  matt 
     93  1.1  matt .Lmemcmp_finish_dword:
     94  1.1  matt 	/*
     95  1.1  matt 	 * we might have gotten here with nothing left.  If so, just bail.
     96  1.1  matt 	 */
     97  1.1  matt 	tst	x2, #7
     98  1.1  matt 	b.eq	.Lmemcmp_ret
     99  1.1  matt 	/*
    100  1.1  matt 	 *
    101  1.1  matt 	 */
    102  1.1  matt 	tbz	x2, #2, .Lmemcmp_finish_word
    103  1.1  matt 	ldr	w4, [x9], #4
    104  1.1  matt 	ldr	w6, [x10], #4
    105  1.1  matt #ifdef __AARCH64EB__
    106  1.1  matt 	lsl	x4, x4, #32		/* move to MSW */
    107  1.1  matt 	lsl	x6, x6, #32		/* move to MSW */
    108  1.1  matt #endif
    109  1.1  matt 
    110  1.1  matt .Lmemcmp_finish_word:
    111  1.1  matt 	tbz	x2, #1, .Lmemcmp_finish_hword
    112  1.1  matt 	ldrh	w5, [x9], #2
    113  1.1  matt 	ldrh	w7, [x10], #2
    114  1.1  matt #ifdef __AARCH64EB__
    115  1.1  matt 	orr	x4, x4, x5, lsl #16
    116  1.1  matt 	orr	x6, x6, x7, lsl #16
    117  1.1  matt #else
    118  1.1  matt 	orr	x4, x4, x5, lsl #32
    119  1.1  matt 	orr	x6, x6, x7, lsl #32
    120  1.1  matt #endif
    121  1.1  matt 
    122  1.1  matt .Lmemcmp_finish_hword:
    123  1.1  matt #ifdef __AARCH64EB__
    124  1.1  matt 	rev	x4, x4			/* byte swap data1 */
    125  1.1  matt 	rev	x6, x6			/* byte swap data1 */
    126  1.1  matt #endif
    127  1.1  matt 	tbz	x2, #0, .Lmemcmp_last_compare
    128  1.1  matt 	ldrb	w5, [x9]
    129  1.1  matt 	ldrb	w7, [x10]
    130  1.1  matt 	orr	x4, x4, x5, lsl #48
    131  1.1  matt 	orr	x6, x6, x7, lsl #48
    132  1.1  matt 	b	.Lmemcmp_last_compare	/* go find the difference. */
    133  1.1  matt 
    134  1.1  matt /*
    135  1.1  matt  * D
    136  1.1  matt  */
    137  1.1  matt .Lmemcmp_lessthan8:
    138  1.1  matt 	sub	x2, x2, #1
    139  1.1  matt 1:	ldrb	w4, [x9], #1
    140  1.1  matt 	ldrb	w5, [x10], #1
    141  1.1  matt 	subs	x2, x2, #1
    142  1.1  matt 	ccmp	x4, x5, #0, cs
    143  1.1  matt 	b.eq	1b
    144  1.1  matt 	sub	x0, x4, x5
    145  1.1  matt 
    146  1.1  matt .Lmemcmp_ret:
    147  1.1  matt 	ret
    148  1.1  matt 
    149  1.1  matt #ifdef _KERNEL
    150  1.1  matt .Lmemcmp_6bytes:
    151  1.1  matt 	ldr	w4, [x9], #4
    152  1.1  matt 	ldrh	w5, [x9]
    153  1.1  matt #if __AARCH64EB__
    154  1.1  matt 	orr	x4, x4, x5, lsl #48
    155  1.1  matt 	rev	x4, x4
    156  1.1  matt #else
    157  1.1  matt 	orr	x4, x4, x5, lsl #32
    158  1.1  matt #endif
    159  1.1  matt 	ldr	w6, [x10], #4
    160  1.1  matt 	ldrh	w7, [x10]
    161  1.1  matt #if __AARCH64EB__
    162  1.1  matt 	orr	x6, x6, x7, lsl #48
    163  1.1  matt 	rev	x6, x6
    164  1.1  matt #else
    165  1.1  matt 	orr	x6, x6, x7, lsl #32
    166  1.1  matt #endif
    167  1.1  matt #endif /* _KERNEL */
    168  1.1  matt 
    169  1.1  matt /*
    170  1.1  matt  * We have loaded the final bytes in x4 and x6 in LE format.  Now we have
    171  1.1  matt  * to figure what the difference is (if any).  First we subtract.  Any bytes
    172  1.1  matt  * that are the same will be 0. So to find the first non-zero byte we byterev
    173  1.1  matt  * and then use clz to find that byte.
    174  1.1  matt  * We mask the location to get the start of the byte.  We shift both
    175  1.1  matt  * data dwords left to remove the equal part.  Then we shift right to discard
    176  1.1  matt  * the trailing bytes.  Then we subtract and return.
    177  1.1  matt  */
    178  1.1  matt 	subs	x0, x4, x6
    179  1.1  matt 	b.eq	.Lmemcmp_ret
    180  1.1  matt .Lmemcmp_last_compare:
    181  1.1  matt 	rev	x1, x0		/* byte reverse */
    182  1.1  matt 	clz	x1, x1		/* find first non-zero byte */
    183  1.1  matt 	bfi	x1, xzr, #0, #3	/* make it byte aligned */
    184  1.1  matt 	lsr	x0, x0, x1	/* shift to LSB */
    185  1.1  matt 	sxtb	w0, w0		/* sign extend */
    186  1.1  matt 	ret
    187  1.1  matt END(memcmp)
    188