Blame sysdeps/i386/memchr.S

Packit Service 82fcde
/* memchr (str, chr, len) -- Return pointer to first occurrence of CHR in STR
Packit Service 82fcde
	 less than LEN.  For Intel 80x86, x>=3.
Packit Service 82fcde
   Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>
Packit Service 82fcde
   Optimised a little by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
Packit Service 82fcde
   This version is developed using the same algorithm as the fast C
Packit Service 82fcde
   version which carries the following introduction:
Packit Service 82fcde
   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
Packit Service 82fcde
   with help from Dan Sahlin (dan@sics.se) and
Packit Service 82fcde
   commentary by Jim Blandy (jimb@ai.mit.edu);
Packit Service 82fcde
   adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
Packit Service 82fcde
   and implemented by Roland McGrath (roland@ai.mit.edu).
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <sysdep.h>
Packit Service 82fcde
#include "asm-syntax.h"
Packit Service 82fcde
Packit Service 82fcde
#define PARMS	4+8		/* space for 2 saved regs */
Packit Service 82fcde
#define RTN	PARMS
Packit Service 82fcde
#define STR	RTN
Packit Service 82fcde
#define CHR	STR+4
Packit Service 82fcde
#define LEN	CHR+4
Packit Service 82fcde
Packit Service 82fcde
	.text
Packit Service 82fcde
ENTRY (__memchr)
Packit Service 82fcde
Packit Service 82fcde
	/* Save callee-safe registers used in this function.  */
Packit Service 82fcde
	pushl %esi
Packit Service 82fcde
	cfi_adjust_cfa_offset (4)
Packit Service 82fcde
	pushl %edi
Packit Service 82fcde
	cfi_adjust_cfa_offset (4)
Packit Service 82fcde
	cfi_rel_offset (edi, 0)
Packit Service 82fcde
Packit Service 82fcde
	/* Load parameters into registers.  */
Packit Service 82fcde
	movl STR(%esp), %eax	/* str: pointer to memory block.  */
Packit Service 82fcde
	movl CHR(%esp), %edx	/* c: byte we are looking for.  */
Packit Service 82fcde
	movl LEN(%esp), %esi	/* len: length of memory block.  */
Packit Service 82fcde
	cfi_rel_offset (esi, 4)
Packit Service 82fcde
Packit Service 82fcde
	/* If my must not test more than three characters test
Packit Service 82fcde
	   them one by one.  This is especially true for 0.  */
Packit Service 82fcde
	cmpl $4, %esi
Packit Service 82fcde
	jb L(3)
Packit Service 82fcde
Packit Service 82fcde
	/* At the moment %edx contains CHR.  What we need for the
Packit Service 82fcde
	   algorithm is CHR in all bytes of the dword.  Avoid
Packit Service 82fcde
	   operations on 16 bit words because these require an
Packit Service 82fcde
	   prefix byte (and one more cycle).  */
Packit Service 82fcde
	movb %dl, %dh		/* Now it is 0|0|c|c */
Packit Service 82fcde
	movl %edx, %ecx
Packit Service 82fcde
	shll $16, %edx		/* Now c|c|0|0 */
Packit Service 82fcde
	movw %cx, %dx		/* And finally c|c|c|c */
Packit Service 82fcde
Packit Service 82fcde
	/* Better performance can be achieved if the word (32
Packit Service 82fcde
	   bit) memory access is aligned on a four-byte-boundary.
Packit Service 82fcde
	   So process first bytes one by one until boundary is
Packit Service 82fcde
	   reached. Don't use a loop for better performance.  */
Packit Service 82fcde
Packit Service 82fcde
	testb $3, %al		/* correctly aligned ? */
Packit Service 82fcde
	je L(2)			/* yes => begin loop */
Packit Service 82fcde
	cmpb %dl, (%eax)	/* compare byte */
Packit Service 82fcde
	je L(9)			/* target found => return */
Packit Service 82fcde
	incl %eax		/* increment source pointer */
