ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/rng_mt.c
(Generate patch)

Comparing ircd-hybrid-8/src/rng_mt.c (file contents):
Revision 1342 by michael, Tue Aug 9 20:29:20 2011 UTC vs.
Revision 1343 by michael, Sat Apr 7 18:46:29 2012 UTC

# Line 43 | Line 43
43     $Id$
44   */
45  
46 + #include "stdinc.h"
47   #include "rng_mt.h"
48  
49   /* Period parameters */  
50   #define N 624
51   #define M 397
52 < #define MATRIX_A 0x9908b0dfUL   /* constant vector a         */
53 < #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
54 < #define LOWER_MASK 0x7fffffffUL /* least significant r bits  */
52 > #define MATRIX_A 0x9908b0dfU   /* constant vector a         */
53 > #define UPPER_MASK 0x80000000U /* most significant w-r bits */
54 > #define LOWER_MASK 0x7fffffffU /* least significant r bits  */
55  
56 < static unsigned long mt[N]; /* the array for the state vector  */
56 > static uint32_t mt[N]; /* the array for the state vector  */
57   static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */
58  
59   /* initializes mt[N] with a seed */
60   void
61 < init_genrand(unsigned long s)
61 > init_genrand(uint32_t s)
62   {
63 <  mt[0] = s & 0xffffffffUL;
63 >  mt[0] = s & 0xffffffffU;
64  
65    for (mti = 1; mti < N; mti++)
66    {
67 <    mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
67 >    mt[mti] = (1812433253U * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
68      /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
69      /* In the previous versions, MSBs of the seed affect   */
70      /* only MSBs of the array mt[].                        */
71      /* 2002/01/09 modified by Makoto Matsumoto             */
72 <    mt[mti] &= 0xffffffffUL;
72 >    mt[mti] &= 0xffffffffU;
73      /* for >32 bit machines */
74    }
75   }
# Line 78 | Line 79 | init_genrand(unsigned long s)
79   /* key_length is its length */
80   /* slight change for C++, 2004/2/26 */
81   void
82 < init_by_array(unsigned long init_key[], int key_length)
82 > init_by_array(uint32_t init_key[], int key_length)
83   {
84    int i, j, k;
85  
86 <  init_genrand(19650218UL);
86 >  init_genrand(19650218U);
87  
88    i = 1; j = 0;
89    k = (N > key_length ? N : key_length);
90  
91    for (; k; k--)
92    {
93 <    mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL))
93 >    mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525U))
94            + init_key[j] + j; /* non linear */
95 <    mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
95 >    mt[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
96      i++; j++;
97  
98      if (i >= N)
# Line 106 | Line 107 | init_by_array(unsigned long init_key[],
107  
108    for (k = N - 1; k; k--)
109    {
110 <    mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL))
110 >    mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941U))
111            - i; /* non linear */
112 <    mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
112 >    mt[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
113      i++;
114  
115      if (i >= N)
# Line 118 | Line 119 | init_by_array(unsigned long init_key[],
119      }
120    }
121  
122 <  mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
122 >  mt[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */
123   }
124  
125   /* generates a random number on [0,0xffffffff]-interval */
126 < unsigned long
126 > uint32_t
127   genrand_int32(void)
128   {
129 <  unsigned long y;
130 <  static unsigned long mag01[2]={ 0x0UL, MATRIX_A };
129 >  uint32_t y;
130 >  static uint32_t mag01[2]={ 0x0U, MATRIX_A };
131    /* mag01[x] = x * MATRIX_A  for x=0,1 */
132  
133    if (mti >= N)
# Line 134 | Line 135 | genrand_int32(void)
135      int kk;
136  
137      if (mti == N + 1)   /* if init_genrand() has not been called, */
138 <      init_genrand(5489UL); /* a default initial seed is used */
138 >      init_genrand(5489U); /* a default initial seed is used */
139  
140      for (kk = 0; kk < N - M; kk++)
141      {
142        y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
143 <      mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL];
143 >      mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1U];
144      }
145  
146      for (; kk < N - 1; kk++)
147      {
148        y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
149 <      mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
149 >      mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1U];
150      }
151  
152      y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
153 <    mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
153 >    mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1U];
154  
155      mti = 0;
156    }
# Line 158 | Line 159 | genrand_int32(void)
159  
160    /* Tempering */
161    y ^= (y >> 11);
162 <  y ^= (y <<  7) & 0x9d2c5680UL;
163 <  y ^= (y << 15) & 0xefc60000UL;
162 >  y ^= (y <<  7) & 0x9d2c5680U;
163 >  y ^= (y << 15) & 0xefc60000U;
164    y ^= (y >> 18);
165  
166    return y;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines