| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/math_funcs.h" |
| 34 |
|
| 35 |
#include "thirdparty/misc/pcg.h" |
| 36 |
|
| 37 |
#if defined(__GNUC__) |
| 38 |
#define CLZ32(x) __builtin_clz(x) |
| 39 |
#elif defined(_MSC_VER) |
| 40 |
#include <intrin.h> |
| 41 |
static int __bsr_clz32(uint32_t x) { |
| 42 |
unsigned long index; |
| 43 |
_BitScanReverse(&index, x); |
| 44 |
return 31 - index; |
| 45 |
} |
| 46 |
#define CLZ32(x) __bsr_clz32(x) |
| 47 |
#endif |
| 48 |
|
| 49 |
template <typename T> |
| 50 |
class Vector; |
| 51 |
|
| 52 |
class RandomPCG { |
| 53 |
pcg32_random_t pcg; |
| 54 |
uint64_t current_seed = 0; |
| 55 |
uint64_t current_inc = 0; |
| 56 |
|
| 57 |
public: |
| 58 |
static const uint64_t DEFAULT_SEED = 12047754176567800795U; |
| 59 |
static const uint64_t DEFAULT_INC = PCG_DEFAULT_INC_64; |
| 60 |
|
| 61 |
RandomPCG(uint64_t p_seed = DEFAULT_SEED, uint64_t p_inc = DEFAULT_INC); |
| 62 |
|
| 63 |
_FORCE_INLINE_ void seed(uint64_t p_seed) { |
| 64 |
current_seed = p_seed; |
| 65 |
pcg32_srandom_r(&pcg, current_seed, current_inc); |
| 66 |
} |
| 67 |
_FORCE_INLINE_ uint64_t get_seed() { return current_seed; } |
| 68 |
|
| 69 |
_FORCE_INLINE_ void set_state(uint64_t p_state) { pcg.state = p_state; } |
| 70 |
_FORCE_INLINE_ uint64_t get_state() const { return pcg.state; } |
| 71 |
|
| 72 |
void randomize(); |
| 73 |
_FORCE_INLINE_ uint32_t rand() { |
| 74 |
return pcg32_random_r(&pcg); |
| 75 |
} |
| 76 |
_FORCE_INLINE_ uint32_t rand(uint32_t bounds) { |
| 77 |
return pcg32_boundedrand_r(&pcg, bounds); |
| 78 |
} |
| 79 |
|
| 80 |
int64_t rand_weighted(const Vector<float> &p_weights); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
_FORCE_INLINE_ double randd() { |
| 95 |
#if defined(CLZ32) |
| 96 |
uint32_t proto_exp_offset = rand(); |
| 97 |
if (unlikely(proto_exp_offset == 0)) { |
| 98 |
return 0; |
| 99 |
} |
| 100 |
uint64_t significand = (((uint64_t)rand()) << 32) | rand() | 0x8000000000000001U; |
| 101 |
return std::ldexp((double)significand, -64 - CLZ32(proto_exp_offset)); |
| 102 |
#else |
| 103 |
#pragma message("RandomPCG::randd - intrinsic clz is not available, falling back to bit truncation") |
| 104 |
return (double)(((((uint64_t)rand()) << 32) | rand()) & 0x1FFFFFFFFFFFFFU) / (double)0x1FFFFFFFFFFFFFU; |
| 105 |
#endif |
| 106 |
} |
| 107 |
_FORCE_INLINE_ float randf() { |
| 108 |
#if defined(CLZ32) |
| 109 |
uint32_t proto_exp_offset = rand(); |
| 110 |
if (unlikely(proto_exp_offset == 0)) { |
| 111 |
return 0; |
| 112 |
} |
| 113 |
return std::ldexp((float)(rand() | 0x80000001), -32 - CLZ32(proto_exp_offset)); |
| 114 |
#else |
| 115 |
#pragma message("RandomPCG::randf - intrinsic clz is not available, falling back to bit truncation") |
| 116 |
return (float)(rand() & 0xFFFFFF) / (float)0xFFFFFF; |
| 117 |
#endif |
| 118 |
} |
| 119 |
|
| 120 |
_FORCE_INLINE_ double randfn(double p_mean, double p_deviation) { |
| 121 |
double temp = randd(); |
| 122 |
if (temp < CMP_EPSILON) { |
| 123 |
temp += CMP_EPSILON; |
| 124 |
} |
| 125 |
return p_mean + p_deviation * (std::cos(Math::TAU * randd()) * std::sqrt(-2.0 * std::log(temp))); |
| 126 |
} |
| 127 |
_FORCE_INLINE_ float randfn(float p_mean, float p_deviation) { |
| 128 |
float temp = randf(); |
| 129 |
if (temp < CMP_EPSILON) { |
| 130 |
temp += CMP_EPSILON; |
| 131 |
} |
| 132 |
return p_mean + p_deviation * (std::cos((float)Math::TAU * randf()) * std::sqrt(-2.0 * std::log(temp))); |
| 133 |
} |
| 134 |
|
| 135 |
double random(double p_from, double p_to); |
| 136 |
float random(float p_from, float p_to); |
| 137 |
int random(int p_from, int p_to); |
| 138 |
}; |