13
0

Move PCG RNG implementation out of header, update API

This commit is contained in:
Robin Gareus 2021-08-15 15:54:42 +02:00
parent e4d1d82ea7
commit a3b28b4114
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
3 changed files with 85 additions and 53 deletions

View File

@ -19,76 +19,43 @@
#ifndef _pbd_pcg_rand_
#define _pbd_pcg_rand_
#include <assert.h>
#include <stdint.h>
#include <time.h>
namespace PBD {
namespace PBD
{
/**
* *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org
*
* To be used in cases where an efficient and realtime-safe random
* generator is needed.
*/
class PCGRand {
class PCGRand
{
public:
PCGRand ()
{
int foo = 0;
uint64_t initseq = (intptr_t)&foo;
_state = 0;
_inc = (initseq << 1) | 1;
rand_u32 ();
_state += time (NULL) ^ (intptr_t)this;
rand_u32 ();
}
PCGRand ();
/* unsigned float [0..1] */
float rand_uf ()
{
return rand_u32 () / 4294967295.f;
}
/* 32bit (0 .. 4294967295) */
uint32_t rand_u32 ();
/* signed float [-1..+1] */
float rand_sf ()
{
return (rand_u32 () / 2147483647.5f) - 1.f;
}
/* uniform integer min <= rand < max */
int rand (int max, int min = 0);
/* uniform integer min <= rand <= max */
int rand (int min, int max)
/* unsigned float [0..1] */
float rand_uf ()
{
int range;
if (min > max) {
range = 1 + min - max;
min = max;
} else {
range = 1 + max - min;
}
assert (range < 4294967296);
while (true) {
uint32_t value = rand_u32 ();
if (value < 4294967296 - 4294967296 % range) {
return min + value % range;
}
}
return rand_u32 () / 4294967295.f;
}
uint32_t rand_u32 ()
{
uint64_t oldstate = _state;
_state = oldstate * 6364136223846793005ULL + _inc;
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
uint32_t rot = oldstate >> 59u;
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}
/* signed float [-1..+1] */
float rand_sf ()
{
return (rand_u32 () / 2147483647.5f) - 1.f;
}
private:
uint64_t _state;
uint64_t _inc;
uint64_t _state;
uint64_t _inc;
int _foo;
};
} // namespace PBD

64
libs/pbd/pcg_rand.cc Normal file
View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2021 Robin Gareus <robin@gareus.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <cassert>
#include <climits>
#include <ctime>
#include "pbd/pcg_rand.h"
using namespace PBD;
PCGRand::PCGRand ()
{
int foo = 0;
uint64_t initseq = (intptr_t)&foo;
_state = 0;
_inc = (initseq << 1) | 1;
rand_u32 ();
_state += time (NULL) ^ (intptr_t)this;
rand_u32 ();
}
/* uniform integer min <= rand < max */
int
PCGRand::rand (int max, int min /* = 0 */)
{
assert (min < max);
assert (min > 0 || max < INT_MAX + min); // max - min overflow
assert (min < 0 || max > INT_MIN + min); // max - min underflow
const int range = max - min;
while (true) {
uint32_t value = rand_u32 ();
if (value < 4294967295 - 4294967295 % range) {
return min + value % range;
}
}
}
uint32_t
PCGRand::rand_u32 ()
{
uint64_t oldstate = _state;
_state = oldstate * 6364136223846793005ULL + _inc;
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
uint32_t rot = oldstate >> 59u;
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}

View File

@ -62,6 +62,7 @@ libpbd_sources = [
'openuri.cc',
'pathexpand.cc',
'pbd.cc',
'pcg_rand.cc',
'pool.cc',
'property_list.cc',
'pthread_utils.cc',