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> 662357939bSOlivier Houchard 672357939bSOlivier Houchard__FBSDID("$FreeBSD$"); 682357939bSOlivier Houchard 692357939bSOlivier HouchardENTRY(memcmp) 702357939bSOlivier Houchard mov ip, r0 712357939bSOlivier Houchard#if defined(_KERNEL) && !defined(_STANDALONE) 722357939bSOlivier Houchard cmp r2, #0x06 732357939bSOlivier Houchard beq .Lmemcmp_6bytes 742357939bSOlivier Houchard#endif 752357939bSOlivier Houchard mov r0, #0x00 762357939bSOlivier Houchard 772357939bSOlivier Houchard /* Are both addresses aligned the same way? */ 782357939bSOlivier Houchard cmp r2, #0x00 792357939bSOlivier Houchard eornes r3, ip, r1 8031489a9aSOlivier Houchard RETeq /* len == 0, or same addresses! */ 812357939bSOlivier Houchard tst r3, #0x03 822357939bSOlivier Houchard subne r2, r2, #0x01 832357939bSOlivier Houchard bne .Lmemcmp_bytewise2 /* Badly aligned. Do it the slow way */ 842357939bSOlivier Houchard 852357939bSOlivier Houchard /* Word-align the addresses, if necessary */ 863e3aced3SOlivier Houchard sub r3, r1, #0x05 873e3aced3SOlivier Houchard ands r3, r3, #0x03 882357939bSOlivier Houchard add r3, r3, r3, lsl #1 892357939bSOlivier Houchard addne pc, pc, r3, lsl #3 902357939bSOlivier Houchard nop 912357939bSOlivier Houchard 922357939bSOlivier Houchard /* Compare up to 3 bytes */ 932357939bSOlivier Houchard ldrb r0, [ip], #0x01 942357939bSOlivier Houchard ldrb r3, [r1], #0x01 952357939bSOlivier Houchard subs r0, r0, r3 9631489a9aSOlivier Houchard RETne 972357939bSOlivier Houchard subs r2, r2, #0x01 9831489a9aSOlivier Houchard RETeq 992357939bSOlivier Houchard 1002357939bSOlivier Houchard /* Compare up to 2 bytes */ 1012357939bSOlivier Houchard ldrb r0, [ip], #0x01 1022357939bSOlivier Houchard ldrb r3, [r1], #0x01 1032357939bSOlivier Houchard subs r0, r0, r3 10431489a9aSOlivier Houchard RETne 1052357939bSOlivier Houchard subs r2, r2, #0x01 10631489a9aSOlivier Houchard RETeq 1072357939bSOlivier Houchard 1082357939bSOlivier Houchard /* Compare 1 byte */ 1092357939bSOlivier Houchard ldrb r0, [ip], #0x01 1102357939bSOlivier Houchard ldrb r3, [r1], #0x01 1112357939bSOlivier Houchard subs r0, r0, r3 11231489a9aSOlivier Houchard RETne 1132357939bSOlivier Houchard subs r2, r2, #0x01 11431489a9aSOlivier Houchard RETeq 1152357939bSOlivier Houchard 1162357939bSOlivier Houchard /* Compare 4 bytes at a time, if possible */ 1172357939bSOlivier Houchard subs r2, r2, #0x04 1182357939bSOlivier Houchard bcc .Lmemcmp_bytewise 1192357939bSOlivier Houchard.Lmemcmp_word_aligned: 1202357939bSOlivier Houchard ldr r0, [ip], #0x04 1212357939bSOlivier Houchard ldr r3, [r1], #0x04 1222357939bSOlivier Houchard subs r2, r2, #0x04 1232357939bSOlivier Houchard cmpcs r0, r3 1242357939bSOlivier Houchard beq .Lmemcmp_word_aligned 1252357939bSOlivier Houchard sub r0, r0, r3 1262357939bSOlivier Houchard 1272357939bSOlivier Houchard /* Correct for extra subtraction, and check if done */ 1282357939bSOlivier Houchard adds r2, r2, #0x04 1292357939bSOlivier Houchard cmpeq r0, #0x00 /* If done, did all bytes match? */ 13031489a9aSOlivier Houchard RETeq /* Yup. Just return */ 1312357939bSOlivier Houchard 1322357939bSOlivier Houchard /* Re-do the final word byte-wise */ 1332357939bSOlivier Houchard sub ip, ip, #0x04 1342357939bSOlivier Houchard sub r1, r1, #0x04 1352357939bSOlivier Houchard 1362357939bSOlivier Houchard.Lmemcmp_bytewise: 1372357939bSOlivier Houchard add r2, r2, #0x03 1382357939bSOlivier Houchard.Lmemcmp_bytewise2: 1392357939bSOlivier Houchard ldrb r0, [ip], #0x01 1402357939bSOlivier Houchard ldrb r3, [r1], #0x01 1412357939bSOlivier Houchard subs r2, r2, #0x01 1422357939bSOlivier Houchard cmpcs r0, r3 1432357939bSOlivier Houchard beq .Lmemcmp_bytewise2 1442357939bSOlivier Houchard sub r0, r0, r3 14531489a9aSOlivier Houchard RET 1462357939bSOlivier Houchard 1472357939bSOlivier Houchard#if defined(_KERNEL) && !defined(_STANDALONE) 1482357939bSOlivier Houchard /* 1492357939bSOlivier Houchard * 6 byte compares are very common, thanks to the network stack. 1502357939bSOlivier Houchard * This code is hand-scheduled to reduce the number of stalls for 1512357939bSOlivier Houchard * load results. Everything else being equal, this will be ~32% 1522357939bSOlivier Houchard * faster than a byte-wise memcmp. 1532357939bSOlivier Houchard */ 1542357939bSOlivier Houchard .align 5 1552357939bSOlivier Houchard.Lmemcmp_6bytes: 1562357939bSOlivier Houchard ldrb r3, [r1, #0x00] /* r3 = b2#0 */ 1572357939bSOlivier Houchard ldrb r0, [ip, #0x00] /* r0 = b1#0 */ 1582357939bSOlivier Houchard ldrb r2, [r1, #0x01] /* r2 = b2#1 */ 1592357939bSOlivier Houchard subs r0, r0, r3 /* r0 = b1#0 - b2#0 */ 1602357939bSOlivier Houchard ldreqb r3, [ip, #0x01] /* r3 = b1#1 */ 16131489a9aSOlivier Houchard RETne /* Return if mismatch on #0 */ 1622357939bSOlivier Houchard subs r0, r3, r2 /* r0 = b1#1 - b2#1 */ 1632357939bSOlivier Houchard ldreqb r3, [r1, #0x02] /* r3 = b2#2 */ 1642357939bSOlivier Houchard ldreqb r0, [ip, #0x02] /* r0 = b1#2 */ 16531489a9aSOlivier Houchard RETne /* Return if mismatch on #1 */ 1662357939bSOlivier Houchard ldrb r2, [r1, #0x03] /* r2 = b2#3 */ 1672357939bSOlivier Houchard subs r0, r0, r3 /* r0 = b1#2 - b2#2 */ 1682357939bSOlivier Houchard ldreqb r3, [ip, #0x03] /* r3 = b1#3 */ 16931489a9aSOlivier Houchard RETne /* Return if mismatch on #2 */ 1702357939bSOlivier Houchard subs r0, r3, r2 /* r0 = b1#3 - b2#3 */ 1712357939bSOlivier Houchard ldreqb r3, [r1, #0x04] /* r3 = b2#4 */ 1722357939bSOlivier Houchard ldreqb r0, [ip, #0x04] /* r0 = b1#4 */ 17331489a9aSOlivier Houchard RETne /* Return if mismatch on #3 */ 1742357939bSOlivier Houchard ldrb r2, [r1, #0x05] /* r2 = b2#5 */ 1752357939bSOlivier Houchard subs r0, r0, r3 /* r0 = b1#4 - b2#4 */ 1762357939bSOlivier Houchard ldreqb r3, [ip, #0x05] /* r3 = b1#5 */ 17731489a9aSOlivier Houchard RETne /* Return if mismatch on #4 */ 1782357939bSOlivier Houchard sub r0, r3, r2 /* r0 = b1#5 - b2#5 */ 17931489a9aSOlivier Houchard RET 1802357939bSOlivier Houchard#endif 181