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:49 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 	pushl	%esi
     18  1.1  christos 	pushl	%ebx
     19  1.1  christos 	movl	12(%esp),%eax
     20  1.1  christos 	movzbl	16(%esp),%ecx
     21  1.1  christos 
     22  1.1  christos 	/*
     23  1.1  christos 	 * Align to word boundary.
     24  1.1  christos 	 * Consider unrolling loop?
     25  1.1  christos 	 */
     26  1.1  christos .Lalign:
     27  1.1  christos 	testb	$3,%al
     28  1.1  christos 	je	.Lword_aligned
     29  1.1  christos 	movb	(%eax),%bl
     30  1.1  christos 	cmpb	%cl,%bl
     31  1.1  christos 	je	.Ldone
     32  1.1  christos 	testb	%bl,%bl
     33  1.1  christos 	je	.Lzero
     34  1.1  christos 	incl	%eax
     35  1.1  christos 	jmp	.Lalign
     36  1.1  christos 
     37  1.1  christos .Lword_aligned:
     38  1.1  christos 	/* copy char to all bytes in word */
     39  1.1  christos 	movb	%cl,%ch
     40  1.1  christos 	movl	%ecx,%edx
     41  1.1  christos 	sall	$16,%ecx
     42  1.1  christos 	orl	%edx,%ecx
     43  1.1  christos 
     44  1.1  christos 	/* Check whether any byte in the word is equal to ch or 0. */
     45  1.1  christos 	_ALIGN_TEXT
     46  1.1  christos .Lloop:
     47  1.1  christos 	movl	(%eax),%ebx
     48  1.1  christos 	addl	$4,%eax
     49  1.1  christos 	movl	%ebx,%esi
     50  1.1  christos 	leal	-0x01010101(%ebx),%edx
     51  1.1  christos 	xorl	%ecx,%esi
     52  1.1  christos 	subl	$0x01010101,%esi
     53  1.1  christos 	orl	%esi,%edx
     54  1.1  christos 	testl	$0x80808080,%edx
     55  1.1  christos 	je	.Lloop
     56  1.1  christos 
     57  1.1  christos 	/*
     58  1.1  christos 	 * In rare cases, the above loop may exit prematurely. We must
     59  1.1  christos 	 * return to the loop if none of the bytes in the word match
     60  1.1  christos 	 * ch or are equal to 0.
     61  1.1  christos 	 */
     62  1.1  christos 
     63  1.1  christos 	/*
     64  1.1  christos 	 * Alignment here avoids a stall on the Athlon, even though
     65  1.1  christos 	 * it's not a branch target.
     66  1.1  christos 	 */
     67  1.1  christos 
     68  1.1  christos 	_ALIGN_TEXT
     69  1.1  christos 	cmpb	%cl,%bl		/* 1st byte == ch? */
     70  1.1  christos 	jne	1f
     71  1.1  christos 	subl	$4,%eax
     72  1.1  christos 	jmp	.Ldone
     73  1.1  christos 1:	testb	%bl,%bl		/* 1st byte == 0? */
     74  1.1  christos 	je	.Lzero
     75  1.1  christos 
     76  1.1  christos 	cmpb	%cl,%bh		/* 2nd byte == ch? */
     77  1.1  christos 	jne	1f
     78  1.1  christos 	subl	$3,%eax
     79  1.1  christos 	jmp	.Ldone
     80  1.1  christos 1:	testb	%bh,%bh		/* 2nd byte == 0? */
     81  1.1  christos 	je	.Lzero
     82  1.1  christos 
     83  1.1  christos 	shrl	$16,%ebx
     84  1.1  christos 	cmpb	%cl,%bl		/* 3rd byte == ch? */
     85  1.1  christos 	jne	1f
     86  1.1  christos 	subl	$2,%eax
     87  1.1  christos 	jmp	.Ldone
     88  1.1  christos 1:	testb	%bl,%bl		/* 3rd byte == 0? */
     89  1.1  christos 	je	.Lzero
     90  1.1  christos 
     91  1.1  christos 	cmpb	%cl,%bh		/* 4th byte == ch? */
     92  1.1  christos 	jne	1f
     93  1.1  christos 	decl	%eax
     94  1.1  christos 	jmp	.Ldone
     95  1.1  christos 1:	testb	%bh,%bh		/* 4th byte == 0? */
     96  1.1  christos 	jne	.Lloop
     97  1.1  christos 
     98  1.1  christos .Lzero:
     99  1.1  christos 	/* If a ch wasn't found, return 0. */
    100  1.1  christos 	xorl	%eax,%eax
    101  1.1  christos 
    102  1.1  christos .Ldone:
    103  1.1  christos 	popl	%ebx
    104  1.1  christos 	popl	%esi
    105  1.1  christos 	ret
    106