MTToolBox  0.2.10
AlgorithmPartialBitPattern.hpp
Go to the documentation of this file.
1 #ifndef MTTOOLBOX_ALGORITHM_PARTIAL_BITPATTERN_HPP
2 #define MTTOOLBOX_ALGORITHM_PARTIAL_BITPATTERN_HPP
3 
27 #include <iostream>
28 #include <iomanip>
29 #include <cstdlib>
30 #include <unistd.h>
34 
35 namespace MTToolBox {
109  template<typename U, typename V,
110  int bit_len, int param_num, int try_bit_len, int step = 5,
111  bool lsb = false>
113  public:
134  bool verbose = false) {
135  using namespace std;
136  if (verbose) {
137  cout << "searching..." << endl;
138  }
139  if (lsb) {
140  rand.setReverseOutput();
141  if (verbose) {
142  cout << "searching from LSB" << endl;
143  }
144  } else {
145  rand.resetReverseOutput();
146  if (verbose) {
147  cout << "searching from MSB" << endl;
148  }
149  }
150  int delta = 1000;
151  for (int p = 0; p < try_bit_len; p += step) {
152  int max_depth = p + step;
153  if (max_depth > try_bit_len) {
154  max_depth = try_bit_len;
155  }
156  for (int i = 0; i < param_num; i++) {
157  delta = search_best_temper(rand, p, i, max_depth, verbose);
158  }
159  }
160  if (verbose) {
161  cout << "delta = " << dec << delta << endl;
162  }
163  rand.resetReverseOutput();
164  return 0;
165  }
166 
178  bool isLSBTempering() const {
179  return lsb;
180  }
181  private:
182  void make_temper_bit(TemperingCalculatable<U, V>& rand,
183  U mask,
184  int param_pos,
185  U pattern) {
186  rand.setTemperingPattern(mask, pattern, param_pos);
187  rand.setUpTempering();
188  }
189 
239  int search_best_temper(TemperingCalculatable<U, V>& rand, int v_bit,
240  int param_pos, int max_v_bit, bool verbose) {
241  using namespace std;
242  int bitSize = rand.bitSize();
243  int delta;
244  int min_delta = bitSize * bit_len;
245  U min_pattern = 0;
246  int size = max_v_bit - v_bit;
247  U pattern;
248  U mask = make_mask(v_bit, size);
249  int length = bit_size<U>() / 4;
250  for (int i = (1 << size) -1; i >= 0; i--) {
251  if (lsb) {
252  pattern = static_cast<U>(i) << v_bit;
253  } else {
254  pattern = static_cast<U>(i) << (bit_len - v_bit - size);
255  }
256  make_temper_bit(rand, mask, param_pos, pattern);
257  delta = get_equidist(rand, bit_len);
258  if (delta < min_delta) {
259  if (verbose) {
260  cout << "pattern change " << hex << min_pattern
261  << ":" << pattern << endl;
262  }
263  min_delta = delta;
264  min_pattern = pattern;
265  } else if (delta == min_delta) {
266  if (count_bit(min_pattern) < count_bit(pattern)) {
267  if (verbose) {
268  cout << "pattern change " << hex << min_pattern
269  << ":" << pattern << endl;
270  }
271  min_pattern = pattern;
272  }
273  }
274  }
275  make_temper_bit(rand, mask, param_pos, min_pattern);
276  if (verbose) {
277  cout << dec << min_delta << ":"
278  << hex << setw(length) << min_pattern << ":"
279  << hex << setw(length) << mask << endl;
280  }
281  return min_delta;
282  }
283 
302  int get_equidist(TemperingCalculatable<U, V>& rand,
303  int bit_length) {
304  AlgorithmEquidistribution<U, V> sb(rand, bit_length);
305  int veq[bit_length];
306  int sum = sb.get_all_equidist(veq);
307  return sum;
308  }
309 
334  U make_mask(int start, int size) {
335  U mask = 0;
336  mask = ~mask;
337  if (start + size > bit_len) {
338  size = bit_len - start;
339  }
340  if (lsb) {
341  mask >>= start;
342  mask <<= bit_len - size;
343  mask >>= bit_len - start - size;
344  } else {
345  mask <<= start;
346  mask >>= bit_len - size;
347  mask <<= bit_len - start - size;
348  }
349  return mask;
350  }
351  };
352 }
353 
354 #endif // MTTOOLBOX_ALGORITHM_PARTIAL_BITPATTERN_HPP
Abstruct class for searching tempering parameters.
virtual void resetReverseOutput()=0
Reset bit order of output.
Algorithm that search tempering parameters to improve dimension of equi-distribution of output of pse...
Definition: AlgorithmPartialBitPattern.hpp:112
bool isLSBTempering() const
Shows if searching tempering parameters is from LSBs.
Definition: AlgorithmPartialBitPattern.hpp:178
virtual void setReverseOutput()=0
Changes bit order of output.
static int count_bit(uint16_t x)
Counts number of 1s.
Definition: util.hpp:377
int operator()(TemperingCalculatable< U, V > &rand, bool verbose=false)
Search tempering parameters.
Definition: AlgorithmPartialBitPattern.hpp:133
Users can search tempering parameters by making GF(2)-linear pseudo random generator class which inhe...
Definition: TemperingCalculatable.hpp:54
Calculate dimension of equi-distribution of output of pseudo random number generators.
Abstract class for searching tempering parameters.
virtual void setUpTempering()=0
If preparing is needed before generation, here is the place to prepare tempering parameters.
virtual void setTemperingPattern(U mask, U pattern, int index)=0
Set tempering parameters.
Algorithm that search tempering parameters to improve dimension of equi-distribution of output of pse...
Definition: AlgorithmTempering.hpp:62
name space for MTToolBox