/* $NetBSD: memcmp.S,v 1.3 2003/10/14 07:51:45 scw Exp $ */12/*3* Copyright 2003 Wasabi Systems, Inc.4* All rights reserved.5*6* Written by Steve C. Woodford for Wasabi Systems, Inc.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. All advertising materials mentioning features or use of this software17* must display the following acknowledgement:18* This product includes software developed for the NetBSD Project by19* Wasabi Systems, Inc.20* 4. The name of Wasabi Systems, Inc. may not be used to endorse21* or promote products derived from this software without specific prior22* written permission.23*24* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND25* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED26* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR27* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC28* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR29* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF30* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN32* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)33* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE34* POSSIBILITY OF SUCH DAMAGE.35*/36/*37* Copyright (c) 2002 ARM Ltd38* All rights reserved.39*40* Redistribution and use in source and binary forms, with or without41* modification, are permitted provided that the following conditions42* are met:43* 1. Redistributions of source code must retain the above copyright44* notice, this list of conditions and the following disclaimer.45* 2. Redistributions in binary form must reproduce the above copyright46* notice, this list of conditions and the following disclaimer in the47* documentation and/or other materials provided with the distribution.48* 3. The name of the company may not be used to endorse or promote49* products derived from this software without specific prior written50* permission.51*52* THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED53* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF54* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.55* IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,56* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED57* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR58* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF59* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING60* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS61* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.62*/6364#include <machine/asm.h>65.syntax unified6667ENTRY(memcmp)68mov ip, r069#if defined(_KERNEL) && !defined(_STANDALONE)70cmp r2, #0x0671beq .Lmemcmp_6bytes72#endif73mov r0, #0x007475/* Are both addresses aligned the same way? */76cmp r2, #0x0077eorsne r3, ip, r178RETeq /* len == 0, or same addresses! */79tst r3, #0x0380subne r2, r2, #0x0181bne .Lmemcmp_bytewise2 /* Badly aligned. Do it the slow way */8283/* Word-align the addresses, if necessary */84sub r3, r1, #0x0585ands r3, r3, #0x0386add r3, r3, r3, lsl #187addne pc, pc, r3, lsl #388nop8990/* Compare up to 3 bytes */91ldrb r0, [ip], #0x0192ldrb r3, [r1], #0x0193subs r0, r0, r394RETne95subs r2, r2, #0x0196RETeq9798/* Compare up to 2 bytes */99ldrb r0, [ip], #0x01100ldrb r3, [r1], #0x01101subs r0, r0, r3102RETne103subs r2, r2, #0x01104RETeq105106/* Compare 1 byte */107ldrb r0, [ip], #0x01108ldrb r3, [r1], #0x01109subs r0, r0, r3110RETne111subs r2, r2, #0x01112RETeq113114/* Compare 4 bytes at a time, if possible */115subs r2, r2, #0x04116bcc .Lmemcmp_bytewise117.Lmemcmp_word_aligned:118ldr r0, [ip], #0x04119ldr r3, [r1], #0x04120subs r2, r2, #0x04121cmpcs r0, r3122beq .Lmemcmp_word_aligned123sub r0, r0, r3124125/* Correct for extra subtraction, and check if done */126adds r2, r2, #0x04127cmpeq r0, #0x00 /* If done, did all bytes match? */128RETeq /* Yup. Just return */129130/* Re-do the final word byte-wise */131sub ip, ip, #0x04132sub r1, r1, #0x04133134.Lmemcmp_bytewise:135add r2, r2, #0x03136.Lmemcmp_bytewise2:137ldrb r0, [ip], #0x01138ldrb r3, [r1], #0x01139subs r2, r2, #0x01140cmpcs r0, r3141beq .Lmemcmp_bytewise2142sub r0, r0, r3143RET144145#if defined(_KERNEL) && !defined(_STANDALONE)146/*147* 6 byte compares are very common, thanks to the network stack.148* This code is hand-scheduled to reduce the number of stalls for149* load results. Everything else being equal, this will be ~32%150* faster than a byte-wise memcmp.151*/152.align 5153.Lmemcmp_6bytes:154ldrb r3, [r1, #0x00] /* r3 = b2#0 */155ldrb r0, [ip, #0x00] /* r0 = b1#0 */156ldrb r2, [r1, #0x01] /* r2 = b2#1 */157subs r0, r0, r3 /* r0 = b1#0 - b2#0 */158ldreqb r3, [ip, #0x01] /* r3 = b1#1 */159RETne /* Return if mismatch on #0 */160subs r0, r3, r2 /* r0 = b1#1 - b2#1 */161ldreqb r3, [r1, #0x02] /* r3 = b2#2 */162ldreqb r0, [ip, #0x02] /* r0 = b1#2 */163RETne /* Return if mismatch on #1 */164ldrb r2, [r1, #0x03] /* r2 = b2#3 */165subs r0, r0, r3 /* r0 = b1#2 - b2#2 */166ldreqb r3, [ip, #0x03] /* r3 = b1#3 */167RETne /* Return if mismatch on #2 */168subs r0, r3, r2 /* r0 = b1#3 - b2#3 */169ldreqb r3, [r1, #0x04] /* r3 = b2#4 */170ldreqb r0, [ip, #0x04] /* r0 = b1#4 */171RETne /* Return if mismatch on #3 */172ldrb r2, [r1, #0x05] /* r2 = b2#5 */173subs r0, r0, r3 /* r0 = b1#4 - b2#4 */174ldreqb r3, [ip, #0x05] /* r3 = b1#5 */175RETne /* Return if mismatch on #4 */176sub r0, r3, r2 /* r0 = b1#5 - b2#5 */177RET178#endif179END(memcmp)180181.section .note.GNU-stack,"",%progbits182183184