Blame sysdeps/i386/strrchr.S

Packit 6c4009
/* strrchr (str, ch) -- Return pointer to last occurrence of CH in STR.
Packit 6c4009
   For Intel 80x86, x>=3.
Packit 6c4009
   Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>
Packit 6c4009
   Some optimisations by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include "asm-syntax.h"
Packit 6c4009
Packit 6c4009
#define PARMS	4+8	/* space for 2 saved regs */
Packit 6c4009
#define RTN	PARMS
Packit 6c4009
#define STR	RTN
Packit 6c4009
#define CHR	STR+4
Packit 6c4009
Packit 6c4009
	.text
Packit 6c4009
ENTRY (strrchr)
Packit 6c4009
Packit 6c4009
	pushl %edi		/* Save callee-safe registers used here.  */
Packit 6c4009
	cfi_adjust_cfa_offset (4)
Packit 6c4009
	cfi_rel_offset (edi, 0)
Packit 6c4009
	pushl %esi
Packit 6c4009
	cfi_adjust_cfa_offset (4)
Packit 6c4009
Packit 6c4009
	xorl %eax, %eax
Packit 6c4009
	movl STR(%esp), %esi
Packit 6c4009
	cfi_rel_offset (esi, 0)
Packit 6c4009
	movl CHR(%esp), %ecx
Packit 6c4009
Packit 6c4009
	/* At the moment %ecx contains C.  What we need for the
Packit 6c4009
	   algorithm is C in all bytes of the dword.  Avoid
Packit 6c4009
	   operations on 16 bit words because these require an
Packit 6c4009
	   prefix byte (and one more cycle).  */
Packit 6c4009
	movb %cl, %ch		/* now it is 0|0|c|c */
Packit 6c4009
	movl %ecx, %edx
Packit 6c4009
	shll $16, %ecx		/* now it is c|c|0|0 */
Packit 6c4009
	movw %dx, %cx		/* and finally c|c|c|c */
Packit 6c4009
Packit 6c4009
	/* Before we start with the main loop we process single bytes
Packit 6c4009
	   until the source pointer is aligned.  This has two reasons:
Packit 6c4009
	   1. aligned 32-bit memory access is faster
Packit 6c4009
	   and (more important)
Packit 6c4009
	   2. we process in the main loop 32 bit in one step although
Packit 6c4009
	      we don't know the end of the string.  But accessing at
Packit 6c4009
	      4-byte alignment guarantees that we never access illegal
Packit 6c4009
	      memory if this would not also be done by the trivial
Packit 6c4009
	      implementation (this is because all processor inherent
Packit 6c4009
	      boundaries are multiples of 4.  */
Packit 6c4009
Packit 6c4009
	testl $3, %esi		/* correctly aligned ? */
Packit 6c4009
	jz L(19)		/* yes => begin loop */
Packit 6c4009
	movb (%esi), %dl	/* load byte in question (we need it twice) */
Packit 6c4009
	cmpb %dl, %cl		/* compare byte */
Packit 6c4009
	jne L(11)			/* target found => return */
Packit 6c4009
	movl %esi, %eax		/* remember pointer as possible result */
Packit 6c4009
L(11):	orb %dl, %dl		/* is NUL? */
Packit 6c4009
	jz L(2)			/* yes => return NULL */
Packit 6c4009
	incl %esi		/* increment pointer */
Packit 6c4009
Packit 6c4009
	testl $3, %esi		/* correctly aligned ? */
Packit 6c4009
	jz L(19)		/* yes => begin loop */
Packit 6c4009
	movb (%esi), %dl	/* load byte in question (we need it twice) */
Packit 6c4009
	cmpb %dl, %cl		/* compare byte */
Packit 6c4009
	jne L(12)			/* target found => return */
Packit 6c4009
	movl %esi, %eax		/* remember pointer as result */
Packit 6c4009
L(12):	orb %dl, %dl		/* is NUL? */
Packit 6c4009
	jz L(2)			/* yes => return NULL */
Packit 6c4009
	incl %esi		/* increment pointer */
Packit 6c4009
Packit 6c4009
	testl $3, %esi		/* correctly aligned ? */
Packit 6c4009
	jz L(19)		/* yes => begin loop */
Packit 6c4009
	movb (%esi), %dl	/* load byte in question (we need it twice) */
Packit 6c4009
	cmpb %dl, %cl		/* compare byte */
