summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libg++/libg++/bitlcomp.c
blob: edf154c4d02cb6c5d7d2e44774f1bf3355a61349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Copyright (C) 1994 Free Software Foundation

This file is part of the GNU BitString Library.  This library is free
software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option)
any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

As a special exception, if you link this library with files
compiled with a GNU compiler to produce an executable, this does not cause
the resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why
the executable file might be covered by the GNU General Public License. */

/*  Written by Per Bothner (bothner@cygnus.com) */

#include <stdlib.h>
#include "bitprims.h"

/* Return -1, 0, 1 depending on whether (ptr0, len0) is
   lexicographically less than, equal, or greater than (ptr1, len1).
   Both bitstrings must be left-aligned. */

int
_BS_lcompare_0 (ptr0, len0, ptr1, len1)
     register _BS_word *ptr0;
     _BS_size_t len0;
     register _BS_word *ptr1;
     _BS_size_t len1;
{
  _BS_size_t nwords0 = len0 / _BS_BITS_PER_WORD;
  _BS_size_t nwords1 = len1 / _BS_BITS_PER_WORD;
  register _BS_word word0, word1, mask;
  _BS_size_t nwords = nwords0 > nwords1 ? nwords1 : nwords0;
  for (; nwords != 0; nwords--)
    {
      word0 = *ptr0++;
      word1 = *ptr1++;
      if (word0 != word1)
	{
#if _BS_BIGENDIAN
	  return (word0 < word1) ? -1 : 1;
#else
	  mask = 1;
	  for (;;)
	    {
	      int bit0 = word0 & 1;
	      int bit1 = word1 & 1;
	      int diff = bit0 - bit1;
	      if (diff)
		return diff;
	      word0 >>= 1;
	      word1 >>= 1;
	    }
#endif
	}
    }
  len0 -= nwords0 * _BS_BITS_PER_WORD;
  len1 -= nwords1 * _BS_BITS_PER_WORD;
  if (len0 == 0 || len1 == 0)
    return len0 == 0 - len1 == 0;
  len0 &= _BS_BITS_PER_WORD - 1;
  len1 &= _BS_BITS_PER_WORD - 1;
  word0 = *ptr0++ & ~((_BS_word)(~0) _BS_RIGHT len0);
  word1 = *ptr1++ & ~((_BS_word)(~0) _BS_RIGHT len1);
  if (word0 == word1)
    return len0 == len1 ? 0 : len0 < len1 ? -1 : 1;
#if _BS_BIGENDIAN
  return (word0 < word1) ? -1 : 1;
#else
  for (;;)
    {
      int bit0 = word0 & 1;
      int bit1 = word1 & 1;
      int diff = bit0 - bit1;
      if (diff)
	return diff;
      word0 >>= 1;
      word1 >>= 1;
    }
#endif
}

OpenPOWER on IntegriCloud