82 private int[] nocoeff1;
83 private int[] nocoeff2;
86 private int[] shifta1;
87 private int[] shifta2;
90 private int state = 0;
91 private int value = 0;
95 private int maskX1Shift;
109 this.nbcoeff1 = nbcoeff1 - 1;
110 this.nocoeff1 = nocoeff1;
120 public CycleBasedLFSR(
int step1,
int step2,
int nbcoeff1,
int nbcoeff2,
int[] nocoeff1,
int[] nocoeff2) {
123 this.nbcoeff1 = nbcoeff1 - 1;
124 this.nbcoeff2 = nbcoeff2 - 1;
125 this.nocoeff1 = nocoeff1;
126 this.nocoeff2 = nocoeff2;
171 readFile(filename, no);
176 private void init() {
178 posa1 =
new int[nbcoeff1];
179 shifta1 =
new int[nbcoeff1];
180 for (
int i = 0; i < nbcoeff1; ++i) {
181 posa1[i] = k1 - nocoeff1[i + 1] - 1;
182 shifta1[i] = 1 << posa1[i];
187 posa2 =
new int[nbcoeff2];
188 shifta2 =
new int[nbcoeff2];
189 for (
int i = 0; i < nbcoeff2; ++i) {
190 posa2[i] = k2 - nocoeff2[i + 1] - 1;
191 shifta2[i] = 1 << posa2[i];
196 System.err.println(
"The degree of the combined polynomials must be < 31");
197 System.err.println(
"k1 = " + k1 +
" k2 = " + k2);
201 maskX1 = (1 << k1) - 1;
202 maskX2 = (1 << k2) - 1;
203 maskX1Shift = maskX1 << k2;
204 maskX = (1 << (k1 + k2)) - 1;
207 normFactor = 1.0 / (1L << numBits);
218 String s =
"CycleBasedLFSR:" +
PrintfFormat.NEWLINE +
"First Polynome: Step: " + step1 +
" Coefficients: ";
219 for (i = 0; i < nbcoeff1; i++) {
220 s += nocoeff1[i] +
", ";
228 for (i = 0; i < nbcoeff2; i++) {
229 s += nocoeff2[i] +
", ";
245 private void nextState() {
250 for (s = 0; s < step1; s++) {
252 for (i = 0; i < nbcoeff1; i++) {
253 b ^= (x1 & shifta1[i]) >> posa1[i];
266 for (s = 0; s < step2; s++) {
268 for (i = 0; i < nbcoeff2; i++) {
269 b ^= (x2 & shifta2[i]) >> posa2[i];
276 value = (x1 ^ x2) & maskX;
288 state = (x1 & maskX1Shift) | ((x2 >>> k1) & maskX2);
292 private void validateState(
int x1x2) {
301 for (s = 0; s < k2; s++) {
303 for (i = 0; i < nbcoeff1; i++) {
304 b ^= (x1 & shifta1[i]) >> posa1[i];
318 for (s = 0; s < k1; s++) {
320 for (i = 0; i < nbcoeff2; i++) {
321 b ^= (x2 & shifta2[i]) >> posa2[i];
327 value = (x1 ^ x2) & maskX;
334 private void fillCyclesLFSR() {
335 int n = 1 << (k1 + k2);
338 boolean stateVisited[] =
new boolean[n];
341 for (i = 0; i < n; i++)
342 stateVisited[i] =
false;
345 while (startState < n) {
346 stateVisited[startState] =
true;
347 c =
new IntArrayList();
350 while (state != startState) {
351 stateVisited[state] =
true;
357 for (i = startState + 1; i < n; i++)
358 if (stateVisited[i] ==
false)
367 private void readFile(String fileName,
int no) {
368 BufferedReader input =
null;
370 if ((
new File(fileName)).exists()) {
371 input =
new BufferedReader(
new FileReader(fileName));
373 DataInputStream dataInput;
374 dataInput =
new DataInputStream(
375 F2wStructure.class.getClassLoader().getResourceAsStream(
"umontreal/ssj/hups/dataLFSR/" + fileName));
376 input =
new BufferedReader(
new InputStreamReader(dataInput));
378 }
catch (FileNotFoundException e) {
379 System.err.println(
"File " + fileName +
" not found" + PrintfFormat.NEWLINE);
390 String line = readOneLine(input);
391 numbers = lineToNumbers(line);
393 if (J != 1 && J != 2) {
394 System.err.println(
"Error: J = " + J + PrintfFormat.NEWLINE
395 +
"CycleBasedLFSR works only for the cases of one or two polynomials");
400 int nbLines = (J + 1) * (no - 1);
403 for (
int i = 0; i < nbLines; i++) {
404 line = readOneLine(input);
407 System.err.println(
"Error CycleBasedLFSR:" + PrintfFormat.NEWLINE +
" no data in file " + fileName +
" for "
414 line = readOneLine(input);
417 System.err.println(
"Error CycleBasedLFSR:" + PrintfFormat.NEWLINE +
" no data in file " + fileName +
" for "
421 numbers = lineToNumbers(line);
426 line = readOneLine(input);
427 nocoeff1 = lineToNumbers(line);
428 nbcoeff1 = nocoeff1.length - 1;
432 line = readOneLine(input);
433 nocoeff1 = lineToNumbers(line);
434 nbcoeff1 = nocoeff1.length - 1;
435 line = readOneLine(input);
436 nocoeff2 = lineToNumbers(line);
437 nbcoeff2 = nocoeff2.length - 1;
443 private String readOneLine(BufferedReader input) {
448 line = input.readLine();
454 if (line.length() == 0) {
455 line = input.readLine();
460 if (line.charAt(0) ==
'#') {
461 line = input.readLine();
466 int index = line.indexOf(
'#');
468 line = line.substring(0, index).trim();
472 }
catch (IOException e) {
473 System.err.println(e);
480 private int[] lineToNumbers(String line) {
485 snumbers = line.split(
"\\s++");
487 int nb = snumbers.length;
488 numbers =
new int[nb];
489 for (
int i = 0; i < nb; i++) {
490 numbers[i] = Integer.valueOf(snumbers[i]).intValue();