Blame sysdeps/i386/strlen.S

Packit 6c4009
/* strlen(str) -- determine the length of the string STR.
Packit 6c4009
   Optimized for Intel 80x86, x>=4.
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>.
Packit 6c4009
   This file is part of the GNU C Library.
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		/* no space for saved regs */
Packit 6c4009
#define STR	PARMS
Packit 6c4009
Packit 6c4009
	.text
Packit 6c4009
ENTRY (strlen)
Packit 6c4009
Packit 6c4009
	movl STR(%esp), %ecx
Packit 6c4009
	movl %ecx, %eax		/* duplicate it */
Packit 6c4009
Packit 6c4009
	andl $3, %ecx		/* mask alignment bits */
Packit 6c4009
	jz L(1)			/* aligned => start loop */
Packit 6c4009
	cmpb %ch, (%eax)	/* is byte NUL? */
Packit 6c4009
	je L(2)			/* yes => return */
Packit 6c4009
	incl %eax		/* increment pointer */
Packit 6c4009
Packit 6c4009
	xorl $3, %ecx		/* was alignment = 3? */
Packit 6c4009
	jz L(1)			/* yes => now it is aligned and start loop */
Packit 6c4009
	cmpb %ch, (%eax)	/* is byte NUL? */
Packit 6c4009
	je L(2)			/* yes => return */
Packit 6c4009
	addl $1, %eax		/* increment pointer */
Packit 6c4009
Packit 6c4009
	subl $1, %ecx		/* was alignment = 2? */
Packit 6c4009
	jz L(1)			/* yes => now it is aligned and start loop */
Packit 6c4009
	cmpb %ch, (%eax)	/* is byte NUL? */
Packit 6c4009
	je L(2)			/* yes => return */
Packit 6c4009
Packit 6c4009
/* Don't change the above `addl $1,%eax' and `subl $1, %ecx' into `incl %eax'
Packit 6c4009
   and `decl %ecx' resp.  The additional two byte per instruction make the
Packit 6c4009
   label 4 to be aligned on a 16 byte boundary with nops.
Packit 6c4009
Packit 6c4009
   The following `sub $15, %eax' is part of this trick, too.  Together with
Packit 6c4009
   the next instruction (`addl $16, %eax') it is in fact a `incl %eax', just
Packit 6c4009
   as expected from the algorithm.  But doing so has the advantage that
Packit 6c4009
   no jump to label 1 is necessary and so the pipeline is not flushed.  */
Packit 6c4009
Packit 6c4009
	subl $15, %eax		/* effectively +1 */
Packit 6c4009
Packit 6c4009
Packit 6c4009
L(4):	addl $16, %eax		/* adjust pointer for full loop */
Packit 6c4009
Packit 6c4009
L(1):	movl (%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edx	/* magic value */
Packit 6c4009
	addl %ecx, %edx		/* 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(3)		/* highest byte is NUL => return pointer */
Packit 6c4009
	xorl %ecx, %edx		/* (word+magic)^word */
Packit 6c4009
	orl $0xfefefeff, %edx	/* set all non-carry bits */
Packit 6c4009
	incl %edx		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(3)		/* found NUL => return pointer */
Packit 6c4009
Packit 6c4009
	movl 4(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edx	/* magic value */
Packit 6c4009
	addl %ecx, %edx		/* 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 NUL => return pointer */
Packit 6c4009
	xorl %ecx, %edx		/* (word+magic)^word */
Packit 6c4009
	orl $0xfefefeff, %edx	/* set all non-carry bits */
Packit 6c4009
	incl %edx		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(5)		/* found NUL => return pointer */
Packit 6c4009
Packit 6c4009
	movl 8(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edx	/* magic value */
Packit 6c4009
	addl %ecx, %edx		/* 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 NUL => return pointer */
Packit 6c4009
	xorl %ecx, %edx		/* (word+magic)^word */
Packit 6c4009
	orl $0xfefefeff, %edx	/* set all non-carry bits */
Packit 6c4009
	incl %edx		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jnz L(6)		/* found NUL => return pointer */
Packit 6c4009
Packit 6c4009
	movl 12(%eax), %ecx	/* get word (= 4 bytes) in question */
Packit 6c4009
	movl $0xfefefeff, %edx	/* magic value */
Packit 6c4009
	addl %ecx, %edx		/* 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 NUL => return pointer */
Packit 6c4009
	xorl %ecx, %edx		/* (word+magic)^word */
Packit 6c4009
	orl $0xfefefeff, %edx	/* set all non-carry bits */
Packit 6c4009
	incl %edx		/* add 1: if one carry bit was *not* set
Packit 6c4009
				   the addition will not result in 0.  */
Packit 6c4009
	jz L(4)			/* no NUL found => continue loop */
Packit 6c4009
Packit 6c4009
L(7):	addl $4, %eax		/* adjust pointer */
Packit 6c4009
L(6):	addl $4, %eax
Packit 6c4009
L(5):	addl $4, %eax
Packit 6c4009
Packit 6c4009
L(3):	testb %cl, %cl		/* is first byte NUL? */
Packit 6c4009
	jz L(2)			/* yes => return */
Packit 6c4009
	incl %eax		/* increment pointer */
Packit 6c4009
Packit 6c4009
	testb %ch, %ch		/* is second byte NUL? */
Packit 6c4009
	jz L(2)			/* yes => return */
Packit 6c4009
	incl %eax		/* increment pointer */
Packit 6c4009
Packit 6c4009
	testl $0xff0000, %ecx	/* is third byte NUL? */
Packit 6c4009
	jz L(2)			/* yes => return pointer */
Packit 6c4009
	incl %eax		/* increment pointer */
Packit 6c4009
Packit 6c4009
L(2):	subl STR(%esp), %eax	/* compute difference to string start */
Packit 6c4009
Packit 6c4009
	ret
Packit 6c4009
END (strlen)
Packit 6c4009
libc_hidden_builtin_def (strlen)