blob: 8a965e7a5dbdee684f44654b9361f6479fa1bcc6 [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{
Ben Gardner90f7d112016-03-15 15:25:22 -050018 static unsigned int a = 18000, b = 30903;
19 int me;
Martin Roth9b1b3352016-02-24 12:27:06 -080020
Ben Gardner90f7d112016-03-15 15:25:22 -050021 me = cpu*16;
Martin Roth9b1b3352016-02-24 12:27:06 -080022
Ben Gardner90f7d112016-03-15 15:25:22 -050023 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);
Martin Roth9b1b3352016-02-24 12:27:06 -080025
Ben Gardner90f7d112016-03-15 15:25:22 -050026 return ((SEED_X[me]<<16) + (SEED_Y[me]&65535));
Martin Roth9b1b3352016-02-24 12:27:06 -080027}
28
29
30void rand_seed( unsigned int seed1, unsigned int seed2, int cpu)
31{
Ben Gardner90f7d112016-03-15 15:25:22 -050032 int me;
Martin Roth9b1b3352016-02-24 12:27:06 -080033
Ben Gardner90f7d112016-03-15 15:25:22 -050034 me = cpu*16;
35 SEED_X[me] = seed1;
36 SEED_Y[me] = seed2;
Martin Roth9b1b3352016-02-24 12:27:06 -080037}