Home | History | Annotate | Line # | Download | only in string
strchr_arm.S revision 1.5.4.3
      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.3 2014/08/19 23:45:12 tls Exp $")
     33  1.5.4.2  tls 
     34  1.5.4.3  tls #if defined(__thumb__) && !defined(_ARM_ARCH_T2)
     35  1.5.4.3  tls #error Only Thumb2 or ARM supported
     36  1.5.4.3  tls #endif
     37  1.5.4.3  tls 
     38  1.5.4.2  tls #ifdef __ARMEL__
     39  1.5.4.2  tls #define	BYTE0	0x000000ff
     40  1.5.4.2  tls #define	BYTE1	0x0000ff00
     41  1.5.4.2  tls #define	BYTE2	0x00ff0000
     42  1.5.4.2  tls #define	BYTE3	0xff000000
     43  1.5.4.2  tls #define	lshi	lsl
     44  1.5.4.3  tls #define	lshis	lsls
     45  1.5.4.2  tls #else
     46  1.5.4.2  tls #define	BYTE0	0xff000000
     47  1.5.4.2  tls #define	BYTE1	0x00ff0000
     48  1.5.4.2  tls #define	BYTE2	0x0000ff00
     49  1.5.4.2  tls #define	BYTE3	0x000000ff
     50  1.5.4.2  tls #define	lshi	lsr
     51  1.5.4.3  tls #define	lshis	lsrs
     52  1.5.4.2  tls #endif
     53  1.5.4.2  tls 
     54  1.5.4.2  tls 	.text
     55  1.5.4.2  tls ENTRY(strchr)
     56  1.5.4.2  tls 	and	r2, r1, #0xff		/* restrict to byte value */
     57  1.5.4.2  tls 1:	tst	r0, #3			/* test for word alignment */
     58  1.5.4.2  tls 	beq	.Lpre_main_loop		/*   finally word aligned */
     59  1.5.4.2  tls 	ldrb	r3, [r0], #1		/* load a byte */
     60  1.5.4.2  tls 	cmp	r3, r2			/* is it a match? */
     61  1.5.4.2  tls 	beq	2f			/*   yes, return current ptr - 1 */
     62  1.5.4.3  tls 	cmp	r3, #0			/* no, was it 0? */
     63  1.5.4.2  tls 	bne	1b			/*   no, try next byte */
     64  1.5.4.3  tls 	movs	r0, #0			/*   yes, set return value to NULL */
     65  1.5.4.2  tls 	RET				/* return */
     66  1.5.4.3  tls 2:	subs	r0, r0, #1		/* back up by one */
     67  1.5.4.2  tls 	RET				/* return */
     68  1.5.4.2  tls .Lpre_main_loop:
     69  1.5.4.2  tls #if defined(_ARM_ARCH_7)
     70  1.5.4.3  tls 	movw	ip, #0xfefe		/* magic constant; 254 in each byte */
     71  1.5.4.3  tls 	movt	ip, #0xfefe		/* magic constant; 254 in each byte */
     72  1.5.4.2  tls #elif defined(_ARM_ARCH_6)
     73  1.5.4.3  tls 	mov	ip, #0xfe		/* put 254 in low byte */
     74  1.5.4.3  tls 	orr	ip, ip, ip, lsl #8	/* move to next byte */
     75  1.5.4.3  tls 	orr	ip, ip, ip, lsl #16	/* move to next halfword */
     76  1.5.4.2  tls #endif /* _ARM_ARCH_6 */
     77  1.5.4.2  tls 	orr	r2, r2, r2, lsl #8	/* move to next byte */
     78  1.5.4.2  tls 	orr	r2, r2, r2, lsl #16	/* move to next halfword */
     79  1.5.4.2  tls .Lmain_loop:
     80  1.5.4.2  tls 	ldr	r3, [r0], #4		/* load next word */
     81  1.5.4.2  tls #if defined(_ARM_ARCH_6)
     82  1.5.4.2  tls 	/*
     83  1.5.4.2  tls 	 * Add 254 to each byte using the UQADD8 (unsigned saturating add 8)
     84  1.5.4.2  tls 	 * instruction.  For every non-NUL byte, the result for that byte will
     85  1.5.4.2  tls 	 * become 255.  For NUL, it will be 254.  When we complement the
     86  1.5.4.2  tls 	 * result, if the result is non-0 then we must have encountered a NUL.
     87  1.5.4.2  tls 	 */
     88  1.5.4.3  tls 	uqadd8	r1, r3, ip		/* NUL detection happens here */
     89  1.5.4.3  tls 	eors	r3, r3, r2		/* xor to clear each lane */
     90  1.5.4.3  tls 	uqadd8	r3, r3, ip		/* char detection happens here */
     91  1.5.4.3  tls 	ands	r3, r3, r1		/* merge results */
     92  1.5.4.2  tls 	mvns	r3, r3			/* is the complement non-0? */
     93  1.5.4.2  tls 	beq	.Lmain_loop		/*    no, then keep going */
     94  1.5.4.2  tls 
     95  1.5.4.2  tls 	/*
     96  1.5.4.2  tls 	 * We've encountered a NUL or a match but we don't know which happened
     97  1.5.4.2  tls 	 * first.
     98  1.5.4.2  tls 	 */
     99  1.5.4.3  tls #if defined(__thumb__) && defined(_ARM_ARCH_T2)
    100  1.5.4.3  tls 	cbz	r2, .Lfind_match	/* searching for NUL? yes, find it */
    101  1.5.4.3  tls #else
    102  1.5.4.3  tls 	cmp	r2, #0			/* searching for NUL? */
    103  1.5.4.2  tls 	beq	.Lfind_match		/*   yes, find the match */
    104  1.5.4.3  tls #endif
    105  1.5.4.3  tls 	mvns	r1, r1			/* did we encounter a NUL? */
    106  1.5.4.2  tls 	beq	.Lfind_match		/*   no, find the match */
    107  1.5.4.3  tls 	bics	r3, r3, r1		/* clear match for the NUL(s) */
    108  1.5.4.2  tls 	beq	.Lnomatch		/*   any left set? if not, no match */
    109  1.5.4.3  tls 	lshis	r1, r1, #8		/* replicate NUL bit to other bytes */
    110  1.5.4.3  tls #ifdef __thumb__
    111  1.5.4.3  tls 	itt	ne
    112  1.5.4.3  tls #endif
    113  1.5.4.3  tls 	orrne	r1, r1, r1, lshi #8	/* replicate NUL bit to other bytes */
    114  1.5.4.3  tls 	orrne	r1, r1, r1, lshi #8	/* replicate NUL bit to other bytes */
    115  1.5.4.3  tls 	bics	r3, r3, r1		/* clear any match bits after the NUL */
    116  1.5.4.2  tls 	beq	.Lnomatch		/*   any left set? if not, no match */
    117  1.5.4.2  tls .Lfind_match:
    118  1.5.4.2  tls #ifdef __ARMEL__
    119  1.5.4.2  tls 	rev	r3, r3			/* we want this in BE for the CLZ */
    120  1.5.4.2  tls #endif
    121  1.5.4.2  tls 	clz	r3, r3			/* count how many leading zeros */
    122  1.5.4.2  tls 	add	r0, r0, r3, lsr #3	/* divide that by 8 and add to count */
    123  1.5.4.3  tls 	subs	r0, r0, #4		/* compensate for the post-inc */
    124  1.5.4.2  tls 	RET
    125  1.5.4.2  tls .Lnomatch:
    126  1.5.4.3  tls 	movs	r0, #0
    127  1.5.4.2  tls 	RET
    128  1.5.4.2  tls #else
    129  1.5.4.2  tls 	/*
    130  1.5.4.2  tls 	 * No fancy shortcuts so just test each byte lane for a NUL.
    131  1.5.4.2  tls 	 * (other tests for NULs in a word take more instructions/cycles).
    132  1.5.4.2  tls 	 */
    133  1.5.4.3  tls 	eor	r1, r3, r2		/* xor .. */
    134  1.5.4.2  tls 	tst	r3, #BYTE0		/* is this byte NUL? */
    135  1.5.4.3  tls 	tstne	r1, #BYTE0		/*   no, does this byte match? */
    136  1.5.4.2  tls 	tstne	r3, #BYTE1		/*   no, is this byte NUL? */
    137  1.5.4.3  tls 	tstne	r1, #BYTE1		/*   no, does this byte match? */
    138  1.5.4.2  tls 	tstne	r3, #BYTE2		/*   no, is this byte NUL? */
    139  1.5.4.3  tls 	tstne	r1, #BYTE2		/*   no, does this byte match? */
    140  1.5.4.2  tls 	tstne	r3, #BYTE3		/*   no, is this byte NUL? */
    141  1.5.4.3  tls 	tstne	r1, #BYTE3		/*   no, does this byte match? */
    142  1.5.4.2  tls 	bne	.Lmain_loop
    143  1.5.4.2  tls 
    144  1.5.4.2  tls 	sub	r2, r0, #4		/* un post-inc */
    145  1.5.4.2  tls 	mov	r0, #0			/* assume no match */
    146  1.5.4.2  tls 
    147  1.5.4.3  tls 	tst	r1, #BYTE0		/* does this byte match? */
    148  1.5.4.2  tls 	moveq	r0, r2			/*   yes, point to it */
    149  1.5.4.2  tls 	RETc(eq)			/*        and return */
    150  1.5.4.2  tls 	tst	r3, #BYTE0		/* is this byte NUL? */
    151  1.5.4.2  tls 	RETc(eq)			/*   yes, return NULL */
    152  1.5.4.2  tls 
    153  1.5.4.3  tls 	tst	r1, #BYTE1		/* does this byte match? */
    154  1.5.4.2  tls 	addeq	r0, r2, #1		/*   yes, point to it */
    155  1.5.4.2  tls 	RETc(eq)			/*        and return */
    156  1.5.4.2  tls 	tst	r3, #BYTE1		/* is this byte NUL? */
    157  1.5.4.2  tls 	RETc(eq)			/*   yes, return NULL */
    158  1.5.4.2  tls 
    159  1.5.4.3  tls 	tst	r1, #BYTE2		/* does this byte match? */
    160  1.5.4.2  tls 	addeq	r0, r2, #2		/*   yes, point to it */
    161  1.5.4.2  tls 	RETc(eq)			/*        and return */
    162  1.5.4.2  tls 	tst	r3, #BYTE2		/* is this byte NUL? */
    163  1.5.4.2  tls 	RETc(eq)			/*   yes, return NULL */
    164  1.5.4.2  tls 
    165  1.5.4.3  tls 	tst	r1, #BYTE3		/* does this byte match? */
    166  1.5.4.2  tls 	addeq	r0, r2, #3		/*   yes, point to it */
    167  1.5.4.2  tls 	/*
    168  1.5.4.2  tls 	 * Since no NULs and no matches this must be the only case left.
    169  1.5.4.2  tls 	 */
    170  1.5.4.2  tls 	RET				/* return */
    171  1.5.4.2  tls #endif /* _ARM_ARCH_6 */
    172  1.5.4.2  tls END(strchr)
    173