Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/linux-mips/linux-dm7025.git] / lib / random32.c
blobca87d86992bdb7bfd6bb30d4dbe6dcefe2bab7b9
1 /*
2 This is a maximally equidistributed combined Tausworthe generator
3 based on code from GNU Scientific Library 1.5 (30 Jun 2004)
5 x_n = (s1_n ^ s2_n ^ s3_n)
7 s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
8 s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
9 s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
11 The period of this generator is about 2^88.
13 From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
14 Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
16 This is available on the net from L'Ecuyer's home page,
18 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
19 ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
21 There is an erratum in the paper "Tables of Maximally
22 Equidistributed Combined LFSR Generators", Mathematics of
23 Computation, 68, 225 (1999), 261--269:
24 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
26 ... the k_j most significant bits of z_j must be non-
27 zero, for each j. (Note: this restriction also applies to the
28 computer code given in [4], but was mistakenly not mentioned in
29 that paper.)
31 This affects the seeding procedure by imposing the requirement
32 s1 > 1, s2 > 7, s3 > 15.
36 #include <linux/types.h>
37 #include <linux/percpu.h>
38 #include <linux/module.h>
39 #include <linux/jiffies.h>
40 #include <linux/random.h>
42 struct rnd_state {
43 u32 s1, s2, s3;
46 static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
48 static u32 __random32(struct rnd_state *state)
50 #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
52 state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
53 state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
54 state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
56 return (state->s1 ^ state->s2 ^ state->s3);
59 static void __set_random32(struct rnd_state *state, unsigned long s)
61 if (s == 0)
62 s = 1; /* default seed is 1 */
64 #define LCG(n) (69069 * n)
65 state->s1 = LCG(s);
66 state->s2 = LCG(state->s1);
67 state->s3 = LCG(state->s2);
69 /* "warm it up" */
70 __random32(state);
71 __random32(state);
72 __random32(state);
73 __random32(state);
74 __random32(state);
75 __random32(state);
78 /**
79 * random32 - pseudo random number generator
81 * A 32 bit pseudo-random number is generated using a fast
82 * algorithm suitable for simulation. This algorithm is NOT
83 * considered safe for cryptographic use.
85 u32 random32(void)
87 unsigned long r;
88 struct rnd_state *state = &get_cpu_var(net_rand_state);
89 r = __random32(state);
90 put_cpu_var(state);
91 return r;
93 EXPORT_SYMBOL(random32);
95 /**
96 * srandom32 - add entropy to pseudo random number generator
97 * @seed: seed value
99 * Add some additional seeding to the random32() pool.
101 void srandom32(u32 entropy)
103 int i;
105 * No locking on the CPUs, but then somewhat random results are, well,
106 * expected.
108 for_each_possible_cpu (i) {
109 struct rnd_state *state = &per_cpu(net_rand_state, i);
110 __set_random32(state, state->s1 ^ entropy);
113 EXPORT_SYMBOL(srandom32);
116 * Generate some initially weak seeding values to allow
117 * to start the random32() engine.
119 static int __init random32_init(void)
121 int i;
123 for_each_possible_cpu(i) {
124 struct rnd_state *state = &per_cpu(net_rand_state,i);
125 __set_random32(state, i + jiffies);
127 return 0;
129 core_initcall(random32_init);
132 * Generate better values after random number generator
133 * is fully initalized.
135 static int __init random32_reseed(void)
137 int i;
138 unsigned long seed;
140 for_each_possible_cpu(i) {
141 struct rnd_state *state = &per_cpu(net_rand_state,i);
143 get_random_bytes(&seed, sizeof(seed));
144 __set_random32(state, seed);
146 return 0;
148 late_initcall(random32_reseed);