strlen_arm.S revision 1.3
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.3SmattRCSID("$NetBSD: strlen_arm.S,v 1.3 2013/01/23 06:59:55 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.3Smatt#ifdef STRNLEN 551.3Smatt push {r4,r5} /* save some registers */ 561.3Smatt add r5, r0, r1 /* get ptr to end of string */ 571.3Smatt mov r4, r1 /* save maxlen */ 581.3Smatt#endif 591.1Smatt add ip, r0, #4 /* for the final post-inc */ 601.1Smatt1: tst r0, #3 /* test for word alignment */ 611.1Smatt beq .Lpre_main_loop /* finally word aligned */ 621.3Smatt#ifdef STRNLEN 631.3Smatt cmp r0, r5 /* have we gone too far? */ 641.3Smatt beq .Lmaxed_out /* yes, return maxlen */ 651.3Smatt#endif 661.1Smatt ldrb r3, [r0], #1 /* load a byte */ 671.1Smatt teq r3, #0 /* is it 0? */ 681.1Smatt bne 1b /* no, try next byte */ 691.1Smatt sub ip, ip, #3 /* subtract (4 - the NUL) */ 701.1Smatt sub r0, r0, ip /* subtract start */ 711.3Smatt#ifdef STRNLEN 721.3Smatt pop {r4, r5} /* restore registers */ 731.3Smatt#endif 741.1Smatt RET /* return */ 751.1Smatt.Lpre_main_loop: 761.1Smatt#if defined(_ARM_ARCH_7) 771.1Smatt movw r1, #0xfefe /* magic constant; 254 in each byte */ 781.2Smatt movt r1, #0xfefe /* magic constant; 254 in each byte */ 791.2Smatt#elif defined(_ARM_ARCH_6) 801.1Smatt mov r1, #0xfe /* put 254 in low byte */ 811.1Smatt orr r1, r1, r1, lsl #8 /* move to next byte */ 821.1Smatt orr r1, r1, r1, lsl #16 /* move to next halfword */ 831.1Smatt#endif /* _ARM_ARCH_6 */ 841.1Smatt.Lmain_loop: 851.3Smatt#ifdef STRNLEN 861.3Smatt cmp r0, r5 /* gone too far? */ 871.3Smatt bge .Lmaxed_out /* yes, return maxlen */ 881.3Smatt#endif 891.1Smatt ldr r3, [r0], #4 /* load next word */ 901.1Smatt#if defined(_ARM_ARCH_6) 911.1Smatt /* 921.1Smatt * Add 254 to each byte using the UQADD8 (unsigned saturating add 8) 931.1Smatt * instruction. For every non-NUL byte, the result for that byte will 941.1Smatt * become 255. For NUL, it will be 254. When we complement the 951.1Smatt * result, if the result is non-0 then we must have encountered a NUL. 961.1Smatt */ 971.1Smatt uqadd8 r3, r3, r1 /* magic happens here */ 981.1Smatt mvns r3, r3 /* is the complemented result non-0? */ 991.1Smatt beq .Lmain_loop /* no, then we encountered no NULs */ 1001.1Smatt#else 1011.1Smatt /* 1021.1Smatt * No fancy shortcuts so just test each byte lane for a NUL. 1031.1Smatt * (other tests for NULs in a word take more instructions/cycles). 1041.1Smatt */ 1051.1Smatt tst r3, #BYTE0 /* is this byte 0? */ 1061.1Smatt tstne r3, #BYTE1 /* no, is this byte 0? */ 1071.1Smatt tstne r3, #BYTE2 /* no, is this byte 0? */ 1081.1Smatt tstne r3, #BYTE3 /* no, is this byte 0? */ 1091.1Smatt bne .Lmain_loop /* no, then get next word */ 1101.1Smatt#endif 1111.1Smatt#if defined(_ARM_ARCH_6) 1121.1Smatt /* 1131.1Smatt * We encountered a NUL. Find out where by doing a CLZ and then 1141.1Smatt * shifting right by 3. That will be the number of non-NUL bytes. 1151.1Smatt */ 1161.1Smatt#ifdef __ARMEL__ 1171.1Smatt rev r3, r3 /* we want this in BE for the CLZ */ 1181.1Smatt#endif 1191.1Smatt clz r3, r3 /* count how many leading zeros */ 1201.1Smatt add r0, r0, r3, lsr #3 /* divide that by 8 and add to count */ 1211.1Smatt#else 1221.1Smatt /* 1231.1Smatt * We encountered a NUL. 1241.1Smatt */ 1251.1Smatt tst r3, #BYTE0 /* 1st byte was NUL? */ 1261.1Smatt beq 1f /* yes, done adding */ 1271.1Smatt add r0, r0, #1 /* we have one more non-NUL byte */ 1281.1Smatt tst r3, #BYTE1 /* 2nd byte was NUL? */ 1291.1Smatt beq 1f /* yes, done adding */ 1301.1Smatt add r0, r0, #1 /* we have one more non-NUL byte */ 1311.1Smatt tst r3, #BYTE2 /* 3rd byte was NUL? */ 1321.1Smatt addne r0, r0, #1 /* no, we have one more non-NUL byte */ 1331.1Smatt1: 1341.1Smatt#endif /* _ARM_ARCH_6 */ 1351.1Smatt /* 1361.1Smatt * r0 now points to 4 past the NUL due to the post-inc. Subtract the 1371.1Smatt * start of the string (which also has 4 added to it to compensate for 1381.1Smatt * the post-inc. 1391.1Smatt */ 1401.1Smatt sub r0, r0, ip /* subtract start to get length */ 1411.3Smatt#ifdef STRNLEN 1421.3Smatt cmp r0, r4 /* is it larger than maxlen? */ 1431.3Smatt movgt r0, r4 /* yes, return maxlen */ 1441.3Smatt pop {r4, r5} /* restore registers */ 1451.3Smatt#endif 1461.3Smatt RET /* return */ 1471.3Smatt 1481.3Smatt#ifdef STRNLEN 1491.3Smatt.Lmaxed_out: 1501.3Smatt mov r0, r4 /* return maxlen */ 1511.3Smatt pop {r4, r5} /* restore registers */ 1521.3Smatt RET /* return */ 1531.3Smatt#endif 1541.3SmattEND(FUNCNAME) 155