1
#include "mt19937-jump.h"
2
#include "mt19937.h"
3

4
/* 32-bits function */
5
/* return the i-th coefficient of the polynomial pf */
6 1
unsigned long get_coef(unsigned long *pf, unsigned int deg) {
7 1
  if ((pf[deg >> 5] & (LSB << (deg & 0x1ful))) != 0)
8
    return (1);
9
  else
10 1
    return (0);
11
}
12

13 1
void copy_state(mt19937_state *target_state, mt19937_state *state) {
14
  int i;
15

16 1
  for (i = 0; i < N; i++)
17 1
    target_state->key[i] = state->key[i];
18

19 1
  target_state->pos = state->pos;
20
}
21

22
/* next state generating function */
23 1
void gen_next(mt19937_state *state) {
24
  int num;
25
  unsigned long y;
26
  static unsigned long mag02[2] = {0x0ul, MATRIX_A};
27

28 1
  num = state->pos;
29 1
  if (num < N - M) {
30 1
    y = (state->key[num] & UPPER_MASK) | (state->key[num + 1] & LOWER_MASK);
31 1
    state->key[num] = state->key[num + M] ^ (y >> 1) ^ mag02[y % 2];
32 1
    state->pos++;
33 1
  } else if (num < N - 1) {
34 1
    y = (state->key[num] & UPPER_MASK) | (state->key[num + 1] & LOWER_MASK);
35 1
    state->key[num] = state->key[num + (M - N)] ^ (y >> 1) ^ mag02[y % 2];
36 1
    state->pos++;
37 1
  } else if (num == N - 1) {
38 1
    y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
39 1
    state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ mag02[y % 2];
40 1
    state->pos = 0;
41
  }
42
}
43

44 1
void add_state(mt19937_state *state1, mt19937_state *state2) {
45 1
  int i, pt1 = state1->pos, pt2 = state2->pos;
46

47 1
  if (pt2 - pt1 >= 0) {
48 1
    for (i = 0; i < N - pt2; i++)
49 1
      state1->key[i + pt1] ^= state2->key[i + pt2];
50 1
    for (; i < N - pt1; i++)
51 1
      state1->key[i + pt1] ^= state2->key[i + (pt2 - N)];
52 1
    for (; i < N; i++)
53 1
      state1->key[i + (pt1 - N)] ^= state2->key[i + (pt2 - N)];
54
  } else {
55 1
    for (i = 0; i < N - pt1; i++)
56 1
      state1->key[i + pt1] ^= state2->key[i + pt2];
57 1
    for (; i < N - pt2; i++)
58 1
      state1->key[i + (pt1 - N)] ^= state2->key[i + pt2];
59 1
    for (; i < N; i++)
60 1
      state1->key[i + (pt1 - N)] ^= state2->key[i + (pt2 - N)];
61
  }
62
}
63

64
/* compute pf(ss) using standard Horner method */
65 1
void horner1(unsigned long *pf, mt19937_state *state) {
66 1
  int i = MEXP - 1;
67
  mt19937_state *temp;
68

69 1
  temp = (mt19937_state *)calloc(1, sizeof(mt19937_state));
70

71 1
  while (get_coef(pf, i) == 0)
72 1
    i--;
73

74 1
  if (i > 0) {
75 1
    copy_state(temp, state);
76 1
    gen_next(temp);
77 1
    i--;
78 1
    for (; i > 0; i--) {
79 1
      if (get_coef(pf, i) != 0)
80 1
        add_state(temp, state);
81
      else
82
        ;
83 1
      gen_next(temp);
84
    }
85 1
    if (get_coef(pf, 0) != 0)
86 1
      add_state(temp, state);
87
    else
88
      ;
89 0
  } else if (i == 0)
90 0
    copy_state(temp, state);
91
  else
92
    ;
93

94 1
  copy_state(state, temp);
95 1
  free(temp);
96
}
97

98 1
void mt19937_jump_state(mt19937_state *state) {
99
  unsigned long *pf;
100
  int i;
101

102 1
  pf = (unsigned long *)calloc(P_SIZE, sizeof(unsigned long));
103 1
  for (i = 0; i<P_SIZE; i++) {
104 1
    pf[i] = poly_coef[i];
105
  }
106

107 1
  if (state->pos >= N) {
108 0
    state->pos = 0;
109
  }
110

111 1
  horner1(pf, state);
112

113 1
  free(pf);
114
}

Read our documentation on viewing source code .

Loading