ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/include/common/random.h
Revision: 1.1
Committed: Sat Jul 21 15:05:40 2007 UTC (19 years, 2 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Log Message:
added missing files

File Contents

# User Rev Content
1 pippijn 1.1 /**
2     * @file random.h
3     *
4     * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
5     * number generator
6     *
7     * @author Mutsuo Saito (Hiroshima University)
8     * @author Makoto Matsumoto (Hiroshima University)
9     *
10     * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
11     * University. All rights reserved.
12     *
13     * The new BSD License is applied to this software.
14     * see LICENSE.txt
15     *
16     * @note We assume that your system has inttypes.h. If your system
17     * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
18     * and you have to define PRIu64 and PRIx64 in this file as follows:
19     * @verbatim
20     typedef unsigned int uint32_t
21     typedef unsigned long long uint64_t
22     #define PRIu64 "llu"
23     #define PRIx64 "llx"
24     @endverbatim
25     * uint32_t must be exactly 32-bit unsigned integer type (no more, no
26     * less), and uint64_t must be exactly 64-bit unsigned integer type.
27     * PRIu64 and PRIx64 are used for printf function to print 64-bit
28     * unsigned int and 64-bit unsigned int in hexadecimal format.
29     */
30    
31     #ifndef COMMON_RANDOM_H
32     #define COMMON_RANDOM_H
33    
34     #include <stdio.h>
35    
36     #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
37     #include <inttypes.h>
38     #elif defined(_MSC_VER) || defined(__BORLANDC__)
39     typedef unsigned int uint32_t;
40     typedef unsigned __int64 uint64_t;
41     #define inline __inline
42     #else
43     #include <inttypes.h>
44     #if defined(__GNUC__)
45     #define inline __inline__
46     #endif
47     #endif
48    
49     #ifndef PRIu64
50     #if defined(_MSC_VER) || defined(__BORLANDC__)
51     #define PRIu64 "I64u"
52     #define PRIx64 "I64x"
53     #else
54     #define PRIu64 "llu"
55     #define PRIx64 "llx"
56     #endif
57     #endif
58    
59     #if defined(__GNUC__)
60     #define ALWAYSINLINE __attribute__((always_inline))
61     #endif
62    
63     uint32_t gen_rand32(void);
64     uint64_t gen_rand64(void);
65     void fill_array32(uint32_t *array, int size);
66     void fill_array64(uint64_t *array, int size);
67     void init_gen_rand(uint32_t seed);
68     void init_by_array(uint32_t *init_key, int key_length);
69     const char *get_idstring(void);
70     int get_min_array_size32(void);
71     int get_min_array_size64(void);
72    
73     /* These real versions are due to Isaku Wada */
74     /** generates a random number on [0,1]-real-interval */
75     inline static double to_real1(uint32_t v)
76     {
77     return v * (1.0/4294967295.0);
78     /* divided by 2^32-1 */
79     }
80    
81     /** generates a random number on [0,1]-real-interval */
82     inline static double genrand_real1(void)
83     {
84     return to_real1(gen_rand32());
85     }
86    
87     /** generates a random number on [0,1)-real-interval */
88     inline static double to_real2(uint32_t v)
89     {
90     return v * (1.0/4294967296.0);
91     /* divided by 2^32 */
92     }
93    
94     /** generates a random number on [0,1)-real-interval */
95     inline static double genrand_real2(void)
96     {
97     return to_real2(gen_rand32());
98     }
99    
100     /** generates a random number on (0,1)-real-interval */
101     inline static double to_real3(uint32_t v)
102     {
103     return (((double)v) + 0.5)*(1.0/4294967296.0);
104     /* divided by 2^32 */
105     }
106    
107     /** generates a random number on (0,1)-real-interval */
108     inline static double genrand_real3(void)
109     {
110     return to_real3(gen_rand32());
111     }
112     /** These real versions are due to Isaku Wada */
113    
114     /** generates a random number on [0,1) with 53-bit resolution*/
115     inline static double to_res53(uint64_t v)
116     {
117     return v * (1.0/18446744073709551616.0L);
118     }
119    
120     /** generates a random number on [0,1) with 53-bit resolution*/
121     inline static double genrand_res53(void)
122     {
123     return to_res53(gen_rand64());
124     }
125    
126     /*------------------------------------------------------
127     128-bit SIMD data type for Altivec, SSE2 or standard C
128     ------------------------------------------------------*/
129     #if defined(HAVE_ALTIVEC)
130     #if !defined(__APPLE__)
131     #include <altivec.h>
132     #endif
133     /** 128-bit data structure */
134     union W128_T {
135     vector unsigned int s;
136     uint32_t u[4];
137     };
138     /** 128-bit data type */
139     typedef union W128_T w128_t;
140    
141     #elif defined(HAVE_SSE2)
142     #include <emmintrin.h>
143    
144     /** 128-bit data structure */
145     union W128_T {
146     __m128i si;
147     uint32_t u[4];
148     };
149     /** 128-bit data type */
150     typedef union W128_T w128_t;
151    
152     #else
153    
154     /** 128-bit data structure */
155     struct W128_T {
156     uint32_t u[4];
157     };
158     /** 128-bit data type */
159     typedef struct W128_T w128_t;
160    
161     #endif
162    
163     #endif