Home | History | Annotate | Line # | Download | only in string
strchr.S revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * Written by J.T. Conklin <jtc (at) acorntoolworks.com>
      3  1.1  christos  * Public domain.
      4  1.1  christos  */
      5  1.1  christos 
      6  1.1  christos #include <machine/asm.h>
      7  1.1  christos 
      8  1.1  christos #if defined(LIBC_SCCS)
      9  1.1  christos 	RCSID("$NetBSD: strchr.S,v 1.1 2005/12/20 19:28:51 christos Exp $")
     10  1.1  christos #endif
     11  1.1  christos 
     12  1.1  christos #ifdef INDEX
     13  1.1  christos ENTRY(index)
     14  1.1  christos #else
     15  1.1  christos ENTRY(strchr)
     16  1.1  christos #endif
     17  1.1  christos 	movzbq	%sil,%rcx
     18  1.1  christos 
     19  1.1  christos 	/*
     20  1.1  christos 	 * Align to word boundary.
     21  1.1  christos 	 * Consider unrolling loop?
     22  1.1  christos 	 */
     23  1.1  christos .Lalign:
     24  1.1  christos 	testb	$7,%dil
     25  1.1  christos 	je	.Lword_aligned
     26  1.1  christos 	movb	(%rdi),%dl
     27  1.1  christos 	cmpb	%cl,%dl
     28  1.1  christos 	je	.Ldone
     29  1.1  christos 	incq	%rdi
     30  1.1  christos 	testb	%dl,%dl
     31  1.1  christos 	jne	.Lalign
     32  1.1  christos 	jmp	.Lzero
     33  1.1  christos 
     34  1.1  christos .Lword_aligned:
     35  1.1  christos 	/* copy char to all bytes in word */
     36  1.1  christos 	movb	%cl,%ch
     37  1.1  christos 	movq	%rcx,%rdx
     38  1.1  christos 	salq	$16,%rcx
     39  1.1  christos 	orq	%rdx,%rcx
     40  1.1  christos 	movq	%rcx,%rdx
     41  1.1  christos 	salq	$32,%rcx
     42  1.1  christos 	orq	%rdx,%rcx
     43  1.1  christos 
     44  1.1  christos 	movabsq	$0x0101010101010101,%r8
     45  1.1  christos 	movabsq	$0x8080808080808080,%r9
     46  1.1  christos 
     47  1.1  christos 	/* Check whether any byte in the word is equal to ch or 0. */
     48  1.1  christos 	_ALIGN_TEXT
     49  1.1  christos .Lloop:
     50  1.1  christos 	movq	(%rdi),%rdx
     51  1.1  christos 	addq	$8,%rdi
     52  1.1  christos 	movq	%rdx,%rsi
     53  1.1  christos 	subq	%r8,%rdx
     54  1.1  christos 	xorq	%rcx,%rsi
     55  1.1  christos 	subq	%r8,%rsi
     56  1.1  christos 	orq	%rsi,%rdx
     57  1.1  christos 	testq	%r9,%rdx
     58  1.1  christos 	je	.Lloop
     59  1.1  christos 
     60  1.1  christos 	/*
     61  1.1  christos 	 * In rare cases, the above loop may exit prematurely. We must
     62  1.1  christos 	 * return to the loop if none of the bytes in the word match
     63  1.1  christos 	 * ch or are equal to 0.
     64  1.1  christos 	 */
     65  1.1  christos 
     66  1.1  christos 	movb	-8(%rdi),%dl
     67  1.1  christos 	cmpb	%cl,%dl		/* 1st byte == ch? */
     68  1.1  christos 	jne	1f
     69  1.1  christos 	subq	$8,%rdi
     70  1.1  christos 	jmp	.Ldone
     71  1.1  christos 1:	testb	%dl,%dl		/* 1st byte == 0? */
     72  1.1  christos 	je	.Lzero
     73  1.1  christos 
     74  1.1  christos 	movb	-7(%rdi),%dl
     75  1.1  christos 	cmpb	%cl,%dl		/* 2nd byte == ch? */
     76  1.1  christos 	jne	1f
     77  1.1  christos 	subq	$7,%rdi
     78  1.1  christos 	jmp	.Ldone
     79  1.1  christos 1:	testb	%dl,%dl		/* 2nd byte == 0? */
     80  1.1  christos 	je	.Lzero
     81  1.1  christos 
     82  1.1  christos 	movb	-6(%rdi),%dl
     83  1.1  christos 	cmpb	%cl,%dl		/* 3rd byte == ch? */
     84  1.1  christos 	jne	1f
     85  1.1  christos 	subq	$6,%rdi
     86  1.1  christos 	jmp	.Ldone
     87  1.1  christos 1:	testb	%dl,%dl		/* 3rd byte == 0? */
     88  1.1  christos 	je	.Lzero
     89  1.1  christos 
     90  1.1  christos 	movb	-5(%rdi),%dl
     91  1.1  christos 	cmpb	%cl,%dl		/* 4th byte == ch? */
     92  1.1  christos 	jne	1f
     93  1.1  christos 	subq	$5,%rdi
     94  1.1  christos 	jmp	.Ldone
     95  1.1  christos 1:	testb	%dl,%dl		/* 4th byte == 0? */
     96  1.1  christos 	je	.Lzero
     97  1.1  christos 
     98  1.1  christos 	movb	-4(%rdi),%dl
     99  1.1  christos 	cmpb	%cl,%dl		/* 5th byte == ch? */
    100  1.1  christos 	jne	1f
    101  1.1  christos 	subq	$4,%rdi
    102  1.1  christos 	jmp	.Ldone
    103  1.1  christos 1:	testb	%dl,%dl		/* 5th byte == 0? */
    104  1.1  christos 	je	.Lzero
    105  1.1  christos 
    106  1.1  christos 	movb	-3(%rdi),%dl
    107  1.1  christos 	cmpb	%cl,%dl		/* 6th byte == ch? */
    108  1.1  christos 	jne	1f
    109  1.1  christos 	subq	$3,%rdi
    110  1.1  christos 	jmp	.Ldone
    111  1.1  christos 1:	testb	%dl,%dl		/* 6th byte == 0? */
    112  1.1  christos 	je	.Lzero
    113  1.1  christos 
    114  1.1  christos 	movb	-2(%rdi),%dl
    115  1.1  christos 	cmpb	%cl,%dl		/* 7th byte == ch? */
    116  1.1  christos 	jne	1f
    117  1.1  christos 	subq	$2,%rdi
    118  1.1  christos 	jmp	.Ldone
    119  1.1  christos 1:	testb	%dl,%dl		/* 7th byte == 0? */
    120  1.1  christos 	je	.Lzero
    121  1.1  christos 
    122  1.1  christos 	movb	-1(%rdi),%dl
    123  1.1  christos 	cmpb	%cl,%dl		/* 8th byte == ch? */
    124  1.1  christos 	jne	1f
    125  1.1  christos 	subq	$1,%rdi
    126  1.1  christos 	jmp	.Ldone
    127  1.1  christos 1:	testb	%dl,%dl		/* 8th byte == 0? */
    128  1.1  christos 	jne	.Lloop
    129  1.1  christos 
    130  1.1  christos .Lzero:
    131  1.1  christos 	/* If a ch wasn't found, return 0. */
    132  1.1  christos 	xorq	%rdi,%rdi
    133  1.1  christos 
    134  1.1  christos .Ldone:
    135  1.1  christos 	movq	%rdi,%rax
    136  1.1  christos 	ret
    137