SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
F2wStructure.java
1/*
2 * Class: F2wStructure
3 * Description: Tools for point sets and sequences based on field F_{2^w}
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.hups;
26
27import java.io.*;
28import java.util.*;
29
439public class F2wStructure {
440
441 private final int ALLONES = 2147483647; // 2^31-1 --> 01111...1
442 int w;
443 int r;
444 int numBits;
445 private int modQ;
446 private int step;
447 private int[] coeff;
448 private int[] nocoeff;
449 private int nbcoeff;
450 int S;
451 private int maskw;
452 private int maskrw;
453 private int maskZrm1;
454 private int mask31;
455 private int t;
456 private int masktrw;
457 private int[] maskv;
458 int state;
459 int output; // augmented state
460 double normFactor;
461 double EpsilonHalf;
462 final static int MBL = 140; // maximum of bytes in 1 line
463 // 92 bytes for a number of coeff = 15
464
465 private void init(int w, int r, int modQ, int step, int nbcoeff, int coeff[], int nocoeff[]) {
466 normFactor = 1.0 / (1L << 31); // 4.65661287307739258e-10;
467 EpsilonHalf = 0.5 * normFactor;
468 numBits = 31;
469 this.step = step;
470 this.w = w;
471 this.r = r;
472 S = 31 - r * w;
473 mask31 = ~(1 << 31);
474 maskw = (1 << w) - 1;
475 maskrw = ((1 << (r * w)) - 1) << S;
476 maskZrm1 = (ALLONES >> (r * w)) ^ (ALLONES >> ((r - 1) * w));
477 this.modQ = modQ;
478 this.nbcoeff = nbcoeff;
479 this.nocoeff = new int[nbcoeff];
480 this.coeff = new int[nbcoeff];
481 for (int j = 0; j < nbcoeff; j++) {
482 this.nocoeff[j] = nocoeff[j];
483 this.coeff[j] = coeff[j];
484 }
485 }
486
487 void initParamLFSR() {
488 t = (31 - r * w) / w;
489 masktrw = (~0) << (31 - (t + r) * w) & mask31;
490 maskv = new int[r];
491 for (int j = 0; j < r; j++) {
492 maskv[j] = maskw << (S + ((r - 1 - j) * w));
493 }
494 }
495
500 F2wStructure(int w, int r, int modQ, int step, int nbcoeff, int coeff[], int nocoeff[]) {
501 init(w, r, modQ, step, nbcoeff, coeff, nocoeff);
502 }
503
524 F2wStructure(String filename, int no) {
525 // If filename can be found starting from the program's directory,
526 // it will be used; otherwise, the filename in the Jar archive will
527 // be used.
528 BufferedReader input;
529 try {
530 if ((new File(filename)).exists()) {
531 input = new BufferedReader(new FileReader(filename));
532 } else {
533 // does not work anymore since the files and directories have been removed
534 // from package hups and put instead on the WWW page.
535 // Should be read with protocol http as in class DigitalNetFromFile
536 DataInputStream dataInput;
537 dataInput = new DataInputStream(F2wStructure.class.getClassLoader()
538 .getResourceAsStream("umontreal/ssj/hups/dataF2w/Panneton/" + filename));
539 input = new BufferedReader(new InputStreamReader(dataInput));
540 }
541 initFromReader(filename, input, no);
542 input.close();
543
544 } catch (Exception e) {
545 System.out.println("IO Error: problems finding file " + filename);
546 System.exit(1);
547 }
548 }
549
550 private int multiplyz(int a, int k) {
551 int i;
552 if (k == 0)
553 return a & maskw;
554 else {
555 for (i = 0; i < k; i++) {
556 if ((1 & a) == 1) {
557 a = (a >> 1) ^ modQ;
558 } else
559 a = a >> 1;
560 }
561 return a & maskw;
562 }
563 }
564
568 int getLog2N() {
569 return r * w;
570 }
571
575 int multiply(int a, int b)
576
577 {
578 int i;
579 int res = 0, verif = 1;
580 for (i = 0; i < w; i++) {
581 if ((b & verif) == verif)
582 res ^= multiplyz(a, w - 1 - i);
583 verif <<= 1;
584 }
585 return res & maskw;
586 }
587
588 void initF2wLFSR() // Initialisation de l'etat d'un LFSR
589 {
590 int v, d = 0;
591 int tempState;
592
593 tempState = state << S;
594 output = tempState;
595 for (int i = 1; i <= t; i++) {
596 d = 0;
597 for (int j = 0; j < nbcoeff; j++) {
598 v = (tempState & maskv[nocoeff[j]]) >> (S + (r - 1 - nocoeff[j]) * w);
599 d ^= multiply(coeff[j], v);
600 }
601 output |= d << (S - i * w);
602 tempState = (output << (i * w)) & maskrw;
603 }
604 }
605
606 void F2wLFSR() // Une iteration d'un LFSR
607 {
608 int v, d = 0;
609 int tempState;
610 for (int i = 0; i < step; i++) {
611 tempState = (output << (t * w)) & maskrw;
612 d = 0;
613 for (int j = 0; j < nbcoeff; j++) {
614 v = (tempState & maskv[nocoeff[j]]) >> (S + (r - 1 - nocoeff[j]) * w);
615 d ^= multiply(coeff[j], v);
616 }
617 output = ((output << w) & masktrw) | (d << (31 - (r + t) * w));
618 }
619 state = (output & maskrw) >> S;
620 }
621
622 int F2wPolyLCG() // Une iteration d'un PolyLCG
623 {
624 int Zrm1, d;
625 for (int i = 0; i < step; i++) {
626 Zrm1 = (state & maskZrm1) >> S;
627 state = (state >> w) & maskrw;
628 for (int j = 0; j < nbcoeff; j++)
629 state ^= multiply(coeff[j], Zrm1) << (S + (r - 1 - nocoeff[j]) * w);
630 }
631 return state;
632 }
633
638 public static void print(String filename) {
639 BufferedReader input;
640 try {
641 if ((new File(filename)).exists()) {
642 input = new BufferedReader(new FileReader(filename));
643 } else {
644 DataInputStream dataInput;
645 dataInput = new DataInputStream(
646 F2wStructure.class.getClassLoader().getResourceAsStream("umontreal/ssj/hups/dataF2w/" + filename));
647 input = new BufferedReader(new InputStreamReader(dataInput));
648 }
649
650 String s;
651 for (int i = 0; i < 4; i++)
652 input.readLine();
653 while ((s = input.readLine()) != null)
654 System.out.println(s);
655 input.close();
656
657 } catch (Exception e) {
658 System.out.println("IO Error: problems reading file " + filename);
659 System.exit(1);
660 }
661 }
662
667 public String toString() {
668 StringBuffer sb = new StringBuffer("z^");
669 sb.append(r);
670 for (int j = nbcoeff - 1; j >= 0; j--)
671 sb.append(" + (" + coeff[j] + ") z^" + nocoeff[j]);
672 sb.append(" modQ = " + modQ + " w = " + w + " step = " + step);
673 return sb.toString();
674 }
675
676 private void initFromReader(String filename, BufferedReader input, int no) {
677 int w, r, modQ, step, nbcoeff;
678 int coeff[], nocoeff[];
679 StringTokenizer line;
680 int nl = no + 4;
681
682 try {
683 for (int j = 1; j < nl; j++)
684 input.readLine();
685
686 line = new StringTokenizer(input.readLine());
687 w = Integer.parseInt(line.nextToken());
688 r = Integer.parseInt(line.nextToken());
689 modQ = Integer.parseInt(line.nextToken());
690 step = Integer.parseInt(line.nextToken());
691 nbcoeff = Integer.parseInt(line.nextToken());
692 nocoeff = new int[nbcoeff];
693 coeff = new int[nbcoeff];
694 for (int i = 0; i < nbcoeff; i++) {
695 coeff[i] = Integer.parseInt(line.nextToken());
696 nocoeff[i] = Integer.parseInt(line.nextToken());
697 }
698 init(w, r, modQ, step, nbcoeff, coeff, nocoeff);
699 input.close();
700
701 } catch (Exception e) {
702 System.out.println("Input Error: problems reading file " + filename);
703 System.exit(1);
704 }
705 }
706}
int getLog2N()
This method returns the product .
int multiply(int a, int b)
Method that multiplies two elements in .
F2wStructure(String filename, int no)
Constructs a polynomial in after reading its parameters from file filename; the parameters of this p...
static void print(String filename)
Prints the content of file filename.
String toString()
This method returns a string containing the polynomial and the stepping parameter.
F2wStructure(int w, int r, int modQ, int step, int nbcoeff, int coeff[], int nocoeff[])
Constructs a F2wStructure object that contains the parameters of a polynomial in ,...