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 
188  template< typename DataType >
189  static void PermuteArrayBranchFree(unsigned int N, const unsigned int* permutation_index_arr, DataType* arr)
190  {
191  DataType a, b;
192  //expects an array of size N
193  unsigned int perm;
194  typename DataType::value_type do_swap;
195  typename DataType::value_type sgn;
196  for(unsigned int i = 0; i < N; i++)
197  {
198  perm = permutation_index_arr[i];
199  do_swap = (i < perm);
200  sgn = (i < perm) - (i >= perm);
201  a = arr[i];
202  b = arr[perm];
203 
204  a = a + do_swap * b;
205  b = do_swap * a - sgn * b;
206  a = a - do_swap * b;
207 
208  arr[i] = a;
209  arr[perm] = b;
210  }
211  }
212 
224  template< typename DataType >
225  static void PermuteArrayBranchFree(unsigned int N, const unsigned int* permutation_index_arr, DataType* arr,
226  unsigned int stride)
227  {
228  DataType a, b;
229  //expects an array of size N
230  unsigned int perm;
231  typename DataType::value_type do_swap;
232  typename DataType::value_type sgn;
233  for(unsigned int i = 0; i < N; i++)
234  {
235 
236  perm = permutation_index_arr[i];
237  do_swap = (i < perm);
238  sgn = (i < perm) - (i >= perm);
239  a = arr[i * stride];
240  b = arr[perm * stride];
241 
242  a = a + do_swap * b;
243  b = do_swap * a - sgn * b;
244  a = a - do_swap * b;
245 
246  arr[i * stride] = a;
247  arr[perm * stride] = b;
248  }
249  }
250 
251  private:
252 };
253 
254 } // namespace hops
255 
256 #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:189
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:225
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_AdhocFlagging.hh:18