blob: 8a965e7a5dbdee684f44654b9361f6479fa1bcc6 [file] [log] [blame]
 Martin Roth 9b1b335 2016-02-24 12:27:06 -0800 [diff] [blame] 1 /******************************************************************/ 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! */ 13 static unsigned int SEED_X[MAX_CPUS*16]; 14 static unsigned int SEED_Y[MAX_CPUS*16]; 15 16 unsigned long rand (int cpu) 17 { Ben Gardner 90f7d11 2016-03-15 15:25:22 -0500 [diff] [blame^] 18 static unsigned int a = 18000, b = 30903; 19 int me; Martin Roth 9b1b335 2016-02-24 12:27:06 -0800 [diff] [blame] 20 Ben Gardner 90f7d11 2016-03-15 15:25:22 -0500 [diff] [blame^] 21 me = cpu*16; Martin Roth 9b1b335 2016-02-24 12:27:06 -0800 [diff] [blame] 22 Ben Gardner 90f7d11 2016-03-15 15:25:22 -0500 [diff] [blame^] 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); Martin Roth 9b1b335 2016-02-24 12:27:06 -0800 [diff] [blame] 25 Ben Gardner 90f7d11 2016-03-15 15:25:22 -0500 [diff] [blame^] 26 return ((SEED_X[me]<<16) + (SEED_Y[me]&65535)); Martin Roth 9b1b335 2016-02-24 12:27:06 -0800 [diff] [blame] 27 } 28 29 30 void rand_seed( unsigned int seed1, unsigned int seed2, int cpu) 31 { Ben Gardner 90f7d11 2016-03-15 15:25:22 -0500 [diff] [blame^] 32 int me; Martin Roth 9b1b335 2016-02-24 12:27:06 -0800 [diff] [blame] 33 Ben Gardner 90f7d11 2016-03-15 15:25:22 -0500 [diff] [blame^] 34 me = cpu*16; 35 SEED_X[me] = seed1; 36 SEED_Y[me] = seed2; Martin Roth 9b1b335 2016-02-24 12:27:06 -0800 [diff] [blame] 37 }