Home | History | Annotate | Line # | Download | only in string
      1  1.6  jakllsch /*	$NetBSD: memchr.S,v 1.6 2014/03/22 19:16:34 jakllsch Exp $	*/
      2  1.4  christos 
      3  1.4  christos /*-
      4  1.4  christos  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.4  christos  * All rights reserved.
      6  1.4  christos  *
      7  1.4  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.4  christos  * by David Laight.
      9  1.4  christos  *
     10  1.4  christos  * Redistribution and use in source and binary forms, with or without
     11  1.4  christos  * modification, are permitted provided that the following conditions
     12  1.4  christos  * are met:
     13  1.4  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.4  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.4  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.4  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.4  christos  *    documentation and/or other materials provided with the distribution.
     18  1.4  christos  *
     19  1.4  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.4  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.4  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.4  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.4  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.4  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.4  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.4  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.4  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.4  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.4  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #include <machine/asm.h>
     33  1.1  christos 
     34  1.1  christos #if defined(LIBC_SCCS)
     35  1.6  jakllsch 	RCSID("$NetBSD: memchr.S,v 1.6 2014/03/22 19:16:34 jakllsch Exp $")
     36  1.1  christos #endif
     37  1.1  christos 
     38  1.4  christos /*
     39  1.4  christos  * The instruction sequences used try to avoid data dependencies
     40  1.4  christos  * between adjacent instructions (to allow parallel execution).
     41  1.4  christos  * The 'imul' for %r9 could be put into the delay following the
     42  1.4  christos  * memory read (ie inside the loop) at no obvious cost - except
     43  1.4  christos  * that the loop is currently exactly 32 bytes - 2 fetch blocks!.
     44  1.4  christos  *
     45  1.4  christos  * I don't think aligning any of the other branch targets is useful.
     46  1.4  christos  */
     47  1.4  christos 
     48  1.3  christos ENTRY(memchr)
     49  1.4  christos 	movabsq	$0x0101010101010101,%r8
     50  1.4  christos 	lea	(%rdi,%rdx),%r10	/* limit of buffer to scan */
     51  1.4  christos 	movzbq	%sil,%rsi	/* mask high bits! */
     52  1.3  christos 
     53  1.4  christos 	/* 'directpath' imuls can execute 3 at a time ... (amd) */
     54  1.4  christos 	imul	%r8,%rsi	/* search byte replicated in word */
     55  1.4  christos 	imul	$0x80,%r8,%r9	/* 0x8080808080808080 */
     56  1.4  christos 	test	$7,%dil
     57  1.4  christos 	jnz	20f		/* jump if misaligned */
     58  1.4  christos 	jmp	1f		/* jump to avoid 4 nops (13 bytes) in gap */
     59  1.4  christos 
     60  1.4  christos 	_ALIGN_TEXT		/* entire loop now in 32 aligned bytes */
     61  1.4  christos 1:
     62  1.4  christos 	cmpq	%r10,%rdi	/* end of buffer ? */
     63  1.4  christos 	jae	30f		/* jump if so */
     64  1.2       dsl 
     65  1.4  christos 	movq	(%rdi),%rax	/* value to check */
     66  1.3  christos 	addq	$8,%rdi
     67  1.4  christos 	xorq	%rsi,%rax	/* now looking for zeros */
     68  1.5       dsl 2:
     69  1.4  christos 	mov	%rax,%rcx
     70  1.4  christos 	subq	%r8,%rax	/* x - 0x01 */
     71  1.4  christos 	not	%rcx
     72  1.4  christos 	andq	%r9,%rax	/* (x - 0x01) & 0x80 */
     73  1.4  christos 	andq	%rcx,%rax	/* ((x - 0x01) & 0x80) & ~x */
     74  1.4  christos 	je	1b		/* jump if not found */
     75  1.4  christos 
     76  1.4  christos /* Found byte in word, get its address */
     77  1.4  christos 	bsf	%rax,%rax
     78  1.4  christos 	shr	$3,%eax
     79  1.4  christos 	lea	-8(%rax,%rdi),%rax
     80  1.4  christos 	cmpq	%r10,%rax	/* need to check not beyond buffer */
     81  1.4  christos 	jae	30f
     82  1.4  christos 	rep
     83  1.4  christos 	ret			/* amd - no ret after jmp */
     84  1.4  christos 
     85  1.5       dsl /* Input misaligned, read aligned and make low bytes invalid */
     86  1.4  christos 20:
     87  1.4  christos 	mov	%dil,%cl	/* misalignment amount 1..7 (+high bits )*/
     88  1.5       dsl 	and	$~7,%dil	/* %rdi now start of word */
     89  1.4  christos 	test	%rdx,%rdx	/* zero length, don't read */
     90  1.4  christos 	jz	30f
     91  1.4  christos 
     92  1.5       dsl 	neg	%cl		/* 7..1 (+high bits) */
     93  1.5       dsl 	mov	(%rdi),%rax	/* word containing first byte */
     94  1.5       dsl 	addq	$8,%rdi
     95  1.5       dsl 	and	$7,%cl		/* 7..1 */
     96  1.5       dsl 
     97  1.5       dsl 	mov	%r8,%r11	/* any value with bits in each byte */
     98  1.5       dsl 	shl	$3,%cl		/* 56..8 */
     99  1.5       dsl 	xorq	%rsi,%rax	/* now looking for zeros */
    100  1.4  christos 
    101  1.5       dsl 	/* Set low bytes non-zero */
    102  1.5       dsl 	shr	%cl,%r11	/* non-zero in unwanted bytes */
    103  1.4  christos 	or	%r11,%rax	/* low bytes now set */
    104  1.4  christos 	jmp	2b
    105  1.4  christos 
    106  1.4  christos /* Not found */
    107  1.4  christos 30:	xorq	%rax,%rax
    108  1.1  christos 	ret
    109  1.6  jakllsch END(memchr)
    110