home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / sysdeps / i386 / __stpncpy.c.old < prev    next >
Encoding:
Text File  |  1994-11-19  |  2.9 KB  |  113 lines

  1. /* stpncpy -- copy no more then N bytes from SRC to DEST, returning the
  2.           address of the terminating '\0' in DEST.
  3.    For Intel 80x86, x>=3.
  4.    Copyright (C) 1994 Free Software Foundation, Inc.
  5.    Contributed by Ulrich Drepper <drepper@ira.uka.de>.
  6.  
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11.  
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Library General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public
  18. License along with the GNU C Library; see the file COPYING.LIB.  If
  19. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  20. Cambridge, MA 02139, USA.  */
  21.  
  22. /* This function is defined neither in ANSI nor POSIX standards but is
  23.    also not invented here.  */
  24.  
  25. #include <string.h>
  26. #include "asm-ops.h"
  27.  
  28. char *
  29. __stpncpy(char * dest, const char * src, size_t n)
  30. {
  31. register char * __res;
  32. __asm__("subl %0,%1\n\t"
  33.     "addl $4,%3\n\t"
  34.     "subl $4,%0\n"
  35. /* four time unfold loop */
  36. LL(1)    "\taddl $4,%0\n\t"
  37.     "subl $4,%3\n\t"
  38.     "testl $0xfffffffc,%3\n\t"
  39.     "je " LF(4) "\n\t"
  40.     "movb (%0,%1),%%dl\n\t"
  41.     "movb %%dl,(%0)\n\t"
  42.     "testb %%dl,%%dl\n\t"
  43.     "je " LF(3) "\n\t"
  44.     "movb 1(%0,%1),%%dl\n\t"
  45.     "movb %%dl,1(%0)\n\t"
  46.     "testb %%dl,%%dl\n\t"
  47.     "je " LF(6) "\n\t"
  48.     "movb 2(%0,%1),%%dl\n\t"
  49.     "movb %%dl,2(%0)\n\t"
  50.     "testb %%dl,%%dl\n\t"
  51.     "je " LF(5) "\n\t"
  52.     "movb 3(%0,%1),%%dl\n\t"
  53.     "movb %%dl,3(%0)\n\t"
  54.     "testb %%dl,%%dl\n\t"
  55.     "jne " LB(1) "\n\t"
  56.     "decl %3\n\t"
  57.     "incl %0\n"
  58. LL(5)    "\tdecl %3\n\t"
  59.     "incl %0\n"
  60. LL(6)    "\tdecl %3\n\t"
  61.     "incl %0\n\t"
  62.     "jmp " LF(3) "\n"
  63.  
  64. /* rest of loop */
  65. LL(4)    "testb %3,%3\n\t"
  66.     "je " LF(2) "\n\t"
  67.     "movb (%0,%1),%%dl\n\t"
  68.     "movb %%dl,(%0)\n\t"
  69.     "incl %0\n\t"
  70.     "testb %%dl,%%dl\n\t"
  71.     "je " LF(32) "\n\t"
  72.     "decl %3\n\t"
  73.     "je " LF(2) "\n\t"
  74.  
  75.     "movb (%0,%1),%%dl\n\t"
  76.     "movb %%dl,(%0)\n\t"
  77.     "incl %0\n\t"
  78.     "testb %%dl,%%dl\n\t"
  79.     "je " LF(32) "\n\t"
  80.     "decl %3\n\t"
  81.     "je " LF(2) "\n\t"
  82.  
  83.     "movb (%0,%1),%%dl\n\t"
  84.     "movb %%dl,(%0)\n\t"
  85.     "incl %0\n\t"
  86.     "testb %%dl,%%dl\n\t"
  87.     "je " LF(32) "\n\t"
  88.     "decl %3\n\t"
  89.     "je " LF(2) "\n\t"
  90.  
  91. /* fill with zero.  Here is a difference between how I understand the
  92.    function and how it is implemented in the GNU libc.  In glibc the pointer
  93.    returned is to the LAST '\0' written.  But I think is more useful to point
  94.    to the '\0' terminating the string.  */
  95. #ifdef GLIBC_COMPAT
  96. LL(3)    "\tmovb $0,(%0)\n\t"
  97.     "incl %0\n"
  98. LL(32)    "\tdecl %3\n\t"
  99. #else
  100. LL(3)    "\tmovb $0,(%3,%0)\n"
  101. LL(32)    "decl %3\n\t"
  102. #endif
  103.     "jne " LB(3) "\n"
  104. LL(2)    "movb $0,(%0)\n\t"
  105.     :"=a" (__res):"S" (src),"0" (dest),"c" (n):"si","cx","dx");
  106. return __res;
  107. }
  108.  
  109. #include <gnu-stabs.h>
  110. #ifdef weak_alias
  111. weak_alias (__stpncpy, stpncpy);
  112. #endif
  113.