| 1 |
pippijn |
1.4 |
/** |
| 2 |
|
|
* random.h: SIMD oriented Fast Mersenne Twister (SFMT) pseudorandom |
| 3 |
|
|
* |
| 4 |
pippijn |
1.2 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
| 5 |
pippijn |
1.4 |
* 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 |
pippijn |
1.2 |
* Copyright © 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima |
| 10 |
|
|
* University. All rights reserved. |
| 11 |
pippijn |
1.4 |
* The new BSD License is applied to this software. |
| 12 |
pippijn |
1.1 |
* |
| 13 |
|
|
* @author Mutsuo Saito (Hiroshima University) |
| 14 |
|
|
* @author Makoto Matsumoto (Hiroshima University) |
| 15 |
|
|
* |
| 16 |
pippijn |
1.5 |
* $Id: random.h,v 1.4 2007-09-16 18:54:42 pippijn Exp $ |
| 17 |
pippijn |
1.1 |
*/ |
| 18 |
|
|
|
| 19 |
|
|
#ifndef COMMON_RANDOM_H |
| 20 |
|
|
#define COMMON_RANDOM_H |
| 21 |
|
|
|
| 22 |
|
|
#include <stdio.h> |
| 23 |
|
|
|
| 24 |
pippijn |
1.3 |
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) |
| 25 |
pippijn |
1.1 |
#include <inttypes.h> |
| 26 |
pippijn |
1.3 |
#elif defined (_MSC_VER) || defined (__BORLANDC__) |
| 27 |
pippijn |
1.1 |
typedef unsigned int uint32_t; |
| 28 |
|
|
typedef unsigned __int64 uint64_t; |
| 29 |
|
|
#define inline __inline |
| 30 |
|
|
#else |
| 31 |
|
|
#include <inttypes.h> |
| 32 |
pippijn |
1.3 |
#if defined (__GNUC__) |
| 33 |
pippijn |
1.1 |
#define inline __inline__ |
| 34 |
|
|
#endif |
| 35 |
|
|
#endif |
| 36 |
|
|
|
| 37 |
|
|
#ifndef PRIu64 |
| 38 |
pippijn |
1.3 |
#if defined (_MSC_VER) || defined (__BORLANDC__) |
| 39 |
pippijn |
1.1 |
#define PRIu64 "I64u" |
| 40 |
|
|
#define PRIx64 "I64x" |
| 41 |
|
|
#else |
| 42 |
pippijn |
1.2 |
#define PRIu64 "lu" |
| 43 |
|
|
#define PRIx64 "lx" |
| 44 |
pippijn |
1.1 |
#endif |
| 45 |
|
|
#endif |
| 46 |
|
|
|
| 47 |
pippijn |
1.3 |
#if defined (__GNUC__) |
| 48 |
|
|
#define ALWAYSINLINE __attribute__ ((always_inline)) |
| 49 |
pippijn |
1.1 |
#endif |
| 50 |
|
|
|
| 51 |
pippijn |
1.3 |
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 |
pippijn |
1.1 |
|
| 61 |
|
|
/* These real versions are due to Isaku Wada */ |
| 62 |
|
|
/** generates a random number on [0,1]-real-interval */ |
| 63 |
pippijn |
1.3 |
inline double |
| 64 |
|
|
to_real1 (uint32_t v) |
| 65 |
pippijn |
1.1 |
{ |
| 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 |
pippijn |
1.3 |
inline double |
| 72 |
|
|
genrand_real1 (void) |
| 73 |
pippijn |
1.1 |
{ |
| 74 |
pippijn |
1.3 |
return to_real1 (gen_rand32 ()); |
| 75 |
pippijn |
1.1 |
} |
| 76 |
|
|
|
| 77 |
|
|
/** generates a random number on [0,1)-real-interval */ |
| 78 |
pippijn |
1.3 |
inline double |
| 79 |
|
|
to_real2 (uint32_t v) |
| 80 |
pippijn |
1.1 |
{ |
| 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 |
pippijn |
1.3 |
inline double |
| 87 |
|
|
genrand_real2 (void) |
| 88 |
pippijn |
1.1 |
{ |
| 89 |
pippijn |
1.3 |
return to_real2 (gen_rand32 ()); |
| 90 |
pippijn |
1.1 |
} |
| 91 |
|
|
|
| 92 |
|
|
/** generates a random number on (0,1)-real-interval */ |
| 93 |
pippijn |
1.3 |
inline double |
| 94 |
|
|
to_real3 (uint32_t v) |
| 95 |
pippijn |
1.1 |
{ |
| 96 |
pippijn |
1.3 |
return ( ((double)v) + 0.5)* (1.0/4294967296.0); |
| 97 |
pippijn |
1.1 |
/* divided by 2^32 */ |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
/** generates a random number on (0,1)-real-interval */ |
| 101 |
pippijn |
1.3 |
inline double |
| 102 |
|
|
genrand_real3 (void) |
| 103 |
pippijn |
1.1 |
{ |
| 104 |
pippijn |
1.3 |
return to_real3 (gen_rand32 ()); |
| 105 |
pippijn |
1.1 |
} |
| 106 |
|
|
/** These real versions are due to Isaku Wada */ |
| 107 |
|
|
|
| 108 |
|
|
/** generates a random number on [0,1) with 53-bit resolution*/ |
| 109 |
pippijn |
1.3 |
inline double |
| 110 |
|
|
to_res53 (uint64_t v) |
| 111 |
pippijn |
1.1 |
{ |
| 112 |
|
|
return v * (1.0/18446744073709551616.0L); |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
/** generates a random number on [0,1) with 53-bit resolution*/ |
| 116 |
pippijn |
1.3 |
inline double |
| 117 |
|
|
genrand_res53 (void) |
| 118 |
pippijn |
1.1 |
{ |
| 119 |
pippijn |
1.3 |
return to_res53 (gen_rand64 ()); |
| 120 |
pippijn |
1.1 |
} |
| 121 |
|
|
|
| 122 |
|
|
/*------------------------------------------------------ |
| 123 |
|
|
128-bit SIMD data type for Altivec, SSE2 or standard C |
| 124 |
|
|
------------------------------------------------------*/ |
| 125 |
pippijn |
1.3 |
#if defined (HAVE_ALTIVEC) |
| 126 |
|
|
#if !defined (__APPLE__) |
| 127 |
pippijn |
1.1 |
#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 |
pippijn |
1.3 |
#elif defined (HAVE_SSE2) |
| 138 |
pippijn |
1.1 |
#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 |