Packit Service 82fcde
	decl %esi		/* decrement length counter */
Packit Service 82fcde
	je L(4)			/* len==0 => return NULL */
Packit Service 82fcde
Packit Service 82fcde
	testb $3, %al		/* correctly aligned ? */
Packit Service 82fcde
	je L(2)			/* yes => begin loop */
Packit Service 82fcde
	cmpb %dl, (%eax)	/* compare byte */
Packit Service 82fcde
	je L(9)			/* target found => return */
Packit Service 82fcde
	incl %eax		/* increment source pointer */
Packit Service 82fcde
	decl %esi		/* decrement length counter */
Packit Service 82fcde
	je L(4)			/* len==0 => return NULL */
Packit Service 82fcde
Packit Service 82fcde
	testb $3, %al		/* correctly aligned ? */
Packit Service 82fcde
	je L(2)			/* yes => begin loop */
Packit Service 82fcde
	cmpb %dl, (%eax)	/* compare byte */
Packit Service 82fcde
	je L(9)			/* target found => return */
Packit Service 82fcde
	incl %eax		/* increment source pointer */
Packit Service 82fcde
	decl %esi		/* decrement length counter */
Packit Service 82fcde
	/* no test for len==0 here, because this is done in the
Packit Service 82fcde
	   loop head */
Packit Service 82fcde
	jmp L(2)
Packit Service 82fcde
Packit Service 82fcde
      /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
Packit Service 82fcde
	 change any of the hole bits of LONGWORD.
Packit Service 82fcde
Packit Service 82fcde
	 1) Is this safe?  Will it catch all the zero bytes?
Packit Service 82fcde
	 Suppose there is a byte with all zeros.  Any carry bits
Packit Service 82fcde
	 propagating from its left will fall into the hole at its
Packit Service 82fcde
	 least significant bit and stop.  Since there will be no
Packit Service 82fcde
	 carry from its most significant bit, the LSB of the
Packit Service 82fcde
	 byte to the left will be unchanged, and the zero will be
Packit Service 82fcde
	 detected.
Packit Service 82fcde
Packit Service 82fcde
	 2) Is this worthwhile?  Will it ignore everything except
Packit Service 82fcde
	 zero bytes?  Suppose every byte of LONGWORD has a bit set
Packit Service 82fcde
	 somewhere.  There will be a carry into bit 8.	If bit 8
Packit Service 82fcde
	 is set, this will carry into bit 16.  If bit 8 is clear,
Packit Service 82fcde
	 one of bits 9-15 must be set, so there will be a carry
Packit Service 82fcde
	 into bit 16.  Similarly, there will be a carry into bit
Packit Service 82fcde
	 24.  If one of bits 24-31 is set, there will be a carry
Packit Service 82fcde
	 into bit 32 (=carry flag), so all of the hole bits will
Packit Service 82fcde
	 be changed.
Packit Service 82fcde
Packit Service 82fcde
	 3) But wait!  Aren't we looking for CHR, not zero?
Packit Service 82fcde
	 Good point.  So what we do is XOR LONGWORD with a longword,
Packit Service 82fcde
	 each of whose bytes is CHR.  This turns each byte that is CHR
Packit Service 82fcde
	 into a zero.  */
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
	/* Each round the main loop processes 16 bytes.  */
Packit Service 82fcde
Packit Service 82fcde
	ALIGN (4)
Packit Service 82fcde
Packit Service 82fcde
L(1):	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit Service 82fcde
	movl $0xfefefeff, %edi	/* magic value */
Packit Service 82fcde
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit Service 82fcde
				   are now 0 */
Packit Service 82fcde
	addl %ecx, %edi		/* add the magic value to the word.  We get
Packit Service 82fcde
				   carry bits reported for each byte which
Packit Service 82fcde
				   is *not* 0 */
