sl@0: /* sl@0: * LIBOIL - Library of Optimized Inner Loops sl@0: * Copyright (c) 2006 David A. Schleef sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, sl@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES sl@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR sl@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING sl@0: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE sl@0: * POSSIBILITY OF SUCH DAMAGE. sl@0: */ sl@0: sl@0: /* sl@0: A C-program for MT19937, with initialization improved 2002/1/26. sl@0: Coded by Takuji Nishimura and Makoto Matsumoto. sl@0: sl@0: Before using, initialize the state by using init_genrand(seed) sl@0: or init_by_array(init_key, key_length). sl@0: sl@0: Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, sl@0: All rights reserved. sl@0: sl@0: Redistribution and use in source and binary forms, with or without sl@0: modification, are permitted provided that the following conditions sl@0: are met: sl@0: sl@0: 1. Redistributions of source code must retain the above copyright sl@0: notice, this list of conditions and the following disclaimer. sl@0: sl@0: 2. Redistributions in binary form must reproduce the above copyright sl@0: notice, this list of conditions and the following disclaimer in the sl@0: documentation and/or other materials provided with the distribution. sl@0: sl@0: 3. The names of its contributors may not be used to endorse or promote sl@0: products derived from this software without specific prior written sl@0: permission. sl@0: sl@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS sl@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT sl@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR sl@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR sl@0: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, sl@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, sl@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR sl@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF sl@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING sl@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS sl@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: sl@0: */ sl@0: sl@0: /* sl@0: Notes about the liboil version: sl@0: sl@0: This program is an adaptation of the Mersenne Twister example sl@0: program downloaded from the web site listed below. The kernel sl@0: of the generator is implemented in liboil, and the function sl@0: genrand_int32() has been replaced with a library call. Note sl@0: that the liboil function calculates an entire output array at sl@0: once instead of individually like the original. This makes it sl@0: easier to use memcpy to copy out many outputs at once. sl@0: sl@0: Notes from the original authors: sl@0: sl@0: Any feedback is very welcome. sl@0: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html sl@0: email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) sl@0: */ sl@0: sl@0: #ifdef HAVE_CONFIG_H sl@0: #include sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #define LOG_FILE "c:\\logs\\examples_oil-mt19937_log.txt" sl@0: #include "std_log_result.h" sl@0: #define LOG_FILENAME_LINE __FILE__, __LINE__ sl@0: sl@0: void create_xml(int result) sl@0: { sl@0: if(result) sl@0: assert_failed = 1; sl@0: sl@0: testResultXml("examples_oil-mt19937"); sl@0: close_log_file(); sl@0: } sl@0: sl@0: /* Period parameters */ sl@0: #define N 624 sl@0: #define M 397 sl@0: #define MATRIX_A 0x9908b0dfUL /* constant vector a */ sl@0: #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ sl@0: #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ sl@0: sl@0: static uint32_t mt[N]; /* the array for the state vector */ sl@0: static uint32_t mt_outputs[N]; /* the array for the outputs */ sl@0: static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ sl@0: sl@0: sl@0: sl@0: /* initializes mt[N] with a seed */ sl@0: void init_genrand(unsigned long s) sl@0: { sl@0: mt[0]= s & 0xffffffffUL; sl@0: for (mti=1; mti> 30)) + mti); sl@0: /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ sl@0: /* In the previous versions, MSBs of the seed affect */ sl@0: /* only MSBs of the array mt[]. */ sl@0: /* 2002/01/09 modified by Makoto Matsumoto */ sl@0: mt[mti] &= 0xffffffffUL; sl@0: /* for >32 bit machines */ sl@0: } sl@0: } sl@0: sl@0: /* initialize by an array with array-length */ sl@0: /* init_key is the array for initializing keys */ sl@0: /* key_length is its length */ sl@0: /* slight change for C++, 2004/2/26 */ sl@0: void init_by_array(unsigned long init_key[], int key_length) sl@0: { sl@0: int i, j, k; sl@0: init_genrand(19650218UL); sl@0: i=1; j=0; sl@0: k = (N>key_length ? N : key_length); sl@0: for (; k; k--) { sl@0: mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) sl@0: + init_key[j] + j; /* non linear */ sl@0: mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ sl@0: i++; j++; sl@0: if (i>=N) { mt[0] = mt[N-1]; i=1; } sl@0: if (j>=key_length) j=0; sl@0: } sl@0: for (k=N-1; k; k--) { sl@0: mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) sl@0: - i; /* non linear */ sl@0: mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ sl@0: i++; sl@0: if (i>=N) { mt[0] = mt[N-1]; i=1; } sl@0: } sl@0: sl@0: mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ sl@0: } sl@0: sl@0: /* generates a random number on [0,0xffffffff]-interval */ sl@0: unsigned long genrand_int32(void) sl@0: { sl@0: if (mti >= N) { /* generate N words at one time */ sl@0: oil_mt19937 (mt_outputs, mt); sl@0: mti = 0; sl@0: } sl@0: sl@0: return mt_outputs[mti++]; sl@0: } sl@0: sl@0: /* generates a random number on [0,0x7fffffff]-interval */ sl@0: long genrand_int31(void) sl@0: { sl@0: return (long)(genrand_int32()>>1); sl@0: } sl@0: sl@0: /* generates a random number on [0,1]-real-interval */ sl@0: double genrand_real1(void) sl@0: { sl@0: return genrand_int32()*(1.0/4294967295.0); sl@0: /* divided by 2^32-1 */ sl@0: } sl@0: sl@0: /* generates a random number on [0,1)-real-interval */ sl@0: double genrand_real2(void) sl@0: { sl@0: return genrand_int32()*(1.0/4294967296.0); sl@0: /* divided by 2^32 */ sl@0: } sl@0: sl@0: /* generates a random number on (0,1)-real-interval */ sl@0: double genrand_real3(void) sl@0: { sl@0: return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0); sl@0: /* divided by 2^32 */ sl@0: } sl@0: sl@0: /* generates a random number on [0,1) with 53-bit resolution*/ sl@0: double genrand_res53(void) sl@0: { sl@0: unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; sl@0: return(a*67108864.0+b)*(1.0/9007199254740992.0); sl@0: } sl@0: /* These real versions are due to Isaku Wada, 2002/01/09 added */ sl@0: sl@0: int main(void) sl@0: { sl@0: int i; sl@0: unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; sl@0: sl@0: oil_init(); sl@0: sl@0: init_by_array(init, length); sl@0: std_log(LOG_FILENAME_LINE,"1000 outputs of genrand_int32()\n"); sl@0: for (i=0; i<1000; i++) { sl@0: std_log(LOG_FILENAME_LINE,"%10lu ", genrand_int32()); sl@0: if (i%5==4) std_log(LOG_FILENAME_LINE,"\n"); sl@0: } sl@0: std_log(LOG_FILENAME_LINE,"\n1000 outputs of genrand_real2()\n"); sl@0: for (i=0; i<1000; i++) { sl@0: std_log(LOG_FILENAME_LINE,"%10.8f ", genrand_real2()); sl@0: if (i%5==4) std_log(LOG_FILENAME_LINE,"\n"); sl@0: } sl@0: if(assert_failed) sl@0: std_log(LOG_FILENAME_LINE,"Test Fail"); sl@0: else sl@0: std_log(LOG_FILENAME_LINE,"Test Successful"); sl@0: create_xml(0); sl@0: return 0; sl@0: return 0; sl@0: }