home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / backup / star-1.3.1.tar.gz / star-1.3.1.tar / star-1.3.1 / lib / cmpbytes.c < prev    next >
C/C++ Source or Header  |  2000-05-07  |  2KB  |  104 lines

  1. /* @(#)cmpbytes.c    1.13 00/05/07 Copyright 1988 J. Schilling */
  2. #ifndef lint
  3. static    char sccsid[] =
  4.     "@(#)cmpbytes.c    1.13 00/05/07 Copyright 1988 J. Schilling";
  5. #endif  /* lint */
  6. /*
  7.  *    compare data
  8.  *
  9.  *    Copyright (c) 1988 J. Schilling
  10.  */
  11. /*
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2, or (at your option)
  15.  * any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; see the file COPYING.  If not, write to
  24.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  */
  26.  
  27. #include <standard.h>
  28. #include <align.h>
  29. #include <schily.h>
  30.  
  31. #define    DO8(a)    a;a;a;a;a;a;a;a;
  32.  
  33. int cmpbytes(fromp, top, cnt)
  34.     const void    *fromp;
  35.     const void    *top;
  36.     int        cnt;
  37. {
  38.     register const char    *from    = (char *)fromp;
  39.     register const char    *to    = (char *)top;
  40.     register int        n;
  41.  
  42.     /*
  43.      * If we change cnt to be unsigned, check for == instead of <=
  44.      */
  45.     if ((n = cnt) <= 0)
  46.         return (cnt);
  47.  
  48.     if (n >= (int)(8 * sizeof(long))) {
  49.         if (l2aligned(from, to)) {
  50.             register const long *froml = (const long *)from;
  51.             register const long *tol   = (const long *)to;
  52.             register int rem = n % (8 * sizeof (long));
  53.  
  54.             n /= (8 * sizeof (long));
  55.             do {
  56.                 DO8 (
  57.                     if (*tol++ != *froml++)
  58.                         break;
  59.                 );
  60.             } while (--n > 0);
  61.  
  62.             if (n > 0) {
  63.                 --froml;
  64.                 --tol;
  65.                 to = (const char *)tol;
  66.                 from = (const char *)froml;
  67.                 goto ldiff;
  68.             }
  69.             to = (const char *)tol;
  70.             from = (const char *)froml;
  71.             n = rem;
  72.         }
  73.  
  74.         if (n >= 8) {
  75.             n -= 8;
  76.             do {
  77.                 DO8 (
  78.                     if (*to++ != *from++)
  79.                         goto cdiff;
  80.                 );
  81.             } while ((n -= 8) >= 0);
  82.             n += 8;
  83.         }
  84.         if (n > 0) do {
  85.             if (*to++ != *from++)
  86.                 goto cdiff;
  87.         } while (--n > 0);
  88.         return (cnt);
  89.     }
  90.     if (n > 0) do {
  91.         if (*to++ != *from++)
  92.             goto cdiff;
  93.     } while (--n > 0);
  94.     return (cnt);
  95. ldiff:
  96.     n = sizeof(long);
  97.     do {
  98.         if (*to++ != *from++)
  99.             goto cdiff;
  100.     } while (--n > 0);
  101. cdiff:
  102.     return (--from - (char *)fromp);
  103. }
  104.