1 /*
2    A C-program for MT19937, with initialisation improved 2002/1/26.
3    Coded by Takuji Nishimura and Makoto Matsumoto.
4 
5    Before using, initialise the state by using csoundSeedRandMT().
6 
7    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
8    All rights reserved.
9 
10    Redistribution and use in source and binary forms, with or without
11    modification, are permitted provided that the following conditions
12    are met:
13 
14      1. Redistributions of source code must retain the above copyright
15         notice, this list of conditions and the following disclaimer.
16 
17      2. Redistributions in binary form must reproduce the above copyright
18         notice, this list of conditions and the following disclaimer in the
19         documentation and/or other materials provided with the distribution.
20 
21      3. The names of its contributors may not be used to endorse or promote
22         products derived from this software without specific prior written
23         permission.
24 
25    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28    A PARTICULAR PURPOSE ARE DISCLAIMED.
29    IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36    POSSIBILITY OF SUCH DAMAGE.
37 
38    Any feedback is very welcome.
39    http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
40    email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
41 */
42 
43 #include "csoundCore.h"
44 
45 /* simple linear congruential generator */
46 
csoundRand31(int32_t * seedVal)47 PUBLIC int32_t csoundRand31(int32_t *seedVal)
48 {
49     uint64_t  tmp1;
50     uint32_t  tmp2;
51 
52     /* x = (742938285 * x) % 0x7FFFFFFF */
53     tmp1 = (uint64_t) ((int32_t) (*seedVal) * (int64_t) 742938285);
54     tmp2 = (uint32_t) tmp1 & (uint32_t) 0x7FFFFFFF;
55     tmp2 += (uint32_t) (tmp1 >> 31);
56     tmp2 = (tmp2 & (uint32_t) 0x7FFFFFFF) + (tmp2 >> 31);
57     (*seedVal) = (int32_t) tmp2;
58     return (int32_t) tmp2;
59 }
60 
61 /* Period parameters */
62 
63 #define N           (624)
64 #define M           (397)
65 #define MATRIX_A    0x9908B0DFU     /* constant vector a */
66 #define UPPER_MASK  0x80000000U     /* most significant w-r bits */
67 #define LOWER_MASK  0x7FFFFFFFU     /* least significant r bits */
68 
MT_update_state(uint32_t * mt)69 static CS_NOINLINE void MT_update_state(uint32_t *mt)
70 {
71     /* mag01[x] = x * MATRIX_A  for x=0,1 */
72     const uint32_t  mag01[2] = { (uint32_t) 0, (uint32_t) MATRIX_A };
73     int32_t       i;
74     uint32_t  y;
75 
76     for (i = 0; i < (N - M); i++) {
77       y = (mt[i] & UPPER_MASK) | (mt[i + 1] & LOWER_MASK);
78       mt[i] = mt[i + M] ^ (y >> 1) ^ mag01[y & (uint32_t) 1];
79     }
80     for ( ; i < (N - 1); i++) {
81       y = (mt[i] & UPPER_MASK) | (mt[i + 1] & LOWER_MASK);
82       mt[i] = mt[i + (M - N)] ^ (y >> 1) ^ mag01[y & (uint32_t) 1];
83     }
84     y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
85     mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & (uint32_t) 1];
86 }
87 
88 /* generates a random number on [0,0xffffffff]-interval */
89 
csoundRandMT(CsoundRandMTState * p)90 PUBLIC uint32_t csoundRandMT(CsoundRandMTState *p)
91 {
92     int32_t       i = p->mti;
93     uint32_t  y;
94 
95     if (i >= N) {                   /* generate N words at one time */
96       MT_update_state(&(p->mt[0]));
97       i = 0;
98     }
99     y = p->mt[i];
100     p->mti = i + 1;
101     /* Tempering */
102     y ^= (y >> 11);
103     y ^= (y << 7) & (uint32_t) 0x9D2C5680U;
104     y ^= (y << 15) & (uint32_t) 0xEFC60000U;
105     y ^= (y >> 18);
106 
107     return y;
108 }
109 
110 /* initialise by an array with array-length */
111 /* init_key is the array for initialising keys */
112 /* key_length is its length */
113 /* slight change for C++, 2004/2/26 */
114 
csoundSeedRandMT(CsoundRandMTState * p,const uint32_t * initKey,uint32_t keyLength)115 PUBLIC void csoundSeedRandMT(CsoundRandMTState *p,
116                              const uint32_t *initKey, uint32_t keyLength)
117 {
118     int32_t       i, j, k;
119     uint32_t  x;
120 
121     /* if array is NULL, use length parameter as simple 32 bit seed */
122     x = (initKey == NULL ? keyLength : (uint32_t) 19650218);
123     p->mt[0] = x;
124     for (i = 1; i < N; i++) {
125       /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
126       /* In the previous versions, MSBs of the seed affect   */
127       /* only MSBs of the array mt[].                        */
128       /* 2002/01/09 modified by Makoto Matsumoto             */
129       x = ((uint32_t) 1812433253 * (x ^ (x >> 30)) + (uint32_t) i);
130       p->mt[i] = x;
131     }
132     p->mti = N;
133     if (initKey == NULL)
134       return;
135     i = 0; j = 0;
136     k = (N > (int32_t) keyLength ? N : (int32_t) keyLength);
137     for ( ; k; k--) {
138       x = p->mt[i++];
139       p->mt[i] = (p->mt[i] ^ ((x ^ (x >> 30)) * (uint32_t) 1664525))
140                  + initKey[j] + (uint32_t) j;   /* non linear */
141       if (i == (N - 1)) {
142         p->mt[0] = p->mt[N - 1];
143         i = 0;
144       }
145       if (++j >= (int32_t) keyLength)
146         j = 0;
147     }
148     for (k = (N - 1); k; k--) {
149       x = p->mt[i++];
150       p->mt[i] = (p->mt[i] ^ ((x ^ (x >> 30)) * (uint32_t) 1566083941))
151                  - (uint32_t) i;                /* non linear */
152       if (i == (N - 1)) {
153         p->mt[0] = p->mt[N - 1];
154         i = 0;
155       }
156     }
157     /* MSB is 1; assuring non-zero initial array */
158     p->mt[0] = (uint32_t) 0x80000000U;
159 }
160 
161 /* called from csoundPreCompile() */
162 
csound_init_rand(CSOUND * csound)163 void csound_init_rand(CSOUND *csound)
164 {
165     uint32_t  tmp;
166 
167     csound->csRandState = &(csound->randState_);
168     csound->randSeed1 = 15937;
169     tmp = (uint32_t) csound->GetRandomSeedFromTime();
170     while (tmp >= (uint32_t) 0x7FFFFFFE)
171       tmp -= (uint32_t) 0x7FFFFFFE;
172     csound->randSeed2 = ((int) tmp + 1);
173     csound->SeedRandMT(&(csound->randState_), NULL, (uint32_t) 5489);
174 }
175 
176