Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / rng / taus113.c
1 /* rng/taus113.c
2  * Copyright (C) 2002 Atakan Gurkan
3  * Based on the file taus.c which has the notice
4  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or (at
9  * your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 /* This is a maximally equidistributed combined, collision free 
22    Tausworthe generator, with a period ~2^{113}. The sequence is,
23
24    x_n = (z1_n ^ z2_n ^ z3_n ^ z4_n)  
25
26    b = (((z1_n <<  6) ^ z1_n) >> 13)
27    z1_{n+1} = (((z1_n & 4294967294) << 18) ^ b)
28    b = (((z2_n <<  2) ^ z2_n) >> 27)
29    z2_{n+1} = (((z2_n & 4294967288) <<  2) ^ b)
30    b = (((z3_n << 13) ^ z3_n) >> 21)
31    z3_{n+1} = (((z3_n & 4294967280) <<  7) ^ b)
32    b = (((z4_n <<  3)  ^ z4_n) >> 12)
33    z4_{n+1} = (((z4_n & 4294967168) << 13) ^ b)
34
35    computed modulo 2^32. In the formulas above '^' means exclusive-or 
36    (C-notation), not exponentiation. 
37    The algorithm is for 32-bit integers, hence a bitmask is used to clear 
38    all but least significant 32 bits, after left shifts, to make the code 
39    work on architectures where integers are 64-bit.
40
41    The generator is initialized with 
42    zi = (69069 * z{i+1}) MOD 2^32 where z0 is the seed provided
43    During initialization a check is done to make sure that the initial seeds 
44    have a required number of their most significant bits set.
45    After this, the state is passed through the RNG 10 times to ensure the
46    state satisfies a recurrence relation.
47
48    References:
49    P. L'Ecuyer, "Tables of Maximally-Equidistributed Combined LFSR Generators",
50    Mathematics of Computation, 68, 225 (1999), 261--269.
51      http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
52    P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe Generators", 
53    Mathematics of Computation, 65, 213 (1996), 203--213.
54      http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
55    the online version of the latter contains corrections to the print version.
56 */
57
58 #include <config.h>
59 #include <stdlib.h>
60 #include <gsl/gsl_rng.h>
61
62 #define LCG(n) ((69069UL * n) & 0xffffffffUL)
63 #define MASK 0xffffffffUL
64
65 static inline unsigned long int taus113_get (void *vstate);
66 static double taus113_get_double (void *vstate);
67 static void taus113_set (void *state, unsigned long int s);
68
69 typedef struct
70 {
71   unsigned long int z1, z2, z3, z4;
72 }
73 taus113_state_t;
74
75 static inline unsigned long
76 taus113_get (void *vstate)
77 {
78   taus113_state_t *state = (taus113_state_t *) vstate;
79   unsigned long b1, b2, b3, b4;
80
81   b1 = ((((state->z1 << 6UL) & MASK) ^ state->z1) >> 13UL);
82   state->z1 = ((((state->z1 & 4294967294UL) << 18UL) & MASK) ^ b1);
83
84   b2 = ((((state->z2 << 2UL) & MASK) ^ state->z2) >> 27UL);
85   state->z2 = ((((state->z2 & 4294967288UL) << 2UL) & MASK) ^ b2);
86
87   b3 = ((((state->z3 << 13UL) & MASK) ^ state->z3) >> 21UL);
88   state->z3 = ((((state->z3 & 4294967280UL) << 7UL) & MASK) ^ b3);
89
90   b4 = ((((state->z4 << 3UL) & MASK) ^ state->z4) >> 12UL);
91   state->z4 = ((((state->z4 & 4294967168UL) << 13UL) & MASK) ^ b4);
92
93   return (state->z1 ^ state->z2 ^ state->z3 ^ state->z4);
94
95 }
96
97 static double
98 taus113_get_double (void *vstate)
99 {
100   return taus113_get (vstate) / 4294967296.0;
101 }
102
103 static void
104 taus113_set (void *vstate, unsigned long int s)
105 {
106   taus113_state_t *state = (taus113_state_t *) vstate;
107
108   if (!s)
109     s = 1UL;                    /* default seed is 1 */
110
111   state->z1 = LCG (s);
112   if (state->z1 < 2UL)
113     state->z1 += 2UL;
114   state->z2 = LCG (state->z1);
115   if (state->z2 < 8UL)
116     state->z2 += 8UL;
117   state->z3 = LCG (state->z2);
118   if (state->z3 < 16UL)
119     state->z3 += 16UL;
120   state->z4 = LCG (state->z3);
121   if (state->z4 < 128UL)
122     state->z4 += 128UL;
123
124   /* Calling RNG ten times to satify recurrence condition */
125   taus113_get (state);
126   taus113_get (state);
127   taus113_get (state);
128   taus113_get (state);
129   taus113_get (state);
130   taus113_get (state);
131   taus113_get (state);
132   taus113_get (state);
133   taus113_get (state);
134   taus113_get (state);
135
136   return;
137 }
138
139 static const gsl_rng_type taus113_type = {
140   "taus113",                    /* name */
141   0xffffffffUL,                 /* RAND_MAX */
142   0,                            /* RAND_MIN */
143   sizeof (taus113_state_t),
144   &taus113_set,
145   &taus113_get,
146   &taus113_get_double
147 };
148
149 const gsl_rng_type *gsl_rng_taus113 = &taus113_type;
150
151
152 /*  Rules for analytic calculations using GNU Emacs Calc:
153     (used to find the values for the test program)
154
155   [ LCG(n) := n * 69069 mod (2^32) ]
156   
157   [ b1(x) := rsh(xor(lsh(x, 6), x), 13),
158   q1(x) := xor(lsh(and(x, 4294967294), 18), b1(x)),
159   b2(x) := rsh(xor(lsh(x, 2), x), 27),
160   q2(x) := xor(lsh(and(x, 4294967288), 2), b2(x)),
161   b3(x) := rsh(xor(lsh(x, 13), x), 21),
162   q3(x) := xor(lsh(and(x, 4294967280), 7), b3(x)),
163   b4(x) := rsh(xor(lsh(x, 3), x), 12),
164   q4(x) := xor(lsh(and(x, 4294967168), 13), b4(x))
165   ]
166   
167   [ S([z1,z2,z3,z4]) := [q1(z1), q2(z2), q3(z3), q4(z4)] ]
168 */