Home | History | Annotate | Line # | Download | only in string
strlen.S revision 1.1.4.2
      1 /* $NetBSD: strlen.S,v 1.1.4.2 2014/08/19 23:45:12 tls Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <machine/asm.h>
     33 
     34 RCSID("$NetBSD: strlen.S,v 1.1.4.2 2014/08/19 23:45:12 tls Exp $")
     35 
     36 #ifdef STRNLEN
     37 #define FUNCNAME	strnlen
     38 /* LINTSTUB: size_t strnlen(const char *, size_t); */
     39 #else
     40 #define FUNCNAME	strlen
     41 /* LINTSTUB: size_t strlen(const char *); */
     42 #endif
     43 
     44 #define	MASK8_0x01	0x0101010101010101
     45 #define	MASK8_0x7f	0x7f7f7f7f7f7f7f7f
     46 
     47 ENTRY(FUNCNAME)
     48 	mov	x4, x0			/* need x0 for return */
     49 	add	x9, x0, #8		/* start + dword */
     50 #ifdef STRNLEN
     51 	add	x10, x0, x1		/* don't go past here */
     52 #endif
     53 	mov	x11, #MASK8_0x01	/* test mask */
     54 
     55 	ands	x3, x4, #7		/* extract alignment */
     56 	neg	x0, x3			/* alignment fixup */
     57 	b.eq	.Lstrlen_dword_loop	/* already dword aligned */
     58 
     59 	/*
     60 	 * Load the dword containing the leading bytes.  Make sure bytes
     61 	 * before the data won't match as NUL.
     62 	 */
     63 	add	x4, x4, x0		/* make dword aligned */
     64 	ldr	x7, [x4], #8		/* load dword */
     65 	lsl	x3, x3, #3		/* convert bytes to bits */
     66 #ifdef __AARCH64EB__
     67 	lsr	x5, x11, x3		/* make mask for BE */
     68 #else
     69 	lsl	x5, x11, x3		/* make mask for LE */
     70 #endif
     71 	eor	x5, x5, x11		/* invert mask */
     72 	orr	x7, x7, x5		/* prevent NULs */
     73 	b	.Lstrlen_dword_loop_noload
     74 
     75 .Lstrlen_dword_loop:
     76 #ifdef STRNLEN
     77 	cmp	x4, x10
     78 	b.ge	.Lstrlen_done
     79 #endif
     80 	ldr	x7, [x4], #8		/* load dword */
     81 .Lstrlen_dword_loop_noload:
     82 	/*
     83 	 * Use the formula (X - 1) & ~(X | 0x7f) to find NUL bytes.
     84 	 * Any NUL byte found will be replaced by 0x80 otherwise any byte
     85 	 * will be replaced by 0x00.
     86 	 */
     87 	sub	x6, x7, x11		/* a = X - 1 */
     88 	orr	x7, x7, #MASK8_0x7f	/* b = X | 0x7f */
     89 	bic	x6, x6, x7		/* a & ~b */
     90 	cbz	x6, .Lstrlen_dword_loop	/* no NULs so get next dword */
     91 
     92 	/*
     93 	 * We know there is a NUL in this dword.  Use clz to find it.
     94 	 */
     95 #ifdef __AARCH64EL__
     96 	rev	x7, x7			/* convert to BE */
     97 #endif
     98 	clz	x7, x7			/* find null byte */
     99 	add	x0, x0, x7, lsr #3	/* add offset to the length */
    100 
    101 	add	x0, x0, x4		/* add end to the length */
    102 	sub	x0, x0, x9		/* subtract start from the length */
    103 #ifdef STRNLEN
    104 	cmp	x0, x1			/* did we go too far? */
    105         csel    x0, x0, x1, lt		/* yes, return max length */
    106 #endif
    107 	ret
    108 #ifdef STRNLEN
    109 .Lstrlen_done:
    110 	mov	x0, x1
    111 	ret
    112 #endif
    113 END(FUNCNAME)
    114