home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / sysdeps / i386 / memchr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  1.7 KB  |  49 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. PTR
  28. DEFUN(memchr, (str, c, len),
  29.       CONST PTR str AND int c AND size_t len)
  30. {
  31.   PTR retval;
  32.   asm("cld\n"            /* Search forward.  */
  33.       "testl %1,%1\n"        /* Clear Z flag, to handle LEN == 0.  */
  34.       /* Some old versions of gas need `repne' instead of `repnz'.  */
  35.       "repnz\n"            /* Search for C in al.  */
  36.       "scasb\n"
  37.       "movl %2,%0\n"        /* Set %0 to 0 (without affecting Z flag).  */
  38.       "jnz done\n"        /* Jump if we found nothing equal to C.  */
  39.       "leal -1(%1),%0\n"    /* edi has been incremented.  Return edi-1.  */
  40.       "done:" :
  41.       "=a" (retval), "=D" (str), "=c" (len) :
  42.       "0" (c), "1" (str), "2" (len));
  43.   return retval;
  44. }
  45.  
  46. #else
  47. #include <sysdeps/generic/memchr.c>
  48. #endif
  49.