Packit 6c4009
	jne L(13)			/* target found => return */
Packit 6c4009
	movl %esi, %eax		/* remember pointer as result */
Packit 6c4009
L(13):	orb %dl, %dl		/* is NUL? */
Packit 6c4009
	jz L(2)			/* yes => return NULL */
Packit 6c4009
	incl %esi		/* increment pointer */
Packit 6c4009
Packit 6c4009
	/* No we have reached alignment.  */
Packit 6c4009
	jmp L(19)		/* begin loop */
Packit 6c4009
Packit 6c4009
      /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
Packit 6c4009
	 change any of the hole bits of LONGWORD.
Packit 6c4009
Packit 6c4009
	 1) Is this safe?  Will it catch all the zero bytes?
Packit 6c4009
	 Suppose there is a byte with all zeros.  Any carry bits
Packit 6c4009
	 propagating from its left will fall into the hole at its
Packit 6c4009
	 least significant bit and stop.  Since there will be no
Packit 6c4009
	 carry from its most significant bit, the LSB of the
Packit 6c4009
	 byte to the left will be unchanged, and the zero will be
Packit 6c4009
	 detected.
Packit 6c4009
Packit 6c4009
	 2) Is this worthwhile?  Will it ignore everything except
Packit 6c4009
	 zero bytes?  Suppose every byte of LONGWORD has a bit set
Packit 6c4009
	 somewhere.  There will be a carry into bit 8.	If bit 8
Packit 6c4009
	 is set, this will carry into bit 16.  If bit 8 is clear,
Packit 6c4009
	 one of bits 9-15 must be set, so there will be a carry
Packit 6c4009
	 into bit 16.  Similarly, there will be a carry into bit
Packit 6c4009
	 24.  If one of bits 24-31 is set, there will be a carry
Packit 6c4009
	 into bit 32 (=carry flag), so all of the hole bits will
Packit 6c4009
	 be changed.
Packit 6c4009
Packit 6c4009
	 3) But wait!  Aren't we looking for C, not zero?
Packit 6c4009
	 Good point.  So what we do is XOR LONGWORD with a longword,
Packit 6c4009
	 each of whose bytes is C.  This turns each byte that is C
Packit 6c4009
	 into a zero.  */
Packit 6c4009
Packit 6c4009
	/* Each round the main loop processes 16 bytes.  */
Packit 6c4009
Packit 6c4009
	/* Jump to here when the character is detected.  We chose this
Packit 6c4009
	   way around because the character one is looking for is not
Packit 6c4009
	   as frequent as the rest and taking a conditional jump is more
Packit 6c4009
	   expensive than ignoring it.
Packit 6c4009
Packit 6c4009
	   Some more words to the code below: it might not be obvious why
Packit 6c4009
	   we decrement the source pointer here.  In the loop the pointer
Packit 6c4009
	   is not pre-incremented and so it still points before the word
Packit 6c4009
	   we are looking at.  But you should take a look at the instruction
Packit 6c4009
	   which gets executed before we get into the loop: `addl $16, %esi'.
Packit 6c4009
	   This makes the following subs into adds.  */
Packit 6c4009
Packit 6c4009
	/* These fill bytes make the main loop be correctly aligned.
Packit 6c4009
	   We cannot use align because it is not the following instruction
Packit 6c4009
	   which should be aligned.  */
Packit 6c4009
	.byte 0, 0
Packit 6c4009
#ifndef	PROF
Packit 6c4009
	/* Profiling adds some code and so changes the alignment.  */
Packit 6c4009
	.byte 0
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
L(4):	subl $4, %esi		/* adjust pointer */
Packit 6c4009
L(41):	subl $4, %esi
Packit 6c4009
L(42):	subl $4, %esi
Packit 6c4009
L(43):	testl $0xff000000, %edx	/* is highest byte == C? */
Packit 6c4009
	jnz L(33)		/* no => try other bytes */
Packit 6c4009
	leal 15(%esi), %eax	/* store address as result */
Packit 6c4009
	jmp L(1)		/* and start loop again */
Packit 6c4009
Packit 6c4009
L(3):	subl $4, %esi		/* adjust pointer */
Packit 6c4009
L(31):	subl $4, %esi
Packit 6c4009
L(32):	subl $4, %esi
Packit 6c4009
L(33):	testl $0xff0000, %edx	/* is C in third byte? */
Packit 6c4009
	jnz L(51)		/* no => try other bytes */
