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