ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/include/common/random.h
Revision: 1.5
Committed: Sat Sep 22 14:27:26 2007 UTC (19 years ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +1 -1 lines
State: FILE REMOVED
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

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