strlen_arm.S revision 1.1
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.1SmattRCSID("$NetBSD: strlen_arm.S,v 1.1 2013/01/08 13:17:45 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.1Smatt .text 471.1SmattENTRY(strlen) 481.1Smatt add ip, r0, #4 /* for the final post-inc */ 491.1Smatt1: tst r0, #3 /* test for word alignment */ 501.1Smatt beq .Lpre_main_loop /* finally word aligned */ 511.1Smatt ldrb r3, [r0], #1 /* load a byte */ 521.1Smatt teq r3, #0 /* is it 0? */ 531.1Smatt bne 1b /* no, try next byte */ 541.1Smatt sub ip, ip, #3 /* subtract (4 - the NUL) */ 551.1Smatt sub r0, r0, ip /* subtract start */ 561.1Smatt RET /* return */ 571.1Smatt.Lpre_main_loop: 581.1Smatt#if defined(_ARM_ARCH_6) 591.1Smatt#if defined(_ARM_ARCH_7) 601.1Smatt movw r1, #0xfefe /* magic constant; 254 in each byte */ 611.1Smatt#else 621.1Smatt mov r1, #0xfe /* put 254 in low byte */ 631.1Smatt orr r1, r1, r1, lsl #8 /* move to next byte */ 641.1Smatt#endif 651.1Smatt orr r1, r1, r1, lsl #16 /* move to next halfword */ 661.1Smatt#endif /* _ARM_ARCH_6 */ 671.1Smatt.Lmain_loop: 681.1Smatt ldr r3, [r0], #4 /* load next word */ 691.1Smatt#if defined(_ARM_ARCH_6) 701.1Smatt /* 711.1Smatt * Add 254 to each byte using the UQADD8 (unsigned saturating add 8) 721.1Smatt * instruction. For every non-NUL byte, the result for that byte will 731.1Smatt * become 255. For NUL, it will be 254. When we complement the 741.1Smatt * result, if the result is non-0 then we must have encountered a NUL. 751.1Smatt */ 761.1Smatt uqadd8 r3, r3, r1 /* magic happens here */ 771.1Smatt mvns r3, r3 /* is the complemented result non-0? */ 781.1Smatt beq .Lmain_loop /* no, then we encountered no NULs */ 791.1Smatt#else 801.1Smatt /* 811.1Smatt * No fancy shortcuts so just test each byte lane for a NUL. 821.1Smatt * (other tests for NULs in a word take more instructions/cycles). 831.1Smatt */ 841.1Smatt tst r3, #BYTE0 /* is this byte 0? */ 851.1Smatt tstne r3, #BYTE1 /* no, is this byte 0? */ 861.1Smatt tstne r3, #BYTE2 /* no, is this byte 0? */ 871.1Smatt tstne r3, #BYTE3 /* no, is this byte 0? */ 881.1Smatt bne .Lmain_loop /* no, then get next word */ 891.1Smatt#endif 901.1Smatt#if defined(_ARM_ARCH_6) 911.1Smatt /* 921.1Smatt * We encountered a NUL. Find out where by doing a CLZ and then 931.1Smatt * shifting right by 3. That will be the number of non-NUL bytes. 941.1Smatt */ 951.1Smatt#ifdef __ARMEL__ 961.1Smatt rev r3, r3 /* we want this in BE for the CLZ */ 971.1Smatt#endif 981.1Smatt clz r3, r3 /* count how many leading zeros */ 991.1Smatt add r0, r0, r3, lsr #3 /* divide that by 8 and add to count */ 1001.1Smatt#else 1011.1Smatt /* 1021.1Smatt * We encountered a NUL. 1031.1Smatt */ 1041.1Smatt tst r3, #BYTE0 /* 1st byte was NUL? */ 1051.1Smatt beq 1f /* yes, done adding */ 1061.1Smatt add r0, r0, #1 /* we have one more non-NUL byte */ 1071.1Smatt tst r3, #BYTE1 /* 2nd byte was NUL? */ 1081.1Smatt beq 1f /* yes, done adding */ 1091.1Smatt add r0, r0, #1 /* we have one more non-NUL byte */ 1101.1Smatt tst r3, #BYTE2 /* 3rd byte was NUL? */ 1111.1Smatt addne r0, r0, #1 /* no, we have one more non-NUL byte */ 1121.1Smatt1: 1131.1Smatt#endif /* _ARM_ARCH_6 */ 1141.1Smatt /* 1151.1Smatt * r0 now points to 4 past the NUL due to the post-inc. Subtract the 1161.1Smatt * start of the string (which also has 4 added to it to compensate for 1171.1Smatt * the post-inc. 1181.1Smatt */ 1191.1Smatt sub r0, r0, ip /* subtract start to get length */ 1201.1Smatt RET 1211.1SmattEND(strlen) 122