strlen_arm.S revision 1.4
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.4SmattRCSID("$NetBSD: strlen_arm.S,v 1.4 2013/08/19 01:17:32 matt Exp $") 331.1Smatt 341.1Smatt#ifdef __ARMEL__ 351.1Smatt#define BYTE0 0x000000ff 361.1Smatt#define BYTE1 0x0000ff00 371.1Smatt#define BYTE2 0x00ff0000 381.1Smatt#define BYTE3 0xff000000 391.1Smatt#else 401.1Smatt#define BYTE0 0xff000000 411.1Smatt#define BYTE1 0x00ff0000 421.1Smatt#define BYTE2 0x0000ff00 431.1Smatt#define BYTE3 0x000000ff 441.1Smatt#endif 451.1Smatt 461.3Smatt#ifdef STRNLEN 471.3Smatt#define FUNCNAME strnlen 481.3Smatt#else 491.3Smatt#define FUNCNAME strlen 501.3Smatt#endif 511.3Smatt 521.1Smatt .text 531.3SmattENTRY(FUNCNAME) 541.4Smatt#ifdef __ARM_EABI__ 551.4Smatt .fnstart 561.4Smatt .cfi_startproc 571.4Smatt#endif 581.3Smatt#ifdef STRNLEN 591.3Smatt push {r4,r5} /* save some registers */ 601.4Smatt#ifdef __ARM_EABI__ 611.4Smatt .save {r4,r5} 621.4Smatt .cfi_def_cfa_offset 8 631.4Smatt .cfi_offset 5, -4 641.4Smatt .cfi_offset 4, -8 651.4Smatt#endif 661.3Smatt add r5, r0, r1 /* get ptr to end of string */ 671.3Smatt mov r4, r1 /* save maxlen */ 681.3Smatt#endif 691.1Smatt add ip, r0, #4 /* for the final post-inc */ 701.1Smatt1: tst r0, #3 /* test for word alignment */ 711.1Smatt beq .Lpre_main_loop /* finally word aligned */ 721.3Smatt#ifdef STRNLEN 731.3Smatt cmp r0, r5 /* have we gone too far? */ 741.3Smatt beq .Lmaxed_out /* yes, return maxlen */ 751.3Smatt#endif 761.1Smatt ldrb r3, [r0], #1 /* load a byte */ 771.1Smatt teq r3, #0 /* is it 0? */ 781.1Smatt bne 1b /* no, try next byte */ 791.1Smatt sub ip, ip, #3 /* subtract (4 - the NUL) */ 801.1Smatt sub r0, r0, ip /* subtract start */ 811.3Smatt#ifdef STRNLEN 821.3Smatt pop {r4, r5} /* restore registers */ 831.3Smatt#endif 841.1Smatt RET /* return */ 851.1Smatt.Lpre_main_loop: 861.1Smatt#if defined(_ARM_ARCH_7) 871.1Smatt movw r1, #0xfefe /* magic constant; 254 in each byte */ 881.2Smatt movt r1, #0xfefe /* magic constant; 254 in each byte */ 891.2Smatt#elif defined(_ARM_ARCH_6) 901.1Smatt mov r1, #0xfe /* put 254 in low byte */ 911.1Smatt orr r1, r1, r1, lsl #8 /* move to next byte */ 921.1Smatt orr r1, r1, r1, lsl #16 /* move to next halfword */ 931.1Smatt#endif /* _ARM_ARCH_6 */ 941.1Smatt.Lmain_loop: 951.3Smatt#ifdef STRNLEN 961.3Smatt cmp r0, r5 /* gone too far? */ 971.3Smatt bge .Lmaxed_out /* yes, return maxlen */ 981.3Smatt#endif 991.1Smatt ldr r3, [r0], #4 /* load next word */ 1001.1Smatt#if defined(_ARM_ARCH_6) 1011.1Smatt /* 1021.1Smatt * Add 254 to each byte using the UQADD8 (unsigned saturating add 8) 1031.1Smatt * instruction. For every non-NUL byte, the result for that byte will 1041.1Smatt * become 255. For NUL, it will be 254. When we complement the 1051.1Smatt * result, if the result is non-0 then we must have encountered a NUL. 1061.1Smatt */ 1071.1Smatt uqadd8 r3, r3, r1 /* magic happens here */ 1081.1Smatt mvns r3, r3 /* is the complemented result non-0? */ 1091.1Smatt beq .Lmain_loop /* no, then we encountered no NULs */ 1101.1Smatt#else 1111.1Smatt /* 1121.1Smatt * No fancy shortcuts so just test each byte lane for a NUL. 1131.1Smatt * (other tests for NULs in a word take more instructions/cycles). 1141.1Smatt */ 1151.1Smatt tst r3, #BYTE0 /* is this byte 0? */ 1161.1Smatt tstne r3, #BYTE1 /* no, is this byte 0? */ 1171.1Smatt tstne r3, #BYTE2 /* no, is this byte 0? */ 1181.1Smatt tstne r3, #BYTE3 /* no, is this byte 0? */ 1191.1Smatt bne .Lmain_loop /* no, then get next word */ 1201.1Smatt#endif 1211.1Smatt#if defined(_ARM_ARCH_6) 1221.1Smatt /* 1231.1Smatt * We encountered a NUL. Find out where by doing a CLZ and then 1241.1Smatt * shifting right by 3. That will be the number of non-NUL bytes. 1251.1Smatt */ 1261.1Smatt#ifdef __ARMEL__ 1271.1Smatt rev r3, r3 /* we want this in BE for the CLZ */ 1281.1Smatt#endif 1291.1Smatt clz r3, r3 /* count how many leading zeros */ 1301.1Smatt add r0, r0, r3, lsr #3 /* divide that by 8 and add to count */ 1311.1Smatt#else 1321.1Smatt /* 1331.1Smatt * We encountered a NUL. 1341.1Smatt */ 1351.1Smatt tst r3, #BYTE0 /* 1st byte was NUL? */ 1361.1Smatt beq 1f /* yes, done adding */ 1371.1Smatt add r0, r0, #1 /* we have one more non-NUL byte */ 1381.1Smatt tst r3, #BYTE1 /* 2nd byte was NUL? */ 1391.1Smatt beq 1f /* yes, done adding */ 1401.1Smatt add r0, r0, #1 /* we have one more non-NUL byte */ 1411.1Smatt tst r3, #BYTE2 /* 3rd byte was NUL? */ 1421.1Smatt addne r0, r0, #1 /* no, we have one more non-NUL byte */ 1431.1Smatt1: 1441.1Smatt#endif /* _ARM_ARCH_6 */ 1451.1Smatt /* 1461.1Smatt * r0 now points to 4 past the NUL due to the post-inc. Subtract the 1471.1Smatt * start of the string (which also has 4 added to it to compensate for 1481.1Smatt * the post-inc. 1491.1Smatt */ 1501.1Smatt sub r0, r0, ip /* subtract start to get length */ 1511.3Smatt#ifdef STRNLEN 1521.3Smatt cmp r0, r4 /* is it larger than maxlen? */ 1531.3Smatt movgt r0, r4 /* yes, return maxlen */ 1541.3Smatt pop {r4, r5} /* restore registers */ 1551.3Smatt#endif 1561.3Smatt RET /* return */ 1571.3Smatt 1581.3Smatt#ifdef STRNLEN 1591.3Smatt.Lmaxed_out: 1601.3Smatt mov r0, r4 /* return maxlen */ 1611.3Smatt pop {r4, r5} /* restore registers */ 1621.3Smatt RET /* return */ 1631.3Smatt#endif 1641.4Smatt#ifdef __ARM_EABI__ 1651.4Smatt .cfi_endproc 1661.4Smatt .fnend 1671.4Smatt#endif 1681.3SmattEND(FUNCNAME) 169