Linux 2.6.31.8
[linux/fpc-iii.git] / lib / random32.c
blob217d5c4b666d22e4f309ad363c157580c2810bf8
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);
60 * Handle minimum values for seeds
62 static inline u32 __seed(u32 x, u32 m)
64 return (x < m) ? x + m : x;
67 /**
68 * random32 - pseudo random number generator
70 * A 32 bit pseudo-random number is generated using a fast
71 * algorithm suitable for simulation. This algorithm is NOT
72 * considered safe for cryptographic use.
74 u32 random32(void)
76 unsigned long r;
77 struct rnd_state *state = &get_cpu_var(net_rand_state);
78 r = __random32(state);
79 put_cpu_var(state);
80 return r;
82 EXPORT_SYMBOL(random32);
84 /**
85 * srandom32 - add entropy to pseudo random number generator
86 * @seed: seed value
88 * Add some additional seeding to the random32() pool.
90 void srandom32(u32 entropy)
92 int i;
94 * No locking on the CPUs, but then somewhat random results are, well,
95 * expected.
97 for_each_possible_cpu (i) {
98 struct rnd_state *state = &per_cpu(net_rand_state, i);
99 state->s1 = __seed(state->s1 ^ entropy, 1);
102 EXPORT_SYMBOL(srandom32);
105 * Generate some initially weak seeding values to allow
106 * to start the random32() engine.
108 static int __init random32_init(void)
110 int i;
112 for_each_possible_cpu(i) {
113 struct rnd_state *state = &per_cpu(net_rand_state,i);
115 #define LCG(x) ((x) * 69069) /* super-duper LCG */
116 state->s1 = __seed(LCG(i + jiffies), 1);
117 state->s2 = __seed(LCG(state->s1), 7);
118 state->s3 = __seed(LCG(state->s2), 15);
120 /* "warm it up" */
121 __random32(state);
122 __random32(state);
123 __random32(state);
124 __random32(state);
125 __random32(state);
126 __random32(state);
128 return 0;
130 core_initcall(random32_init);
133 * Generate better values after random number generator
134 * is fully initalized.
136 static int __init random32_reseed(void)
138 int i;
140 for_each_possible_cpu(i) {
141 struct rnd_state *state = &per_cpu(net_rand_state,i);
142 u32 seeds[3];
144 get_random_bytes(&seeds, sizeof(seeds));
145 state->s1 = __seed(seeds[0], 1);
146 state->s2 = __seed(seeds[1], 7);
147 state->s3 = __seed(seeds[2], 15);
149 /* mix it in */
150 __random32(state);
152 return 0;
154 late_initcall(random32_reseed);