1/*- 2 * Copyright (c) 2012 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: strlen_arm.S,v 1.8 2013/09/05 05:15:47 matt Exp $") 33 34#if defined(__thumb__) && !defined(_ARM_ARCH_T2) 35#error Only Thumb2 or ARM supported 36#endif 37 38#ifdef __ARMEL__ 39#define BYTE0 0x000000ff 40#define BYTE1 0x0000ff00 41#define BYTE2 0x00ff0000 42#define BYTE3 0xff000000 43#else 44#define BYTE0 0xff000000 45#define BYTE1 0x00ff0000 46#define BYTE2 0x0000ff00 47#define BYTE3 0x000000ff 48#endif 49 50#ifdef STRNLEN 51#define FUNCNAME strnlen 52#else 53#define FUNCNAME strlen 54#endif 55 56 .text 57ENTRY(FUNCNAME) 58#if defined(__ARM_EABI__) && defined(__UNWIND_TABLES__) 59 .fnstart 60 .cfi_startproc 61#endif 62#ifdef STRNLEN 63 push {r4,r5} /* save some registers */ 64#if defined(__ARM_EABI__) && defined(__UNWIND_TABLES__) 65 .save {r4,r5} 66 .cfi_def_cfa_offset 8 67 .cfi_offset 5, -4 68 .cfi_offset 4, -8 69#endif 70 adds r5, r0, r1 /* get ptr to end of string */ 71 mov r4, r1 /* save maxlen */ 72#endif 73 adds r2, r0, #4 /* for the final post-inc */ 741: tst r0, #3 /* test for word alignment */ 75 beq .Lpre_main_loop /* finally word aligned */ 76#ifdef STRNLEN 77 cmp r0, r5 /* have we gone too far? */ 78 beq .Lmaxed_out /* yes, return maxlen */ 79#endif 80 ldrb r3, [r0], #1 /* load a byte */ 81 cmp r3, #0 /* is it 0? */ 82 bne 1b /* no, try next byte */ 83 subs r2, r2, #3 /* subtract (4 - the NUL) */ 84 subs r0, r0, r2 /* subtract start */ 85#ifdef STRNLEN 86 pop {r4, r5} /* restore registers */ 87#endif 88 RET /* return */ 89.Lpre_main_loop: 90#if defined(_ARM_ARCH_7) 91 movw r1, #0xfefe /* magic constant; 254 in each byte */ 92 movt r1, #0xfefe /* magic constant; 254 in each byte */ 93#elif defined(_ARM_ARCH_6) 94 mov r1, #0xfe /* put 254 in low byte */ 95 orr r1, r1, r1, lsl #8 /* move to next byte */ 96 orr r1, r1, r1, lsl #16 /* move to next halfword */ 97#endif /* _ARM_ARCH_6 */ 98.Lmain_loop: 99#ifdef STRNLEN 100 cmp r0, r5 /* gone too far? */ 101 bge .Lmaxed_out /* yes, return maxlen */ 102#endif 103 ldr r3, [r0], #4 /* load next word */ 104#if defined(_ARM_ARCH_6) 105 /* 106 * Add 254 to each byte using the UQADD8 (unsigned saturating add 8) 107 * instruction. For every non-NUL byte, the result for that byte will 108 * become 255. For NUL, it will be 254. When we complement the 109 * result, if the result is non-0 then we must have encountered a NUL. 110 */ 111 uqadd8 r3, r3, r1 /* magic happens here */ 112 mvns r3, r3 /* is the complemented result non-0? */ 113 beq .Lmain_loop /* no, then we encountered no NULs */ 114#else 115 /* 116 * No fancy shortcuts so just test each byte lane for a NUL. 117 * (other tests for NULs in a word take more instructions/cycles). 118 */ 119 tst r3, #BYTE0 /* is this byte 0? */ 120 tstne r3, #BYTE1 /* no, is this byte 0? */ 121 tstne r3, #BYTE2 /* no, is this byte 0? */ 122 tstne r3, #BYTE3 /* no, is this byte 0? */ 123 bne .Lmain_loop /* no, then get next word */ 124#endif 125#if defined(_ARM_ARCH_6) 126 /* 127 * We encountered a NUL. Find out where by doing a CLZ and then 128 * shifting right by 3. That will be the number of non-NUL bytes. 129 */ 130#ifdef __ARMEL__ 131 rev r3, r3 /* we want this in BE for the CLZ */ 132#endif 133 clz r3, r3 /* count how many leading zeros */ 134#ifdef __thumb__ 135 lsrs r3, r3, #3 136 adds r0, r0, r3 /* divide that by 8 and add to count */ 137#else 138 add r0, r0, r3, lsr #3 /* divide that by 8 and add to count */ 139#endif 140#else 141 /* 142 * We encountered a NUL. 143 */ 144 tst r3, #BYTE0 /* 1st byte was NUL? */ 145 beq 1f /* yes, done adding */ 146 add r0, r0, #1 /* we have one more non-NUL byte */ 147 tst r3, #BYTE1 /* 2nd byte was NUL? */ 148 beq 1f /* yes, done adding */ 149 add r0, r0, #1 /* we have one more non-NUL byte */ 150 tst r3, #BYTE2 /* 3rd byte was NUL? */ 151 addne r0, r0, #1 /* no, we have one more non-NUL byte */ 1521: 153#endif /* _ARM_ARCH_6 */ 154 /* 155 * r0 now points to 4 past the NUL due to the post-inc. Subtract the 156 * start of the string (which also has 4 added to it to compensate for 157 * the post-inc. 158 */ 159 subs r0, r0, r2 /* subtract start to get length */ 160#ifdef STRNLEN 161 cmp r0, r4 /* is it larger than maxlen? */ 162#ifdef __thumb__ 163 it gt 164#endif 165 movgt r0, r4 /* yes, return maxlen */ 166 pop {r4, r5} /* restore registers */ 167#endif 168 RET /* return */ 169 170#ifdef STRNLEN 171.Lmaxed_out: 172 mov r0, r4 /* return maxlen */ 173 pop {r4, r5} /* restore registers */ 174 RET /* return */ 175#endif 176#if defined(__ARM_EABI__) && defined(__UNWIND_TABLES__) 177 .cfi_endproc 178 .fnend 179#endif 180END(FUNCNAME) 181