The flutter shutter camera simulator
mt19937ar.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  *   1. Redistributions of source code must retain the above copyright
00010  *      notice, this list of conditions and the following disclaimer.
00011  *
00012  *   2. Redistributions in binary form must reproduce the above copyright
00013  *      notice, this list of conditions and the following disclaimer in the
00014  *      documentation and/or other materials provided with the distribution.
00015  *
00016  *   3. The names of its contributors may not be used to endorse or promote
00017  *      products derived from this software without specific prior written
00018  *      permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00027  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00029  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00031  * OF THE POSSIBILITY OF SUCH DAMAGE.
00032  */
00033 
00045 #include <stdio.h>
00046 
00047 /* Period parameters */
00048 #define MT_N 624
00049 #define MT_M 397
00050 #define MT_MATRIX_A 0x9908b0dfUL        
00051 #define MT_UPPER_MASK 0x80000000UL      
00052 #define MT_LOWER_MASK 0x7fffffffUL      
00054 static unsigned long mt[MT_N];  
00055 static int mti = MT_N + 1;      
00061 static void init_genrand(unsigned long s)
00062 {
00063     mt[0] = s & 0xffffffffUL;
00064     for (mti = 1; mti < MT_N; mti++)
00065     {
00066         mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
00067         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
00068         /* In the previous versions, MSBs of the seed affect   */
00069         /* only MSBs of the array mt[].                        */
00070         /* 2002/01/09 modified by Makoto Matsumoto             */
00071         mt[mti] &= 0xffffffffUL;
00072         /* for >32 bit machines */
00073     }
00074 }
00075 
00079 static unsigned long genrand_int32(void)
00080 {
00081     unsigned long y;
00082     static unsigned long mag01[2] = { 0x0UL, MT_MATRIX_A };
00083     /* mag01[x] = x * MT_MATRIX_A  for x=0,1 */
00084 
00085     if (mti >= MT_N)
00086     {                           /* generate MT_N words at one time */
00087         int kk;
00088 
00089         if (mti == MT_N + 1)    /* if init_genrand() has not been called, */
00090             init_genrand(5489UL);       /* a default initial seed is used */
00091 
00092         for (kk = 0; kk < MT_N - MT_M; kk++)
00093         {
00094             y = (mt[kk] & MT_UPPER_MASK) | (mt[kk + 1] & MT_LOWER_MASK);
00095             mt[kk] = mt[kk + MT_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
00096         }
00097         for (; kk < MT_N - 1; kk++)
00098         {
00099             y = (mt[kk] & MT_UPPER_MASK) | (mt[kk + 1] & MT_LOWER_MASK);
00100             mt[kk] = mt[kk + (MT_M - MT_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
00101         }
00102         y = (mt[MT_N - 1] & MT_UPPER_MASK) | (mt[0] & MT_LOWER_MASK);
00103         mt[MT_N - 1] = mt[MT_M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
00104 
00105         mti = 0;
00106     }
00107 
00108     y = mt[mti++];
00109 
00110     /* Tempering */
00111     y ^= (y >> 11);
00112     y ^= (y << 7) & 0x9d2c5680UL;
00113     y ^= (y << 15) & 0xefc60000UL;
00114     y ^= (y >> 18);
00115 
00116     return y;
00117 }
00118 
00122 static double genrand_res53(void)
00123 {
00124     unsigned long a = genrand_int32() >> 5, b = genrand_int32() >> 6;
00125     return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
00126 }
00127 
00128 /*
00129  * non-static content
00130  */
00131 
00135 void mt_init_genrand(unsigned long s)
00136 {
00137     init_genrand(s);
00138     return;
00139 }
00140 
00144 double mt_genrand_res53(void)
00145 {
00146     return genrand_res53();
00147 }
00148 
00149 #undef MT_N
00150 #undef MT_M
00151 #undef MT_MATRIX_A
00152 #undef MT_UPPER_MASK
00153 #undef MT_LOWER_MASK
 All Files Functions Variables Typedefs Defines