Blame sysdeps/i386/stpncpy.S

Packit Service 82fcde
/* copy no more than N bytes from SRC to DEST, returning the address of
Packit Service 82fcde
   the terminating '\0' in DEST.
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
   Some bug fixes by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
Packit Service 82fcde
     - original wrote n+1 chars in some cases.
Packit Service 82fcde
     - stpncpy() ought to behave like strncpy() ie. not null-terminate
Packit Service 82fcde
       if limited by n.  glibc-1.09 stpncpy() does this.
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 DEST	RTN
Packit Service 82fcde
#define SRC	DEST+4
Packit Service 82fcde
#define LEN	SRC+4
Packit Service 82fcde
Packit Service 82fcde
	.text
Packit Service 82fcde
ENTRY (__stpncpy)
Packit Service 82fcde
Packit Service 82fcde
	pushl %esi
Packit Service 82fcde
	cfi_adjust_cfa_offset (4)
Packit Service 82fcde
Packit Service 82fcde
	movl DEST(%esp), %eax
Packit Service 82fcde
	movl SRC(%esp), %esi
Packit Service 82fcde
	cfi_rel_offset (esi, 0)
Packit Service 82fcde
	movl LEN(%esp), %ecx
Packit Service 82fcde
Packit Service 82fcde
	subl %eax, %esi		/* magic: reduce number of loop variants
Packit Service 82fcde
				   to one using addressing mode */
Packit Service 82fcde
	jmp L(1)		/* jump to loop "head" */
Packit Service 82fcde
Packit Service 82fcde
	ALIGN(4)
Packit Service 82fcde
Packit Service 82fcde
	/* Four times unfolded loop with two loop counters.  We get the
Packit Service 82fcde
	   third value (the source address) by using the index+base
Packit Service 82fcde
	   addressing mode.  */
Packit Service 82fcde
L(2):	movb (%eax,%esi), %dl	/* load current char */
Packit Service 82fcde
	movb %dl, (%eax)	/* and store it */
Packit Service 82fcde
	testb %dl, %dl		/* was it NUL? */
Packit Service 82fcde
	jz L(7)			/* yes, then exit */
Packit Service 82fcde
Packit Service 82fcde
	movb 1(%eax,%esi), %dl	/* load current char */
Packit Service 82fcde
	movb %dl, 1(%eax)	/* and store it */
Packit Service 82fcde
	testb %dl, %dl		/* was it NUL? */
Packit Service 82fcde
	jz L(6)			/* yes, then exit */
Packit Service 82fcde
Packit Service 82fcde
	movb 2(%eax,%esi), %dl	/* load current char */
Packit Service 82fcde
	movb %dl, 2(%eax)	/* and store it */
Packit Service 82fcde
	testb %dl, %dl		/* was it NUL? */
Packit Service 82fcde
	jz L(5)			/* yes, then exit */
Packit Service 82fcde
Packit Service 82fcde
	movb 3(%eax,%esi), %dl	/* load current char */
Packit Service 82fcde
	movb %dl, 3(%eax)	/* and store it */
Packit Service 82fcde
	testb %dl, %dl		/* was it NUL? */
Packit Service 82fcde
	jz L(4)			/* yes, then exit */
Packit Service 82fcde
Packit Service 82fcde
	addl $4, %eax		/* increment loop counter for full round */
Packit Service 82fcde
Packit Service 82fcde
L(1):	subl $4, %ecx		/* still more than 4 bytes allowed? */
Packit Service 82fcde
	jae L(2)		/* yes, then go to start of loop */
Packit Service 82fcde
Packit Service 82fcde
	/* The maximal remaining 15 bytes are not processed in a loop.  */
Packit Service 82fcde
Packit Service 82fcde
	addl $4, %ecx		/* correct above subtraction */
Packit Service 82fcde
	jz L(9)			/* maximal allowed char reached => go to end */
Packit Service 82fcde
Packit Service 82fcde
	movb (%eax,%esi), %dl	/* load current char */
Packit Service 82fcde
	movb %dl, (%eax)	/* and store it */
Packit Service 82fcde
	testb %dl, %dl		/* was it NUL? */
Packit Service 82fcde
	jz L(3)			/* yes, then exit */
Packit Service 82fcde
Packit Service 82fcde
	incl %eax		/* increment pointer */
Packit Service 82fcde
	decl %ecx		/* decrement length counter */
Packit Service 82fcde
	jz L(9)			/* no more allowed => exit */
Packit Service 82fcde
Packit Service 82fcde
	movb (%eax,%esi), %dl	/* load current char */
Packit Service 82fcde
	movb %dl, (%eax)	/* and store it */
Packit Service 82fcde
	testb %dl, %dl		/* was it NUL? */
Packit Service 82fcde
	jz L(3)			/* yes, then exit */
Packit Service 82fcde
Packit Service 82fcde
	incl %eax		/* increment pointer */
Packit Service 82fcde
	decl %ecx		/* decrement length counter */
Packit Service 82fcde
	jz L(9)			/* no more allowed => exit */
Packit Service 82fcde
Packit Service 82fcde
	movb (%eax,%esi), %dl	/* load current char */
Packit Service 82fcde
	movb %dl, (%eax)	/* and store it */
Packit Service 82fcde
	testb %dl, %dl		/* was it NUL? */
Packit Service 82fcde
	jz L(3)			/* yes, then exit */
Packit Service 82fcde
Packit Service 82fcde
	incl %eax		/* increment pointer */
Packit Service 82fcde
	jmp L(9)		/* we don't have to test for counter underflow
Packit Service 82fcde
				   because we know we had a most 3 bytes
Packit Service 82fcde
				   remaining => exit */
Packit Service 82fcde
Packit Service 82fcde
	/* When coming from the main loop we have to adjust the pointer.  */
Packit Service 82fcde
L(4):	decl %ecx		/* decrement counter */
Packit Service 82fcde
	incl %eax		/* increment pointer */
Packit Service 82fcde
Packit Service 82fcde
L(5):	decl %ecx		/* increment pointer */
Packit Service 82fcde
	incl %eax		/* increment pointer */
Packit Service 82fcde
Packit Service 82fcde
L(6):	decl %ecx		/* increment pointer */
Packit Service 82fcde
	incl %eax		/* increment pointer */
Packit Service 82fcde
L(7):
Packit Service 82fcde
Packit Service 82fcde
	addl $3, %ecx		/* correct pre-decrementation of counter
Packit Service 82fcde
				   at the beginning of the loop; but why 3
Packit Service 82fcde
				   and not 4?  Very simple, we have to count
Packit Service 82fcde
				   the NUL char we already wrote.  */
Packit Service 82fcde
	jz L(9)			/* counter is also 0 => exit */
Packit Service 82fcde
Packit Service 82fcde
	/* We now have to fill the rest of the buffer with NUL.  This
Packit Service 82fcde
	   is done in a tricky way.  Please note that the addressing mode
Packit Service 82fcde
	   used below is not the same we used above.  Here we use the
Packit Service 82fcde
	   %ecx register.  */
Packit Service 82fcde
L(8):
Packit Service 82fcde
	movb $0, (%ecx,%eax)	/* store NUL char */
Packit Service 82fcde
L(3):	decl %ecx		/* all bytes written? */
Packit Service 82fcde
	jnz L(8)		/* no, then again */
Packit Service 82fcde
Packit Service 82fcde
L(9):	popl %esi		/* restore saved register content */
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 (__stpncpy)
Packit Service 82fcde
Packit Service 82fcde
libc_hidden_def (__stpncpy)
Packit Service 82fcde
weak_alias (__stpncpy, stpncpy)