summaryrefslogtreecommitdiffstats
path: root/contrib/gcc/f/bit.c
blob: 00f064b1da22a6c545abd35781f7441cfbf4a3aa (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* bit.c -- Implementation File (module.c template V1.0)
   Copyright (C) 1995 Free Software Foundation, Inc.
   Contributed by James Craig Burley.

This file is part of GNU Fortran.

GNU Fortran 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.

GNU Fortran 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 Fortran; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.

   Related Modules:
      None

   Description:
      Tracks arrays of booleans in useful ways.

   Modifications:
*/

/* Include files. */

#include "proj.h"
#include "bit.h"
#include "malloc.h"

/* Externals defined here. */


/* Simple definitions and enumerations. */


/* Internal typedefs. */


/* Private include files. */


/* Internal structure definitions. */


/* Static objects accessed by functions in this module. */


/* Static functions (internal). */


/* Internal macros. */


/* ffebit_count -- Count # of bits set a particular way

   ffebit b;  // the ffebit object
   ffebitCount offset;	// 0..size-1
   bool value;	// FALSE (0), TRUE (1)
   ffebitCount range;  // # bits to test
   ffebitCount number;	// # bits equal to value
   ffebit_count(b,offset,value,range,&number);

   Sets <number> to # bits at <offset> through <offset + range - 1> set to
   <value>.  If <range> is 0, <number> is set to 0.  */

void
ffebit_count (ffebit b, ffebitCount offset, bool value, ffebitCount range,
	      ffebitCount *number)
{
  ffebitCount element;
  ffebitCount bitno;

  assert (offset + range <= b->size);

  for (*number = 0; range != 0; --range, ++offset)
    {
      element = offset / CHAR_BIT;
      bitno = offset % CHAR_BIT;
      if (value
	  == ((b->bits[element] & ((unsigned char) 1 << bitno)) == 0 ? FALSE : TRUE))
	++ * number;
    }
}

/* ffebit_new -- Create a new ffebit object

   ffebit b;
   ffebit_kill(b);

   Destroys an ffebit object obtained via ffebit_new.  */

void
ffebit_kill (ffebit b)
{
  malloc_kill_ks (b->pool, b,
		  offsetof (struct _ffebit_, bits)
		  + (b->size + CHAR_BIT - 1) / CHAR_BIT);
}

/* ffebit_new -- Create a new ffebit object

   ffebit b;
   mallocPool pool;
   ffebitCount size;
   b = ffebit_new(pool,size);

   Allocates an ffebit object that holds the values of <size> bits in pool
   <pool>.  */

ffebit
ffebit_new (mallocPool pool, ffebitCount size)
{
  ffebit b;

  b = malloc_new_zks (pool, "ffebit",
		      offsetof (struct _ffebit_, bits)
		      + (size + CHAR_BIT - 1) / CHAR_BIT,
		      0);
  b->pool = pool;
  b->size = size;

  return b;
}

/* ffebit_set -- Set value of # of bits

   ffebit b;  // the ffebit object
   ffebitCount offset;	// 0..size-1
   bool value;	// FALSE (0), TRUE (1)
   ffebitCount length;	// # bits to set starting at offset (usually 1)
   ffebit_set(b,offset,value,length);

   Sets bit #s <offset> through <offset + length - 1> to <value>.  */

void
ffebit_set (ffebit b, ffebitCount offset, bool value, ffebitCount length)
{
  ffebitCount i;
  ffebitCount element;
  ffebitCount bitno;

  assert (offset + length <= b->size);

  for (i = 0; i < length; ++i, ++offset)
    {
      element = offset / CHAR_BIT;
      bitno = offset % CHAR_BIT;
      b->bits[element] = (((unsigned char) (value ? 1 : 0)) << bitno)
	| (b->bits[element] & ~((unsigned char) 1 << bitno));
    }
}

/* ffebit_test -- Test value of # of bits

   ffebit b;  // the ffebit object
   ffebitCount offset;	// 0..size-1
   bool value;	// FALSE (0), TRUE (1)
   ffebitCount length;	// # bits with same value
   ffebit_test(b,offset,&value,&length);

   Returns value of bits at <offset> through <offset + length - 1> in
   <value>.  If <offset> is already at the end of the bit array (if
   offset == ffebit_size(b)), <length> is set to 0 and <value> is
   undefined.  */

void
ffebit_test (ffebit b, ffebitCount offset, bool *value, ffebitCount *length)
{
  ffebitCount i;
  ffebitCount element;
  ffebitCount bitno;

  if (offset >= b->size)
    {
      assert (offset == b->size);
      *length = 0;
      return;
    }

  element = offset / CHAR_BIT;
  bitno = offset % CHAR_BIT;
  *value = (b->bits[element] & ((unsigned char) 1 << bitno)) == 0 ? FALSE : TRUE;
  *length = 1;

  for (i = b->size - offset - 1, ++offset; i != 0; --i, ++offset, ++*length)
    {
      element = offset / CHAR_BIT;
      bitno = offset % CHAR_BIT;
      if (*value
	  != ((b->bits[element] & ((unsigned char) 1 << bitno)) == 0 ? FALSE : TRUE))
	break;
    }
}
OpenPOWER on IntegriCloud