1/*- 2 * Copyright (c) 2013 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Matt Thomas of 3am Software Foundry. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <machine/asm.h> 31 32RCSID("$NetBSD: strchr_arm.S,v 1.3 2013/01/26 07:49:11 matt Exp $") 33 34#ifdef __ARMEL__ 35#define BYTE0 0x000000ff 36#define BYTE1 0x0000ff00 37#define BYTE2 0x00ff0000 38#define BYTE3 0xff000000 39#define lshi lsl 40#else 41#define BYTE0 0xff000000 42#define BYTE1 0x00ff0000 43#define BYTE2 0x0000ff00 44#define BYTE3 0x000000ff 45#define lshi lsr 46#endif 47 48 .text 49ENTRY(strchr) 50 and r2, r1, #0xff /* restrict to byte value */ 511: tst r0, #3 /* test for word alignment */ 52 beq .Lpre_main_loop /* finally word aligned */ 53 ldrb r3, [r0], #1 /* load a byte */ 54 cmp r3, r2 /* is it a match? */ 55 beq 2f /* yes, return current ptr - 1 */ 56 teq r3, #0 /* no, was it 0? */ 57 bne 1b /* no, try next byte */ 58 mov r0, #0 /* yes, set return value to NULL */ 59 RET /* return */ 602: sub r0, r0, #1 /* back up by one */ 61 RET /* return */ 62.Lpre_main_loop: 63#if defined(_ARM_ARCH_7) 64 movw r1, #0xfefe /* magic constant; 254 in each byte */ 65 movt r1, #0xfefe /* magic constant; 254 in each byte */ 66#elif defined(_ARM_ARCH_6) 67 mov r1, #0xfe /* put 254 in low byte */ 68 orr r1, r1, r1, lsl #8 /* move to next byte */ 69 orr r1, r1, r1, lsl #16 /* move to next halfword */ 70#endif /* _ARM_ARCH_6 */ 71 orr r2, r2, r2, lsl #8 /* move to next byte */ 72 orr r2, r2, r2, lsl #16 /* move to next halfword */ 73.Lmain_loop: 74 ldr r3, [r0], #4 /* load next word */ 75#if defined(_ARM_ARCH_6) 76 /* 77 * Add 254 to each byte using the UQADD8 (unsigned saturating add 8) 78 * instruction. For every non-NUL byte, the result for that byte will 79 * become 255. For NUL, it will be 254. When we complement the 80 * result, if the result is non-0 then we must have encountered a NUL. 81 */ 82 uqadd8 ip, r3, r1 /* NUL detection happens here */ 83 eor r3, r3, r2 /* xor to clear each lane */ 84 uqadd8 r3, r3, r1 /* char detection happens here */ 85 and r3, r3, ip /* merge results */ 86 mvns r3, r3 /* is the complement non-0? */ 87 beq .Lmain_loop /* no, then keep going */ 88 89 /* 90 * We've encountered a NUL or a match but we don't know which happened 91 * first. 92 */ 93 mvns ip, ip /* did we encounter a NUL? */ 94 beq .Lfind_match /* no, find the match */ 95 bics r3, r3, ip /* clear match for the NUL(s) */ 96 beq .Lnomatch /* any left set? if not, no match */ 97 movs ip, ip, lshi #8 /* replicate NUL bit to other bytes */ 98 orrne ip, ip, lshi #8 /* replicate NUL bit to other bytes */ 99 orrne ip, ip, lshi #8 /* replicate NUL bit to other bytes */ 100 bics r3, r3, ip /* clear any match bits after the NUL */ 101 beq .Lnomatch /* any left set? if not, no match */ 102.Lfind_match: 103#ifdef __ARMEL__ 104 rev r3, r3 /* we want this in BE for the CLZ */ 105#endif 106 clz r3, r3 /* count how many leading zeros */ 107 add r0, r0, r3, lsr #3 /* divide that by 8 and add to count */ 108 sub r0, r0, #4 /* compensate for the post-inc */ 109 RET 110.Lnomatch: 111 mov r0, #0 112 RET 113#else 114 /* 115 * No fancy shortcuts so just test each byte lane for a NUL. 116 * (other tests for NULs in a word take more instructions/cycles). 117 */ 118 eor ip, r3, r2 /* xor .. */ 119 tst r3, #BYTE0 /* is this byte NUL? */ 120 tstne ip, #BYTE0 /* no, does this byte match? */ 121 tstne r3, #BYTE1 /* no, is this byte NUL? */ 122 tstne ip, #BYTE1 /* no, does this byte match? */ 123 tstne r3, #BYTE2 /* no, is this byte NUL? */ 124 tstne ip, #BYTE2 /* no, does this byte match? */ 125 tstne r3, #BYTE3 /* no, is this byte NUL? */ 126 tstne ip, #BYTE3 /* no, does this byte match? */ 127 bne .Lmain_loop 128 129 sub r2, r0, #4 /* un post-inc */ 130 mov r0, #0 /* assume no match */ 131 132 tst ip, #BYTE0 /* does this byte match? */ 133 moveq r0, r2 /* yes, point to it */ 134 RETc(eq) /* and return */ 135 tst r3, #BYTE0 /* is this byte NUL? */ 136 RETc(eq) /* yes, return NULL */ 137 138 tst ip, #BYTE1 /* does this byte match? */ 139 addeq r0, r2, #1 /* yes, point to it */ 140 RETc(eq) /* and return */ 141 tst r3, #BYTE1 /* is this byte NUL? */ 142 RETc(eq) /* yes, return NULL */ 143 144 tst ip, #BYTE2 /* does this byte match? */ 145 addeq r0, r2, #2 /* yes, point to it */ 146 RETc(eq) /* and return */ 147 tst r3, #BYTE2 /* is this byte NUL? */ 148 RETc(eq) /* yes, return NULL */ 149 150 tst ip, #BYTE3 /* does this byte match? */ 151 addeq r0, r2, #3 /* yes, point to it */ 152 /* 153 * Since no NULs and no matches this must be the only case left. 154 */ 155 RET /* return */ 156#endif /* _ARM_ARCH_6 */ 157END(strchr) 158