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