Home | History | Annotate | Download | only in test
      1 /*
      2  * This file derives from SFMT 1.3.3
      3  * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was
      4  * released under the terms of the following license:
      5  *
      6  *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
      7  *   University. All rights reserved.
      8  *
      9  *   Redistribution and use in source and binary forms, with or without
     10  *   modification, are permitted provided that the following conditions are
     11  *   met:
     12  *
     13  *       * Redistributions of source code must retain the above copyright
     14  *         notice, this list of conditions and the following disclaimer.
     15  *       * Redistributions in binary form must reproduce the above
     16  *         copyright notice, this list of conditions and the following
     17  *         disclaimer in the documentation and/or other materials provided
     18  *         with the distribution.
     19  *       * Neither the name of the Hiroshima University nor the names of
     20  *         its contributors may be used to endorse or promote products
     21  *         derived from this software without specific prior written
     22  *         permission.
     23  *
     24  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     25  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     26  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     27  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     28  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     29  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     30  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     34  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 /**
     37  * @file SFMT.h
     38  *
     39  * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
     40  * number generator
     41  *
     42  * @author Mutsuo Saito (Hiroshima University)
     43  * @author Makoto Matsumoto (Hiroshima University)
     44  *
     45  * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
     46  * University. All rights reserved.
     47  *
     48  * The new BSD License is applied to this software.
     49  * see LICENSE.txt
     50  *
     51  * @note We assume that your system has inttypes.h.  If your system
     52  * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
     53  * and you have to define PRIu64 and PRIx64 in this file as follows:
     54  * @verbatim
     55  typedef unsigned int uint32_t
     56  typedef unsigned long long uint64_t
     57  #define PRIu64 "llu"
     58  #define PRIx64 "llx"
     59 @endverbatim
     60  * uint32_t must be exactly 32-bit unsigned integer type (no more, no
     61  * less), and uint64_t must be exactly 64-bit unsigned integer type.
     62  * PRIu64 and PRIx64 are used for printf function to print 64-bit
     63  * unsigned int and 64-bit unsigned int in hexadecimal format.
     64  */
     65 
     66 #ifndef SFMT_H
     67 #define SFMT_H
     68 
     69 typedef struct sfmt_s sfmt_t;
     70 
     71 uint32_t gen_rand32(sfmt_t *ctx);
     72 uint32_t gen_rand32_range(sfmt_t *ctx, uint32_t limit);
     73 uint64_t gen_rand64(sfmt_t *ctx);
     74 uint64_t gen_rand64_range(sfmt_t *ctx, uint64_t limit);
     75 void fill_array32(sfmt_t *ctx, uint32_t *array, int size);
     76 void fill_array64(sfmt_t *ctx, uint64_t *array, int size);
     77 sfmt_t *init_gen_rand(uint32_t seed);
     78 sfmt_t *init_by_array(uint32_t *init_key, int key_length);
     79 void fini_gen_rand(sfmt_t *ctx);
     80 const char *get_idstring(void);
     81 int get_min_array_size32(void);
     82 int get_min_array_size64(void);
     83 
     84 /* These real versions are due to Isaku Wada */
     85 /** generates a random number on [0,1]-real-interval */
     86 static inline double to_real1(uint32_t v) {
     87     return v * (1.0/4294967295.0);
     88     /* divided by 2^32-1 */
     89 }
     90 
     91 /** generates a random number on [0,1]-real-interval */
     92 static inline double genrand_real1(sfmt_t *ctx) {
     93     return to_real1(gen_rand32(ctx));
     94 }
     95 
     96 /** generates a random number on [0,1)-real-interval */
     97 static inline double to_real2(uint32_t v) {
     98     return v * (1.0/4294967296.0);
     99     /* divided by 2^32 */
    100 }
    101 
    102 /** generates a random number on [0,1)-real-interval */
    103 static inline double genrand_real2(sfmt_t *ctx) {
    104     return to_real2(gen_rand32(ctx));
    105 }
    106 
    107 /** generates a random number on (0,1)-real-interval */
    108 static inline double to_real3(uint32_t v) {
    109     return (((double)v) + 0.5)*(1.0/4294967296.0);
    110     /* divided by 2^32 */
    111 }
    112 
    113 /** generates a random number on (0,1)-real-interval */
    114 static inline double genrand_real3(sfmt_t *ctx) {
    115     return to_real3(gen_rand32(ctx));
    116 }
    117 /** These real versions are due to Isaku Wada */
    118 
    119 /** generates a random number on [0,1) with 53-bit resolution*/
    120 static inline double to_res53(uint64_t v) {
    121     return v * (1.0/18446744073709551616.0L);
    122 }
    123 
    124 /** generates a random number on [0,1) with 53-bit resolution from two
    125  * 32 bit integers */
    126 static inline double to_res53_mix(uint32_t x, uint32_t y) {
    127     return to_res53(x | ((uint64_t)y << 32));
    128 }
    129 
    130 /** generates a random number on [0,1) with 53-bit resolution
    131  */
    132 static inline double genrand_res53(sfmt_t *ctx) {
    133     return to_res53(gen_rand64(ctx));
    134 }
    135 
    136 /** generates a random number on [0,1) with 53-bit resolution
    137     using 32bit integer.
    138  */
    139 static inline double genrand_res53_mix(sfmt_t *ctx) {
    140     uint32_t x, y;
    141 
    142     x = gen_rand32(ctx);
    143     y = gen_rand32(ctx);
    144     return to_res53_mix(x, y);
    145 }
    146 #endif
    147