strlen_arm.S revision 1.5
11.1Smatt/*- 21.1Smatt * Copyright (c) 2012 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.5SmattRCSID("$NetBSD: strlen_arm.S,v 1.5 2013/08/20 08:06:30 matt Exp $") 331.5Smatt 341.5Smatt#if defined(__thumb__) && !defined(_ARM_ARCH_T2) 351.5Smatt#error Only Thumb2 or ARM supported 361.5Smatt#endif 371.1Smatt 381.1Smatt#ifdef __ARMEL__ 391.1Smatt#define BYTE0 0x000000ff 401.1Smatt#define BYTE1 0x0000ff00 411.1Smatt#define BYTE2 0x00ff0000 421.1Smatt#define BYTE3 0xff000000 431.1Smatt#else 441.1Smatt#define BYTE0 0xff000000 451.1Smatt#define BYTE1 0x00ff0000 461.1Smatt#define BYTE2 0x0000ff00 471.1Smatt#define BYTE3 0x000000ff 481.1Smatt#endif 491.1Smatt 501.3Smatt#ifdef STRNLEN 511.3Smatt#define FUNCNAME strnlen 521.3Smatt#else 531.3Smatt#define FUNCNAME strlen 541.3Smatt#endif 551.3Smatt 561.1Smatt .text 571.3SmattENTRY(FUNCNAME) 581.4Smatt#ifdef __ARM_EABI__ 591.4Smatt .fnstart 601.4Smatt .cfi_startproc 611.4Smatt#endif 621.3Smatt#ifdef STRNLEN 631.3Smatt push {r4,r5} /* save some registers */ 641.4Smatt#ifdef __ARM_EABI__ 651.4Smatt .save {r4,r5} 661.4Smatt .cfi_def_cfa_offset 8 671.4Smatt .cfi_offset 5, -4 681.4Smatt .cfi_offset 4, -8 691.4Smatt#endif 701.5Smatt adds r5, r0, r1 /* get ptr to end of string */ 711.3Smatt mov r4, r1 /* save maxlen */ 721.3Smatt#endif 731.5Smatt adds r2, r0, #4 /* for the final post-inc */ 741.1Smatt1: tst r0, #3 /* test for word alignment */ 751.1Smatt beq .Lpre_main_loop /* finally word aligned */ 761.3Smatt#ifdef STRNLEN 771.3Smatt cmp r0, r5 /* have we gone too far? */ 781.3Smatt beq .Lmaxed_out /* yes, return maxlen */ 791.3Smatt#endif 801.1Smatt ldrb r3, [r0], #1 /* load a byte */ 811.5Smatt cmp r3, #0 /* is it 0? */ 821.1Smatt bne 1b /* no, try next byte */ 831.5Smatt subs r2, r2, #3 /* subtract (4 - the NUL) */ 841.5Smatt subs r0, r0, r2 /* subtract start */ 851.3Smatt#ifdef STRNLEN 861.3Smatt pop {r4, r5} /* restore registers */ 871.3Smatt#endif 881.1Smatt RET /* return */ 891.1Smatt.Lpre_main_loop: 901.1Smatt#if defined(_ARM_ARCH_7) 911.1Smatt movw r1, #0xfefe /* magic constant; 254 in each byte */ 921.2Smatt movt r1, #0xfefe /* magic constant; 254 in each byte */ 931.2Smatt#elif defined(_ARM_ARCH_6) 941.1Smatt mov r1, #0xfe /* put 254 in low byte */ 951.1Smatt orr r1, r1, r1, lsl #8 /* move to next byte */ 961.1Smatt orr r1, r1, r1, lsl #16 /* move to next halfword */ 971.1Smatt#endif /* _ARM_ARCH_6 */ 981.1Smatt.Lmain_loop: 991.3Smatt#ifdef STRNLEN 1001.3Smatt cmp r0, r5 /* gone too far? */ 1011.3Smatt bge .Lmaxed_out /* yes, return maxlen */ 1021.3Smatt#endif 1031.1Smatt ldr r3, [r0], #4 /* load next word */ 1041.1Smatt#if defined(_ARM_ARCH_6) 1051.1Smatt /* 1061.1Smatt * Add 254 to each byte using the UQADD8 (unsigned saturating add 8) 1071.1Smatt * instruction. For every non-NUL byte, the result for that byte will 1081.1Smatt * become 255. For NUL, it will be 254. When we complement the 1091.1Smatt * result, if the result is non-0 then we must have encountered a NUL. 1101.1Smatt */ 1111.1Smatt uqadd8 r3, r3, r1 /* magic happens here */ 1121.1Smatt mvns r3, r3 /* is the complemented result non-0? */ 1131.1Smatt beq .Lmain_loop /* no, then we encountered no NULs */ 1141.1Smatt#else 1151.1Smatt /* 1161.1Smatt * No fancy shortcuts so just test each byte lane for a NUL. 1171.1Smatt * (other tests for NULs in a word take more instructions/cycles). 1181.1Smatt */ 1191.1Smatt tst r3, #BYTE0 /* is this byte 0? */ 1201.1Smatt tstne r3, #BYTE1 /* no, is this byte 0? */ 1211.1Smatt tstne r3, #BYTE2 /* no, is this byte 0? */ 1221.1Smatt tstne r3, #BYTE3 /* no, is this byte 0? */ 1231.1Smatt bne .Lmain_loop /* no, then get next word */ 1241.1Smatt#endif 1251.1Smatt#if defined(_ARM_ARCH_6) 1261.1Smatt /* 1271.1Smatt * We encountered a NUL. Find out where by doing a CLZ and then 1281.1Smatt * shifting right by 3. That will be the number of non-NUL bytes. 1291.1Smatt */ 1301.1Smatt#ifdef __ARMEL__ 1311.1Smatt rev r3, r3 /* we want this in BE for the CLZ */ 1321.1Smatt#endif 1331.1Smatt clz r3, r3 /* count how many leading zeros */ 1341.5Smatt#ifdef __thumb__ 1351.5Smatt lsrs r3, r3, #3 1361.5Smatt adds r0, r0, r3 /* divide that by 8 and add to count */ 1371.5Smatt#else 1381.1Smatt add r0, r0, r3, lsr #3 /* divide that by 8 and add to count */ 1391.5Smatt#endif 1401.1Smatt#else 1411.1Smatt /* 1421.1Smatt * We encountered a NUL. 1431.1Smatt */ 1441.1Smatt tst r3, #BYTE0 /* 1st byte was NUL? */ 1451.1Smatt beq 1f /* yes, done adding */ 1461.1Smatt add r0, r0, #1 /* we have one more non-NUL byte */ 1471.1Smatt tst r3, #BYTE1 /* 2nd byte was NUL? */ 1481.1Smatt beq 1f /* yes, done adding */ 1491.1Smatt add r0, r0, #1 /* we have one more non-NUL byte */ 1501.1Smatt tst r3, #BYTE2 /* 3rd byte was NUL? */ 1511.1Smatt addne r0, r0, #1 /* no, we have one more non-NUL byte */ 1521.1Smatt1: 1531.1Smatt#endif /* _ARM_ARCH_6 */ 1541.1Smatt /* 1551.1Smatt * r0 now points to 4 past the NUL due to the post-inc. Subtract the 1561.1Smatt * start of the string (which also has 4 added to it to compensate for 1571.1Smatt * the post-inc. 1581.1Smatt */ 1591.5Smatt subs r0, r0, r2 /* subtract start to get length */ 1601.3Smatt#ifdef STRNLEN 1611.3Smatt cmp r0, r4 /* is it larger than maxlen? */ 1621.3Smatt movgt r0, r4 /* yes, return maxlen */ 1631.3Smatt pop {r4, r5} /* restore registers */ 1641.3Smatt#endif 1651.3Smatt RET /* return */ 1661.3Smatt 1671.3Smatt#ifdef STRNLEN 1681.3Smatt.Lmaxed_out: 1691.3Smatt mov r0, r4 /* return maxlen */ 1701.3Smatt pop {r4, r5} /* restore registers */ 1711.3Smatt RET /* return */ 1721.3Smatt#endif 1731.4Smatt#ifdef __ARM_EABI__ 1741.4Smatt .cfi_endproc 1751.4Smatt .fnend 1761.4Smatt#endif 1771.3SmattEND(FUNCNAME) 178