ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/include/common/random.h
Revision: 1.3
Committed: Thu Aug 30 19:56:20 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.2: +41 -33 lines
Log Message:
- put faultcodes into their own namespace
- removed old files
- limited header garbage in atheme.h
- macros to inline bools for connection_t::is_*
- put some connection_t functions into the connection_t class

File Contents

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