diff options
author | Marcus Müller <mueller@kit.edu> | 2018-02-16 18:05:52 +0100 |
---|---|---|
committer | Marcus Müller <marcus.mueller@ettus.com> | 2018-03-30 16:50:32 +0200 |
commit | c31f52882a7eefda683cec42e65a95fd2127f780 (patch) | |
tree | 4fb7501e2a992de498b77b904ffeea12544a98ea /gr-analog/lib | |
parent | c4af98a8c9a1a1189d650370ddf50f05a0112c94 (diff) |
Converted fastnoise_source from rand() to xoroshiro128+
This solves the issue with libc rand() relying on global
(not thread-local!) state and thus not being suited for multithreaded
applications. This fixes #1542.
Diffstat (limited to 'gr-analog/lib')
-rw-r--r-- | gr-analog/lib/fastnoise_source_X_impl.cc.t | 30 | ||||
-rw-r--r-- | gr-analog/lib/fastnoise_source_X_impl.h.t | 1 |
2 files changed, 12 insertions, 19 deletions
diff --git a/gr-analog/lib/fastnoise_source_X_impl.cc.t b/gr-analog/lib/fastnoise_source_X_impl.cc.t index 940918b11b..9283f11733 100644 --- a/gr-analog/lib/fastnoise_source_X_impl.cc.t +++ b/gr-analog/lib/fastnoise_source_X_impl.cc.t @@ -27,6 +27,7 @@ #endif #include "@IMPL_NAME@.h" +#include <gnuradio/xoroshiro128p.h> #include <gnuradio/io_signature.h> #include <stdexcept> @@ -46,13 +47,13 @@ namespace gr { io_signature::make(1, 1, sizeof(@TYPE@))), d_type(type), #if @IS_COMPLEX@ // complex? - d_ampl(ampl/sqrtf(2.0f)), + d_ampl(ampl/sqrtf(2.0f)) #else - d_ampl(ampl), + d_ampl(ampl) #endif - d_rng(seed) { d_samples.resize(samples); + xoroshiro128p_seed(d_state, (uint64_t) seed); generate(); } @@ -144,30 +145,21 @@ namespace gr { @TYPE@ @IMPL_NAME@::sample() { -#ifdef HAVE_RAND48 - size_t idx = lrand48() % d_samples.size(); -#else - size_t idx = rand() % d_samples.size(); -#endif + size_t idx = xoroshiro128p_next(d_state) % d_samples.size(); return d_samples[idx]; } -#ifndef FASTNOISE_RANDOM_SIGN -#ifndef HAVE_RAND48 -#define FASTNOISE_RANDOM_SIGN ((rand()%2==0)?1:-1) -#else -#define FASTNOISE_RANDOM_SIGN ((lrand48()%2==0)?1:-1) -#endif -#endif - @TYPE@ @IMPL_NAME@::sample_unbiased() { + uint64_t random_int = xoroshiro128p_next(d_state); #if @IS_COMPLEX@ gr_complex s(sample()); - return gr_complex(FASTNOISE_RANDOM_SIGN * s.real(), - FASTNOISE_RANDOM_SIGN * s.imag()); + float re = (random_int & (UINT64_C(1)<<23)) ? (- s.real()) : (s.real()); + float im = (random_int & (UINT64_C(1)<<42)) ? (- s.real()) : (s.real()); + return gr_complex(re, im); #else - return FASTNOISE_RANDOM_SIGN * sample(); + float s = sample(); + return (random_int & (1<<23)) ? (-s) : s; #endif } diff --git a/gr-analog/lib/fastnoise_source_X_impl.h.t b/gr-analog/lib/fastnoise_source_X_impl.h.t index 8ad1e4f8fe..5bea010e62 100644 --- a/gr-analog/lib/fastnoise_source_X_impl.h.t +++ b/gr-analog/lib/fastnoise_source_X_impl.h.t @@ -38,6 +38,7 @@ namespace gr { float d_ampl; gr::random d_rng; std::vector<@TYPE@> d_samples; + uint64_t d_state[2]; public: @IMPL_NAME@(noise_type_t type, float ampl, long seed, long samples); |