1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rng.core.source64;
19
20 import java.util.List;
21 import java.util.ArrayList;
22 import org.apache.commons.rng.core.util.NumberFactory;
23
24
25
26
27
28
29
30
31
32
33
34
35 public class TwoCmres extends LongProvider {
36
37 private static final String INTERNAL_ERROR_MSG = "Internal error: Please file a bug report";
38
39 private static final byte SEED_GUARD = 9;
40
41 private static final Cmres.Factory FACTORY = new Cmres.Factory();
42
43 private final Cmres x;
44
45 private final Cmres y;
46
47 private long xx;
48
49 private long yy;
50
51
52
53
54
55
56
57
58 private TwoCmres(int seed,
59 Cmres x,
60 Cmres y) {
61 this.x = x;
62 this.y = y;
63 setSeedInternal(seed);
64 }
65
66
67
68
69
70
71 public TwoCmres(Integer seed) {
72 this(seed, 0, 1);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public TwoCmres(Integer seed,
88 int i,
89 int j) {
90 this(seed, FACTORY.getIfDifferent(i, j), FACTORY.get(j));
91 }
92
93
94 @Override
95 public long next() {
96 xx = x.transform(xx);
97 yy = y.transform(yy);
98
99 return xx + yy;
100 }
101
102
103 @Override
104 public String toString() {
105 return super.toString() + " (" + x + " + " + y + ")";
106 }
107
108
109
110
111
112
113 public static int numberOfSubcycleGenerators() {
114 return FACTORY.numberOfSubcycleGenerators();
115 }
116
117
118 @Override
119 protected byte[] getStateInternal() {
120 return composeStateInternal(NumberFactory.makeByteArray(new long[] {xx, yy}),
121 super.getStateInternal());
122 }
123
124
125 @Override
126 protected void setStateInternal(byte[] s) {
127 final byte[][] c = splitStateInternal(s, 16);
128
129 final long[] state = NumberFactory.makeLongArray(c[0]);
130 xx = state[0];
131 yy = state[1];
132
133 super.setStateInternal(c[1]);
134 }
135
136
137
138
139 private void setSeedInternal(int seed) {
140
141
142
143
144
145
146
147
148 final int xMax = (seed & 0xffff) + (SEED_GUARD & 0xff);
149 final int yMax = (seed >>> 16) + (SEED_GUARD & 0xff);
150
151 xx = x.getStart();
152 for (int i = xMax; i > 0; i--) {
153 xx = x.transform(xx);
154 }
155
156 yy = y.getStart();
157 for (int i = yMax; i > 0; i--) {
158 yy = y.transform(yy);
159 }
160 }
161
162
163
164
165
166 static class Cmres {
167
168 private static final String SEP = ", ";
169
170 private static final String HEX_FORMAT = "0x%016xL";
171
172 private final int start;
173
174 private final long multiply;
175
176 private final int rotate;
177
178
179
180
181
182
183 Cmres(long multiply,
184 int rotate,
185 int start) {
186 this.multiply = multiply;
187 this.rotate = rotate;
188 this.start = start;
189 }
190
191
192 @Override
193 public String toString() {
194 final String m = String.format((java.util.Locale) null, HEX_FORMAT, multiply);
195 return "Cmres: [" + m + SEP + rotate + SEP + start + "]";
196 }
197
198
199
200
201 public long getMultiply() {
202 return multiply;
203 }
204
205
206
207
208 public int getStart() {
209 return start;
210 }
211
212
213
214
215
216 long transform(long state) {
217 long s = state;
218 s *= multiply;
219 s = Long.rotateLeft(s, rotate);
220 s -= state;
221 return s;
222 }
223
224
225 static class Factory {
226
227 private static final List<Cmres> TABLE = new ArrayList<>();
228
229
230
231
232
233
234
235
236 static {
237 add(0xedce446814d3b3d9L, 33, 0x13b572e7);
238 add(0xc5b3cf786c806df7L, 33, 0x13c8e18a);
239 add(0xdd91bbb8ab9e0e65L, 31, 0x06dd03a6);
240 add(0x7b69342c0790221dL, 31, 0x1646bb8b);
241 add(0x0c72c0d18614c32bL, 33, 0x06014a3d);
242 add(0xd8d98c13bebe26c9L, 33, 0x014e8475);
243 add(0xcb039dc328bbc40fL, 31, 0x008684bd);
244 add(0x858c5ef3c021ed2fL, 32, 0x0dc8d622);
245 add(0x4c8be96bfc23b127L, 33, 0x0b6b20cc);
246 add(0x11eab77f808cf641L, 32, 0x06534421);
247 add(0xbc9bd78810fd28fdL, 31, 0x1d9ba40d);
248 add(0x0f1505c780688cb5L, 33, 0x0b7b7b67);
249 add(0xadc174babc2053afL, 31, 0x267f4197);
250 add(0x900b6b82b31686d9L, 31, 0x023c6985);
251
252 }
253
254
255
256
257 int numberOfSubcycleGenerators() {
258 return TABLE.size();
259 }
260
261
262
263
264
265 Cmres get(int index) {
266 if (index < 0 ||
267 index >= TABLE.size()) {
268 throw new IndexOutOfBoundsException("Out of interval [0, " +
269 (TABLE.size() - 1) + "]");
270 }
271
272 return TABLE.get(index);
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286 Cmres getIfDifferent(int index, int other) {
287 if (index == other) {
288 throw new IllegalArgumentException("Subcycle generators must be different");
289 }
290 return get(index);
291 }
292
293
294
295
296
297
298
299
300 private static void add(long multiply,
301 int rotate,
302 int start) {
303
304
305 checkUnique(TABLE, multiply);
306
307 TABLE.add(new Cmres(multiply, rotate, start));
308 }
309
310
311
312
313
314
315
316
317 static void checkUnique(List<Cmres> table, long multiply) {
318 for (final Cmres sg : table) {
319 if (multiply == sg.getMultiply()) {
320 throw new IllegalStateException(INTERNAL_ERROR_MSG);
321 }
322 }
323 }
324 }
325 }
326 }