home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libg++ / src / bitlcomp.c < prev    next >
C/C++ Source or Header  |  1994-05-22  |  3KB  |  93 lines

  1. /* Copyright (C) 1994 Free Software Foundation
  2.  
  3. This file is part of the GNU BitString Library.  This library is free
  4. software; you can redistribute it and/or modify it under the
  5. terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with GNU CC; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. As a special exception, if you link this library with files
  19. compiled with a GNU compiler to produce an executable, this does not cause
  20. the resulting executable to be covered by the GNU General Public License.
  21. This exception does not however invalidate any other reasons why
  22. the executable file might be covered by the GNU General Public License. */
  23.  
  24. /*  Written by Per Bothner (bothner@cygnus.com) */
  25.  
  26. #include "bitprims.h"
  27. #include <stdlib.h>
  28.  
  29. /* Return -1, 0, 1 depending on whether (ptr0, len0) is
  30.    lexicographically less than, equal, or greater than (ptr1, len1).
  31.    Both bitstrings must be left-aligned. */
  32.  
  33. int
  34. _BS_lcompare_0 (ptr0, len0, ptr1, len1)
  35.      register _BS_word *ptr0;
  36.      _BS_size_t len0;
  37.      register _BS_word *ptr1;
  38.      _BS_size_t len1;
  39. {
  40.   _BS_size_t nwords0 = len0 / _BS_BITS_PER_WORD;
  41.   _BS_size_t nwords1 = len1 / _BS_BITS_PER_WORD;
  42.   register _BS_word word0, word1, mask;
  43.   _BS_size_t nwords = nwords0 > nwords1 ? nwords1 : nwords0;
  44.   for (; nwords != 0; nwords--)
  45.     {
  46.       word0 = *ptr0++;
  47.       word1 = *ptr1++;
  48.       if (word0 != word1)
  49.     {
  50. #if _BS_BIGENDIAN
  51.       return (word0 < word1) ? -1 : 1;
  52. #else
  53.       mask = 1;
  54.       for (;;)
  55.         {
  56.           int bit0 = word0 & 1;
  57.           int bit1 = word1 & 1;
  58.           int diff = bit0 - bit1;
  59.           if (diff)
  60.         return diff;
  61.           word0 >>= 1;
  62.           word1 >>= 1;
  63.         }
  64. #endif
  65.     }
  66.     }
  67.   len0 -= nwords0 * _BS_BITS_PER_WORD;
  68.   len1 -= nwords1 * _BS_BITS_PER_WORD;
  69.   if (len0 == 0 || len1 == 0)
  70.     return len0 == 0 - len1 == 0;
  71.   len0 &= _BS_BITS_PER_WORD - 1;
  72.   len1 &= _BS_BITS_PER_WORD - 1;
  73.   word0 = *ptr0++ & ~((_BS_word)(~0) _BS_RIGHT len0);
  74.   word1 = *ptr1++ & ~((_BS_word)(~0) _BS_RIGHT len1);
  75.   if (word0 == word1)
  76.     return len0 == len1 ? 0 : len0 < len1 ? -1 : 1;
  77. #if _BS_BIGENDIAN
  78.   return (word0 < word1) ? -1 : 1;
  79. #else
  80.   for (;;)
  81.     {
  82.       int bit0 = word0 & 1;
  83.       int bit1 = word1 & 1;
  84.       int diff = bit0 - bit1;
  85.       if (diff)
  86.     return diff;
  87.       word0 >>= 1;
  88.       word1 >>= 1;
  89.     }
  90. #endif
  91. }
  92.  
  93.