HOPS
HOPS class reference
MHO_BitReversalPermutation.hh
Go to the documentation of this file.
1 #ifndef MHO_BitReversalPermutation_HH__
2 #define MHO_BitReversalPermutation_HH__
3 
4 #include <cstddef>
5 
6 #define USE_STDALGO_SWAP
7 
8 #ifdef USE_STDALGO_SWAP
9  #include <algorithm>
10 #endif
11 
12 namespace hops
13 {
14 
24 {
25  public:
28 
36  static bool IsPowerOfTwo(unsigned int N);
37 
45  static unsigned int TwoToThePowerOf(unsigned int N);
46 
54  static unsigned int LogBaseTwo(unsigned int N);
55 
63  static unsigned int NextLowestPowerOfTwo(unsigned int N);
64 
73  static unsigned int ReverseIndexBits(unsigned int nbits, unsigned int x);
74 
83  static bool IsPowerOfBase(unsigned int N, unsigned int B);
84 
93  static unsigned int RaiseBaseToThePower(unsigned int B, unsigned int N);
94 
103  static unsigned int LogBaseB(unsigned int N, unsigned int B);
104 
112  static void ComputeBitReversedIndicesBaseTwo(unsigned int N, unsigned int* index_arr);
113 
123  template< typename DataType >
124  static void PermuteArray(unsigned int N, const unsigned int* permutation_index_arr, DataType* arr)
125  {
126  //expects an array of size N
127  DataType val;
128  for(unsigned int i = 0; i < N; i++)
129  {
130  unsigned int perm = permutation_index_arr[i];
131  if(i < perm)
132  {
133 //swap values
134 #ifdef USE_STDALGO_SWAP
135  std::swap(arr[i], arr[perm]);
136 #else
137  val = arr[i];
138  arr[i] = arr[perm];
139  arr[perm] = val;
140 #endif
141  }
142  }
143  }
144 
155  template< typename DataType >
156  static void PermuteArray(unsigned int N, const unsigned int* permutation_index_arr, DataType* arr, unsigned int stride)
157  {
158  //expects an array of size N
159  DataType val;
160  for(unsigned int i = 0; i < N; i++)
161  {
162  unsigned int perm = permutation_index_arr[i];
163  if(i < perm)
164  {
165 //swap values
166 #ifdef USE_STDALGO_SWAP
167  std::swap(arr[i * stride], arr[perm * stride]);
168 #else
169  val = arr[i * stride];
170  arr[i * stride] = arr[perm * stride];
171  arr[perm * stride] = val;
172 #endif
173  }
174  }
175  }
176 
177 
189  template< typename DataType >
190  static void PermuteArrayBranchFree(unsigned int N, const unsigned int* permutation_index_arr, DataType* arr)
191  {
192  DataType a, b;
193  //expects an array of size N
194  unsigned int perm;
195  typename DataType::value_type do_swap;
196  typename DataType::value_type sgn;
197  for(unsigned int i = 0; i < N; i++)
198  {
199  perm = permutation_index_arr[i];
200  do_swap = (i < perm);
201  sgn = (i < perm) - (i >= perm);
202  a = arr[i];
203  b = arr[perm];
204 
205  a = a + do_swap * b;
206  b = do_swap * a - sgn * b;
207  a = a - do_swap * b;
208 
209  arr[i] = a;
210  arr[perm] = b;
211  }
212  }
213 
214 
226  template< typename DataType >
227  static void PermuteArrayBranchFree(unsigned int N, const unsigned int* permutation_index_arr, DataType* arr,
228  unsigned int stride)
229  {
230  DataType a, b;
231  //expects an array of size N
232  unsigned int perm;
233  typename DataType::value_type do_swap;
234  typename DataType::value_type sgn;
235  for(unsigned int i = 0; i < N; i++)
236  {
237 
238  perm = permutation_index_arr[i];
239  do_swap = (i < perm);
240  sgn = (i < perm) - (i >= perm);
241  a = arr[i * stride];
242  b = arr[perm * stride];
243 
244  a = a + do_swap * b;
245  b = do_swap * a - sgn * b;
246  a = a - do_swap * b;
247 
248  arr[i * stride] = a;
249  arr[perm * stride] = b;
250  }
251  }
252 
253  private:
254 };
255 
256 } // namespace hops
257 
258 #endif
bit reversal permutation function for power-of-two FFTs
Definition: MHO_BitReversalPermutation.hh:24
static unsigned int RaiseBaseToThePower(unsigned int B, unsigned int N)
Calculates B raised to the power N.
Definition: MHO_BitReversalPermutation.cc:82
MHO_BitReversalPermutation()
Definition: MHO_BitReversalPermutation.hh:26
static void ComputeBitReversedIndicesBaseTwo(unsigned int N, unsigned int *index_arr)
Computes bit-reversed indices using Buneman algorithm for input N, must have N = 2^P,...
Definition: MHO_BitReversalPermutation.cc:119
static unsigned int NextLowestPowerOfTwo(unsigned int N)
Calculates the next lowest power of two for a given unsigned integer.
Definition: MHO_BitReversalPermutation.cc:35
static unsigned int ReverseIndexBits(unsigned int nbits, unsigned int x)
Reverses the bit indices of a given unsigned integer.
Definition: MHO_BitReversalPermutation.cc:50
static void PermuteArrayBranchFree(unsigned int N, const unsigned int *permutation_index_arr, DataType *arr)
Function PermuteArrayBranchFree non-strided data access pattern branch free (this is actually slower ...
Definition: MHO_BitReversalPermutation.hh:190
virtual ~MHO_BitReversalPermutation()
Definition: MHO_BitReversalPermutation.hh:27
static void PermuteArray(unsigned int N, const unsigned int *permutation_index_arr, DataType *arr, unsigned int stride)
Permutes a DataType array using an index permutation (strided data access version)
Definition: MHO_BitReversalPermutation.hh:156
static bool IsPowerOfBase(unsigned int N, unsigned int B)
Checks if an unsigned integer N is a perfect power of another unsigned integer B.
Definition: MHO_BitReversalPermutation.cc:60
static void PermuteArray(unsigned int N, const unsigned int *permutation_index_arr, DataType *arr)
Permutes an array using a given permutation index array (non-strided data access pattern).
Definition: MHO_BitReversalPermutation.hh:124
static unsigned int TwoToThePowerOf(unsigned int N)
Calculates 2 raised to the power of N using bit shifting.
Definition: MHO_BitReversalPermutation.cc:29
static unsigned int LogBaseB(unsigned int N, unsigned int B)
Calculates the logarithm base B of N, assuming N is a perfect power of B.
Definition: MHO_BitReversalPermutation.cc:92
static bool IsPowerOfTwo(unsigned int N)
Checks if an unsigned integer is a power of two.
Definition: MHO_BitReversalPermutation.cc:10
static void PermuteArrayBranchFree(unsigned int N, const unsigned int *permutation_index_arr, DataType *arr, unsigned int stride)
Function PermuteArrayBranchFree strided data access version branch free (this is actually slower on C...
Definition: MHO_BitReversalPermutation.hh:227
static unsigned int LogBaseTwo(unsigned int N)
Calculates the logarithm base two of an unsigned integer N using bitwise operations.
Definition: MHO_BitReversalPermutation.cc:17
Definition: MHO_ChannelLabeler.hh:17