Update from main archive 961219
[glibc.git] / sysdeps / i386 / stpcpy.S
blob73292ab0fbee8f2d5721225da22160fc2e583077
1 /* stpcpy -- copy SRC to DEST returning the address of the terminating '\0'
2              in DEST.
3    For Intel 80x86, x>=3.
4    Copyright (C) 1994, 1995, 1996 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 Library General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    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    Library General Public License for more details.
18    You should have received a copy of the GNU Library General Public
19    License along with the GNU C Library; see the file COPYING.LIB.  If not,
20    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 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"
30    INPUT PARAMETERS:
31    dest         (sp + 4)
32    src          (sp + 8)
35         .text
36 ENTRY (__stpcpy)
37         movl 4(%esp), %eax      /* load destination pointer */
38         movl 8(%esp), %ecx      /* load source pointer */
40         subl %eax, %ecx         /* magic: reduce number of loop variants
41                                    to one using addressing mode */
43         /* Here we would like to write
45         subl $4, %eax
46         ALIGN (4)
48         but the assembler is too smart and optimizes for the shortest
49         form where the number only needs one byte.  But if we could
50         have the long form we would not need the alignment.  */
52         .byte 0x81, 0xe8        /* This is `subl $0x00000004, %eax' */
53         .long 0x00000004
55         /* Four times unfolded loop with only one loop counter.  This
56            is achieved by the use of index+base addressing mode.  As the
57            loop counter we use the destination address because this is
58            also the result.  */
59 L1:     addl $4, %eax           /* increment loop counter */
61         movb (%eax,%ecx), %dl   /* load current char */
62         movb %dl, (%eax)        /* and store it */
63         testb %dl, %dl          /* was it NUL? */
64         jz L2                   /* yes, then exit */
66         movb 1(%eax,%ecx), %dl  /* load current char */
67         movb %dl, 1(%eax)       /* and store it */
68         testb %dl, %dl          /* was it NUL? */
69         jz L3                   /* yes, then exit */
71         movb 2(%eax,%ecx), %dl  /* load current char */
72         movb %dl, 2(%eax)       /* and store it */
73         testb %dl, %dl          /* was it NUL? */
74         jz L4                   /* yes, then exit */
76         movb 3(%eax,%ecx), %dl  /* load current char */
77         movb %dl, 3(%eax)       /* and store it */
78         testb %dl, %dl          /* was it NUL? */
79         jnz L1                  /* no, then continue loop */
81         incl %eax               /* correct loop counter */
82 L4:     incl %eax
83 L3:     incl %eax
84 L2:
85         ret
86 END (__stpcpy)
88 weak_alias (__stpcpy, stpcpy)