diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3ff7d6af1..e8d452d56 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/utility/i_time.cpp b/src/common/utility/i_time.cpp similarity index 100% rename from src/utility/i_time.cpp rename to src/common/utility/i_time.cpp diff --git a/src/utility/i_time.h b/src/common/utility/i_time.h similarity index 100% rename from src/utility/i_time.h rename to src/common/utility/i_time.h diff --git a/src/utility/m_argv.cpp b/src/common/utility/m_argv.cpp similarity index 100% rename from src/utility/m_argv.cpp rename to src/common/utility/m_argv.cpp diff --git a/src/utility/m_argv.h b/src/common/utility/m_argv.h similarity index 100% rename from src/utility/m_argv.h rename to src/common/utility/m_argv.h diff --git a/src/utility/memarena.cpp b/src/common/utility/memarena.cpp similarity index 99% rename from src/utility/memarena.cpp rename to src/common/utility/memarena.cpp index f576d49fa..c586a42c4 100644 --- a/src/utility/memarena.cpp +++ b/src/common/utility/memarena.cpp @@ -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); } //========================================================================== diff --git a/src/utility/memarena.h b/src/common/utility/memarena.h similarity index 99% rename from src/utility/memarena.h rename to src/common/utility/memarena.h index f8a15aefa..7f4903546 100644 --- a/src/utility/memarena.h +++ b/src/common/utility/memarena.h @@ -46,7 +46,7 @@ public: void *Alloc(size_t size); void FreeAll(); void FreeAllBlocks(); - void DumpInfo(); + FString DumpInfo(); void DumpData(FILE *f); protected: diff --git a/src/utility/s_playlist.cpp b/src/common/utility/s_playlist.cpp similarity index 93% rename from src/utility/s_playlist.cpp rename to src/common/utility/s_playlist.cpp index fcec0506c..49e6f2d81 100644 --- a/src/utility/s_playlist.cpp +++ b/src/common/utility/s_playlist.cpp @@ -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; } diff --git a/src/utility/s_playlist.h b/src/common/utility/s_playlist.h similarity index 100% rename from src/utility/s_playlist.h rename to src/common/utility/s_playlist.h diff --git a/src/sound/s_music.cpp b/src/sound/s_music.cpp index 7c023e151..a9c12d8c6 100644 --- a/src/sound/s_music.cpp +++ b/src/sound/s_music.cpp @@ -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) { diff --git a/src/utility/m_random.cpp b/src/utility/m_random.cpp index 9a765b851..eaf252a67 100644 --- a/src/utility/m_random.cpp +++ b/src/utility/m_random.cpp @@ -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); } //========================================================================== diff --git a/src/utility/m_random.h b/src/utility/m_random.h index c6f89a7b1..71bff03ae 100644 --- a/src/utility/m_random.h +++ b/src/utility/m_random.h @@ -37,11 +37,11 @@ #include #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) diff --git a/src/utility/sfmt/SFMT.cpp b/src/utility/sfmt/SFMT.cpp index 102497a8c..7c905990e 100644 --- a/src/utility/sfmt/SFMT.cpp +++ b/src/utility/sfmt/SFMT.cpp @@ -12,7 +12,7 @@ */ #include #include -#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; diff --git a/src/utility/sfmt/SFMTObj.h b/src/utility/sfmt/SFMTObj.h new file mode 100644 index 000000000..3b85ab939 --- /dev/null +++ b/src/utility/sfmt/SFMTObj.h @@ -0,0 +1,43 @@ +#pragma once +#include +#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 + +}; \ No newline at end of file