Packit 6c4009
	leal 14(%esi), %eax	/* store address as result */
Packit 6c4009
	jmp L(1)		/* and start loop again */
Packit 6c4009
Packit 6c4009
L(51):
Packit 6c4009
	/* At this point we know that the byte is in one of the lower bytes.
Packit 6c4009
	   We make a guess and correct it if necessary.  This reduces the
Packit 6c4009
	   number of necessary jumps.  */
Packit 6c4009
	leal 12(%esi), %eax	/* guess address of lowest byte as result */
Packit 6c4009
	testb %dh, %dh		/* is guess correct? */
Packit 6c4009
	jnz L(1)		/* yes => start loop */
Packit 6c4009
	leal 13(%esi), %eax	/* correct guess to second byte */
Packit 6c4009
Packit 6c4009
L(1):	addl $16, %esi		/* increment pointer for full round */
Packit 6c4009
Packit 6c4009
L(19):	movl (%esi), %edx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	addl %edx, %edi		/* add the magic value to the word.  We get
Packit 6c4009
				   carry bits reported for each byte which
Packit 6c4009
				   is *not* 0 */
Packit 6c4009
Packit 6c4009
	/* According to the algorithm we had to reverse the effect of the
Packit 6c4009
	   XOR first and then test the overflow bits.  But because the
Packit 6c4009
	   following XOR would destroy the carry flag and it would (in a
Packit 6c4009
	   representation with more than 32 bits) not alter then last
Packit 6c4009
	   overflow, we can now test this condition.  If no carry is signaled
Packit 6c4009
	   no overflow must have occurred in the last byte => it was 0.	*/
Packit 6c4009
Packit 6c4009
	jnc L(20)			/* found NUL => check last word */
Packit 6c4009
Packit 6c4009
	/* We are only interested in carry bits that change due to the
Packit 6c4009
	   previous add, so remove original bits */
Packit 6c4009
	xorl %edx, %edi		/* (word+magic)^word */
Packit 6c4009
Packit 6c4009
	/* Now test for the other three overflow bits.  */
Packit 6c4009
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit 6c4009
	incl %edi		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
Packit 6c4009
	/* If at least one byte of the word is C we don't get 0 in %edi.  */
Packit 6c4009
	jnz L(20)			/* found NUL => check last word */
Packit 6c4009
Packit 6c4009
	/* Now we made sure the dword does not contain the character we are
Packit 6c4009
	   looking for.  But because we deal with strings we have to check
Packit 6c4009
	   for the end of string before testing the next dword.  */
Packit 6c4009
Packit 6c4009
	xorl %ecx, %edx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	addl %edx, %edi		/* add the magic value to the word.  We get
Packit 6c4009
				   carry bits reported for each byte which
Packit 6c4009
				   is *not* 0 */
Packit 6c4009
	jnc L(4)		/* highest byte is C => examine dword */
Packit 6c4009
	xorl %edx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit 6c4009
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit 6c4009
	incl %edi		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(3)		/* C is detected in the word => examine it */
Packit 6c4009
Packit 6c4009
	movl 4(%esi), %edx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	addl %edx, %edi		/* add the magic value to the word.  We get
Packit 6c4009
				   carry bits reported for each byte which
Packit 6c4009
				   is *not* 0 */
Packit 6c4009
	jnc L(21)		/* found NUL => check last word */
Packit 6c4009
	xorl %edx, %edi		/* (word+magic)^word */
Packit 6c4009
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit 6c4009
	incl %edi		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(21)		/* found NUL => check last word */
Packit 6c4009
	xorl %ecx, %edx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	addl %edx, %edi		/* add the magic value to the word.  We get
Packit 6c4009
				   carry bits reported for each byte which
Packit 6c4009
				   is *not* 0 */
Packit 6c4009
	jnc L(41)		/* highest byte is C => examine dword */
Packit 6c4009
	xorl %edx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit 6c4009
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit 6c4009
	incl %edi		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(31)		/* C is detected in the word => examine it */
Packit 6c4009
Packit 6c4009
	movl 8(%esi), %edx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	addl %edx, %edi		/* add the magic value to the word.  We get
Packit 6c4009
				   carry bits reported for each byte which
Packit 6c4009
				   is *not* 0 */
Packit 6c4009
	jnc L(22)		/* found NUL => check last word */
Packit 6c4009
	xorl %edx, %edi		/* (word+magic)^word */
