- moved more stuff and split FRandom in two so that the high level baggage does not need to be pulled in to use the RNG.

This commit is contained in:
Christoph Oelckers 2020-04-11 13:01:29 +02:00
parent fb1a7679ec
commit 9ef6b15bfa
14 changed files with 70 additions and 71 deletions

View file

@ -1131,7 +1131,7 @@ set (PCH_SOURCES
common/utility/files_decompress.cpp
utility/m_png.cpp
utility/m_random.cpp
utility/memarena.cpp
common/utility/memarena.cpp
utility/nodebuilder/nodebuild.cpp
utility/nodebuilder/nodebuild_classify_nosse2.cpp
utility/nodebuilder/nodebuild_events.cpp
@ -1142,11 +1142,11 @@ set (PCH_SOURCES
utility/stats.cpp
common/utility/cmdlib.cpp
common/utility/configfile.cpp
utility/i_time.cpp
utility/m_argv.cpp
common/utility/i_time.cpp
common/utility/m_argv.cpp
utility/m_bbox.cpp
utility/name.cpp
utility/s_playlist.cpp
common/utility/s_playlist.cpp
utility/v_collection.cpp
common/utility/zstrformat.cpp
common/thirdparty/md5.cpp

View file

@ -160,7 +160,7 @@ void FMemArena::FreeAllBlocks()
//
//==========================================================================
void FMemArena::DumpInfo()
FString FMemArena::DumpInfo()
{
size_t allocated = 0;
size_t used = 0;
@ -169,7 +169,7 @@ void FMemArena::DumpInfo()
allocated += BlockSize;
used += BlockSize - ((char*)block->Limit - (char*)block->Avail);
}
Printf("%zu bytes allocated, %zu bytes in use\n", allocated, used);
return FStringf("%zu bytes allocated, %zu bytes in use\n", allocated, used);
}
//==========================================================================

View file

@ -46,7 +46,7 @@ public:
void *Alloc(size_t size);
void FreeAll();
void FreeAllBlocks();
void DumpInfo();
FString DumpInfo();
void DumpData(FILE *f);
protected:

View file

@ -54,14 +54,12 @@ bool FPlayList::ChangeList (const char *path)
bool pls;
int i;
Songs.Clear();
Position = 0;
if (!fr.OpenFile(path))
{
Printf ("Could not open " TEXTCOLOR_BOLD "%s" TEXTCOLOR_NORMAL ": %s\n", path, strerror(errno));
return false;
}
Songs.Clear();
Position = 0;
first = true;
pls = false;
@ -176,7 +174,6 @@ int FPlayList::SetPosition (int position)
{
Position = position;
}
DPrintf (DMSG_NOTIFY, "Playlist position set to %d\n", Position);
return Position;
}
@ -191,7 +188,6 @@ int FPlayList::Advance ()
{
Position = 0;
}
DPrintf (DMSG_NOTIFY, "Playlist advanced to song %d\n", Position);
return Position;
}
@ -201,7 +197,6 @@ int FPlayList::Backup ()
{
Position = Songs.Size() - 1;
}
DPrintf (DMSG_NOTIFY, "Playlist backed up to song %d\n", Position);
return Position;
}

View file

