Home | History | Annotate | Line # | Download | only in string
strchr_arm.S revision 1.5.4.2
      1  1.5.4.2  tls /*-
      2  1.5.4.2  tls  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      3  1.5.4.2  tls  * All rights reserved.
      4  1.5.4.2  tls  *
      5  1.5.4.2  tls  * This code is derived from software contributed to The NetBSD Foundation
      6  1.5.4.2  tls  * by Matt Thomas of 3am Software Foundry.
      7  1.5.4.2  tls  *
      8  1.5.4.2  tls  * Redistribution and use in source and binary forms, with or without
      9  1.5.4.2  tls  * modification, are permitted provided that the following conditions
     10  1.5.4.2  tls  * are met:
     11  1.5.4.2  tls  * 1. Redistributions of source code must retain the above copyright
     12  1.5.4.2  tls  *    notice, this list of conditions and the following disclaimer.
     13  1.5.4.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.5.4.2  tls  *    notice, this list of conditions and the following disclaimer in the
     15  1.5.4.2  tls  *    documentation and/or other materials provided with the distribution.
     16  1.5.4.2  tls  *
     17  1.5.4.2  tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.5.4.2  tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.5.4.2  tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.5.4.2  tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.5.4.2  tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.5.4.2  tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.5.4.2  tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.5.4.2  tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.5.4.2  tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.5.4.2  tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.5.4.2  tls  * POSSIBILITY OF SUCH DAMAGE.
     28  1.5.4.2  tls  */
     29  1.5.4.2  tls 
     30  1.5.4.2  tls #include <machine/asm.h>
     31  1.5.4.2  tls 
     32  1.5.4.2  tls RCSID("$NetBSD: strchr_arm.S,v 1.5.4.2 2013/02/25 00:23:57 tls Exp $")
     33  1.5.4.2  tls 
     34  1.5.4.2  tls #ifdef __ARMEL__
     35  1.5.4.2  tls #define	BYTE0	0x000000ff
     36  1.5.4.2  tls #define	BYTE1	0x0000ff00
     37  1.5.4.2  tls #define	BYTE2	0x00ff0000
     38  1.5.4.2  tls #define	BYTE3	0xff000000
     39  1.5.4.2  tls #define	lshi	lsl
     40  1.5.4.2  tls #else
     41  1.5.4.2  tls #define	BYTE0	0xff000000
     42  1.5.4.2  tls #define	BYTE1	0x00ff0000
     43  1.5.4.2  tls #define	BYTE2	0x0000ff00
     44  1.5.4.2  tls #define	BYTE3	0x000000ff
     45  1.5.4.2  tls #define	lshi	lsr
     46  1.5.4.2  tls #endif
     47  1.5.4.2  tls 
     48  1.5.4.2  tls 	.text
     49  1.5.4.2  tls ENTRY(strchr)
     50  1.5.4.2  tls 	and	r2, r1, #0xff		/* restrict to byte value */
     51  1.5.4.2  tls 1:	tst	r0, #3			/* test for word alignment */
     52  1.5.4.2  tls 	beq	.Lpre_main_loop		/*   finally word aligned */
     53  1.5.4.2  tls 	ldrb	r3, [r0], #1		/* load a byte */
     54  1.5.4.2  tls 	cmp	r3, r2			/* is it a match? */
     55  1.5.4.2  tls 	beq	2f			/*   yes, return current ptr - 1 */
     56  1.5.4.2  tls 	teq	r3, #0			/* no, was it 0? */
     57  1.5.4.2  tls 	bne	1b			/*   no, try next byte */
     58  1.5.4.2  tls 	mov	r0, #0			/*   yes, set return value to NULL */
     59  1.5.4.2  tls 	RET				/* return */
     60  1.5.4.2  tls 2:	sub	r0, r0, #1		/* back up by one */
     61  1.5.4.2  tls 	RET				/* return */
     62  1.5.4.2  tls .Lpre_main_loop:
     63  1.5.4.2  tls #if defined(_ARM_ARCH_7)
     64  1.5.4.2  tls 	movw	r1, #0xfefe		/* magic constant; 254 in each byte */
     65  1.5.4.2  tls 	movt	r1, #0xfefe		/* magic constant; 254 in each byte */
     66  1.5.4.2  tls #elif defined(_ARM_ARCH_6)
     67  1.5.4.2  tls 	mov	r1, #0xfe		/* put 254 in low byte */
     68  1.5.4.2  tls 	orr	r1, r1, r1, lsl #8	/* move to next byte */
     69  1.5.4.2  tls 	orr	r1, r1, r1, lsl #16	/* move to next halfword */
     70  1.5.4.2  tls #endif /* _ARM_ARCH_6 */
     71  1.5.4.2  tls 	orr	r2, r2, r2, lsl #8	/* move to next byte */
     72  1.5.4.2  tls 	orr	r2, r2, r2, lsl #16	/* move to next halfword */
     73  1.5.4.2  tls .Lmain_loop:
     74  1.5.4.2  tls 	ldr	r3, [r0], #4		/* load next word */
     75  1.5.4.2  tls #if defined(_ARM_ARCH_6)
     76  1.5.4.2  tls 	/*
     77  1.5.4.2  tls 	 * Add 254 to each byte using the UQADD8 (unsigned saturating add 8)
     78  1.5.4.2  tls 	 * instruction.  For every non-NUL byte, the result for that byte will
     79  1.5.4.2  tls 	 * become 255.  For NUL, it will be 254.  When we complement the
     80  1.5.4.2  tls 	 * result, if the result is non-0 then we must have encountered a NUL.
     81  1.5.4.2  tls 	 */
     82  1.5.4.2  tls 	uqadd8	ip, r3, r1		/* NUL detection happens here */
     83  1.5.4.2  tls 	eor	r3, r3, r2		/* xor to clear each lane */
     84  1.5.4.2  tls 	uqadd8	r3, r3, r1		/* char detection happens here */
     85  1.5.4.2  tls 	and	r3, r3, ip		/* merge results */
     86  1.5.4.2  tls 	mvns	r3, r3			/* is the complement non-0? */
     87  1.5.4.2  tls 	beq	.Lmain_loop		/*    no, then keep going */
     88  1.5.4.2  tls 
     89  1.5.4.2  tls 	/*
     90  1.5.4.2  tls 	 * We've encountered a NUL or a match but we don't know which happened
     91  1.5.4.2  tls 	 * first.
     92  1.5.4.2  tls 	 */
     93  1.5.4.2  tls 	teq	r2, #0			/* searching for NUL? */
     94  1.5.4.2  tls 	beq	.Lfind_match		/*   yes, find the match */
     95  1.5.4.2  tls 	mvns	ip, ip			/* did we encounter a NUL? */
     96  1.5.4.2  tls 	beq	.Lfind_match		/*   no, find the match */
     97  1.5.4.2  tls 	bics	r3, r3, ip		/* clear match for the NUL(s) */
     98  1.5.4.2  tls 	beq	.Lnomatch		/*   any left set? if not, no match */
     99  1.5.4.2  tls 	movs	ip, ip, lshi #8		/* replicate NUL bit to other bytes */
    100  1.5.4.2  tls 	orrne	ip, ip, ip, lshi #8	/* replicate NUL bit to other bytes */
    101  1.5.4.2  tls 	orrne	ip, ip, ip, lshi #8	/* replicate NUL bit to other bytes */
    102  1.5.4.2  tls 	bics	r3, r3, ip		/* clear any match bits after the NUL */
    103  1.5.4.2  tls 	beq	.Lnomatch		/*   any left set? if not, no match */
    104  1.5.4.2  tls .Lfind_match:
    105  1.5.4.2  tls #ifdef __ARMEL__
    106  1.5.4.2  tls 	rev	r3, r3			/* we want this in BE for the CLZ */
    107  1.5.4.2  tls #endif
    108  1.5.4.2  tls 	clz	r3, r3			/* count how many leading zeros */
    109  1.5.4.2  tls 	add	r0, r0, r3, lsr #3	/* divide that by 8 and add to count */
    110  1.5.4.2  tls 	sub	r0, r0, #4		/* compensate for the post-inc */
    111  1.5.4.2  tls 	RET
    112  1.5.4.2  tls .Lnomatch:
    113  1.5.4.2  tls 	mov	r0, #0
    114  1.5.4.2  tls 	RET
    115  1.5.4.2  tls #else
    116  1.5.4.2  tls 	/*
    117  1.5.4.2  tls 	 * No fancy shortcuts so just test each byte lane for a NUL.
    118  1.5.4.2  tls 	 * (other tests for NULs in a word take more instructions/cycles).
    119  1.5.4.2  tls 	 */
    120  1.5.4.2  tls 	eor	ip, r3, r2		/* xor .. */
    121  1.5.4.2  tls 	tst	r3, #BYTE0		/* is this byte NUL? */
    122  1.5.4.2  tls 	tstne	ip, #BYTE0		/*   no, does this byte match? */
    123  1.5.4.2  tls 	tstne	r3, #BYTE1		/*   no, is this byte NUL? */
    124  1.5.4.2  tls 	tstne	ip, #BYTE1		/*   no, does this byte match? */
    125  1.5.4.2  tls 	tstne	r3, #BYTE2		/*   no, is this byte NUL? */
    126  1.5.4.2  tls 	tstne	ip, #BYTE2		/*   no, does this byte match? */
    127  1.5.4.2  tls 	tstne	r3, #BYTE3		/*   no, is this byte NUL? */
    128  1.5.4.2  tls 	tstne	ip, #BYTE3		/*   no, does this byte match? */
    129  1.5.4.2  tls 	bne	.Lmain_loop
    130  1.5.4.2  tls 
    131  1.5.4.2  tls 	sub	r2, r0, #4		/* un post-inc */
    132  1.5.4.2  tls 	mov	r0, #0			/* assume no match */
    133  1.5.4.2  tls 
    134  1.5.4.2  tls 	tst	ip, #BYTE0		/* does this byte match? */
    135  1.5.4.2  tls 	moveq	r0, r2			/*   yes, point to it */
    136  1.5.4.2  tls 	RETc(eq)			/*        and return */
    137  1.5.4.2  tls 	tst	r3, #BYTE0		/* is this byte NUL? */
    138  1.5.4.2  tls 	RETc(eq)			/*   yes, return NULL */
    139  1.5.4.2  tls 
    140  1.5.4.2  tls 	tst	ip, #BYTE1		/* does this byte match? */
    141  1.5.4.2  tls 	addeq	r0, r2, #1		/*   yes, point to it */
    142  1.5.4.2  tls 	RETc(eq)			/*        and return */
    143  1.5.4.2  tls 	tst	r3, #BYTE1		/* is this byte NUL? */
    144  1.5.4.2  tls 	RETc(eq)			/*   yes, return NULL */
    145  1.5.4.2  tls 
    146  1.5.4.2  tls 	tst	ip, #BYTE2		/* does this byte match? */
    147  1.5.4.2  tls 	addeq	r0, r2, #2		/*   yes, point to it */
    148  1.5.4.2  tls 	RETc(eq)			/*        and return */
    149  1.5.4.2  tls 	tst	r3, #BYTE2		/* is this byte NUL? */
    150  1.5.4.2  tls 	RETc(eq)			/*   yes, return NULL */
    151  1.5.4.2  tls 
    152  1.5.4.2  tls 	tst	ip, #BYTE3		/* does this byte match? */
    153  1.5.4.2  tls 	addeq	r0, r2, #3		/*   yes, point to it */
    154  1.5.4.2  tls 	/*
    155  1.5.4.2  tls 	 * Since no NULs and no matches this must be the only case left.
    156  1.5.4.2  tls 	 */
    157  1.5.4.2  tls 	RET				/* return */
    158  1.5.4.2  tls #endif /* _ARM_ARCH_6 */
    159  1.5.4.2  tls END(strchr)
    160