| 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.2 |
* 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.2 |
* $Id: flags.h,v 1.2 2007-07-21 01:29: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 |
|
|
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) |
| 21 |
|
|
#include <inttypes.h> |
| 22 |
|
|
#elif defined(_MSC_VER) || defined(__BORLANDC__) |
| 23 |
|
|
typedef unsigned int uint32_t; |
| 24 |
|
|
typedef unsigned __int64 uint64_t; |
| 25 |
|
|
#define inline __inline |
| 26 |
|
|
#else |
| 27 |
|
|
#include <inttypes.h> |
| 28 |
|
|
#if defined(__GNUC__) |
| 29 |
|
|
#define inline __inline__ |
| 30 |
|
|
#endif |
| 31 |
|
|
#endif |
| 32 |
|
|
|
| 33 |
|
|
#ifndef PRIu64 |
| 34 |
|
|
#if defined(_MSC_VER) || defined(__BORLANDC__) |
| 35 |
|
|
#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 |
|
|
#if defined(__GNUC__) |
| 44 |
|
|
#define ALWAYSINLINE __attribute__((always_inline)) |
| 45 |
|
|
#endif |
| 46 |
|
|
|
| 47 |
|
|
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 |
pippijn |
1.2 |
char const * const get_idstring(void); |
| 54 |
pippijn |
1.1 |
int get_min_array_size32(void); |
| 55 |
|
|
int get_min_array_size64(void); |
| 56 |
|
|
|
| 57 |
|
|
/* These real versions are due to Isaku Wada */ |
| 58 |
|
|
/** generates a random number on [0,1]-real-interval */ |
| 59 |
|
|
inline static double to_real1(uint32_t v) |
| 60 |
|
|
{ |
| 61 |
|
|
return v * (1.0/4294967295.0); |
| 62 |
|
|
/* divided by 2^32-1 */ |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
|
|
/** generates a random number on [0,1]-real-interval */ |
| 66 |
|
|
inline static double genrand_real1(void) |
| 67 |
|
|
{ |
| 68 |
|
|
return to_real1(gen_rand32()); |
| 69 |
|
|
} |
| 70 |
|
|
|
| 71 |
|
|
/** generates a random number on [0,1)-real-interval */ |
| 72 |
|
|
inline static double to_real2(uint32_t v) |
| 73 |
|
|
{ |
| 74 |
|
|
return v * (1.0/4294967296.0); |
| 75 |
|
|
/* divided by 2^32 */ |
| 76 |
|
|
} |
| 77 |
|
|
|
| 78 |
|
|
/** generates a random number on [0,1)-real-interval */ |
| 79 |
|
|
inline static double genrand_real2(void) |
| 80 |
|
|
{ |
| 81 |
|
|
return to_real2(gen_rand32()); |
| 82 |
|
|
} |
| 83 |
|
|
|
| 84 |
|
|
/** generates a random number on (0,1)-real-interval */ |
| 85 |
|
|
inline static double to_real3(uint32_t v) |
| 86 |
|
|
{ |
| 87 |
|
|
return (((double)v) + 0.5)*(1.0/4294967296.0); |
| 88 |
|
|
/* divided by 2^32 */ |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
/** generates a random number on (0,1)-real-interval */ |
| 92 |
|
|
inline static double genrand_real3(void) |
| 93 |
|
|
{ |
| 94 |
|
|
return to_real3(gen_rand32()); |
| 95 |
|
|
} |
| 96 |
|
|
/** These real versions are due to Isaku Wada */ |
| 97 |
|
|
|
| 98 |
|
|
/** generates a random number on [0,1) with 53-bit resolution*/ |
| 99 |
|
|
inline static double to_res53(uint64_t v) |
| 100 |
|
|
{ |
| 101 |
|
|
return v * (1.0/18446744073709551616.0L); |
| 102 |
|
|
} |
| 103 |
|
|
|
| 104 |
|
|
/** generates a random number on [0,1) with 53-bit resolution*/ |
| 105 |
|
|
inline static double genrand_res53(void) |
| 106 |
|
|
{ |
| 107 |
|
|
return to_res53(gen_rand64()); |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
/*------------------------------------------------------ |
| 111 |
|
|
128-bit SIMD data type for Altivec, SSE2 or standard C |
| 112 |
|
|
------------------------------------------------------*/ |
| 113 |
|
|
#if defined(HAVE_ALTIVEC) |
| 114 |
|
|
#if !defined(__APPLE__) |
| 115 |
|
|
#include <altivec.h> |
| 116 |
|
|
#endif |
| 117 |
|
|
/** 128-bit data structure */ |
| 118 |
|
|
union W128_T { |
| 119 |
|
|
vector unsigned int s; |
| 120 |
|
|
uint32_t u[4]; |
| 121 |
|
|
}; |
| 122 |
|
|
/** 128-bit data type */ |
| 123 |
|
|
typedef union W128_T w128_t; |
| 124 |
|
|
|
| 125 |
|
|
#elif defined(HAVE_SSE2) |
| 126 |
|
|
#include <emmintrin.h> |
| 127 |
|
|
|
| 128 |
|
|
/** 128-bit data structure */ |
| 129 |
|
|
union W128_T { |
| 130 |
|
|
__m128i si; |
| 131 |
|
|
uint32_t u[4]; |
| 132 |
|
|
}; |
| 133 |
|
|
/** 128-bit data type */ |
| 134 |
|
|
typedef union W128_T w128_t; |
| 135 |
|
|
|
| 136 |
|
|
#else |
| 137 |
|
|
|
| 138 |
|
|
/** 128-bit data structure */ |
| 139 |
|
|
struct W128_T { |
| 140 |
|
|
uint32_t u[4]; |
| 141 |
|
|
}; |
| 142 |
|
|
/** 128-bit data type */ |
| 143 |
|
|
typedef struct W128_T w128_t; |
| 144 |
|
|
|
| 145 |
|
|
#endif |
| 146 |
|
|
|
| 147 |
|
|
#endif |