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 / memchr.c.old < prev    next >
Encoding:
Text File  |  1994-08-20  |  1.9 KB  |  66 lines

  1. /* memchr (str, ch, n) -- Return pointer to first occurrence of CH in STR less
  2.    than N.
  3.    For Intel 80x86, x>=3.
  4.    Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  5.    Contributed by Torbjorn Granlund (tege@sics.se).
  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. #include <ansidecl.h>
  23. #include <string.h>
  24.  
  25. #ifdef    __GNUC__
  26.  
  27. #include "asm-ops.h"
  28.  
  29. PTR
  30. DEFUN(memchr, (str, c, len),
  31.       CONST PTR str AND int c AND size_t len)
  32. {
  33. #if 1
  34.   PTR retval;
  35.  
  36.   if (!len) return NULL;
  37.  
  38.   __asm__ __volatile__ ("cld\n\t"
  39.       "repne\n\t"
  40.       "scasb\n\t"
  41.       "je " LF(1) "\n\t"
  42.       "movl $1,%0\n"
  43.       LL(1) "\tdecl %0"
  44.       :"=D" (retval):"a" (c),"D" (str),"c" (len) :"cx");
  45.  
  46. #else
  47.   PTR retval;
  48.   asm("cld\n"            /* Search forward.  */
  49.       "testl %1,%1\n"        /* Clear Z flag, to handle LEN == 0.  */
  50.       /* Some old versions of gas need `repne' instead of `repnz'.  */
  51.       "repnz\n"            /* Search for C in al.  */
  52.       "scasb\n"
  53.       "movl %2,%0\n"        /* Set %0 to 0 (without affecting Z flag).  */
  54.       "jnz done\n"        /* Jump if we found nothing equal to C.  */
  55.       "leal -1(%1),%0\n"    /* edi has been incremented.  Return edi-1.  */
  56.       "done:" :
  57.       "=a" (retval), "=D" (str), "=c" (len) :
  58.       "0" (c), "1" (str), "2" (len));
  59. #endif
  60.   return retval;
  61. }
  62.  
  63. #else
  64. #include <sysdeps/generic/memchr.c>
  65. #endif
  66.