Blame sysdeps/i386/memchr.S

Packit 6c4009
/* memchr (str, chr, len) -- Return pointer to first occurrence of CHR in STR
Packit 6c4009
	 less than LEN.  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
   Optimised a little by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
Packit 6c4009
   This version is developed using the same algorithm as the fast C
Packit 6c4009
   version which carries the following introduction:
Packit 6c4009
   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
Packit 6c4009
   with help from Dan Sahlin (dan@sics.se) and
Packit 6c4009
   commentary by Jim Blandy (jimb@ai.mit.edu);
Packit 6c4009
   adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
Packit 6c4009
   and implemented by Roland McGrath (roland@ai.mit.edu).
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
#define LEN	CHR+4
Packit 6c4009
Packit 6c4009
	.text
Packit 6c4009
ENTRY (__memchr)
Packit 6c4009
Packit 6c4009
	/* Save callee-safe registers used in this function.  */
Packit 6c4009
	pushl %esi
Packit 6c4009
	cfi_adjust_cfa_offset (4)
Packit 6c4009
	pushl %edi
Packit 6c4009
	cfi_adjust_cfa_offset (4)
Packit 6c4009
	cfi_rel_offset (edi, 0)
Packit 6c4009
Packit 6c4009
	/* Load parameters into registers.  */
Packit 6c4009
	movl STR(%esp), %eax	/* str: pointer to memory block.  */
Packit 6c4009
	movl CHR(%esp), %edx	/* c: byte we are looking for.  */
Packit 6c4009
	movl LEN(%esp), %esi	/* len: length of memory block.  */
Packit 6c4009
	cfi_rel_offset (esi, 4)
Packit 6c4009
Packit 6c4009
	/* If my must not test more than three characters test
Packit 6c4009
	   them one by one.  This is especially true for 0.  */
Packit 6c4009
	cmpl $4, %esi
Packit 6c4009
	jb L(3)
Packit 6c4009
Packit 6c4009
	/* At the moment %edx contains CHR.  What we need for the
Packit 6c4009
	   algorithm is CHR 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 %dl, %dh		/* Now it is 0|0|c|c */
Packit 6c4009
	movl %edx, %ecx
Packit 6c4009
	shll $16, %edx		/* Now c|c|0|0 */
Packit 6c4009
	movw %cx, %dx		/* And finally c|c|c|c */
Packit 6c4009
Packit 6c4009
	/* Better performance can be achieved if the word (32
Packit 6c4009
	   bit) memory access is aligned on a four-byte-boundary.
Packit 6c4009
	   So process first bytes one by one until boundary is
Packit 6c4009
	   reached. Don't use a loop for better performance.  */
Packit 6c4009
Packit 6c4009
	testb $3, %al		/* correctly aligned ? */
Packit 6c4009
	je L(2)			/* yes => begin loop */
Packit 6c4009
	cmpb %dl, (%eax)	/* compare byte */
Packit 6c4009
	je L(9)			/* target found => return */
Packit 6c4009
	incl %eax		/* increment source pointer */
Packit 6c4009
	decl %esi		/* decrement length counter */
Packit 6c4009
	je L(4)			/* len==0 => return NULL */
Packit 6c4009
Packit 6c4009
	testb $3, %al		/* correctly aligned ? */
Packit 6c4009
	je L(2)			/* yes => begin loop */
Packit 6c4009
	cmpb %dl, (%eax)	/* compare byte */
Packit 6c4009
	je L(9)			/* target found => return */
Packit 6c4009
	incl %eax		/* increment source pointer */
Packit 6c4009
	decl %esi		/* decrement length counter */
Packit 6c4009
	je L(4)			/* len==0 => return NULL */
Packit 6c4009
Packit 6c4009
	testb $3, %al		/* correctly aligned ? */
Packit 6c4009
	je L(2)			/* yes => begin loop */
Packit 6c4009
	cmpb %dl, (%eax)	/* compare byte */
Packit 6c4009
	je L(9)			/* target found => return */
Packit 6c4009
	incl %eax		/* increment source pointer */
Packit 6c4009
	decl %esi		/* decrement length counter */
Packit 6c4009
	/* no test for len==0 here, because this is done in the
Packit 6c4009
	   loop head */
Packit 6c4009
	jmp L(2)
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 CHR, not zero?
Packit 6c4009
	 Good point.  So what we do is XOR LONGWORD with a longword,
Packit 6c4009
	 each of whose bytes is CHR.  This turns each byte that is CHR
Packit 6c4009
	 into a zero.  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
	/* Each round the main loop processes 16 bytes.  */
Packit 6c4009
Packit 6c4009
	ALIGN (4)
Packit 6c4009
Packit 6c4009
L(1):	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	addl %ecx, %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
	jnc L(8)
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 %ecx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
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 CHR we don't get 0 in %edi.  */
Packit 6c4009
	jnz L(8)		/* found it => return pointer */
Packit 6c4009
Packit 6c4009
	/* This process is unfolded four times for better performance.
Packit 6c4009
	   we don't increment the source pointer each time.  Instead we
Packit 6c4009
	   use offsets and increment by 16 in each run of the loop.  But
Packit 6c4009
	   before probing for the matching byte we need some extra code
Packit 6c4009
	   (following LL(13) below).  Even the len can be compared with
Packit 6c4009
	   constants instead of decrementing each time.  */
