TBTK
ExtensiveBitRegister.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Bj√∂rnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_EXTENSIVE_BIT_REGISTER
24 #define COM_DAFER45_TBTK_EXTENSIVE_BIT_REGISTER
25 
26 #include "TBTK/Streams.h"
27 #include "TBTK/TBTKMacros.h"
28 
29 #include <climits>
30 
31 namespace TBTK{
32 
34 public:
37 
39  ExtensiveBitRegister(unsigned int numBits);
40 
42  ExtensiveBitRegister(const ExtensiveBitRegister &extensiveBitRegister);
43 
46 
49  const ExtensiveBitRegister &rhs
50  ) const;
51 
54  const ExtensiveBitRegister &rhs
55  ) const;
56 
59  const ExtensiveBitRegister &rhs
60  ) const;
61 
64  const ExtensiveBitRegister &rhs
65  ) const;
66 
69  const ExtensiveBitRegister &rhs
70  ) const;
71 
73  bool operator<(const ExtensiveBitRegister &rhs) const;
74 
76  bool operator>(const ExtensiveBitRegister &rhs) const;
77 
79  bool operator==(const ExtensiveBitRegister &rhs) const;
80 
82  void operator+=(const ExtensiveBitRegister &rhs);
83 
85  void operator-=(const ExtensiveBitRegister &rhs);
86 
89 
92 
95 
98 
100  void operator=(const ExtensiveBitRegister &rhs);
101 
103  void operator=(const unsigned int rhs);
104 
106  ExtensiveBitRegister operator<<(unsigned int rhs) const;
107 
109  ExtensiveBitRegister operator>>(unsigned int rhs) const;
110 
112  void setBit(unsigned int position, bool values);
113 
115  bool getBit(unsigned int position) const;
116 
119  bool toBool() const;
120 
123  unsigned int toUnsignedInt() const;
124 
126  void clear();
127 
129  void print() const;
130 
132  unsigned int getNumBits() const;
133 
135  unsigned int getNumOneBits() const;
136 
138  bool getMostSignificantBit() const;
139 
141  void setMostSignificantBit();
142 
145 
148 private:
150  unsigned int size;
151 
153  unsigned int *values;
154 
156  static constexpr unsigned int MOST_SIGNIFICANT_BIT_MASK
157  = (unsigned int)0x1 << (8*sizeof(unsigned int) - 1);
158 };
159 
161  const ExtensiveBitRegister &rhs
162 ) const{
163  TBTKAssert(
164  size == rhs.size,
165  "ExtensiveBitRegister::operator|()",
166  "Incompatible register sizes.",
167  ""
168  );
169 
171  for(unsigned int n = 0; n < size; n++)
172  result.values[n] = values[n] | rhs.values[n];
173 
174  return result;
175 }
176 
178  const ExtensiveBitRegister &rhs
179 ) const{
180  TBTKAssert(
181  size == rhs.size,
182  "ExtensiveBitRegister::operator|()",
183  "Incompatible register sizes.",
184  ""
185  );
186 
188  for(unsigned int n = 0; n < size; n++)
189  result.values[n] = values[n] & rhs.values[n];
190 
191  return result;
192 }
193 
195  const ExtensiveBitRegister &rhs
196 ) const{
197  TBTKAssert(
198  size == rhs.size,
199  "ExtensiveBitRegister::operator|()",
200  "Incompatible register sizes.",
201  ""
202  );
203 
205  for(unsigned int n = 0; n < size; n++)
206  result.values[n] = values[n] ^ rhs.values[n];
207 
208  return result;
209 }
210 
212  const ExtensiveBitRegister &rhs
213 ) const{
214  TBTKAssert(
215  size == rhs.size,
216  "ExtensiveBitRegister::operator+()",
217  "Incompatible register sizes.",
218  ""
219  );
220 
222  unsigned int carry = 0;
223  for(unsigned int n = 0; n < size; n++){
224  result.values[n] = values[n] + rhs.values[n];
225  unsigned int newCarry = (result.values[n] < values[n]);
226  result.values[n] += carry;
227  carry = newCarry | (result.values[n] < values[n]);
228  }
229 
230  return result;
231 }
232 
234  const ExtensiveBitRegister &rhs
235 ) const{
236  TBTKAssert(
237  size == rhs.size,
238  "ExtensiveBitRegister::operator+()",
239  "Incompatible register sizes.",
240  ""
241  );
242 
244  unsigned int carry = 0;
245  for(unsigned int n = 0; n < size; n++){
246  result.values[n] = values[n] - rhs.values[n];
247  unsigned int newCarry = (result.values[n] > values[n]);
248  result.values[n] -= carry;
249  carry = newCarry | (result.values[n] > values[n]);
250  }
251 
252  return result;
253 }
254 
256  const ExtensiveBitRegister &rhs
257 ) const{
258  for(int n = size-1; n >= 0; n--){
259  if(values[n] < rhs.values[n])
260  return true;
261  if(values[n] > rhs.values[n])
262  return false;
263  }
264 
265  return false;
266 }
267 
269  const ExtensiveBitRegister &rhs
270 ) const{
271  for(int n = size-1; n >= 0; n--){
272  if(values[n] > rhs.values[n])
273  return true;
274  if(values[n] < rhs.values[n])
275  return false;
276  }
277 
278  return false;
279 }
280 
282  const ExtensiveBitRegister &rhs
283 ) const{
284  for(unsigned int n = 0; n < size; n++){
285  if(values[n] != rhs.values[n])
286  return false;
287  }
288 
289  return true;
290 }
291 
293  TBTKAssert(
294  size == rhs.size,
295  "ExtensiveBitRegister::operator+=()",
296  "Incompatible register sizes.",
297  ""
298  );
299 
300  unsigned int carry = 0;
301  for(unsigned int n = 0; n < size; n++){
302  unsigned int temp = values[n];
303  values[n] = temp + rhs.values[n];
304  unsigned int newCarry = (values[n] < temp);
305  values[n] += carry;
306  carry = newCarry | (values[n] < temp);
307  }
308 }
309 
311  TBTKAssert(
312  size == rhs.size,
313  "ExtensiveBitRegister::operator-=()",
314  "Incompatible register sizes.",
315  ""
316  );
317 
318  unsigned int carry = 0;
319  for(unsigned int n = 0; n < size; n++){
320  unsigned int temp = values[n];
321  values[n] = temp - rhs.values[n];
322  unsigned int newCarry = (values[n] > temp);
323  values[n] -= carry;
324  carry = newCarry | (values[n] > temp);
325  }
326 }
327 
329  for(unsigned int n = 0; n < size; n++){
330  if(values[n] == UINT_MAX){
331  values[n]++;
332  }
333  else{
334  values[n]++;
335  break;
336  }
337  }
338 
339  return *this;
340 }
341 
343  ExtensiveBitRegister returnValue(*this);
344  ++(*this);
345 
346  return returnValue;
347 }
348 
350  for(unsigned int n = 0; n < size; n++){
351  if(values[n] == 0){
352  values[n]--;
353  }
354  else{
355  values[n]--;
356  break;
357  }
358  }
359 
360  return *this;
361 }
362 
364  ExtensiveBitRegister returnValue(*this);
365  --(*this);
366 
367  return returnValue;
368 }
369 
371  if(this != &rhs){
372  TBTKAssert(
373  size == rhs.size,
374  "ExtensiveBitRegister::operator=()",
375  "Incompatible register sizes.",
376  ""
377  );
378 
379  for(unsigned int n = 0; n < size; n++)
380  values[n] = rhs.values[n];
381  }
382 }
383 
384 inline void ExtensiveBitRegister::operator=(const unsigned int rhs){
385  values[0] = rhs;
386  for(unsigned int n = 1; n < size; n++)
387  values[n] = 0;
388 }
389 
391  ExtensiveBitRegister result(*this);
392  if(rhs > 8*sizeof(unsigned int)){
393  result = result << (rhs - 8*sizeof(unsigned int));
394 
395  for(int n = size-1; n > 0; n--)
396  result.values[n] = result.values[n-1];
397  result.values[0] = 0;
398  }
399  else if(rhs == 8*sizeof(unsigned int)){
400  for(int n = size-1; n > 0; n--)
401  result.values[n] = result.values[n-1];
402  result.values[0] = 0;
403  }
404  else if(rhs != 0){
405  for(int n = size-1; n >= 0; n--){
406  result.values[n] = result.values[n] << rhs;
407  if(n > 0)
408  result.values[n] |= result.values[n-1] >> (8*sizeof(unsigned int) - rhs);
409  }
410  }
411 
412  return result;
413 }
414 
416  ExtensiveBitRegister result(*this);
417  if(rhs > 8*sizeof(unsigned int)){
418  result = result >> (rhs - 8*sizeof(unsigned int));
419 
420  for(unsigned int n = 0; n < size-1; n++)
421  result.values[n] = result.values[n+1];
422  result.values[size-1] = 0;
423  }
424  else if(rhs != 0){
425  for(unsigned int n = 0; n < size; n++){
426  result.values[n] = result.values[n] >> rhs;
427  if(n < size-1)
428  result.values[n] |= result.values[n+1] << (8*sizeof(unsigned int) - rhs);
429  }
430  }
431 
432  return result;
433 }
434 
435 inline void ExtensiveBitRegister::print() const{
436  for(unsigned int n = size; n > 0; n--){
437  for(int c = 8*sizeof(unsigned int)-1; c >= 0; c--)
438  Streams::out << (0x1 & (values[n-1] >> c));
439  Streams::out << " ";
440  }
441  Streams::out << "\n";
442 }
443 
444 inline void ExtensiveBitRegister::setBit(unsigned int position, bool value){
445  values[position/(8*sizeof(unsigned int))] &= ~(1 << (position%(8*sizeof(unsigned int))));
446  values[position/(8*sizeof(unsigned int))] ^= (value << (position%(8*sizeof(unsigned int))));
447 }
448 
449 inline bool ExtensiveBitRegister::getBit(unsigned int position) const{
450  return (0x1 & (values[position/(8*sizeof(unsigned int))] >> (position%(8*sizeof(unsigned int)))));
451 }
452 
453 inline bool ExtensiveBitRegister::toBool() const{
454  for(unsigned int n = 0; n < size; n++)
455  if(values)
456  return true;
457 
458  return false;
459 }
460 
461 inline unsigned int ExtensiveBitRegister::toUnsignedInt() const{
462  return values[0];
463 }
464 
466  for(unsigned int n = 0; n < size; n++)
467  values[n] = 0;
468 }
469 
470 inline unsigned int ExtensiveBitRegister::getNumBits() const{
471  return size*8*sizeof(unsigned int);
472 }
473 
474 inline unsigned int ExtensiveBitRegister::getNumOneBits() const{
475  unsigned int numOnes = 0;
476  for(unsigned int n = 0; n < size; n++){
477  unsigned int x = values[n];
478  x = x - ((x >> 1) & 0x55555555);
479  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
480  x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
481  x = x + (x >> 8);
482  x = x + (x >> 16);
483  numOnes += (x & 0x0000003F);
484  }
485 
486  return numOnes;
487 }
488 
490  return values[size-1] & MOST_SIGNIFICANT_BIT_MASK;
491 }
492 
494  values[size-1] |= MOST_SIGNIFICANT_BIT_MASK;
495 }
496 
498  values[size-1] &= !MOST_SIGNIFICANT_BIT_MASK;
499 }
500 
502  return ExtensiveBitRegister(size*8*sizeof(unsigned int));
503 }
504 
505 }; //End of namespace TBTK
506 
507 #endif
bool operator<(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:255
const ExtensiveBitRegister & operator--()
Definition: ExtensiveBitRegister.h:349
const ExtensiveBitRegister & operator++()
Definition: ExtensiveBitRegister.h:328
bool getMostSignificantBit() const
Definition: ExtensiveBitRegister.h:489
void setBit(unsigned int position, bool values)
Definition: ExtensiveBitRegister.h:444
bool operator>(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:268
void setMostSignificantBit()
Definition: ExtensiveBitRegister.h:493
Precompiler macros.
ExtensiveBitRegister operator>>(unsigned int rhs) const
Definition: ExtensiveBitRegister.h:415
unsigned int getNumBits() const
Definition: ExtensiveBitRegister.h:470
void operator+=(const ExtensiveBitRegister &rhs)
Definition: ExtensiveBitRegister.h:292
bool operator==(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:281
unsigned int toUnsignedInt() const
Definition: ExtensiveBitRegister.h:461
void operator-=(const ExtensiveBitRegister &rhs)
Definition: ExtensiveBitRegister.h:310
static std::ostream out
Definition: Streams.h:36
const ExtensiveBitRegister operator&(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:177
void print() const
Definition: ExtensiveBitRegister.h:435
const ExtensiveBitRegister operator^(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:194
bool toBool() const
Definition: ExtensiveBitRegister.h:453
void clear()
Definition: ExtensiveBitRegister.h:465
Definition: ModelFactory.h:35
const ExtensiveBitRegister operator-(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:233
ExtensiveBitRegister cloneStructure() const
Definition: ExtensiveBitRegister.h:501
unsigned int getNumOneBits() const
Definition: ExtensiveBitRegister.h:474
const ExtensiveBitRegister operator|(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:160
ExtensiveBitRegister operator<<(unsigned int rhs) const
Definition: ExtensiveBitRegister.h:390
Definition: ExtensiveBitRegister.h:33
Streams for TBTK output.
bool getBit(unsigned int position) const
Definition: ExtensiveBitRegister.h:449
const ExtensiveBitRegister operator+(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:211
void operator=(const ExtensiveBitRegister &rhs)
Definition: ExtensiveBitRegister.h:370
void clearMostSignificantBit()
Definition: ExtensiveBitRegister.h:497