Packit Service 82fcde
Packit Service 82fcde
	/* According to the algorithm we had to reverse the effect of the
Packit Service 82fcde
	   XOR first and then test the overflow bits.  But because the
Packit Service 82fcde
	   following XOR would destroy the carry flag and it would (in a
Packit Service 82fcde
	   representation with more than 32 bits) not alter then last
Packit Service 82fcde
	   overflow, we can now test this condition.  If no carry is signaled
Packit Service 82fcde
	   no overflow must have occurred in the last byte => it was 0.	*/
Packit Service 82fcde
	jnc L(8)
Packit Service 82fcde
Packit Service 82fcde
	/* We are only interested in carry bits that change due to the
Packit Service 82fcde
	   previous add, so remove original bits */
Packit Service 82fcde
	xorl %ecx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit Service 82fcde
Packit Service 82fcde
	/* Now test for the other three overflow bits.  */
Packit Service 82fcde
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit Service 82fcde
	incl %edi		/* add 1: if one carry bit was *not* set
Packit Service 82fcde
				   the addition will not result in 0.  */
Packit Service 82fcde
Packit Service 82fcde
	/* If at least one byte of the word is CHR we don't get 0 in %edi.  */
Packit Service 82fcde
	jnz L(8)		/* found it => return pointer */
Packit Service 82fcde
Packit Service 82fcde
	/* This process is unfolded four times for better performance.
Packit Service 82fcde
	   we don't increment the source pointer each time.  Instead we
Packit Service 82fcde
	   use offsets and increment by 16 in each run of the loop.  But
Packit Service 82fcde
	   before probing for the matching byte we need some extra code
Packit Service 82fcde
	   (following LL(13) below).  Even the len can be compared with
Packit Service 82fcde
	   constants instead of decrementing each time.  */
