os/ossrv/genericopenlibs/liboil/tsrc/examples/oil-mt19937/src/oil-mt19937.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2  * LIBOIL - Library of Optimized Inner Loops
     3  * Copyright (c) 2006 David A. Schleef <ds@schleef.org>
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions
     8  * are met:
     9  * 1. Redistributions of source code must retain the above copyright
    10  *    notice, this list of conditions and the following disclaimer.
    11  * 2. Redistributions in binary form must reproduce the above copyright
    12  *    notice, this list of conditions and the following disclaimer in the
    13  *    documentation and/or other materials provided with the distribution.
    14  * 
    15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  * POSSIBILITY OF SUCH DAMAGE.
    26  */
    27 
    28 /* 
    29    A C-program for MT19937, with initialization improved 2002/1/26.
    30    Coded by Takuji Nishimura and Makoto Matsumoto.
    31 
    32    Before using, initialize the state by using init_genrand(seed)  
    33    or init_by_array(init_key, key_length).
    34 
    35    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
    36    All rights reserved.                          
    37 
    38    Redistribution and use in source and binary forms, with or without
    39    modification, are permitted provided that the following conditions
    40    are met:
    41 
    42      1. Redistributions of source code must retain the above copyright
    43         notice, this list of conditions and the following disclaimer.
    44 
    45      2. Redistributions in binary form must reproduce the above copyright
    46         notice, this list of conditions and the following disclaimer in the
    47         documentation and/or other materials provided with the distribution.
    48 
    49      3. The names of its contributors may not be used to endorse or promote 
    50         products derived from this software without specific prior written 
    51         permission.
    52 
    53    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    54    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    55    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    56    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    57    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    58    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    59    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    60    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    61    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    62    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    63    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    64 
    65 */
    66 
    67 /*
    68    Notes about the liboil version:
    69    
    70      This program is an adaptation of the Mersenne Twister example
    71      program downloaded from the web site listed below.  The kernel
    72      of the generator is implemented in liboil, and the function
    73      genrand_int32() has been replaced with a library call.  Note
    74      that the liboil function calculates an entire output array at
    75      once instead of individually like the original.  This makes it
    76      easier to use memcpy to copy out many outputs at once.
    77 
    78    Notes from the original authors:
    79 
    80      Any feedback is very welcome.
    81      http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
    82      email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
    83 */
    84 
    85 #ifdef HAVE_CONFIG_H
    86 #include <config.h>
    87 #endif
    88 
    89 #include <stdio.h>
    90 
    91 #include <liboil/liboil.h>
    92 #include <liboil/globals.h>
    93 #define LOG_FILE "c:\\logs\\examples_oil-mt19937_log.txt"
    94 #include "std_log_result.h"
    95 #define LOG_FILENAME_LINE __FILE__, __LINE__
    96 
    97 void create_xml(int result)
    98 {
    99     if(result)
   100         assert_failed = 1;
   101     
   102     testResultXml("examples_oil-mt19937");
   103     close_log_file();
   104 }
   105 
   106 /* Period parameters */  
   107 #define N 624
   108 #define M 397
   109 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
   110 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
   111 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
   112 
   113 static uint32_t mt[N]; /* the array for the state vector  */
   114 static uint32_t mt_outputs[N]; /* the array for the outputs  */
   115 static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
   116 
   117 
   118 
   119 /* initializes mt[N] with a seed */
   120 void init_genrand(unsigned long s)
   121 {
   122     mt[0]= s & 0xffffffffUL;
   123     for (mti=1; mti<N; mti++) {
   124         mt[mti] = 
   125 	    (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
   126         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
   127         /* In the previous versions, MSBs of the seed affect   */
   128         /* only MSBs of the array mt[].                        */
   129         /* 2002/01/09 modified by Makoto Matsumoto             */
   130         mt[mti] &= 0xffffffffUL;
   131         /* for >32 bit machines */
   132     }
   133 }
   134 
   135 /* initialize by an array with array-length */
   136 /* init_key is the array for initializing keys */
   137 /* key_length is its length */
   138 /* slight change for C++, 2004/2/26 */
   139 void init_by_array(unsigned long init_key[], int key_length)
   140 {
   141     int i, j, k;
   142     init_genrand(19650218UL);
   143     i=1; j=0;
   144     k = (N>key_length ? N : key_length);
   145     for (; k; k--) {
   146         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
   147           + init_key[j] + j; /* non linear */
   148         mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
   149         i++; j++;
   150         if (i>=N) { mt[0] = mt[N-1]; i=1; }
   151         if (j>=key_length) j=0;
   152     }
   153     for (k=N-1; k; k--) {
   154         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
   155           - i; /* non linear */
   156         mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
   157         i++;
   158         if (i>=N) { mt[0] = mt[N-1]; i=1; }
   159     }
   160 
   161     mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
   162 }
   163 
   164 /* generates a random number on [0,0xffffffff]-interval */
   165 unsigned long genrand_int32(void)
   166 {
   167     if (mti >= N) { /* generate N words at one time */
   168       oil_mt19937 (mt_outputs, mt);
   169       mti = 0;
   170     }
   171   
   172     return mt_outputs[mti++];
   173 }
   174 
   175 /* generates a random number on [0,0x7fffffff]-interval */
   176 long genrand_int31(void)
   177 {
   178     return (long)(genrand_int32()>>1);
   179 }
   180 
   181 /* generates a random number on [0,1]-real-interval */
   182 double genrand_real1(void)
   183 {
   184     return genrand_int32()*(1.0/4294967295.0); 
   185     /* divided by 2^32-1 */ 
   186 }
   187 
   188 /* generates a random number on [0,1)-real-interval */
   189 double genrand_real2(void)
   190 {
   191     return genrand_int32()*(1.0/4294967296.0); 
   192     /* divided by 2^32 */
   193 }
   194 
   195 /* generates a random number on (0,1)-real-interval */
   196 double genrand_real3(void)
   197 {
   198     return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0); 
   199     /* divided by 2^32 */
   200 }
   201 
   202 /* generates a random number on [0,1) with 53-bit resolution*/
   203 double genrand_res53(void) 
   204 { 
   205     unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; 
   206     return(a*67108864.0+b)*(1.0/9007199254740992.0); 
   207 } 
   208 /* These real versions are due to Isaku Wada, 2002/01/09 added */
   209 
   210 int main(void)
   211 {
   212     int i;
   213     unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
   214 
   215     oil_init();
   216 
   217     init_by_array(init, length);
   218     std_log(LOG_FILENAME_LINE,"1000 outputs of genrand_int32()\n");
   219     for (i=0; i<1000; i++) {
   220     std_log(LOG_FILENAME_LINE,"%10lu ", genrand_int32());
   221       if (i%5==4) std_log(LOG_FILENAME_LINE,"\n");
   222     }
   223     std_log(LOG_FILENAME_LINE,"\n1000 outputs of genrand_real2()\n");
   224     for (i=0; i<1000; i++) {
   225     std_log(LOG_FILENAME_LINE,"%10.8f ", genrand_real2());
   226       if (i%5==4) std_log(LOG_FILENAME_LINE,"\n");
   227     }
   228         if(assert_failed)
   229           std_log(LOG_FILENAME_LINE,"Test Fail");
   230     else
   231           std_log(LOG_FILENAME_LINE,"Test Successful");
   232     create_xml(0);
   233     return 0;
   234     return 0;
   235 }