Blame sysdeps/i386/rawmemchr.S

Packit Service 82fcde
/* rawmemchr (str, ch) -- Return pointer to first occurrence of CH in STR.
Packit Service 82fcde
   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+4	/* space for 1 saved reg */
Packit Service 82fcde
#define RTN	PARMS
Packit Service 82fcde
#define STR	RTN
Packit Service 82fcde
#define CHR	STR+4
Packit Service 82fcde
Packit Service 82fcde
	.text
Packit Service 82fcde
ENTRY (__rawmemchr)
Packit Service 82fcde
Packit Service 82fcde
	/* Save callee-safe register used in this function.  */
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
Packit Service 82fcde
	movl CHR(%esp), %edx
Packit Service 82fcde
Packit Service 82fcde
	/* At the moment %edx contains C.  What we need for the
Packit Service 82fcde
	   algorithm is C 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(1)			/* 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
Packit Service 82fcde
	testb $3, %al		/* correctly aligned ? */
Packit Service 82fcde
	je L(1)			/* 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
Packit Service 82fcde
	testb $3, %al		/* correctly aligned ? */
Packit Service 82fcde
	je L(1)			/* 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
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 C, 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 C.  This turns each byte that is C
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
	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 C 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 C => 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 C => 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 C => 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
	jmp L(1)
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
Packit Service 82fcde
L(9):
Packit Service 82fcde
	popl %edi		/* pop saved register */
Packit Service 82fcde
	cfi_adjust_cfa_offset (-4)
Packit Service 82fcde
	cfi_restore (edi)
Packit Service 82fcde
Packit Service 82fcde
	ret
Packit Service 82fcde
END (__rawmemchr)
Packit Service 82fcde
Packit Service 82fcde
libc_hidden_def (__rawmemchr)
Packit Service 82fcde
weak_alias (__rawmemchr, rawmemchr)