os/ossrv/genericopenlibs/liboil/tsrc/examples/oil-mt19937/src/oil-mt19937.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/liboil/tsrc/examples/oil-mt19937/src/oil-mt19937.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,235 @@
     1.4 +/*
     1.5 + * LIBOIL - Library of Optimized Inner Loops
     1.6 + * Copyright (c) 2006 David A. Schleef <ds@schleef.org>
     1.7 + * All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions
    1.11 + * are met:
    1.12 + * 1. Redistributions of source code must retain the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer.
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer in the
    1.16 + *    documentation and/or other materials provided with the distribution.
    1.17 + * 
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    1.20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.21 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    1.22 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.25 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.26 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    1.27 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.28 + * POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +
    1.31 +/* 
    1.32 +   A C-program for MT19937, with initialization improved 2002/1/26.
    1.33 +   Coded by Takuji Nishimura and Makoto Matsumoto.
    1.34 +
    1.35 +   Before using, initialize the state by using init_genrand(seed)  
    1.36 +   or init_by_array(init_key, key_length).
    1.37 +
    1.38 +   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
    1.39 +   All rights reserved.                          
    1.40 +
    1.41 +   Redistribution and use in source and binary forms, with or without
    1.42 +   modification, are permitted provided that the following conditions
    1.43 +   are met:
    1.44 +
    1.45 +     1. Redistributions of source code must retain the above copyright
    1.46 +        notice, this list of conditions and the following disclaimer.
    1.47 +
    1.48 +     2. Redistributions in binary form must reproduce the above copyright
    1.49 +        notice, this list of conditions and the following disclaimer in the
    1.50 +        documentation and/or other materials provided with the distribution.
    1.51 +
    1.52 +     3. The names of its contributors may not be used to endorse or promote 
    1.53 +        products derived from this software without specific prior written 
    1.54 +        permission.
    1.55 +
    1.56 +   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.57 +   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.58 +   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.59 +   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.60 +   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.61 +   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.62 +   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.63 +   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.64 +   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.65 +   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.66 +   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.67 +
    1.68 +*/
    1.69 +
    1.70 +/*
    1.71 +   Notes about the liboil version:
    1.72 +   
    1.73 +     This program is an adaptation of the Mersenne Twister example
    1.74 +     program downloaded from the web site listed below.  The kernel
    1.75 +     of the generator is implemented in liboil, and the function
    1.76 +     genrand_int32() has been replaced with a library call.  Note
    1.77 +     that the liboil function calculates an entire output array at
    1.78 +     once instead of individually like the original.  This makes it
    1.79 +     easier to use memcpy to copy out many outputs at once.
    1.80 +
    1.81 +   Notes from the original authors:
    1.82 +
    1.83 +     Any feedback is very welcome.
    1.84 +     http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
    1.85 +     email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
    1.86 +*/
    1.87 +
    1.88 +#ifdef HAVE_CONFIG_H
    1.89 +#include <config.h>
    1.90 +#endif
    1.91 +
    1.92 +#include <stdio.h>
    1.93 +
    1.94 +#include <liboil/liboil.h>
    1.95 +#include <liboil/globals.h>
    1.96 +#define LOG_FILE "c:\\logs\\examples_oil-mt19937_log.txt"
    1.97 +#include "std_log_result.h"
    1.98 +#define LOG_FILENAME_LINE __FILE__, __LINE__
    1.99 +
   1.100 +void create_xml(int result)
   1.101 +{
   1.102 +    if(result)
   1.103 +        assert_failed = 1;
   1.104 +    
   1.105 +    testResultXml("examples_oil-mt19937");
   1.106 +    close_log_file();
   1.107 +}
   1.108 +
   1.109 +/* Period parameters */  
   1.110 +#define N 624
   1.111 +#define M 397
   1.112 +#define MATRIX_A 0x9908b0dfUL   /* constant vector a */
   1.113 +#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
   1.114 +#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
   1.115 +
   1.116 +static uint32_t mt[N]; /* the array for the state vector  */
   1.117 +static uint32_t mt_outputs[N]; /* the array for the outputs  */
   1.118 +static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
   1.119 +
   1.120 +
   1.121 +
   1.122 +/* initializes mt[N] with a seed */
   1.123 +void init_genrand(unsigned long s)
   1.124 +{
   1.125 +    mt[0]= s & 0xffffffffUL;
   1.126 +    for (mti=1; mti<N; mti++) {
   1.127 +        mt[mti] = 
   1.128 +	    (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
   1.129 +        /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
   1.130 +        /* In the previous versions, MSBs of the seed affect   */
   1.131 +        /* only MSBs of the array mt[].                        */
   1.132 +        /* 2002/01/09 modified by Makoto Matsumoto             */
   1.133 +        mt[mti] &= 0xffffffffUL;
   1.134 +        /* for >32 bit machines */
   1.135 +    }
   1.136 +}
   1.137 +
   1.138 +/* initialize by an array with array-length */
   1.139 +/* init_key is the array for initializing keys */
   1.140 +/* key_length is its length */
   1.141 +/* slight change for C++, 2004/2/26 */
   1.142 +void init_by_array(unsigned long init_key[], int key_length)
   1.143 +{
   1.144 +    int i, j, k;
   1.145 +    init_genrand(19650218UL);
   1.146 +    i=1; j=0;
   1.147 +    k = (N>key_length ? N : key_length);
   1.148 +    for (; k; k--) {
   1.149 +        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
   1.150 +          + init_key[j] + j; /* non linear */
   1.151 +        mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
   1.152 +        i++; j++;
   1.153 +        if (i>=N) { mt[0] = mt[N-1]; i=1; }
   1.154 +        if (j>=key_length) j=0;
   1.155 +    }
   1.156 +    for (k=N-1; k; k--) {
   1.157 +        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
   1.158 +          - i; /* non linear */
   1.159 +        mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
   1.160 +        i++;
   1.161 +        if (i>=N) { mt[0] = mt[N-1]; i=1; }
   1.162 +    }
   1.163 +
   1.164 +    mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
   1.165 +}
   1.166 +
   1.167 +/* generates a random number on [0,0xffffffff]-interval */
   1.168 +unsigned long genrand_int32(void)
   1.169 +{
   1.170 +    if (mti >= N) { /* generate N words at one time */
   1.171 +      oil_mt19937 (mt_outputs, mt);
   1.172 +      mti = 0;
   1.173 +    }
   1.174 +  
   1.175 +    return mt_outputs[mti++];
   1.176 +}
   1.177 +
   1.178 +/* generates a random number on [0,0x7fffffff]-interval */
   1.179 +long genrand_int31(void)
   1.180 +{
   1.181 +    return (long)(genrand_int32()>>1);
   1.182 +}
   1.183 +
   1.184 +/* generates a random number on [0,1]-real-interval */
   1.185 +double genrand_real1(void)
   1.186 +{
   1.187 +    return genrand_int32()*(1.0/4294967295.0); 
   1.188 +    /* divided by 2^32-1 */ 
   1.189 +}
   1.190 +
   1.191 +/* generates a random number on [0,1)-real-interval */
   1.192 +double genrand_real2(void)
   1.193 +{
   1.194 +    return genrand_int32()*(1.0/4294967296.0); 
   1.195 +    /* divided by 2^32 */
   1.196 +}
   1.197 +
   1.198 +/* generates a random number on (0,1)-real-interval */
   1.199 +double genrand_real3(void)
   1.200 +{
   1.201 +    return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0); 
   1.202 +    /* divided by 2^32 */
   1.203 +}
   1.204 +
   1.205 +/* generates a random number on [0,1) with 53-bit resolution*/
   1.206 +double genrand_res53(void) 
   1.207 +{ 
   1.208 +    unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; 
   1.209 +    return(a*67108864.0+b)*(1.0/9007199254740992.0); 
   1.210 +} 
   1.211 +/* These real versions are due to Isaku Wada, 2002/01/09 added */
   1.212 +
   1.213 +int main(void)
   1.214 +{
   1.215 +    int i;
   1.216 +    unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
   1.217 +
   1.218 +    oil_init();
   1.219 +
   1.220 +    init_by_array(init, length);
   1.221 +    std_log(LOG_FILENAME_LINE,"1000 outputs of genrand_int32()\n");
   1.222 +    for (i=0; i<1000; i++) {
   1.223 +    std_log(LOG_FILENAME_LINE,"%10lu ", genrand_int32());
   1.224 +      if (i%5==4) std_log(LOG_FILENAME_LINE,"\n");
   1.225 +    }
   1.226 +    std_log(LOG_FILENAME_LINE,"\n1000 outputs of genrand_real2()\n");
   1.227 +    for (i=0; i<1000; i++) {
   1.228 +    std_log(LOG_FILENAME_LINE,"%10.8f ", genrand_real2());
   1.229 +      if (i%5==4) std_log(LOG_FILENAME_LINE,"\n");
   1.230 +    }
   1.231 +        if(assert_failed)
   1.232 +          std_log(LOG_FILENAME_LINE,"Test Fail");
   1.233 +    else
   1.234 +          std_log(LOG_FILENAME_LINE,"Test Successful");
   1.235 +    create_xml(0);
   1.236 +    return 0;
   1.237 +    return 0;
   1.238 +}