Home | History | Annotate | Line # | Download | only in string
strrchr.S revision 1.2
      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.2       dsl 	RCSID("$NetBSD: strrchr.S,v 1.2 2009/07/17 19:37:57 dsl Exp $")
     10  1.1  christos #endif
     11  1.1  christos 
     12  1.1  christos ENTRY(strrchr)
     13  1.1  christos 	pushl	%esi
     14  1.1  christos 	pushl	%edi
     15  1.1  christos 	pushl	%ebx
     16  1.1  christos 	movl	16(%esp),%edx
     17  1.1  christos 	movzbl	20(%esp),%ecx
     18  1.1  christos 
     19  1.1  christos 	/* zero return value */
     20  1.1  christos 	xorl	%eax,%eax
     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,%dl
     28  1.1  christos 	je	.Lword_aligned
     29  1.1  christos 	movb	(%edx),%bl
     30  1.1  christos 	cmpb	%cl,%bl
     31  1.1  christos 	jne	1f
     32  1.1  christos 	movl	%edx,%eax
     33  1.1  christos 1:	testb	%bl,%bl
     34  1.1  christos 	je	.Ldone
     35  1.1  christos 	incl	%edx
     36  1.1  christos 	jmp	.Lalign
     37  1.1  christos 
     38  1.1  christos .Lword_aligned:
     39  1.1  christos 	/* copy char to all bytes in word */
     40  1.1  christos 	movb	%cl,%ch
     41  1.1  christos 	movl	%ecx,%edi
     42  1.1  christos 	sall	$16,%ecx
     43  1.1  christos 	orl	%edi,%ecx
     44  1.1  christos 
     45  1.1  christos 	/* Check whether any byte in the word is equal to ch or 0.  */
     46  1.1  christos 	_ALIGN_TEXT
     47  1.1  christos .Lloop:
     48  1.1  christos 	movl	(%edx),%ebx
     49  1.1  christos 	addl	$4,%edx
     50  1.1  christos 	movl	%ebx,%esi
     51  1.1  christos 	leal	-0x01010101(%ebx),%edi
     52  1.1  christos 	xorl	%ecx,%esi
     53  1.1  christos 	subl	$0x01010101,%esi
     54  1.1  christos 	orl	%esi,%edi
     55  1.1  christos 	testl	$0x80808080,%edi
     56  1.1  christos 	je	.Lloop
     57  1.1  christos 
     58  1.1  christos 	/*
     59  1.1  christos 	 * In rare cases, the above loop may exit prematurely. We must
     60  1.1  christos 	 * return to the loop if none of the bytes in the word match
     61  1.1  christos 	 * ch or are equal to 0.
     62  1.1  christos 	 */
     63  1.1  christos 
     64  1.1  christos 	_ALIGN_TEXT
     65  1.1  christos 	cmpb	%cl,%bl		/* 1st byte == ch? */
     66  1.1  christos 	jne	1f
     67  1.1  christos 	leal	-4(%edx),%eax
     68  1.1  christos 1:	testb	%bl,%bl		/* 1st byte == 0? */
     69  1.1  christos 	je	.Ldone
     70  1.1  christos 
     71  1.1  christos 	cmpb	%cl,%bh		/* 2nd byte == ch? */
     72  1.1  christos 	jne	1f
     73  1.1  christos 	leal	-3(%edx),%eax
     74  1.1  christos 1:	testb	%bh,%bh		/* 2nd byte == 0? */
     75  1.1  christos 	je	.Ldone
     76  1.1  christos 
     77  1.1  christos 	shrl	$16,%ebx
     78  1.1  christos 	cmpb	%cl,%bl		/* 3rd byte == ch? */
     79  1.1  christos 	jne	1f
     80  1.1  christos 	leal	-2(%edx),%eax
     81  1.1  christos 1:	testb	%bl,%bl		/* 3rd byte == 0? */
     82  1.1  christos 	je	.Ldone
     83  1.1  christos 
     84  1.1  christos 	cmpb	%cl,%bh		/* 4th byte == ch? */
     85  1.1  christos 	jne	1f
     86  1.1  christos 	leal	-1(%edx),%eax
     87  1.1  christos 1:	testb	%bh,%bh		/* 4th byte == 0? */
     88  1.1  christos 	jne	.Lloop
     89  1.1  christos 
     90  1.1  christos .Ldone:
     91  1.1  christos 	popl	%ebx
     92  1.1  christos 	popl	%edi
     93  1.1  christos 	popl	%esi
     94  1.1  christos 	ret
     95  1.2       dsl 
     96  1.2       dsl STRONG_ALIAS(rindex,strrchr)
     97