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:
36  ExtensiveBitRegister(unsigned int numBits);
37 
39  ExtensiveBitRegister(const ExtensiveBitRegister &extensiveBitRegister);
40 
43 
46  const ExtensiveBitRegister &rhs
47  ) const;
48 
51  const ExtensiveBitRegister &rhs
52  ) const;
53 
56  const ExtensiveBitRegister &rhs
57  ) const;
58 
61  const ExtensiveBitRegister &rhs
62  ) const;
63 
66  const ExtensiveBitRegister &rhs
67  ) const;
68 
70  bool operator<(const ExtensiveBitRegister &rhs) const;
71 
73  bool operator>(const ExtensiveBitRegister &rhs) const;
74 
76  bool operator==(const ExtensiveBitRegister &rhs) const;
77 
79  void operator+=(const ExtensiveBitRegister &rhs);
80 
82  void operator-=(const ExtensiveBitRegister &rhs);
83 
86 
89 
92 
95 
97  void operator=(const ExtensiveBitRegister &rhs);
98 
100  void operator=(const unsigned int rhs);
101 
103  ExtensiveBitRegister operator<<(unsigned int rhs) const;
104 
106  ExtensiveBitRegister operator>>(unsigned int rhs) const;
107 
109  void setBit(unsigned int position, bool values);
110 
112  bool getBit(unsigned int position) const;
113 
116  bool toBool() const;
117 
120  unsigned int toUnsignedInt() const;
121 
123  void clear();
124 
126  void print() const;
127 
129  unsigned int getNumBits() const;
130 
132  unsigned int getNumOneBits() const;
133 
135  bool getMostSignificantBit() const;
136 
138  void setMostSignificantBit();
139 
142 
145 private:
147  unsigned int size;
148 
150  unsigned int *values;
151 
153  static constexpr unsigned int MOST_SIGNIFICANT_BIT_MASK
154  = (unsigned int)0x1 << (8*sizeof(unsigned int) - 1);
155 };
156 
158  const ExtensiveBitRegister &rhs
159 ) const{
160  TBTKAssert(
161  size == rhs.size,
162  "ExtensiveBitRegister::operator|()",
163  "Incompatible register sizes.",
164  ""
165  );
166 
168  for(unsigned int n = 0; n < size; n++)
169  result.values[n] = values[n] | rhs.values[n];
170 
171  return result;
172 }
173 
175  const ExtensiveBitRegister &rhs
176 ) const{
177  TBTKAssert(
178  size == rhs.size,
179  "ExtensiveBitRegister::operator|()",
180  "Incompatible register sizes.",
181  ""
182  );
183 
185  for(unsigned int n = 0; n < size; n++)
186  result.values[n] = values[n] & rhs.values[n];
187 
188  return result;
189 }
190 
192  const ExtensiveBitRegister &rhs
193 ) const{
194  TBTKAssert(
195  size == rhs.size,
196  "ExtensiveBitRegister::operator|()",
197  "Incompatible register sizes.",
198  ""
199  );
200 
202  for(unsigned int n = 0; n < size; n++)
203  result.values[n] = values[n] ^ rhs.values[n];
204 
205  return result;
206 }
207 
209  const ExtensiveBitRegister &rhs
210 ) const{
211  TBTKAssert(
212  size == rhs.size,
213  "ExtensiveBitRegister::operator+()",
214  "Incompatible register sizes.",
215  ""
216  );
217 
219  unsigned int carry = 0;
220  for(unsigned int n = 0; n < size; n++){
221  result.values[n] = values[n] + rhs.values[n];
222  unsigned int newCarry = (result.values[n] < values[n]);
223  result.values[n] += carry;
224  carry = newCarry | (result.values[n] < values[n]);
225  }
226 
227  return result;
228 }
229 
231  const ExtensiveBitRegister &rhs
232 ) const{
233  TBTKAssert(
234  size == rhs.size,
235  "ExtensiveBitRegister::operator+()",
236  "Incompatible register sizes.",
237  ""
238  );
239 
241  unsigned int carry = 0;
242  for(unsigned int n = 0; n < size; n++){
243  result.values[n] = values[n] - rhs.values[n];
244  unsigned int newCarry = (result.values[n] > values[n]);
245  result.values[n] -= carry;
246  carry = newCarry | (result.values[n] > values[n]);
247  }
248 
249  return result;
250 }
251 
253  const ExtensiveBitRegister &rhs
254 ) const{
255  for(int n = size-1; n >= 0; n--){
256  if(values[n] < rhs.values[n])
257  return true;
258  if(values[n] > rhs.values[n])
259  return false;
260  }
261 
262  return false;
263 }
264 
266  const ExtensiveBitRegister &rhs
267 ) const{
268  for(int n = size-1; n >= 0; n--){
269  if(values[n] > rhs.values[n])
270  return true;
271  if(values[n] < rhs.values[n])
272  return false;
273  }
274 
275  return false;
276 }
277 
279  const ExtensiveBitRegister &rhs
280 ) const{
281  for(unsigned int n = 0; n < size; n++){
282  if(values[n] != rhs.values[n])
283  return false;
284  }
285 
286  return true;
287 }
288 
290  TBTKAssert(
291  size == rhs.size,
292  "ExtensiveBitRegister::operator+=()",
293  "Incompatible register sizes.",
294  ""
295  );
296 
297  unsigned int carry = 0;
298  for(unsigned int n = 0; n < size; n++){
299  unsigned int temp = values[n];
300  values[n] = temp + rhs.values[n];
301  unsigned int newCarry = (values[n] < temp);
302  values[n] += carry;
303  carry = newCarry | (values[n] < temp);
304  }
305 }
306 
308  TBTKAssert(
309  size == rhs.size,
310  "ExtensiveBitRegister::operator-=()",
311  "Incompatible register sizes.",
312  ""
313  );
314 
315  unsigned int carry = 0;
316  for(unsigned int n = 0; n < size; n++){
317  unsigned int temp = values[n];
318  values[n] = temp - rhs.values[n];
319  unsigned int newCarry = (values[n] > temp);
320  values[n] -= carry;
321  carry = newCarry | (values[n] > temp);
322  }
323 }
324 
326  for(unsigned int n = 0; n < size; n++){
327  if(values[n] == UINT_MAX){
328  values[n]++;
329  }
330  else{
331  values[n]++;
332  break;
333  }
334  }
335 
336  return *this;
337 }
338 
340  ExtensiveBitRegister returnValue(*this);
341  ++(*this);
342 
343  return returnValue;
344 }
345 
347  for(unsigned int n = 0; n < size; n++){
348  if(values[n] == 0){
349  values[n]--;
350  }
351  else{
352  values[n]--;
353  break;
354  }
355  }
356 
357  return *this;
358 }
359 
361  ExtensiveBitRegister returnValue(*this);
362  --(*this);
363 
364  return returnValue;
365 }
366 
368  if(this != &rhs){
369  TBTKAssert(
370  size == rhs.size,
371  "ExtensiveBitRegister::operator=()",
372  "Incompatible register sizes.",
373  ""
374  );
375 
376  for(unsigned int n = 0; n < size; n++)
377  values[n] = rhs.values[n];
378  }
379 }
380 
381 inline void ExtensiveBitRegister::operator=(const unsigned int rhs){
382  values[0] = rhs;
383  for(unsigned int n = 1; n < size; n++)
384  values[n] = 0;
385 }
386 
388  ExtensiveBitRegister result(*this);
389  if(rhs > 8*sizeof(unsigned int)){
390  result = result << (rhs - 8*sizeof(unsigned int));
391 
392  for(int n = size-1; n > 0; n--)
393  result.values[n] = result.values[n-1];
394  result.values[0] = 0;
395  }
396  else if(rhs == 8*sizeof(unsigned int)){
397  for(int n = size-1; n > 0; n--)
398  result.values[n] = result.values[n-1];
399  result.values[0] = 0;
400  }
401  else if(rhs != 0){
402  for(int n = size-1; n >= 0; n--){
403  result.values[n] = result.values[n] << rhs;
404  if(n > 0)
405  result.values[n] |= result.values[n-1] >> (8*sizeof(unsigned int) - rhs);
406  }
407  }
408 
409  return result;
410 }
411 
413  ExtensiveBitRegister result(*this);
414  if(rhs > 8*sizeof(unsigned int)){
415  result = result >> (rhs - 8*sizeof(unsigned int));
416 
417  for(unsigned int n = 0; n < size-1; n++)
418  result.values[n] = result.values[n+1];
419  result.values[size-1] = 0;
420  }
421  else if(rhs != 0){
422  for(unsigned int n = 0; n < size; n++){
423  result.values[n] = result.values[n] >> rhs;
424  if(n < size-1)
425  result.values[n] |= result.values[n+1] << (8*sizeof(unsigned int) - rhs);
426  }
427  }
428 
429  return result;
430 }
431 
432 inline void ExtensiveBitRegister::print() const{
433  for(unsigned int n = size; n > 0; n--){
434  for(int c = 8*sizeof(unsigned int)-1; c >= 0; c--)
435  Streams::out << (0x1 & (values[n-1] >> c));
436  Streams::out << " ";
437  }
438  Streams::out << "\n";
439 }
440 
441 inline void ExtensiveBitRegister::setBit(unsigned int position, bool value){
442  values[position/(8*sizeof(unsigned int))] &= ~(1 << (position%(8*sizeof(unsigned int))));
443  values[position/(8*sizeof(unsigned int))] ^= (value << (position%(8*sizeof(unsigned int))));
444 }
445 
446 inline bool ExtensiveBitRegister::getBit(unsigned int position) const{
447  return (0x1 & (values[position/(8*sizeof(unsigned int))] >> (position%(8*sizeof(unsigned int)))));
448 }
449 
450 inline bool ExtensiveBitRegister::toBool() const{
451  for(unsigned int n = 0; n < size; n++)
452  if(values)
453  return true;
454 
455  return false;
456 }
457 
458 inline unsigned int ExtensiveBitRegister::toUnsignedInt() const{
459  return values[0];
460 }
461 
463  for(unsigned int n = 0; n < size; n++)
464  values[n] = 0;
465 }
466 
467 inline unsigned int ExtensiveBitRegister::getNumBits() const{
468  return size*8*sizeof(unsigned int);
469 }
470 
471 inline unsigned int ExtensiveBitRegister::getNumOneBits() const{
472  unsigned int numOnes = 0;
473  for(unsigned int n = 0; n < size; n++){
474  unsigned int x = values[n];
475  x = x - ((x >> 1) & 0x55555555);
476  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
477  x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
478  x = x + (x >> 8);
479  x = x + (x >> 16);
480  numOnes += (x & 0x0000003F);
481  }
482 
483  return numOnes;
484 }
485 
487  return values[size-1] & MOST_SIGNIFICANT_BIT_MASK;
488 }
489 
491  values[size-1] |= MOST_SIGNIFICANT_BIT_MASK;
492 }
493 
495  values[size-1] &= !MOST_SIGNIFICANT_BIT_MASK;
496 }
497 
499  return ExtensiveBitRegister(size*8*sizeof(unsigned int));
500 }
501 
502 }; //End of namespace TBTK
503 
504 #endif
bool operator<(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:252
const ExtensiveBitRegister & operator--()
Definition: ExtensiveBitRegister.h:346
const ExtensiveBitRegister & operator++()
Definition: ExtensiveBitRegister.h:325
bool getMostSignificantBit() const
Definition: ExtensiveBitRegister.h:486
void setBit(unsigned int position, bool values)
Definition: ExtensiveBitRegister.h:441
bool operator>(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:265
void setMostSignificantBit()
Definition: ExtensiveBitRegister.h:490
Precompiler macros.
ExtensiveBitRegister operator>>(unsigned int rhs) const
Definition: ExtensiveBitRegister.h:412
unsigned int getNumBits() const
Definition: ExtensiveBitRegister.h:467
void operator+=(const ExtensiveBitRegister &rhs)
Definition: ExtensiveBitRegister.h:289
bool operator==(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:278
unsigned int toUnsignedInt() const
Definition: ExtensiveBitRegister.h:458
void operator-=(const ExtensiveBitRegister &rhs)
Definition: ExtensiveBitRegister.h:307
static std::ostream out
Definition: Streams.h:36
const ExtensiveBitRegister operator&(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:174
void print() const
Definition: ExtensiveBitRegister.h:432
const ExtensiveBitRegister operator^(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:191
bool toBool() const
Definition: ExtensiveBitRegister.h:450
void clear()
Definition: ExtensiveBitRegister.h:462
Definition: ModelFactory.h:35
const ExtensiveBitRegister operator-(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:230
ExtensiveBitRegister(unsigned int numBits)
ExtensiveBitRegister cloneStructure() const
Definition: ExtensiveBitRegister.h:498
unsigned int getNumOneBits() const
Definition: ExtensiveBitRegister.h:471
const ExtensiveBitRegister operator|(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:157
ExtensiveBitRegister operator<<(unsigned int rhs) const
Definition: ExtensiveBitRegister.h:387
Definition: ExtensiveBitRegister.h:33
Streams for TBTK output.
bool getBit(unsigned int position) const
Definition: ExtensiveBitRegister.h:446
const ExtensiveBitRegister operator+(const ExtensiveBitRegister &rhs) const
Definition: ExtensiveBitRegister.h:208
void operator=(const ExtensiveBitRegister &rhs)
Definition: ExtensiveBitRegister.h:367
void clearMostSignificantBit()
Definition: ExtensiveBitRegister.h:494