COMBINATORIAL_BLAS  1.6
binUtils.cpp
Go to the documentation of this file.
1 
9 #include <vector>
10 #include <cassert>
11 #include "binUtils.h"
12 
13 namespace binOp {
14 
15  unsigned int fastLog2(unsigned int num) {
16  if(num) {
17  return (binLength(num) - 1);
18  } else {
19  assert(false);
20  return -1;
21  }
22  }//end function
23 
24  unsigned int binLength(unsigned int num) {
25  unsigned int len = 1;
26  while(num > 1) {
27  num = (num >> 1);
28  len++;
29  }
30  return len;
31  }//end function
32 
33  int toBin(unsigned int num, unsigned int binLen, std::vector<bool>& numBin) {
34  numBin = std::vector<bool>(binLen);
35  for(unsigned int i = 0; i < binLen; i++) {
36  numBin[i]=0;
37  }//end for
38  unsigned int pos = binLen -1;
39  while(num > 0) {
40  numBin[pos] = (num%2);
41  num = num/2;
42  pos--;
43  } //end while
44  return 1;
45  }//end function
46 
47  unsigned int binToDec(unsigned int* numBin, unsigned int binLen) {
48  unsigned int res = 0;
49  for(unsigned int i = 0; i< binLen; i++) {
50  res = (2*res) + numBin[i];
51  }
52  return res;
53  }//end function
54 
55 
56  bool isPowerOfTwo(unsigned int n) {
57  return (n && (!(n & (n - 1))));
58  }
59 
60  // compute the next highest power of 2 of 32-bit v
61  int getNextHighestPowerOfTwo(unsigned int n) {
62  unsigned int v = n;
63  assert(v > 0);
64  v--;
65  v |= (v >> 1);
66  v |= (v >> 2);
67  v |= (v >> 4);
68  v |= (v >> 8);
69  v |= (v >> 16);
70  v++;
71  return v;
72  }
73 
74  // compute the prev highest power of 2 of 32-bit v
75  int getPrevHighestPowerOfTwo(unsigned int n) {
76  unsigned int v = n;
77  assert(v > 0);
78  v--;
79  v |= (v >> 1);
80  v |= (v >> 2);
81  v |= (v >> 4);
82  v |= (v >> 8);
83  v |= (v >> 16);
84  v++;
85  return (v >> 1);
86  }
87 
88  unsigned int reversibleHash(unsigned int x) {
89  x*=0xDEADBEEF;
90  x=x^(x>>17);
91  x*=0x01234567;
92  x+=0x88776655;
93  x=x^(x>>4);
94  x=x^(x>>9);
95  x*=0x91827363;
96  x=x^(x>>7);
97  x=x^(x>>11);
98  x=x^(x>>20);
99  x*=0x77773333;
100  return x;
101  }
102 
103 }//end namespace
104 
unsigned int binLength(unsigned int num)
Definition: binUtils.cpp:24
unsigned int fastLog2(unsigned int num)
Definition: binUtils.cpp:15
int getPrevHighestPowerOfTwo(unsigned int n)
Definition: binUtils.cpp:75
int getNextHighestPowerOfTwo(unsigned int n)
Definition: binUtils.cpp:61
unsigned int binToDec(unsigned int *numBin, unsigned int binLen)
Definition: binUtils.cpp:47
A set of functions for fast binary operations.
int toBin(unsigned int dec, unsigned int binLen, std::vector< bool > &result)
Converts a decimal number to binary.
Definition: binUtils.cpp:33
unsigned int reversibleHash(unsigned int x)
Definition: binUtils.cpp:88
bool isPowerOfTwo(unsigned int n)
Definition: binUtils.cpp:56
A set of efficient functions that use binary operations to perform some small computations.