2.9
[glibc/nacl-glibc.git] / sysdeps / i386 / stpcpy.S
blob50f9c5ea5ff1221c14abb6adc150d76a11c05855
1 /* Copy SRC to DEST returning the address of the terminating '\0' in DEST.
2    For Intel 80x86, x>=3.
3    Copyright (C) 1994, 1995, 1996, 1997, 2000, 2002, 2004
4    Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6    Contributed by Ulrich Drepper (drepper@gnu.ai.mit.edu).
8    The GNU C Library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 2.1 of the License, or (at your option) any later version.
13    The GNU C Library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
18    You should have received a copy of the GNU Lesser General Public
19    License along with the GNU C Library; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21    02111-1307 USA.  */
23 /* This function is defined neither in ANSI nor POSIX standards but is
24    also not invented here.  */
26 #include <sysdep.h>
27 #include "asm-syntax.h"
28 #include "bp-sym.h"
29 #include "bp-asm.h"
31 #define PARMS   LINKAGE         /* no space for saved regs */
32 #define RTN     PARMS
33 #define DEST    RTN+RTN_SIZE
34 #define SRC     DEST+PTR_SIZE
36         .text
37 ENTRY (BP_SYM (__stpcpy))
38         ENTER
40         movl DEST(%esp), %eax
41         movl SRC(%esp), %ecx
42         CHECK_BOUNDS_LOW (%eax, DEST(%esp))
43         CHECK_BOUNDS_LOW (%ecx, SRC(%esp))
44         subl %eax, %ecx         /* magic: reduce number of loop variants
45                                    to one using addressing mode */
47         /* Here we would like to write
49         subl $4, %eax
50         ALIGN (4)
52         but the assembler is too smart and optimizes for the shortest
53         form where the number only needs one byte.  But if we could
54         have the long form we would not need the alignment.  */
56         .byte 0x81, 0xe8        /* This is `subl $0x00000004, %eax' */
57         .long 0x00000004
59         /* Four times unfolded loop with only one loop counter.  This
60            is achieved by the use of index+base addressing mode.  As the
61            loop counter we use the destination address because this is
62            also the result.  */
63 L(1):   addl $4, %eax           /* increment loop counter */
65         movb (%eax,%ecx), %dl   /* load current char */
66         movb %dl, (%eax)        /* and store it */
67         testb %dl, %dl          /* was it NUL? */
68         jz L(2)                 /* yes, then exit */
70         movb 1(%eax,%ecx), %dl  /* load current char */
71         movb %dl, 1(%eax)       /* and store it */
72         testb %dl, %dl          /* was it NUL? */
73         jz L(3)                 /* yes, then exit */
75         movb 2(%eax,%ecx), %dl  /* load current char */
76         movb %dl, 2(%eax)       /* and store it */
77         testb %dl, %dl          /* was it NUL? */
78         jz L(4)                 /* yes, then exit */
80         movb 3(%eax,%ecx), %dl  /* load current char */
81         movb %dl, 3(%eax)       /* and store it */
82         testb %dl, %dl          /* was it NUL? */
83         jnz L(1)                /* no, then continue loop */
85         incl %eax               /* correct loop counter */
86 L(4):   incl %eax
87 L(3):   incl %eax
88 L(2):
89         CHECK_BOUNDS_HIGH (%eax, DEST(%esp), jb)
90         RETURN_BOUNDED_POINTER (DEST(%esp))
92         LEAVE
93         RET_PTR
94 END (BP_SYM (__stpcpy))
96 weak_alias (BP_SYM (__stpcpy), BP_SYM (stpcpy))
97 libc_hidden_def (__stpcpy)
98 libc_hidden_builtin_def (stpcpy)