Packit 6c4009
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit 6c4009
	incl %edi		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(22)		/* found NUL => check last word */
Packit 6c4009
	xorl %ecx, %edx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	addl %edx, %edi		/* add the magic value to the word.  We get
Packit 6c4009
				   carry bits reported for each byte which
Packit 6c4009
				   is *not* 0 */
Packit 6c4009
	jnc L(42)		/* highest byte is C => examine dword */
Packit 6c4009
	xorl %edx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit 6c4009
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit 6c4009
	incl %edi		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(32)		/* C is detected in the word => examine it */
Packit 6c4009
Packit 6c4009
	movl 12(%esi), %edx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	addl %edx, %edi		/* add the magic value to the word.  We get
Packit 6c4009
				   carry bits reported for each byte which
Packit 6c4009
				   is *not* 0 */
Packit 6c4009
	jnc L(23)		/* found NUL => check last word */
Packit 6c4009
	xorl %edx, %edi		/* (word+magic)^word */
Packit 6c4009
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit 6c4009
	incl %edi		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(23)		/* found NUL => check last word */
Packit 6c4009
	xorl %ecx, %edx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	addl %edx, %edi		/* add the magic value to the word.  We get
Packit 6c4009
				   carry bits reported for each byte which
Packit 6c4009
				   is *not* 0 */
Packit 6c4009
	jnc L(43)		/* highest byte is C => examine dword */
Packit 6c4009
	xorl %edx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit 6c4009
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit 6c4009
	incl %edi		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jz L(1)			/* C is not detected => restart loop */
Packit 6c4009
	jmp L(33)		/* examine word */
Packit 6c4009
Packit 6c4009
L(23):	addl $4, %esi		/* adjust pointer */
Packit 6c4009
L(22):	addl $4, %esi
Packit 6c4009
L(21):	addl $4, %esi
Packit 6c4009
Packit 6c4009
	/* What remains to do is to test which byte the NUL char is and
Packit 6c4009
	   whether the searched character appears in one of the bytes
Packit 6c4009
	   before.  A special case is that the searched byte maybe NUL.
Packit 6c4009
	   In this case a pointer to the terminating NUL char has to be
Packit 6c4009
	   returned.  */
Packit 6c4009
Packit 6c4009
L(20):	cmpb %cl, %dl		/* is first byte == C? */
Packit 6c4009
	jne L(24)		/* no => skip */
Packit 6c4009
	movl %esi, %eax		/* store address as result */
Packit 6c4009
L(24):	testb %dl, %dl		/* is first byte == NUL? */
Packit 6c4009
	jz L(2)			/* yes => return */
Packit 6c4009
Packit 6c4009
	cmpb %cl, %dh		/* is second byte == C? */
Packit 6c4009
	jne L(25)		/* no => skip */
Packit 6c4009
	leal 1(%esi), %eax	/* store address as result */
Packit 6c4009
L(25):	testb %dh, %dh		/* is second byte == NUL? */
Packit 6c4009
	jz L(2)			/* yes => return */
Packit 6c4009
Packit 6c4009
	shrl $16,%edx		/* make upper bytes accessible */
Packit 6c4009
	cmpb %cl, %dl		/* is third byte == C */
Packit 6c4009
	jne L(26)		/* no => skip */
Packit 6c4009
	leal 2(%esi), %eax	/* store address as result */
Packit 6c4009
L(26):	testb %dl, %dl		/* is third byte == NUL */
Packit 6c4009
	jz L(2)			/* yes => return */
Packit 6c4009
Packit 6c4009
	cmpb %cl, %dh		/* is fourth byte == C */
Packit 6c4009
	jne L(2)		/* no => skip */
Packit 6c4009
	leal 3(%esi), %eax	/* store address as result */
Packit 6c4009
Packit 6c4009
L(2):	popl %esi		/* restore saved register content */
Packit 6c4009
	cfi_adjust_cfa_offset (-4)
Packit 6c4009
	cfi_restore (esi)
Packit 6c4009
	popl %edi
Packit 6c4009
	cfi_adjust_cfa_offset (-4)
Packit 6c4009
	cfi_restore (edi)
Packit 6c4009
Packit 6c4009
	ret
Packit 6c4009
END (strrchr)
Packit 6c4009
Packit 6c4009
weak_alias (strrchr, rindex)
Packit 6c4009
libc_hidden_builtin_def (strrchr)