blob: 69dd140faab62a9d1567c45933c4b0d5012f3449 [file] [log] [blame]
Martin Roth9b1b3352016-02-24 12:27:06 -08001/******************************************************************/
2/* Random number generator */
3/* concatenation of following two 16-bit multiply with carry generators */
4/* x(n)=a*x(n-1)+carry mod 2^16 and y(n)=b*y(n-1)+carry mod 2^16, */
5/* number and carry packed within the same 32 bit integer. */
6/******************************************************************/
7#include "stdint.h"
8#include "cpuid.h"
9#include "smp.h"
10
11/* Keep a separate seed for each CPU */
12/* Space the seeds by at least a cache line or performance suffers big time! */
13static unsigned int SEED_X[MAX_CPUS*16];
14static unsigned int SEED_Y[MAX_CPUS*16];
15
16unsigned long rand (int cpu)
17{
18 static unsigned int a = 18000, b = 30903;
19 int me;
20
21 me = cpu*16;
22
23 SEED_X[me] = a*(SEED_X[me]&65535) + (SEED_X[me]>>16);
24 SEED_Y[me] = b*(SEED_Y[me]&65535) + (SEED_Y[me]>>16);
25
26 return ((SEED_X[me]<<16) + (SEED_Y[me]&65535));
27}
28
29
30void rand_seed( unsigned int seed1, unsigned int seed2, int cpu)
31{
32 int me;
33
34 me = cpu*16;
35 SEED_X[me] = seed1;
36 SEED_Y[me] = seed2;
37}
38