Blame sysdeps/i386/stpcpy.S

Packit 6c4009
/* Copy SRC to DEST returning the address of the terminating '\0' in DEST.
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
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
/* This function is defined neither in ANSI nor POSIX standards but is
Packit 6c4009
   also not invented here.  */
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 RTN	PARMS
Packit 6c4009
#define DEST	RTN
Packit 6c4009
#define SRC	DEST+4
Packit 6c4009
Packit 6c4009
	.text
Packit 6c4009
ENTRY (__stpcpy)
Packit 6c4009
Packit 6c4009
	movl DEST(%esp), %eax
Packit 6c4009
	movl SRC(%esp), %ecx
Packit 6c4009
	subl %eax, %ecx		/* magic: reduce number of loop variants
Packit 6c4009
				   to one using addressing mode */
Packit 6c4009
Packit 6c4009
	/* Here we would like to write
Packit 6c4009
Packit 6c4009
	subl $4, %eax
Packit 6c4009
	ALIGN (4)
Packit 6c4009
Packit 6c4009
	but the assembler is too smart and optimizes for the shortest
Packit 6c4009
	form where the number only needs one byte.  But if we could
Packit 6c4009
	have the long form we would not need the alignment.  */
Packit 6c4009
Packit 6c4009
	.byte 0x81, 0xe8	/* This is `subl $0x00000004, %eax' */
Packit 6c4009
	.long 0x00000004
Packit 6c4009
Packit 6c4009
	/* Four times unfolded loop with only one loop counter.  This
Packit 6c4009
	   is achieved by the use of index+base addressing mode.  As the
Packit 6c4009
	   loop counter we use the destination address because this is
Packit 6c4009
	   also the result.  */
Packit 6c4009
L(1):	addl $4, %eax		/* increment loop counter */
Packit 6c4009
Packit 6c4009
	movb (%eax,%ecx), %dl	/* load current char */
Packit 6c4009
	movb %dl, (%eax)	/* and store it */
Packit 6c4009
	testb %dl, %dl		/* was it NUL? */
Packit 6c4009
	jz L(2)			/* yes, then exit */
Packit 6c4009
Packit 6c4009
	movb 1(%eax,%ecx), %dl	/* load current char */
Packit 6c4009
	movb %dl, 1(%eax)	/* and store it */
Packit 6c4009
	testb %dl, %dl		/* was it NUL? */
Packit 6c4009
	jz L(3)			/* yes, then exit */
Packit 6c4009
Packit 6c4009
	movb 2(%eax,%ecx), %dl	/* load current char */
Packit 6c4009
	movb %dl, 2(%eax)	/* and store it */
Packit 6c4009
	testb %dl, %dl		/* was it NUL? */
Packit 6c4009
	jz L(4)			/* yes, then exit */
Packit 6c4009
Packit 6c4009
	movb 3(%eax,%ecx), %dl	/* load current char */
Packit 6c4009
	movb %dl, 3(%eax)	/* and store it */
Packit 6c4009
	testb %dl, %dl		/* was it NUL? */
Packit 6c4009
	jnz L(1)		/* no, then continue loop */
Packit 6c4009
Packit 6c4009
	incl %eax		/* correct loop counter */
Packit 6c4009
L(4):	incl %eax
Packit 6c4009
L(3):	incl %eax
Packit 6c4009
L(2):
Packit 6c4009
Packit 6c4009
	ret
Packit 6c4009
END (__stpcpy)
Packit 6c4009
Packit 6c4009
weak_alias (__stpcpy, stpcpy)
Packit 6c4009
libc_hidden_def (__stpcpy)
Packit 6c4009
libc_hidden_builtin_def (stpcpy)