strrchr_arm.S revision 1.2
11.1Smatt/*- 21.1Smatt * Copyright (c) 2013 The NetBSD Foundation, Inc. 31.1Smatt * All rights reserved. 41.1Smatt * 51.1Smatt * This code is derived from software contributed to The NetBSD Foundation 61.1Smatt * by Matt Thomas of 3am Software Foundry. 71.1Smatt * 81.1Smatt * Redistribution and use in source and binary forms, with or without 91.1Smatt * modification, are permitted provided that the following conditions 101.1Smatt * are met: 111.1Smatt * 1. Redistributions of source code must retain the above copyright 121.1Smatt * notice, this list of conditions and the following disclaimer. 131.1Smatt * 2. Redistributions in binary form must reproduce the above copyright 141.1Smatt * notice, this list of conditions and the following disclaimer in the 151.1Smatt * documentation and/or other materials provided with the distribution. 161.1Smatt * 171.1Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 181.1Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 191.1Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 201.1Smatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 211.1Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 221.1Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 231.1Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 241.1Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 251.1Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 261.1Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 271.1Smatt * POSSIBILITY OF SUCH DAMAGE. 281.1Smatt */ 291.1Smatt 301.1Smatt#include <machine/asm.h> 311.1Smatt 321.2SmattRCSID("$NetBSD: strrchr_arm.S,v 1.2 2013/01/28 06:23:14 matt Exp $") 331.1Smatt 341.1Smatt#ifdef __ARMEL__ 351.1Smatt#define BYTE0 0x000000ff 361.1Smatt#define BYTE1 0x0000ff00 371.1Smatt#define BYTE2 0x00ff0000 381.1Smatt#define BYTE3 0xff000000 391.1Smatt#define lshi lsl 401.1Smatt#else 411.1Smatt#define BYTE0 0xff000000 421.1Smatt#define BYTE1 0x00ff0000 431.1Smatt#define BYTE2 0x0000ff00 441.1Smatt#define BYTE3 0x000000ff 451.1Smatt#define lshi lsr 461.1Smatt#endif 471.1Smatt 481.1Smatt .text 491.1SmattENTRY(strrchr) 501.1Smatt mov ip, r0 /* we use r0 at the return value */ 511.1Smatt mov r0, #0 /* return NULL by default */ 521.1Smatt and r2, r1, #0xff /* restrict to byte value */ 531.1Smatt1: tst ip, #3 /* test for word alignment */ 541.1Smatt beq .Lpre_main_loop /* finally word aligned */ 551.1Smatt ldrb r3, [ip], #1 /* load a byte */ 561.1Smatt cmp r3, r2 /* did it match? */ 571.1Smatt subeq r0, ip, #1 /* yes, remember that it did */ 581.1Smatt teq r3, #0 /* was it NUL? */ 591.1Smatt bne 1b /* no, try next byte */ 601.1Smatt RET /* return */ 611.1Smatt.Lpre_main_loop: 621.1Smatt push {r4, r5} /* save some registers */ 631.1Smatt#if defined(_ARM_ARCH_7) 641.1Smatt movw r1, #0xfefe /* magic constant; 254 in each byte */ 651.1Smatt movt r1, #0xfefe /* magic constant; 254 in each byte */ 661.1Smatt#elif defined(_ARM_ARCH_6) 671.1Smatt mov r1, #0xfe /* put 254 in low byte */ 681.1Smatt orr r1, r1, r1, lsl #8 /* move to next byte */ 691.1Smatt orr r1, r1, r1, lsl #16 /* move to next halfword */ 701.1Smatt#endif /* _ARM_ARCH_6 */ 711.1Smatt orr r2, r2, r2, lsl #8 /* move to next byte */ 721.1Smatt orr r2, r2, r2, lsl #16 /* move to next halfword */ 731.1Smatt.Lmain_loop: 741.1Smatt ldr r3, [ip], #4 /* load next word */ 751.1Smatt#if defined(_ARM_ARCH_6) 761.1Smatt /* 771.1Smatt * Add 254 to each byte using the UQADD8 (unsigned saturating add 8) 781.1Smatt * instruction. For every non-NUL byte, the result for that byte will 791.1Smatt * become 255. For NUL, it will be 254. When we complement the 801.1Smatt * result, if the result is non-0 then we must have encountered a NUL. 811.1Smatt */ 821.1Smatt uqadd8 r4, r3, r1 /* NUL detection happens here */ 831.1Smatt usub8 r3, r3, r2 /* bias for char looked for? */ 841.1Smatt uqadd8 r5, r3, r1 /* char detection happens here */ 851.1Smatt and r3, r4, r5 /* merge results */ 861.1Smatt mvns r3, r3 /* is the complement non-0? */ 871.1Smatt beq .Lmain_loop /* no, then keep going */ 881.1Smatt 891.1Smatt mvns r5, r5 /* get we find any matching bytes? */ 901.1Smatt beq .Ldone /* no, then we hit the end, return */ 911.1Smatt mvns r4, r4 /* did we encounter a NUL? */ 921.1Smatt beq .Lfind_match /* no, find matching byte */ 931.1Smatt /* 941.1Smatt * Copy the NUL bit to the following byte lanes. Then clear any match 951.1Smatt * bits in those byte lanes to prevent false positives in those bytes. 961.1Smatt */ 971.2Smatt bics r5, r5, r4 /* clear any NUL match bits */ 981.2Smatt beq .Ldone /* no remaining matches, we're done */ 991.1Smatt movs r3, r4, lshi #8 /* shift up a byte */ 1001.1Smatt orrnes r3, r3, r3, lshi #8 /* if non 0, copy up to next byte */ 1011.1Smatt orrnes r3, r3, r3, lshi #8 /* if non 0, copy up to last byte */ 1021.1Smatt bics r5, r5, r3 /* clear match bits */ 1031.1Smatt beq .Ldone /* no remaining matches, we're done */ 1041.1Smatt.Lfind_match: 1051.1Smatt#ifdef __ARMEL__ 1061.1Smatt rev r5, r5 /* we want this in BE for the CLZ */ 1071.1Smatt#endif 1081.1Smatt /* 1091.1Smatt * If we have multiple matches, we want to the select the "last" match 1101.1Smatt * in the word which will be the lowest bit set. 1111.1Smatt */ 1121.1Smatt sub r3, r5, #1 /* subtract 1 */ 1131.1Smatt and r3, r3, r5 /* and with mask */ 1141.1Smatt eor r5, r5, r3 /* only have the lowest bit set left */ 1151.1Smatt clz r5, r5 /* count how many leading zeros */ 1161.1Smatt add r0, ip, r5, lsr #3 /* divide that by 8 and add to count */ 1171.1Smatt sub r0, r0, #4 /* compensate for the post-inc */ 1181.1Smatt teq r4, #0 /* did we read any NULs? */ 1191.1Smatt beq .Lmain_loop /* no, get next word */ 1201.1Smatt#else 1211.1Smatt /* 1221.1Smatt * No fancy shortcuts so just test each byte lane for a NUL. 1231.1Smatt * (other tests for NULs in a word take more instructions/cycles). 1241.1Smatt */ 1251.1Smatt eor r4, r3, r2 /* xor .. */ 1261.1Smatt tst r3, #BYTE0 /* is byte 0 a NUL? */ 1271.1Smatt beq .Ldone /* yes, then we're done */ 1281.1Smatt tst r4, #BYTE0 /* is byte 0 a match? */ 1291.1Smatt subeq r0, ip, #4 /* yes, remember its location */ 1301.1Smatt tst r3, #BYTE1 /* is byte 1 a NUL? */ 1311.1Smatt beq .Ldone /* yes, then we're done */ 1321.1Smatt tst r4, #BYTE1 /* is byte 1 a match? */ 1331.1Smatt subeq r0, ip, #3 /* yes, remember its location */ 1341.1Smatt tst r3, #BYTE2 /* is byte 2 a NUL? */ 1351.1Smatt beq .Ldone /* yes, then we're done */ 1361.1Smatt tst r4, #BYTE2 /* is byte 2 a match? */ 1371.1Smatt subeq r0, ip, #2 /* yes, remember its location */ 1381.1Smatt tst r3, #BYTE3 /* is byte 3 a NUL? */ 1391.1Smatt beq .Ldone /* yes, then we're done */ 1401.1Smatt tst r4, #BYTE3 /* is byte 3 a match? */ 1411.1Smatt subeq r0, ip, #1 /* yes, remember its location */ 1421.1Smatt b .Lmain_loop 1431.1Smatt#endif /* _ARM_ARCH_6 */ 1441.1Smatt.Ldone: 1451.1Smatt pop {r4, r5} 1461.1Smatt RET 1471.1SmattEND(strrchr) 148