Changeset 821 for soft/giet_vm/applications/rosenfeld/src/mt19937.c
- Timestamp:
- May 6, 2016, 3:06:29 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/rosenfeld/src/mt19937.c
r772 r821 54 54 55 55 static unsigned long mt[N]; /* the array for the state vector */ 56 static int mti =N+1;/* mti==N+1 means mt[N] is not initialized */56 static int mti = N + 1 ; /* mti==N+1 means mt[N] is not initialized */ 57 57 58 58 static int global_seed; … … 61 61 void init_genrand(unsigned long s) 62 62 { 63 mt[0] = s & 0xffffffffUL;63 mt[0] = s & 0xffffffffUL; 64 64 global_seed = s; // modif LL 65 for (mti=1; mti<N; mti++) { 66 mt[mti] = 67 (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 65 for (mti = 1; mti < N; mti++) { 66 mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti); 68 67 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 69 68 /* In the previous versions, MSBs of the seed affect */ … … 84 83 //init_genrand(19650218UL); 85 84 init_genrand(global_seed); 86 i=1; j=0; 87 k = (N>key_length ? N : key_length); 85 i = 1; 86 j = 0; 87 k = (N > key_length ? N : key_length); 88 88 for (; k; k--) { 89 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) 90 + init_key[j] + j; /* non linear */ 91 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ 92 i++; j++; 93 if (i>=N) { mt[0] = mt[N-1]; i=1; } 94 if (j>=key_length) j=0; 95 } 96 for (k=N-1; k; k--) { 97 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) 98 - i; /* non linear */ 89 mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) + init_key[j] + j; /* non linear */ 99 90 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ 100 91 i++; 101 if (i>=N) { mt[0] = mt[N-1]; i=1; } 92 j++; 93 if (i >= N) { 94 mt[0] = mt[N - 1]; 95 i = 1; 96 } 97 if (j >= key_length) { 98 j = 0; 99 } 100 } 101 for (k = N - 1; k; k--) { 102 mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL)) - i; /* non linear */ 103 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ 104 i++; 105 if (i >= N) { 106 mt[0] = mt[N - 1]; 107 i = 1; 108 } 102 109 } 103 110 … … 109 116 { 110 117 unsigned long y; 111 static unsigned long mag01[2] ={0x0UL, MATRIX_A};118 static unsigned long mag01[2] = {0x0UL, MATRIX_A}; 112 119 /* mag01[x] = x * MATRIX_A for x=0,1 */ 113 120 … … 115 122 int kk; 116 123 117 if (mti == N +1)/* if init_genrand() has not been called, */124 if (mti == N + 1) { /* if init_genrand() has not been called, */ 118 125 //init_genrand(5489UL); /* a default initial seed is used */ 119 126 init_genrand(global_seed); 120 121 for (kk=0;kk<N-M;kk++) { 122 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 123 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL]; 124 } 125 for (;kk<N-1;kk++) { 126 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); 127 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; 128 } 129 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); 130 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; 127 } 128 129 for (kk = 0;kk < N - M; kk++) { 130 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 131 mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL]; 132 } 133 for (; kk < N - 1; kk++) { 134 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); 135 mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; 136 } 137 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 138 mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL]; 131 139 132 140 mti = 0; … … 147 155 long genrand_int31(void) 148 156 { 149 return (long) (genrand_int32()>>1);157 return (long) (genrand_int32() >> 1); 150 158 } 151 159 … … 153 161 double genrand_real1(void) 154 162 { 155 return genrand_int32() *(1.0/4294967295.0);163 return genrand_int32() * (1.0 / 4294967295.0); 156 164 /* divided by 2^32-1 */ 157 165 } … … 160 168 double genrand_real2(void) 161 169 { 162 return genrand_int32() *(1.0/4294967296.0);170 return genrand_int32() * (1.0 / 4294967296.0); 163 171 /* divided by 2^32 */ 164 172 } … … 167 175 double genrand_real3(void) 168 176 { 169 return (((double) genrand_int32()) + 0.5)*(1.0/4294967296.0);177 return (((double) genrand_int32()) + 0.5) * (1.0 / 4294967296.0); 170 178 /* divided by 2^32 */ 171 179 } … … 174 182 double genrand_res53(void) 175 183 { 176 unsigned long a =genrand_int32()>>5, b=genrand_int32()>>6;177 return (a*67108864.0+b)*(1.0/9007199254740992.0);184 unsigned long a = genrand_int32() >> 5, b = genrand_int32() >> 6; 185 return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0); 178 186 } 179 187 /* These real versions are due to Isaku Wada, 2002/01/09 added */ … … 182 190 { 183 191 int i; 184 unsigned long init[4] ={0x123, 0x234, 0x345, 0x456}, length=4;192 unsigned long init[4] = {0x123, 0x234, 0x345, 0x456}, length = 4; 185 193 init_by_array(init, length); 186 194 printf("1000 outputs of genrand_int32()\n"); 187 for (i =0; i<1000; i++) {195 for (i = 0; i < 1000; i++) { 188 196 printf("%10lu ", genrand_int32()); 189 if (i%5==4) printf("\n"); 197 if (i % 5 == 4) { 198 printf("\n"); 199 } 190 200 } 191 201 printf("\n1000 outputs of genrand_real2()\n"); 192 for (i =0; i<1000; i++) {202 for (i = 0; i < 1000; i++) { 193 203 printf("%10.8f ", genrand_real2()); 194 if (i%5==4) printf("\n"); 204 if (i % 5 == 4) { 205 printf("\n"); 206 } 195 207 } 196 208 return 0; 197 209 } 210 211 212 // Local Variables: 213 // tab-width: 4 214 // c-basic-offset: 4 215 // c-file-offsets:((innamespace . 0)(inline-open . 0)) 216 // indent-tabs-mode: nil 217 // End: 218 219 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 220
Note: See TracChangeset
for help on using the changeset viewer.