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