home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / string.arc / BCMP.C < prev    next >
C/C++ Source or Header  |  1984-12-31  |  2KB  |  53 lines

  1. /*  File   : bcmp.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 23 April 1984
  4.     Defines: bcmp()
  5.  
  6.     bcmp(s1, s2, len) returns 0 if the "len" bytes starting at "s1" are
  7.     identical to the "len" bytes starting at "s2", non-zero if they are
  8.     different.   The 4.2bsd manual page doesn't say what non-zero value
  9.     is returned, though the BUGS note says that it takes its parameters
  10.     backwards from strcmp.  This suggests that it is something like
  11.     for (; --len >= 0; s1++, s2++)
  12.         if (*s1 != *s2) return *s2-*s1;
  13.     return 0;
  14.     There, I've told you how to do it.  As the manual page doesn't come
  15.     out and *say* that this is the result, I tried to figure out what a
  16.     useful result might be.   (I'd forgotten than strncmp stops when it
  17.     hits a NUL, which the above does not do.)  What I came up with was:
  18.     the result is the number of bytes in the differing tails.  That is,
  19.     after you've skipped the equal parts, how many characters are left?
  20.     To put it another way, N-bcmp(s1,s2,N) is the number of equal bytes
  21.     (the size of the common prefix).  After deciding on this definition
  22.     I discovered that the CMPC3 instruction does exactly what I wanted.
  23.     The code assumes that N is non-negative.
  24.  
  25.     Note: the "b" routines are there to exploit certain VAX order codes,
  26.     but the CMPC3 instruction will only test 65535 characters.   The asm
  27.     code is presented for your interest and amusement.
  28. */
  29.  
  30. #include "strings.h"
  31.  
  32. #if    VaxAsm
  33.  
  34. int bcmp(s1, s2, len)
  35.     char *s1, *s2;
  36.     int len;
  37.     {
  38.     asm("cmpc3 12(ap),*4(ap),*8(ap)");
  39.     }
  40.  
  41. #else  ~VaxAsm
  42.  
  43. int bcmp(s1, s2, len)
  44.     register char *s1, *s2;
  45.     register int len;
  46.     {
  47.     while (--len >= 0 && *s1++ == *s2++) ;
  48.     return len+1;
  49.     }
  50.  
  51. #endif    VaxAsm
  52.