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 / strstr.c.new < prev    next >
Encoding:
Text File  |  1994-12-13  |  4.2 KB  |  166 lines

  1. /* 
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.    Contributed by Ulrich Drepper <drepper@ira.uka.de>.
  4.  
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with the GNU C Library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.  */
  19.  
  20. #include <string.h>
  21.  
  22. #include "asm-ops.h"
  23.  
  24. char *
  25. strstr(const char *haystack, const char *needle)
  26. {
  27. register char *__res;
  28. __asm__(
  29. #if defined(__PIC__) || defined(__pic__)
  30.         /*
  31.      * As with gcc-2.6.2 it has problems with the %ebx handling.  It
  32.      * should know that %ebx is a special register and must be saved
  33.      * but it don't.  So we have to do it by hand...
  34.      */
  35.         "pushl %%ebx\n\t"
  36. #endif
  37.  
  38.     "movl %1,%0\n\t"    /* we keep the possible result in %0 */
  39.     
  40.     "testb $0xff,(%2)\n\t"    /* is needle == "" ? */
  41.     "je " LF(1) "\n\t"    /* yes, return haystack */
  42.  
  43.     /* Because the first two characters are the ones that are
  44.      * compared more often than all others we keep them in %cx
  45.      * all the time.
  46.      */    
  47.     "movw (%2),%%cx\n\t"
  48.     "addl $2,%2\n\t"
  49.  
  50.     /* But we have to handle the special case of an one-char string */
  51.     "orb %%ch,%%ch\n\t"    /* seconf character NULL? */
  52.     "jne " LF(5) "\n"    /* no, than branch */
  53.  
  54.     
  55. /* handle special case of a one-character-needle */
  56. LL(6)    "\tmovb (%1),%%al\n\t"    /* load char */
  57.     "orb %%al,%%al\n\t"    /* is NULL? */
  58.     "je " LF(2) "\n\t"    /* yes, than exit */
  59.     "incl %1\n\t"        /* increment string pointer */
  60.     "xorb %%cl,%%al\n\t"    /* equal to string character ? */
  61.     "jne " LB(6) "\n\t"    /* no, than search again */
  62.     "leal -1(%1),%0\n\t"    /* load result pointer to %0 */
  63.     "jmp " LF(1) "\n"    /* the END */
  64.  
  65.     /* Handle normal case: length needle >= 2 */
  66. LL(5)    "\ttestb $0xff,(%1)\n\t"
  67.     "je " LF(2) "\n\t"
  68.  
  69.     "cmpb %%cl,%%ch\n\t"
  70.     "jne " LF(8) "\n"
  71.  
  72. LL(4)    "\tmovw (%1),%%dx\n\t"
  73.     "cmpw %%dx,%%cx\n\t"
  74.     "je " LF(30) "\n\t"
  75.     "orb %%dh,%%dh\n\t"
  76.     "je " LF(2) "\n\t"
  77.     "movw 1(%1),%%dx\n\t"
  78.     "cmpw %%dx,%%cx\n\t"
  79.     "je " LF(31) "\n\t"
  80.     "orb %%dh,%%dh\n\t"
  81.     "je " LF(2) "\n\t"
  82.     "movw 2(%1),%%dx\n\t"
  83.     "cmpw %%dx,%%cx\n\t"
  84.     "je " LF(32) "\n\t"
  85.     "orb %%dh,%%dh\n\t"
  86.     "je " LF(2) "\n\t"
  87.     "movw 3(%1),%%dx\n\t"
  88.     "cmpw %%dx,%%cx\n\t"
  89.     "je " LF(33) "\n\t"
  90.     "orb %%dh,%%dh\n\t"
  91.     "je " LF(2) "\n\t"
  92.     "addl $4,%1\n\t"
  93.     "jmp " LB(4) "\n"
  94.  
  95. LL(33)    "\tincl %1\n"
  96. LL(32)    "\tincl %1\n"
  97. LL(31)    "\tincl %1\n"
  98. LL(30)    "\tmovl %1,%0\n\t"
  99.     "addl $2,%1\n\t"
  100.     "movl %2,%%ebx\n"
  101. LL(7)    "\tmovb (%%ebx),%%dh\n\t"
  102.     "incl %%ebx\n\t"
  103.     "orb %%dh,%%dh\n\t"
  104.     "je " LF(1) "\n\t"
  105.     "movb (%1),%%dl\n\t"
  106.     "incl %1\n\t"
  107.     "xorb %%dl,%%dh\n\t"
  108.     "je " LB(7) "\n\t"
  109.     "orb %%dl,%%dl\n\t"
  110.     "je " LF(2) "\n\t"
  111.     "leal 1(%0),%1\n\t"
  112.     "jmp " LB(4) "\n"
  113.  
  114. LL(8)    "\tmovw (%1),%%dx\n\t"
  115.     "cmpw %%dx,%%cx\n\t"
  116.     "je " LF(90) "\n\t"
  117.     "orb %%dh,%%dh\n\t"
  118.     "je " LF(2) "\n\t"
  119.     "movw 1(%1),%%dx\n\t"
  120.     "cmpw %%dx,%%cx\n\t"
  121.     "je " LF(91) "\n\t"
  122.     "orb %%dh,%%dh\n\t"
  123.     "je " LF(2) "\n\t"
  124.     "movw 2(%1),%%dx\n\t"
  125.     "cmpw %%dx,%%cx\n\t"
  126.     "je " LF(92) "\n\t"
  127.     "orb %%dh,%%dh\n\t"
  128.     "je " LF(2) "\n\t"
  129.     "movw 3(%1),%%dx\n\t"
  130.     "cmpw %%dx,%%cx\n\t"
  131.     "je " LF(93) "\n\t"
  132.     "orb %%dh,%%dh\n\t"
  133.     "je " LF(2) "\n\t"
  134.     "addl $4,%1\n\t"
  135.     "jmp " LB(8) "\n"
  136.  
  137. LL(93)    "\tincl %1\n"
  138. LL(92)    "\tincl %1\n"
  139. LL(91)    "\tincl %1\n"
  140. LL(90)    "\tmovl %1,%0\n\t"
  141.     "addl $2,%1\n\t"
  142.     "movl %2,%%ebx\n"
  143. LL(10)    "\tmovb (%%ebx),%%dh\n\t"
  144.     "incl %%ebx\n\t"
  145.     "orb %%dh,%%dh\n\t"
  146.     "je " LF(1) "\n\t"
  147.     "movb (%1),%%dl\n\t"
  148.     "incl %1\n\t"
  149.     "xorb %%dl,%%dh\n\t"
  150.     "je " LB(10) "\n\t"
  151.     "orb %%dl,%%dl\n\t"
  152.     "je " LF(2) "\n\t"
  153.     "leal 2(%0),%1\n\t"
  154.     "jmp " LB(8) "\n"
  155.  
  156. LL(2)    "\txorl %0,%0\n"
  157. LL(1)
  158. #if defined(__PIC__) || defined(__pic__)
  159.     "\tpopl %%ebx\n"
  160.         :"=a" (__res):"S" (haystack),"D" (needle):"si","di","cx","dx");
  161. #else
  162.         :"=a" (__res):"S" (haystack),"D" (needle):"si","di","bx","cx","dx");
  163. #endif
  164.   return __res;
  165. }
  166.