forked from electrum/ssb-dbgen
-
Notifications
You must be signed in to change notification settings - Fork 10
/
speed_seed.c
325 lines (251 loc) · 7.65 KB
/
speed_seed.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* @(#)speed_seed.c 2.1.8.2 */
#include <stdio.h>
#include <stdlib.h>
#include "dss.h"
/* _tal long RandSeed = "Random^SeedFromTimestamp" (void); */
#define FAKE_V_STR(avg, sd, cnt) \
ADVANCE_STREAM(sd, \
(long)(Seed[sd].boundary*cnt))
#define ADVANCE_STREAM(stream_id, num_calls) \
NthElement(num_calls, &Seed[stream_id].value)
#define MAX_COLOR 92
long name_bits[MAX_COLOR / BITS_PER_LONG];
extern seed_t Seed[];
/* WARNING! This routine assumes the existence of 64-bit */
/* integers. The notation used here- "HUGE" is *not* ANSI standard. */
/* Hopefully, you have this extension as well. If not, use whatever */
/* nonstandard trick you need to in order to get 64 bit integers. */
/* The book says that this will work if MAXINT for the type you choose */
/* is at least 2**46 - 1, so 64 bits is more than you *really* need */
static DSS_HUGE Multiplier = 16807; /* or whatever nonstandard */
static DSS_HUGE Modulus = 2147483647; /* trick you use to get 64 bit int */
/* Advances value of Seed after N applications of the random number generator
with multiplier Mult and given Modulus.
NthElement(Seed[],count);
Theory: We are using a generator of the form
X_n = [Mult * X_(n-1)] mod Modulus. It turns out that
X_n = [(Mult ** n) X_0] mod Modulus.
This can be computed using a divide-and-conquer technique, see
the code below.
In words, this means that if you want the value of the Seed after n
applications of the generator, you multiply the initial value of the
Seed by the "super multiplier" which is the basic multiplier raised
to the nth power, and then take mod Modulus.
*/
/* Nth Element of sequence starting with StartSeed */
/* Warning, needs 64-bit integers */
#ifdef SUPPORT_64BITS
void NthElement (long N, long *StartSeed)
{
DSS_HUGE Z;
DSS_HUGE Mult;
static int ln=-1;
int i;
if ((verbose > 0) && ++ln % 1000 == 0)
{
i = ln % LN_CNT;
fprintf(stderr, "%c\b", lnoise[i]);
}
Mult = Multiplier;
Z = (DSS_HUGE) *StartSeed;
while (N > 0 )
{
if (N % 2 != 0) /* testing for oddness, this seems portable */
Z = (Mult * Z) % Modulus;
N = N / 2; /* integer division, truncates */
Mult = (Mult * Mult) % Modulus;
}
*StartSeed = (long)Z;
return;
}
#else
/* add 32 bit version of NthElement HERE */
/*
* MODMULT.C
* R. M. Shelton -- Unisys
* July 26, 1995
*
* RND_seed: Computes the nth seed in the total sequence
* RND_shift: Shifts a random number by a given number of seeds
* RND_ModMult: Multiplies two numbers mod (2^31 - 1)
*
*/
#include <math.h>
#include <stdio.h> /* required only for F_FatalError */
typedef signed long RND;
typedef unsigned long URND;
#define FatalError(e) F_FatalError( (e), __FILE__, __LINE__ )
void F_FatalError( int x, char *y, int z ) {fprintf(stderr, "Bang!\n");}
/* Prototypes */
RND RND_seed( RND );
RND RND_shift( RND, RND );
static RND RND_ModMult( RND, RND );
RND
RND_seed ( RND Order )
{
static const RND TopMask = 0x40000000;
RND Mask;
RND Result;
if (Order <= -Modulus || Order >= Modulus)
FatalError(1023);
if (Order < 0) Order = Modulus - 1L + Order;
Mask = TopMask;
Result = 1L;
while (Mask > Order) Mask >>= 1;
while (Mask > 0)
{
if (Mask & Order)
{
Result = RND_ModMult( Result, Result);
Result = RND_ModMult( Result, Multiplier );
}
else
{
Result = RND_ModMult( Result, Result );
}
Mask >>= 1;
}
return (Result);
} /* RND_seed */
/***********************************************************************
RND_shift: Shifts a random number by a given number of seeds
***********************************************************************/
void
NthElement ( long Shift, long *Seed)
{
RND Power;
static int ln=-1;
int i;
if ((verbose > 0) && ++ln % 100 == 0)
{
i = (ln/100) % LN_CNT;
fprintf(stderr, "%c\b", lnoise[i]);
}
if (*Seed <= 0 || *Seed >= Modulus)
FatalError(1023);
if (Shift <= -Modulus || Shift >= Modulus)
FatalError(1023);
Power = RND_seed( Shift );
*Seed = RND_ModMult( *Seed, Power );
return;
} /* RND_shift */
/*********************************************************************
RND_ModMult: Multiplies two numbers mod (2^31 - 1)
*********************************************************************/
static RND
RND_ModMult ( RND nA, RND nB)
{
static const double dTwoPowPlus31 = 2147483648.;
static const double dTwoPowMinus31 = 1./2147483648.;
static const double dTwoPowPlus15 = 32768.;
static const double dTwoPowMinus15 = 1./32768.;
static const RND nLowMask = 0xFFFFL;
static const URND ulBit31 = 1uL << 31;
double dAH, dAL, dX, dY, dZ, dW;
RND nH, nL;
URND ulP, ulQ, ulResult;
nL = nB & nLowMask;
nH = (nB - nL) >> 16;
dAH = (double)nA * (double)nH;
dAL = (double)nA * (double)nL;
dX = floor( dAH * dTwoPowMinus15 );
dY = dAH - dX*dTwoPowPlus15;
dZ = floor( dAL * dTwoPowMinus31 );
dW = dAL - dZ*dTwoPowPlus31;
ulQ = (URND)dW + ((URND)dY << 16);
ulP = (URND)dX + (URND)dZ;
if (ulQ & ulBit31) { ulQ -= ulBit31; ulP++; }
ulResult = ulP + ulQ;
if (ulResult & ulBit31) { ulResult -= ulBit31; ulResult++; }
return (RND)ulResult;
}
#endif /* SUPPORT_64BITS */
/* updates Seed[column] using the a_rnd algorithm */
void
fake_a_rnd(int min, int max, int column)
{
long len, itcount;
RANDOM(len, (long)min, (long)max, (long)column);
if (len % 5L == 0)
itcount = len/5;
else itcount = len/5 + 1L;
NthElement(itcount, &Seed[column].usage);
return;
}
long
sd_part(int child, long skip_count)
{
int i;
for (i=P_MFG_SD; i<= P_CNTR_SD; i++)
ADVANCE_STREAM(i, skip_count);
FAKE_V_STR(P_CMNT_LEN, P_CMNT_SD, skip_count);
ADVANCE_STREAM(P_NAME_SD, skip_count * 92);
return(0L);
}
long
sd_line(int child, long skip_count)
{
int i,j;
for (j=0; j < O_LCNT_MAX; j++)
{
for (i=L_QTY_SD; i<= L_RFLG_SD; i++)
ADVANCE_STREAM(i, skip_count);
}
FAKE_V_STR(L_CMNT_LEN, L_CMNT_SD, skip_count);
/* need to special case this as the link between master and detail */
if (child == 1)
{
ADVANCE_STREAM(O_ODATE_SD, skip_count);
ADVANCE_STREAM(O_LCNT_SD, skip_count);
}
return(0L);
}
long
sd_order(int child, long skip_count)
{
ADVANCE_STREAM(O_LCNT_SD, skip_count);
ADVANCE_STREAM(O_CKEY_SD, skip_count);
FAKE_V_STR(O_CMNT_LEN, O_CMNT_SD, skip_count);
ADVANCE_STREAM(O_SUPP_SD, skip_count);
ADVANCE_STREAM(O_CLRK_SD, skip_count);
ADVANCE_STREAM(O_PRIO_SD, skip_count);
ADVANCE_STREAM(O_ODATE_SD, skip_count);
return (0L);
}
long
sd_psupp(int child, long skip_count)
{
int j;
for (j=0; j < SUPP_PER_PART; j++)
{
ADVANCE_STREAM(PS_QTY_SD, skip_count);
ADVANCE_STREAM(PS_SCST_SD, skip_count);
}
FAKE_V_STR(PS_CMNT_LEN, PS_CMNT_SD, skip_count);
return(0L);
}
long
sd_cust(int child, long skip_count)
{
FAKE_V_STR(C_ADDR_LEN, C_ADDR_SD, skip_count);
FAKE_V_STR(C_CMNT_LEN, C_CMNT_SD, skip_count);
ADVANCE_STREAM(C_NTRG_SD, skip_count);
ADVANCE_STREAM(C_PHNE_SD, 3L * skip_count);
ADVANCE_STREAM(C_ABAL_SD, skip_count);
ADVANCE_STREAM(C_MSEG_SD, skip_count);
return(0L);
}
long
sd_supp(int child, long skip_count)
{
ADVANCE_STREAM(S_NTRG_SD, skip_count);
ADVANCE_STREAM(S_PHNE_SD, 3L * skip_count);
ADVANCE_STREAM(S_ABAL_SD, skip_count);
FAKE_V_STR(S_ADDR_LEN, S_ADDR_SD, skip_count);
FAKE_V_STR(S_CMNT_LEN, S_CMNT_SD, skip_count);
ADVANCE_STREAM(BBB_CMNT_SD, skip_count);
ADVANCE_STREAM(BBB_JNK_SD, skip_count);
ADVANCE_STREAM(BBB_OFFSET_SD, skip_count);
ADVANCE_STREAM(BBB_TYPE_SD, skip_count); /* avoid one trudge */
return(0L);
}