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