Home | History | Annotate | Line # | Download | only in string
strchr.S revision 1.3
      1  1.3       dsl /*	$NetBSD: strchr.S,v 1.3 2009/07/18 11:41:23 dsl Exp $	*/
      2  1.3       dsl 
      3  1.3       dsl /*-
      4  1.3       dsl  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.3       dsl  * All rights reserved.
      6  1.3       dsl  *
      7  1.3       dsl  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3       dsl  * by David Laight.
      9  1.3       dsl  *
     10  1.3       dsl  * Redistribution and use in source and binary forms, with or without
     11  1.3       dsl  * modification, are permitted provided that the following conditions
     12  1.3       dsl  * are met:
     13  1.3       dsl  * 1. Redistributions of source code must retain the above copyright
     14  1.3       dsl  *    notice, this list of conditions and the following disclaimer.
     15  1.3       dsl  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3       dsl  *    notice, this list of conditions and the following disclaimer in the
     17  1.3       dsl  *    documentation and/or other materials provided with the distribution.
     18  1.3       dsl  *
     19  1.3       dsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.3       dsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.3       dsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.3       dsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.3       dsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.3       dsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.3       dsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.3       dsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.3       dsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.3       dsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.3       dsl  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.3       dsl /* See comments in strlen.S about checking words for byte values */
     33  1.3       dsl 
     34  1.1  christos #include <machine/asm.h>
     35  1.1  christos 
     36  1.1  christos #if defined(LIBC_SCCS)
     37  1.3       dsl 	RCSID("$NetBSD: strchr.S,v 1.3 2009/07/18 11:41:23 dsl Exp $")
     38  1.1  christos #endif
     39  1.1  christos 
     40  1.3       dsl /*
     41  1.3       dsl  * On entry %rdi is the buffer and the low byte of %rsi (%sil) the
     42  1.3       dsl  * character to search for.
     43  1.3       dsl  *
     44  1.3       dsl  * Registers %rdx, %rcx, %r8-%r11 and %rax are also usable
     45  1.3       dsl  */
     46  1.3       dsl 
     47  1.3       dsl /* Uncomment below to get regression test to run this version but
     48  1.3       dsl  * have everything else use the trivial one below. */
     49  1.3       dsl /* #define TEST_STRCHR */
     50  1.3       dsl 
     51  1.3       dsl #ifdef TEST_STRCHR
     52  1.3       dsl ENTRY(test_strchr)
     53  1.3       dsl #else
     54  1.1  christos ENTRY(strchr)
     55  1.3       dsl #endif
     56  1.3       dsl 	movabsq	$0x0101010101010101,%r8
     57  1.1  christos 
     58  1.3       dsl 	movzbq	%sil,%rdx	/* value to search for (c) */
     59  1.3       dsl 	imul	$0x80,%r8,%r9	/* 0x8080808080808080 */
     60  1.3       dsl 	imul	%r8,%rdx	/* (c) copied to all bytes */
     61  1.3       dsl 	test	$7,%dil
     62  1.3       dsl 	jnz	20f		/* jump if misaligned */
     63  1.3       dsl 
     64  1.3       dsl 	_ALIGN_TEXT		/* one byte nop */
     65  1.3       dsl 1:
     66  1.3       dsl 	movq	(%rdi),%rax	/* bytes to check (x) */
     67  1.3       dsl 2:
     68  1.3       dsl 	addq	$8,%rdi
     69  1.3       dsl 	mov	%rax,%r10
     70  1.3       dsl 	mov	%rax,%r11	/* for 'char' check */
     71  1.3       dsl 	not	%r10		/* invert of data (~x) */
     72  1.3       dsl 
     73  1.3       dsl 	xorq	%rdx,%r11	/* convert 'char' test to one for NUL */
     74  1.3       dsl 	subq	%r8,%rax	/* x - 0x10 */
     75  1.3       dsl 	movq	%r10,%rsi	/* ~x */
     76  1.3       dsl 	subq	%r8,%r11	/* (x ^ c) - 0x10 */
     77  1.3       dsl /*
     78  1.3       dsl  * Here we could check ((x - 0x10) | ((x ^ c) - 0x10)) & 0x80
     79  1.3       dsl  * and short-circuit the case where no top bits are set, and
     80  1.3       dsl  * we continue the loop.
     81  1.3       dsl  * However it needs 3 more clocks that are difficult to interleave
     82  1.3       dsl  * in the existing dependency chain ...
     83  1.3       dsl  */
     84  1.3       dsl 	andq	%r9,%rax	/* (x - 0x10) & 0x80 */
     85  1.3       dsl 	xorq	%rdx,%rsi	/* c ^ ~x == ~(c ^ x) */
     86  1.3       dsl 	andq	%r9,%r11	/* ((x ^ c) - 0x10) & 0x80 */
     87  1.3       dsl 	andq	%r10,%rax	/* (x - 0x10) & 0x80 & ~x */
     88  1.3       dsl 	jne	10f		/* jump if string ends */
     89  1.3       dsl 	andq	%rsi,%r11	/* ((x ^ c) - 0x10) & 0x80 & ~(x ^ c) */
     90  1.3       dsl 	je	1b		/* jump if no match */
     91  1.3       dsl 
     92  1.3       dsl 	/* Found char, since LE can use bit scan */
     93  1.3       dsl 	bsf	%r11,%r11	/* 7, 15, 23 ... 63 */
     94  1.3       dsl 8:	shr	$3,%r11		/* 0, 1, 2 .. 7 */
     95  1.3       dsl 	lea	-8(%r11,%rdi),%rax
     96  1.3       dsl 	ret
     97  1.1  christos 
     98  1.3       dsl /* End of string, check whether char is before NUL */
     99  1.3       dsl 	_ALIGN_TEXT		/* adds three byte nop */
    100  1.3       dsl 10:
    101  1.3       dsl 	bsf	%rax,%rax	/* count to NUL */
    102  1.3       dsl 	andq	%rsi,%r11	/* check for char in last 8 bytes */
    103  1.3       dsl 	je	11f
    104  1.3       dsl 	bsf	%r11,%r11	/* NUL and char - see which was first */
    105  1.3       dsl 	cmp	%r11,%rax
    106  1.3       dsl 	jae	8b		/* return 'found' if same - searching for NUL */
    107  1.3       dsl 11:	xor	%eax,%eax	/* char not found */
    108  1.3       dsl 	ret
    109  1.1  christos 
    110  1.3       dsl /* Source misaligned: read aligned word and make low bytes invalid */
    111  1.3       dsl /* I (dsl) think a _ALIGN_TEXT here will slow things down! */
    112  1.3       dsl 20:
    113  1.3       dsl 	xor	%rcx,%rcx
    114  1.3       dsl 	mov	%rdx,%rsi	/* repeated char pattern (c) */
    115  1.3       dsl 	sub	%dil,%cl	/* Convert low address values 1..7 */
    116  1.3       dsl 	and	$7,%cl		/* to 7..1 */
    117  1.3       dsl 	and	$~7,%dil	/* move address to start of word */
    118  1.3       dsl 	shl	$3,%cl		/* now 56, 48 ... 16, 8 */
    119  1.3       dsl 	movq	(%rdi),%rax	/* aligned word containing first data */
    120  1.3       dsl 	neg	%rsi		/* generate ~c (not doesn't set flags) */
    121  1.3       dsl 	dec	%rsi
    122  1.3       dsl 	je	22f		/* searching for 0xff */
    123  1.3       dsl 21:	shr	%cl,%rsi	/* ~c in low bytes */
    124  1.3       dsl 	or	%rsi,%rax	/* set some bits making low bytes invalid */
    125  1.3       dsl 	jmp	2b
    126  1.3       dsl 
    127  1.3       dsl /* We are searching for 0xff, so can't use ~pattern for invalid value */
    128  1.3       dsl 22:
    129  1.3       dsl 	mov	%r8,%r10	/* 0x01 pattern */
    130  1.3       dsl 	lea	(%r8,%r8),%rsi	/* 0x02 - bits gets set (above) */
    131  1.3       dsl 	not	%r10		/* now 0xfe */
    132  1.3       dsl 	sar	%cl,%r10	/* top bytes 0xff */
    133  1.3       dsl 	and	%r10,%rax	/* clear lsb from unwanted low bytes */
    134  1.3       dsl 	jmp	21b
    135  1.1  christos 
    136  1.3       dsl #ifdef TEST_STRCHR
    137  1.3       dsl /* Trivial version for bug-fixing above */
    138  1.3       dsl ENTRY(strchr)
    139  1.3       dsl 	movq	%rsi,%rdx
    140  1.3       dsl 	movq	%rdi,%rsi
    141  1.3       dsl 1:
    142  1.3       dsl 	lodsb
    143  1.3       dsl 	cmp	%al,%dl
    144  1.3       dsl 	je	2f
    145  1.3       dsl 	test	%al,%al
    146  1.3       dsl 	jne	1b
    147  1.3       dsl 	xor	%eax,%eax
    148  1.3       dsl 	ret
    149  1.3       dsl 2:	lea	-1(%rsi),%rax
    150  1.1  christos 	ret
    151  1.3       dsl #endif
    152  1.3       dsl 
    153  1.2       dsl STRONG_ALIAS(index,strchr)
    154