Packit Service 82fcde
Packit Service 82fcde
	movl 4(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit Service 82fcde
	movl $0xfefefeff, %edi	/* magic value */
Packit Service 82fcde
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit Service 82fcde
				   are now 0 */
Packit Service 82fcde
	addl %ecx, %edi		/* add the magic value to the word.  We get
Packit Service 82fcde
				   carry bits reported for each byte which
Packit Service 82fcde
				   is *not* 0 */
Packit Service 82fcde
	jnc L(7)		/* highest byte is CHR => return pointer */
Packit Service 82fcde
	xorl %ecx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit Service 82fcde
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit Service 82fcde
	incl %edi		/* add 1: if one carry bit was *not* set
Packit Service 82fcde
				   the addition will not result in 0.  */
Packit Service 82fcde
	jnz L(7)		/* found it => return pointer */
Packit Service 82fcde
Packit Service 82fcde
	movl 8(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit Service 82fcde
	movl $0xfefefeff, %edi	/* magic value */
Packit Service 82fcde
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit Service 82fcde
				   are now 0 */
Packit Service 82fcde
	addl %ecx, %edi		/* add the magic value to the word.  We get
Packit Service 82fcde
				   carry bits reported for each byte which
Packit Service 82fcde
				   is *not* 0 */
Packit Service 82fcde
	jnc L(6)		/* highest byte is CHR => return pointer */
Packit Service 82fcde
	xorl %ecx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit Service 82fcde
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit Service 82fcde
	incl %edi		/* add 1: if one carry bit was *not* set
Packit Service 82fcde
				   the addition will not result in 0.  */
Packit Service 82fcde
	jnz L(6)		/* found it => return pointer */
Packit Service 82fcde
Packit Service 82fcde
	movl 12(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit Service 82fcde
	movl $0xfefefeff, %edi	/* magic value */
Packit Service 82fcde
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit Service 82fcde
				   are now 0 */
Packit Service 82fcde
	addl %ecx, %edi		/* add the magic value to the word.  We get
Packit Service 82fcde
				   carry bits reported for each byte which
Packit Service 82fcde
				   is *not* 0 */
Packit Service 82fcde
	jnc L(5)		/* highest byte is CHR => return pointer */
Packit Service 82fcde
	xorl %ecx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit Service 82fcde
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit Service 82fcde
	incl %edi		/* add 1: if one carry bit was *not* set
Packit Service 82fcde
				   the addition will not result in 0.  */
Packit Service 82fcde
	jnz L(5)		/* found it => return pointer */
Packit Service 82fcde
Packit Service 82fcde
	/* Adjust both counters for a full round, i.e. 16 bytes.  */
Packit Service 82fcde
	addl $16, %eax
Packit Service 82fcde
L(2):	subl $16, %esi
Packit Service 82fcde
	jae L(1)		/* Still more than 16 bytes remaining */
Packit Service 82fcde
Packit Service 82fcde
	/* Process remaining bytes separately.  */
Packit Service 82fcde
	cmpl $4-16, %esi	/* rest < 4 bytes? */
Packit Service 82fcde
	jb L(3)			/* yes, than test byte by byte */
Packit Service 82fcde
Packit Service 82fcde
	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit Service 82fcde
	movl $0xfefefeff, %edi	/* magic value */
Packit Service 82fcde
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit Service 82fcde
				   are now 0 */
Packit Service 82fcde
	addl %ecx, %edi		/* add the magic value to the word.  We get
Packit Service 82fcde
				   carry bits reported for each byte which
Packit Service 82fcde
				   is *not* 0 */
Packit Service 82fcde
	jnc L(8)		/* highest byte is CHR => return pointer */
Packit Service 82fcde
	xorl %ecx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit Service 82fcde
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit Service 82fcde
	incl %edi		/* add 1: if one carry bit was *not* set
Packit Service 82fcde
				   the addition will not result in 0.  */
Packit Service 82fcde
	jne L(8)		/* found it => return pointer */
Packit Service 82fcde
	addl $4, %eax		/* adjust source pointer */
Packit Service 82fcde
Packit Service 82fcde
	cmpl $8-16, %esi	/* rest < 8 bytes? */
Packit Service 82fcde
	jb L(3)			/* yes, than test byte by byte */
Packit Service 82fcde
Packit Service 82fcde
	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit Service 82fcde
	movl $0xfefefeff, %edi	/* magic value */
Packit Service 82fcde
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit Service 82fcde
				   are now 0 */
Packit Service 82fcde
	addl %ecx, %edi		/* add the magic value to the word.  We get
Packit Service 82fcde
				   carry bits reported for each byte which
Packit Service 82fcde
				   is *not* 0 */
Packit Service 82fcde
	jnc L(8)		/* highest byte is CHR => return pointer */
Packit Service 82fcde
	xorl %ecx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit Service 82fcde
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit Service 82fcde
	incl %edi		/* add 1: if one carry bit was *not* set
Packit Service 82fcde
				   the addition will not result in 0.  */
Packit Service 82fcde
	jne L(8)		/* found it => return pointer */
Packit Service 82fcde
	addl $4, %eax		/* adjust source pointer */
Packit Service 82fcde
Packit Service 82fcde
	cmpl $12-16, %esi	/* rest < 12 bytes? */
Packit Service 82fcde
	jb L(3)			/* yes, than test byte by byte */
Packit Service 82fcde
Packit Service 82fcde
	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit Service 82fcde
	movl $0xfefefeff, %edi	/* magic value */
Packit Service 82fcde
	xorl %edx, %ecx		/* XOR with word c|c|c|c => bytes of str == c
Packit Service 82fcde
				   are now 0 */
Packit Service 82fcde
	addl %ecx, %edi		/* add the magic value to the word.  We get
Packit Service 82fcde
				   carry bits reported for each byte which
Packit Service 82fcde
				   is *not* 0 */
Packit Service 82fcde
	jnc L(8)		/* highest byte is CHR => return pointer */
Packit Service 82fcde
	xorl %ecx, %edi		/* ((word^charmask)+magic)^(word^charmask) */
Packit Service 82fcde
	orl $0xfefefeff, %edi	/* set all non-carry bits */
Packit Service 82fcde
	incl %edi		/* add 1: if one carry bit was *not* set
Packit Service 82fcde
				   the addition will not result in 0.  */
Packit Service 82fcde
	jne L(8)		/* found it => return pointer */
Packit Service 82fcde
	addl $4, %eax		/* adjust source pointer */
Packit Service 82fcde
Packit Service 82fcde
	/* Check the remaining bytes one by one.  */
Packit Service 82fcde
L(3):	andl $3, %esi		/* mask out uninteresting bytes */
Packit Service 82fcde
	jz L(4)			/* no remaining bytes => return NULL */
Packit Service 82fcde
Packit Service 82fcde
	cmpb %dl, (%eax)	/* compare byte with CHR */
Packit Service 82fcde
	je L(9)			/* equal, than return pointer */
Packit Service 82fcde
	incl %eax		/* increment source pointer */
Packit Service 82fcde
	decl %esi		/* decrement length */
Packit Service 82fcde
	jz L(4)			/* no remaining bytes => return NULL */
Packit Service 82fcde
Packit Service 82fcde
	cmpb %dl, (%eax)	/* compare byte with CHR */
Packit Service 82fcde
	je L(9)			/* equal, than return pointer */
Packit Service 82fcde
	incl %eax		/* increment source pointer */
Packit Service 82fcde
	decl %esi		/* decrement length */
Packit Service 82fcde
	jz L(4)			/* no remaining bytes => return NULL */
Packit Service 82fcde
Packit Service 82fcde
	cmpb %dl, (%eax)	/* compare byte with CHR */
Packit Service 82fcde
	je L(9)			/* equal, than return pointer */
Packit Service 82fcde
Packit Service 82fcde
L(4):	/* no byte found => return NULL */
Packit Service 82fcde
	xorl %eax, %eax
Packit Service 82fcde
	jmp L(9)
Packit Service 82fcde
Packit Service 82fcde
	/* add missing source pointer increments */
Packit Service 82fcde
L(5):	addl $4, %eax
Packit Service 82fcde
L(6):	addl $4, %eax
Packit Service 82fcde
L(7):	addl $4, %eax
Packit Service 82fcde
Packit Service 82fcde
	/* Test for the matching byte in the word.  %ecx contains a NUL
Packit Service 82fcde
	   char in the byte which originally was the byte we are looking
Packit Service 82fcde
	   at.  */
Packit Service 82fcde
L(8):	testb %cl, %cl		/* test first byte in dword */
Packit Service 82fcde
	jz L(9)			/* if zero => return pointer */
Packit Service 82fcde
	incl %eax		/* increment source pointer */
Packit Service 82fcde
Packit Service 82fcde
	testb %ch, %ch		/* test second byte in dword */
Packit Service 82fcde
	jz L(9)			/* if zero => return pointer */
Packit Service 82fcde
	incl %eax		/* increment source pointer */
Packit Service 82fcde
Packit Service 82fcde
	testl $0xff0000, %ecx	/* test third byte in dword */
Packit Service 82fcde
	jz L(9)			/* if zero => return pointer */
Packit Service 82fcde
	incl %eax		/* increment source pointer */
Packit Service 82fcde
Packit Service 82fcde
	/* No further test needed we we know it is one of the four bytes.  */
Packit Service 82fcde
L(9):	popl %edi		/* pop saved registers */
Packit Service 82fcde
	cfi_adjust_cfa_offset (-4)
Packit Service 82fcde
	cfi_restore (edi)
Packit Service 82fcde
	popl %esi
Packit Service 82fcde
	cfi_adjust_cfa_offset (-4)
Packit Service 82fcde
	cfi_restore (esi)
Packit Service 82fcde
Packit Service 82fcde
	ret
Packit Service 82fcde
END (__memchr)
Packit Service 82fcde
Packit Service 82fcde
weak_alias (__memchr, memchr)
Packit Service 82fcde
libc_hidden_builtin_def (memchr)