@ -730,13 +730,10 @@ UNSAFE_CCMD (playlist)
}
else
{
if (PlayList.GetNumSongs() > 0)
if (!PlayList.ChangeList(argv[1]))
{
PlayList.ChangeList (argv[1]);
}
else
{
PlayList.ChangeList(argv[1]);
Printf("Could not open " TEXTCOLOR_BOLD "%s" TEXTCOLOR_NORMAL ": %s\n", argv[1], strerror(errno));
return;
}
if (PlayList.GetNumSongs () > 0)
{

View file

@ -148,7 +148,6 @@ FRandom::FRandom ()
{
#ifndef NDEBUG
Name = NULL;
initialized = false;
#endif
Next = RNGList;
RNGList = this;
@ -167,7 +166,6 @@ FRandom::FRandom (const char *name)
{
NameCRC = CalcCRC32 ((const uint8_t *)name, (unsigned int)strlen (name));
#ifndef NDEBUG
initialized = false;
Name = name;
// A CRC of 0 is reserved for nameless RNGs that don't get stored
// in savegames. The chance is very low that you would get a CRC of 0,
@ -258,8 +256,7 @@ void FRandom::Init(uint32_t seed)
// [RH] Use the RNG's name's CRC to modify the original seed.
// This way, new RNGs can be added later, and it doesn't matter
// which order they get initialized in.
uint32_t seeds[2] = { NameCRC, seed };
InitByArray(seeds, 2);
SFMTObj::Init(NameCRC, seed);
}
//==========================================================================

View file

@ -37,11 +37,11 @@
#include <stdio.h>
#include "basics.h"
#include "sfmt/SFMT.h"
#include "SFMT/SFMTObj.h"
class FSerializer;
class FRandom
class FRandom : public SFMTObj
{
public:
FRandom ();
@ -88,16 +88,6 @@ public:
void Init(uint32_t seed);
// SFMT interface
unsigned int GenRand32();
uint64_t GenRand64();
void FillArray32(uint32_t *array, int size);
void FillArray64(uint64_t *array, int size);
void InitGenRand(uint32_t seed);
void InitByArray(uint32_t *init_key, int key_length);
int GetMinArraySize32();
int GetMinArraySize64();
/* These real versions are due to Isaku Wada */
/** generates a random number on [0,1]-real-interval */
static inline double ToReal1(uint32_t v)
@ -190,29 +180,6 @@ private:
uint32_t NameCRC;
static FRandom *RNGList;
/*-------------------------------------------
SFMT internal state, index counter and flag
-------------------------------------------*/
void GenRandAll();
void GenRandArray(w128_t *array, int size);
void PeriodCertification();
/** the 128-bit internal state array */
union
{
w128_t w128[SFMT::N];
unsigned int u[SFMT::N32];
uint64_t u64[SFMT::N64];
} sfmt;
/** index counter to the 32-bit internal state array */
int idx;
/** a flag: it is 0 if and only if the internal state is not yet
* initialized. */
#ifndef NDEBUG
bool initialized;
#endif
};
extern uint32_t rngseed; // The starting seed (not part of state)

View file

@ -12,7 +12,7 @@
*/
#include <string.h>
#include <assert.h>
#include "m_random.h"
#include "SFMT/SFMTObj.h"
#include "SFMT-params.h"
#if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64)
@ -201,7 +201,7 @@ inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
* This function fills the internal state array with pseudorandom
* integers.
*/
void FRandom::GenRandAll()
void SFMTObj::GenRandAll()
{
int i;
w128_t *r1, *r2;
@ -227,7 +227,7 @@ void FRandom::GenRandAll()
* @param array an 128-bit array to be filled by pseudorandom numbers.
* @param size number of 128-bit pseudorandom numbers to be generated.
*/
void FRandom::GenRandArray(w128_t *array, int size)
void SFMTObj::GenRandArray(w128_t *array, int size)
{
int i, j;
w128_t *r1, *r2;
@ -301,7 +301,7 @@ static uint32_t func2(uint32_t x)
/**
* This function certificate the period of 2^{MEXP}
*/
void FRandom::PeriodCertification()
void SFMTObj::PeriodCertification()
{
int inner = 0;
int i, j;
@ -337,7 +337,7 @@ PUBLIC FUNCTIONS
* fill_array32() function.
* @return minimum size of array used for FillArray32() function.
*/
int FRandom::GetMinArraySize32()
int SFMTObj::GetMinArraySize32()
{
return SFMT::N32;
}
@ -347,7 +347,7 @@ int FRandom::GetMinArraySize32()
* fill_array64() function.
* @return minimum size of array used for FillArray64() function.
*/
int FRandom::GetMinArraySize64()
int SFMTObj::GetMinArraySize64()
{
return SFMT::N64;
}
@ -358,7 +358,7 @@ int FRandom::GetMinArraySize64()
* init_gen_rand or init_by_array must be called before this function.
* @return 32-bit pseudorandom number
*/
unsigned int FRandom::GenRand32()
unsigned int SFMTObj::GenRand32()
{
uint32_t r;
@ -379,7 +379,7 @@ unsigned int FRandom::GenRand32()
* unless an initialization is again executed.
* @return 64-bit pseudorandom number
*/
uint64_t FRandom::GenRand64()
uint64_t SFMTObj::GenRand64()
{
#if defined(BIG_ENDIAN64) && !defined(ONLY64)
uint32_t r1, r2;
@ -433,7 +433,7 @@ uint64_t FRandom::GenRand64()
* memory. Mac OSX doesn't have these functions, but \b malloc of OSX
* returns the pointer to the aligned memory block.
*/
void FRandom::FillArray32(uint32_t *array, int size)
void SFMTObj::FillArray32(uint32_t *array, int size)
{
assert(initialized);
assert(idx == SFMT::N32);
@ -470,7 +470,7 @@ void FRandom::FillArray32(uint32_t *array, int size)
* memory. Mac OSX doesn't have these functions, but \b malloc of OSX
* returns the pointer to the aligned memory block.
*/
void FRandom::FillArray64(uint64_t *array, int size)
void SFMTObj::FillArray64(uint64_t *array, int size)
{
assert(initialized);
assert(idx == SFMT::N32);
@ -491,7 +491,7 @@ void FRandom::FillArray64(uint64_t *array, int size)
*
* @param seed a 32-bit integer used as the seed.
*/
void FRandom::InitGenRand(uint32_t seed)
void SFMTObj::InitGenRand(uint32_t seed)
{
int i;
@ -515,7 +515,7 @@ void FRandom::InitGenRand(uint32_t seed)
* @param init_key the array of 32-bit integers, used as a seed.
* @param key_length the length of init_key.
*/
void FRandom::InitByArray(uint32_t *init_key, int key_length)
void SFMTObj::InitByArray(uint32_t *init_key, int key_length)
{
int i, j, count;
uint32_t r;

View file

@ -0,0 +1,43 @@
#pragma once
#include <stdint.h>
#include "SFMT.h"
struct SFMTObj
{
void Init(uint32_t seed1, uint32_t seed2)
{
uint32_t seeds[2] = { seed1, seed2 };
InitByArray(seeds, 2);
}
void GenRandAll();
void GenRandArray(w128_t *array, int size);
void PeriodCertification();
int GetMinArraySize32();
int GetMinArraySize64();
unsigned int GenRand32();
uint64_t GenRand64();
void FillArray32(uint32_t *array, int size);
void FillArray64(uint64_t *array, int size);
void InitGenRand(uint32_t seed);
void InitByArray(uint32_t *init_key, int key_length);
protected:
/** index counter to the 32-bit internal state array */
int idx;
/** the 128-bit internal state array */
union
{
w128_t w128[SFMT::N];
unsigned int u[SFMT::N32];
uint64_t u64[SFMT::N64];
} sfmt;
/** a flag: it is 0 if and only if the internal state is not yet
* initialized. */
#ifndef NDEBUG
bool initialized = false;
#endif
};