debian: Created a debian equivalent to xen-bugtool
[sliver-openvswitch.git] / datapath / linux-2.6 / compat-2.6 / random32.c
1 #include <linux/version.h>
2 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
3
4 /*
5   This is a maximally equidistributed combined Tausworthe generator
6   based on code from GNU Scientific Library 1.5 (30 Jun 2004)
7
8    x_n = (s1_n ^ s2_n ^ s3_n)
9
10    s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
11    s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
12    s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
13
14    The period of this generator is about 2^88.
15
16    From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
17    Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
18
19    This is available on the net from L'Ecuyer's home page,
20
21    http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
22    ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
23
24    There is an erratum in the paper "Tables of Maximally
25    Equidistributed Combined LFSR Generators", Mathematics of
26    Computation, 68, 225 (1999), 261--269:
27    http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
28
29         ... the k_j most significant bits of z_j must be non-
30         zero, for each j. (Note: this restriction also applies to the
31         computer code given in [4], but was mistakenly not mentioned in
32         that paper.)
33
34    This affects the seeding procedure by imposing the requirement
35    s1 > 1, s2 > 7, s3 > 15.
36
37 */
38
39 #include <linux/types.h>
40 #include <linux/module.h>
41 #include <linux/jiffies.h>
42 #include <linux/random.h>
43 #include <linux/smp.h>
44
45 #include "compat26.h"
46
47 struct rnd_state {
48         u32 s1, s2, s3;
49 };
50
51 static struct rnd_state net_rand_state[NR_CPUS];
52
53 static u32 __random32(struct rnd_state *state)
54 {
55 #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
56
57         state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
58         state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
59         state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
60
61         return (state->s1 ^ state->s2 ^ state->s3);
62 }
63
64 static void __set_random32(struct rnd_state *state, unsigned long s)
65 {
66         if (s == 0)
67                 s = 1;      /* default seed is 1 */
68
69 #define LCG(n) (69069 * n)
70         state->s1 = LCG(s);
71         state->s2 = LCG(state->s1);
72         state->s3 = LCG(state->s2);
73
74         /* "warm it up" */
75         __random32(state);
76         __random32(state);
77         __random32(state);
78         __random32(state);
79         __random32(state);
80         __random32(state);
81 }
82
83 /**
84  *      random32 - pseudo random number generator
85  *
86  *      A 32 bit pseudo-random number is generated using a fast
87  *      algorithm suitable for simulation. This algorithm is NOT
88  *      considered safe for cryptographic use.
89  */
90 u32 random32(void)
91 {
92         return __random32(&net_rand_state[smp_processor_id()]);
93 }
94
95 /**
96  *      srandom32 - add entropy to pseudo random number generator
97  *      @seed: seed value
98  *
99  *      Add some additional seeding to the random32() pool.
100  *      Note: this pool is per cpu so it only affects current CPU.
101  */
102 void srandom32(u32 entropy)
103 {
104         struct rnd_state *state = &net_rand_state[smp_processor_id()];
105         __set_random32(state, state->s1 ^ entropy);
106 }
107
108 static int __init random32_reseed(void);
109
110 /*
111  *      Generate some initially weak seeding values to allow
112  *      to start the random32() engine.
113  */
114 int __init random32_init(void)
115 {
116         int i;
117
118         for (i = 0; i < NR_CPUS; i++) {
119                 struct rnd_state *state = &net_rand_state[i];
120                 __set_random32(state, i + jiffies);
121         }
122         random32_reseed();
123         return 0;
124 }
125
126 /*
127  *      Generate better values after random number generator
128  *      is fully initalized.
129  */
130 static int __init random32_reseed(void)
131 {
132         int i;
133         unsigned long seed;
134
135         for (i = 0; i < NR_CPUS; i++) {
136                 struct rnd_state *state = &net_rand_state[i];
137
138                 get_random_bytes(&seed, sizeof(seed));
139                 __set_random32(state, seed);
140         }
141         return 0;
142 }
143
144 #endif /* kernel < 2.6.19 */