Packit 6c4009
Packit 6c4009
	movl 4(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	addl %ecx, %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(7)		/* highest byte is CHR => return pointer */
Packit 6c4009
	xorl %ecx, %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(7)		/* found it => return pointer */
Packit 6c4009
Packit 6c4009
	movl 8(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	addl %ecx, %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(6)		/* highest byte is CHR => return pointer */
Packit 6c4009
	xorl %ecx, %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(6)		/* found it => return pointer */
Packit 6c4009
Packit 6c4009
	movl 12(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	addl %ecx, %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(5)		/* highest byte is CHR => return pointer */
Packit 6c4009
	xorl %ecx, %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(5)		/* found it => return pointer */
Packit 6c4009
Packit 6c4009
	/* Adjust both counters for a full round, i.e. 16 bytes.  */
Packit 6c4009
	addl $16, %eax
Packit 6c4009
L(2):	subl $16, %esi
Packit 6c4009
	jae L(1)		/* Still more than 16 bytes remaining */
Packit 6c4009
Packit 6c4009
	/* Process remaining bytes separately.  */
Packit 6c4009
	cmpl $4-16, %esi	/* rest < 4 bytes? */
Packit 6c4009
	jb L(3)			/* yes, than test byte by byte */
Packit 6c4009
Packit 6c4009
	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	addl %ecx, %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(8)		/* highest byte is CHR => return pointer */
Packit 6c4009
	xorl %ecx, %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
	jne L(8)		/* found it => return pointer */
Packit 6c4009
	addl $4, %eax		/* adjust source pointer */
Packit 6c4009
Packit 6c4009
	cmpl $8-16, %esi	/* rest < 8 bytes? */
Packit 6c4009
	jb L(3)			/* yes, than test byte by byte */
Packit 6c4009
Packit 6c4009
	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	addl %ecx, %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(8)		/* highest byte is CHR => return pointer */
Packit 6c4009
	xorl %ecx, %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
	jne L(8)		/* found it => return pointer */
Packit 6c4009
	addl $4, %eax		/* adjust source pointer */
Packit 6c4009
Packit 6c4009
	cmpl $12-16, %esi	/* rest < 12 bytes? */
Packit 6c4009
	jb L(3)			/* yes, than test byte by byte */
Packit 6c4009
Packit 6c4009
	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edi	/* magic value */
Packit 6c4009
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit 6c4009
				   are now 0 */
Packit 6c4009
	addl %ecx, %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(8)		/* highest byte is CHR => return pointer */
Packit 6c4009
	xorl %ecx, %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
	jne L(8)		/* found it => return pointer */
Packit 6c4009
	addl $4, %eax		/* adjust source pointer */
Packit 6c4009
Packit 6c4009
	/* Check the remaining bytes one by one.  */
Packit 6c4009
L(3):	andl $3, %esi		/* mask out uninteresting bytes */
Packit 6c4009
	jz L(4)			/* no remaining bytes => return NULL */
Packit 6c4009
Packit 6c4009
	cmpb %dl, (%eax)	/* compare byte with CHR */
Packit 6c4009
	je L(9)			/* equal, than return pointer */
Packit 6c4009
	incl %eax		/* increment source pointer */
Packit 6c4009
	decl %esi		/* decrement length */
Packit 6c4009
	jz L(4)			/* no remaining bytes => return NULL */
Packit 6c4009
Packit 6c4009
	cmpb %dl, (%eax)	/* compare byte with CHR */
Packit 6c4009
	je L(9)			/* equal, than return pointer */
Packit 6c4009
	incl %eax		/* increment source pointer */
Packit 6c4009
	decl %esi		/* decrement length */
Packit 6c4009
	jz L(4)			/* no remaining bytes => return NULL */
Packit 6c4009
Packit 6c4009
	cmpb %dl, (%eax)	/* compare byte with CHR */
Packit 6c4009
	je L(9)			/* equal, than return pointer */
Packit 6c4009
Packit 6c4009
L(4):	/* no byte found => return NULL */
Packit 6c4009
	xorl %eax, %eax
Packit 6c4009
	jmp L(9)
Packit 6c4009
Packit 6c4009
	/* add missing source pointer increments */
Packit 6c4009
L(5):	addl $4, %eax
Packit 6c4009
L(6):	addl $4, %eax
Packit 6c4009
L(7):	addl $4, %eax
Packit 6c4009
Packit 6c4009
	/* Test for the matching byte in the word.  %ecx contains a NUL
Packit 6c4009
	   char in the byte which originally was the byte we are looking
Packit 6c4009
	   at.  */
Packit 6c4009
L(8):	testb %cl, %cl		/* test first byte in dword */
Packit 6c4009
	jz L(9)			/* if zero => return pointer */
Packit 6c4009
	incl %eax		/* increment source pointer */
Packit 6c4009
Packit 6c4009
	testb %ch, %ch		/* test second byte in dword */
Packit 6c4009
	jz L(9)			/* if zero => return pointer */
Packit 6c4009
	incl %eax		/* increment source pointer */
Packit 6c4009
Packit 6c4009
	testl $0xff0000, %ecx	/* test third byte in dword */
Packit 6c4009
	jz L(9)			/* if zero => return pointer */
Packit 6c4009
	incl %eax		/* increment source pointer */
Packit 6c4009
Packit 6c4009
	/* No further test needed we we know it is one of the four bytes.  */
Packit 6c4009
L(9):	popl %edi		/* pop saved registers */
Packit 6c4009
	cfi_adjust_cfa_offset (-4)
Packit 6c4009
	cfi_restore (edi)
Packit 6c4009
	popl %esi
Packit 6c4009
	cfi_adjust_cfa_offset (-4)
Packit 6c4009
	cfi_restore (esi)
Packit 6c4009
Packit 6c4009
	ret
Packit 6c4009
END (__memchr)
Packit 6c4009
Packit 6c4009
weak_alias (__memchr, memchr)
Packit 6c4009
libc_hidden_builtin_def (memchr)