diff --git a/docs/rh-log.txt b/docs/rh-log.txt index 551109f7ff..0918d1f08f 100644 --- a/docs/rh-log.txt +++ b/docs/rh-log.txt @@ -1,6 +1,14 @@ -March 25, 2009 -- Made fmodex.dll delay-loaded so the game should no be runnable on Windows - 95 again, though without sound. +March 26, 2009 +- Changed random seed initialization so that it uses the system's + cryptographically secure random number generator, if available, instead + of the current time. +- Changed the random number generator from Lee Killough's algorithm to the + SFMT607 variant of the Mersenne Twister. + + +March 25, 2009 +- Made fmodex.dll delay-loaded so the game should be runnable on Windows 95 + again, though without sound. - Changed gameinfo_t and gameborder_t to be named structs instead of typedef'ed anonymous structs. - Fixed: P_AutoUseHealth() used autousemodes 0 and 1 instead of 1 and 2. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8e6a008a39..6ccaef3491 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -636,6 +636,7 @@ add_executable( zdoom WIN32 oplsynth/music_opldumper_mididevice.cpp oplsynth/music_opl_mididevice.cpp oplsynth/opl_mus_player.cpp + sfmt/SFMT.cpp sound/fmodsound.cpp sound/i_music.cpp sound/i_sound.cpp diff --git a/src/d_main.cpp b/src/d_main.cpp index ce821adbcc..147cec3b39 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -1580,7 +1580,7 @@ void D_DoomMain (void) SetLanguageIDs (); - rngseed = (DWORD)time (NULL); + rngseed = I_MakeRNGSeed(); FRandom::StaticClearRandom (); M_FindResponseFile (); diff --git a/src/g_level.cpp b/src/g_level.cpp index d5c7d0f67c..c0be550d7a 100644 --- a/src/g_level.cpp +++ b/src/g_level.cpp @@ -480,7 +480,7 @@ void G_InitNew (const char *mapname, bool bTitleLevel) { if (!netgame) { // [RH] Change the random seed for each new single player game - rngseed = rngseed*3/2; + rngseed = rngseed + 1; } FRandom::StaticClearRandom (); P_ClearACSVars(true); diff --git a/src/m_random.cpp b/src/m_random.cpp index 3fd1bde642..3cc977ce5f 100644 --- a/src/m_random.cpp +++ b/src/m_random.cpp @@ -1,33 +1,61 @@ -// Emacs style mode select -*- C++ -*- -//----------------------------------------------------------------------------- -// -// $Id: m_random.c,v 1.6 1998/05/03 23:13:18 killough Exp $ -// -// Copyright (C) 1993-1996 by id Software, Inc. -// -// This source is available for distribution and/or modification -// only under the terms of the DOOM Source Code License as -// published by id Software. All rights reserved. -// -// The source is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License -// for more details. -// -// -// DESCRIPTION: -// Random number LUT. -// -// 1/19/98 killough: Rewrote random number generator for better randomness, -// while at the same time maintaining demo sync and backward compatibility. -// -// 2/16/98 killough: Made each RNG local to each control-equivalent block, -// to reduce the chances of demo sync problems. -// -// [RH] Changed to use different class instances for different RNGs. Be -// sure to compile with _DEBUG if you want to catch bad RNG names. -// -//----------------------------------------------------------------------------- +/* +** m_random.cpp +** Random number generators +** +**--------------------------------------------------------------------------- +** Copyright 2002-2009 Randy Heit +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +** This file employs the techniques for improving demo sync and backward +** compatibility that Lee Killough introduced with BOOM. However, none of +** the actual code he wrote is left. In contrast to BOOM, each RNG source +** in ZDoom is implemented as a separate class instance that provides an +** interface to the high-quality Mersenne Twister. See +** . +** +** As Killough's description from m_random.h is still mostly relevant, +** here it is: +** killough 2/16/98: +** +** Make every random number generator local to each control-equivalent block. +** Critical for demo sync. The random number generators are made local to +** reduce the chances of sync problems. In Doom, if a single random number +** generator call was off, it would mess up all random number generators. +** This reduces the chances of it happening by making each RNG local to a +** control flow block. +** +** Notes to developers: if you want to reduce your demo sync hassles, follow +** this rule: for each call to P_Random you add, add a new class to the enum +** type below for each block of code which calls P_Random. If two calls to +** P_Random are not in "control-equivalent blocks", i.e. there are any cases +** where one is executed, and the other is not, put them in separate classes. +*/ + +// HEADER FILES ------------------------------------------------------------ #include @@ -41,36 +69,73 @@ #include "c_dispatch.h" #include "files.h" +// MACROS ------------------------------------------------------------------ + #define RAND_ID MAKE_ID('r','a','N','d') +// TYPES ------------------------------------------------------------------- + +// EXTERNAL FUNCTION PROTOTYPES -------------------------------------------- + +// PUBLIC FUNCTION PROTOTYPES ---------------------------------------------- + +// PRIVATE FUNCTION PROTOTYPES --------------------------------------------- + +// EXTERNAL DATA DECLARATIONS ---------------------------------------------- + +extern FRandom pr_spawnmobj; +extern FRandom pr_acs; +extern FRandom pr_chase; +extern FRandom pr_lost; +extern FRandom pr_slam; +extern FRandom pr_exrandom; + +// PUBLIC DATA DEFINITIONS ------------------------------------------------- + FRandom M_Random; -inline int UpdateSeed (DWORD &seed) -{ - DWORD oldseed = seed; - seed = oldseed * 1664525ul + 221297ul; - return (int)oldseed; -} +// Global seed. This is modified predictably to initialize every RNG. +DWORD rngseed; -DWORD rngseed = 1993; // killough 3/26/98: The seed +// PRIVATE DATA DEFINITIONS ------------------------------------------------ FRandom *FRandom::RNGList; +static TDeletingArray NewRNGs; + +// CODE -------------------------------------------------------------------- + +//========================================================================== +// +// FRandom - Nameless constructor +// +// Constructing an RNG in this way means it won't be stored in savegames. +// +//========================================================================== FRandom::FRandom () -: Seed (0), Next (NULL), NameCRC (0) +: NameCRC (0) { -#ifdef _DEBUG +#ifndef NDEBUG Name = NULL; + initialized = false; #endif Next = RNGList; RNGList = this; } +//========================================================================== +// +// FRandom - Named constructor +// +// This is the standard way to construct RNGs. +// +//========================================================================== + FRandom::FRandom (const char *name) -: Seed (0) { NameCRC = CalcCRC32 ((const BYTE *)name, (unsigned int)strlen (name)); -#ifdef _DEBUG +#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, @@ -87,7 +152,7 @@ FRandom::FRandom (const char *name) probe = probe->Next; } -#ifdef _DEBUG +#ifndef NDEBUG if (probe != NULL) { // Because RNGs are identified by their CRCs in save games, @@ -101,6 +166,12 @@ FRandom::FRandom (const char *name) *prev = this; } +//========================================================================== +// +// FRandom - Destructor +// +//========================================================================== + FRandom::~FRandom () { FRandom *rng, **prev; @@ -122,101 +193,100 @@ FRandom::~FRandom () } } -int FRandom::operator() () -{ - return (UpdateSeed (Seed) >> 20) & 255; -} - -int FRandom::operator() (int mod) -{ - if (mod <= 256) - { // The mod is small enough, so a byte is enough to get a good number. - return (*this)() % mod; - } - else - { // For mods > 256, construct a 32-bit int and modulo that. - int num = (*this)(); - num = (num << 8) | (*this)(); - num = (num << 8) | (*this)(); - num = (num << 8) | (*this)(); - return (num&0x7fffffff) % mod; - } -} - -int FRandom::Random2 () -{ - int t = (*this)(); - int u = (*this)(); - return t - u; -} - -int FRandom::Random2 (int mask) -{ - int t = (*this)() & mask; - int u = (*this)() & mask; - return t - u; -} - -int FRandom::HitDice (int count) -{ - return (1 + ((UpdateSeed (Seed) >> 20) & 7)) * count; -} - -// Initialize all the seeds +//========================================================================== // -// This initialization method is critical to maintaining demo sync. -// Each seed is initialized according to its class. killough +// FRandom :: StaticClearRandom // +// Initialize every RNGs. RNGs are seeded based on the global seed and their +// name, so each different RNG can have a different starting value despite +// being derived from a common global seed. +// +//========================================================================== void FRandom::StaticClearRandom () { - const DWORD seed = rngseed*2+1; // add 3/26/98: add rngseed - FRandom *rng = FRandom::RNGList; - // go through each RNG and set each starting seed differently - while (rng != NULL) + for (FRandom *rng = FRandom::RNGList; rng != NULL; rng = rng->Next) { - // [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. - rng->Seed = seed * rng->NameCRC; - rng = rng->Next; + rng->Init(rngseed); } } +//========================================================================== +// +// FRandom :: Init +// +// Initialize a single RNG with a given seed. +// +//========================================================================== + +void FRandom::Init(DWORD 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. + DWORD seeds[2] = { NameCRC, seed }; + InitByArray(seeds, 2); +} + +//========================================================================== +// +// FRandom :: StaticSumSeeds +// // This function produces a DWORD that can be used to check the consistancy // of network games between different machines. Only a select few RNGs are // used for the sum, because not all RNGs are important to network sync. - -extern FRandom pr_spawnmobj; -extern FRandom pr_acs; -extern FRandom pr_chase; -extern FRandom pr_lost; -extern FRandom pr_slam; +// +//========================================================================== DWORD FRandom::StaticSumSeeds () { - return pr_spawnmobj.Seed + pr_acs.Seed + pr_chase.Seed + pr_lost.Seed + pr_slam.Seed; + return + pr_spawnmobj.sfmt.u[0] + pr_spawnmobj.idx + + pr_acs.sfmt.u[0] + pr_acs.idx + + pr_chase.sfmt.u[0] + pr_chase.idx + + pr_lost.sfmt.u[0] + pr_lost.idx + + pr_slam.sfmt.u[0] + pr_slam.idx; } +//========================================================================== +// +// FRandom :: StaticWriteRNGState +// +// Stores the state of every RNG into a savegame. +// +//========================================================================== + void FRandom::StaticWriteRNGState (FILE *file) { FRandom *rng; - const DWORD seed = rngseed*2+1; FPNGChunkArchive arc (file, RAND_ID); arc << rngseed; - // Only write those RNGs that have been used for (rng = FRandom::RNGList; rng != NULL; rng = rng->Next) { - if (rng->NameCRC != 0 && rng->Seed != seed + rng->NameCRC) + // Only write those RNGs that have names + if (rng->NameCRC != 0) { - arc << rng->NameCRC << rng->Seed; + arc << rng->NameCRC << rng->idx; + for (int i = 0; i < SFMT::N32; ++i) + { + arc << rng->sfmt.u[i]; + } } } } +//========================================================================== +// +// FRandom :: StaticReadRNGState +// +// Restores the state of every RNG from a savegame. RNGs that were added +// since the savegame was created are cleared to their initial value. +// +//========================================================================== + void FRandom::StaticReadRNGState (PNGHandle *png) { FRandom *rng; @@ -241,29 +311,46 @@ void FRandom::StaticReadRNGState (PNGHandle *png) { if (rng->NameCRC == crc) { - arc << rng->Seed; + arc << rng->idx; + for (int i = 0; i < SFMT::N32; ++i) + { + arc << rng->sfmt.u[i]; + } break; } } + if (rng == NULL) + { // The RNG was removed. Skip it. + int idx; + DWORD sfmt; + arc << idx; + for (int i = 0; i < SFMT::N32; ++i) + { + arc << sfmt; + } + } } png->File->ResetFilePtr(); } } +//========================================================================== +// +// FRandom :: StaticFindRNG +// // This function attempts to find an RNG with the given name. // If it can't it will create a new one. Duplicate CRCs will // be ignored and if it happens map to the same RNG. // This is for use by DECORATE. -extern FRandom pr_exrandom; - -static TDeletingArray NewRNGs; +// +//========================================================================== FRandom *FRandom::StaticFindRNG (const char *name) { DWORD NameCRC = CalcCRC32 ((const BYTE *)name, (unsigned int)strlen (name)); // Use the default RNG if this one happens to have a CRC of 0. - if (NameCRC==0) return &pr_exrandom; + if (NameCRC == 0) return &pr_exrandom; // Find the RNG in the list, sorted by CRC FRandom **prev = &RNGList, *probe = RNGList; @@ -285,14 +372,23 @@ FRandom *FRandom::StaticFindRNG (const char *name) return probe; } -#ifdef _DEBUG +//========================================================================== +// +// FRandom :: StaticPrintSeeds +// +// Prints a snapshot of the current RNG states. This is probably wrong. +// +//========================================================================== + +#ifndef NDEBUG void FRandom::StaticPrintSeeds () { FRandom *rng = RNGList; while (rng != NULL) { - Printf ("%s: %08x\n", rng->Name, rng->Seed); + int idx = rng->idx < SFMT::N32 ? rng->idx : 0; + Printf ("%s: %08x .. %d\n", rng->Name, rng->sfmt.u[idx], idx); rng = rng->Next; } } diff --git a/src/m_random.h b/src/m_random.h index ea806510a6..6b00d72a71 100644 --- a/src/m_random.h +++ b/src/m_random.h @@ -1,57 +1,43 @@ -// Emacs style mode select -*- C++ -*- -//----------------------------------------------------------------------------- -// -// $Id: m_random.h,v 1.9 1998/05/01 14:20:31 killough Exp $ -// -// Copyright (C) 1993-1996 by id Software, Inc. -// -// This source is available for distribution and/or modification -// only under the terms of the DOOM Source Code License as -// published by id Software. All rights reserved. -// -// The source is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License -// for more details. -// -// DESCRIPTION: -// -// [RH] We now use BOOM's random number generator -// -//----------------------------------------------------------------------------- - +/* +** m_random.h +** Random number generators +** +**--------------------------------------------------------------------------- +** Copyright 2002-2009 Randy Heit +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ #ifndef __M_RANDOM__ #define __M_RANDOM__ #include #include "basictypes.h" - -// killough 1/19/98: rewritten to use a better random number generator -// in the new engine, although the old one is available for compatibility. - -// killough 2/16/98: -// -// Make every random number generator local to each control-equivalent block. -// Critical for demo sync. Changing the order of this list breaks all previous -// versions' demos. The random number generators are made local to reduce the -// chances of sync problems. In Doom, if a single random number generator call -// was off, it would mess up all random number generators. This reduces the -// chances of it happening by making each RNG local to a control flow block. -// -// Notes to developers: if you want to reduce your demo sync hassles, follow -// this rule: for each call to P_Random you add, add a new class to the enum -// type below for each block of code which calls P_Random. If two calls to -// P_Random are not in "control-equivalent blocks", i.e. there are any cases -// where one is executed, and the other is not, put them in separate classes. -// -// Keep all current entries in this list the same, and in the order -// indicated by the #'s, because they're critical for preserving demo -// sync. Do not remove entries simply because they become unused later. -// -// [RH] Changed to use different class instances for different generators. -// This makes adding new RNGs easier, because I don't need to recompile every -// file that uses random numbers. +#include "sfmt/SFMT.h" struct PNGHandle; @@ -62,42 +48,169 @@ public: FRandom (const char *name); ~FRandom (); - int operator() (); // Returns a random number in the range [0,255] - int operator() (int mod); // Returns a random number in the range [0,mod) - int Random2(); // Returns rand# - rand# - int Random2(int mask); // Returns (rand# & mask) - (rand# & mask) - int HitDice(int count); // HITDICE macro used in Heretic and Hexen + // Returns a random number in the range [0,255] + int operator()() + { + return GenRand32() & 255; + } + + // Returns a random number in the range [0,mod) + int operator() (int mod) + { + return GenRand32() % mod; + } + + // Returns rand# - rand# + int Random2() + { + return Random2(255); + } + +// Returns (rand# & mask) - (rand# & mask) + int Random2(int mask) + { + int t = GenRand32() & mask; + return t - (GenRand32() & mask); + } + + // HITDICE macro used in Heretic and Hexen + int HitDice(int count) + { + return (1 + (GenRand32() & 7)) * count; + } int Random() // synonym for () { return operator()(); } - DWORD GetSeed() + void Init(DWORD seed); + + // SFMT interface + unsigned int GenRand32(); + QWORD GenRand64(); + void FillArray32(DWORD *array, int size); + void FillArray64(QWORD *array, int size); + void InitGenRand(DWORD seed); + void InitByArray(DWORD *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(DWORD v) { - return Seed; + return v * (1.0/4294967295.0); + /* divided by 2^32-1 */ } + /** generates a random number on [0,1]-real-interval */ + inline double GenRand_Real1() + { + return ToReal1(GenRand32()); + } + + /** generates a random number on [0,1)-real-interval */ + static inline double ToReal2(DWORD v) + { + return v * (1.0/4294967296.0); + /* divided by 2^32 */ + } + + /** generates a random number on [0,1)-real-interval */ + inline double GenRand_Real2() + { + return ToReal2(GenRand32()); + } + + /** generates a random number on (0,1)-real-interval */ + static inline double ToReal3(DWORD v) + { + return (((double)v) + 0.5)*(1.0/4294967296.0); + /* divided by 2^32 */ + } + + /** generates a random number on (0,1)-real-interval */ + inline double GenRand_Real3(void) + { + return ToReal3(GenRand32()); + } + /** These real versions are due to Isaku Wada */ + + /** generates a random number on [0,1) with 53-bit resolution*/ + static inline double ToRes53(QWORD v) + { + return v * (1.0/18446744073709551616.0L); + } + + /** generates a random number on [0,1) with 53-bit resolution from two + * 32 bit integers */ + static inline double ToRes53Mix(DWORD x, DWORD y) + { + return ToRes53(x | ((QWORD)y << 32)); + } + + /** generates a random number on [0,1) with 53-bit resolution + */ + inline double GenRand_Res53(void) + { + return ToRes53(GenRand64()); + } + + /** generates a random number on [0,1) with 53-bit resolution + using 32bit integer. + */ + inline double GenRand_Res53_Mix() + { + DWORD x, y; + + x = GenRand32(); + y = GenRand32(); + return ToRes53Mix(x, y); + } + + // Static interface static void StaticClearRandom (); static DWORD StaticSumSeeds (); static void StaticReadRNGState (PNGHandle *png); static void StaticWriteRNGState (FILE *file); static FRandom *StaticFindRNG(const char *name); -#ifdef _DEBUG +#ifndef NDEBUG static void StaticPrintSeeds (); #endif private: - DWORD Seed; +#ifndef NDEBUG + const char *Name; +#endif FRandom *Next; DWORD NameCRC; -#ifdef _DEBUG - const char *Name; -#endif - 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]; + QWORD 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 DWORD rngseed; // The starting seed (not part of state) diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 1f6a15fab9..395a6e4af3 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #ifndef NO_GTK #include #include @@ -619,3 +620,26 @@ FString I_GetFromClipboard (bool use_primary_selection) #endif return ""; } + +// Return a random seed, preferably one with lots of entropy. +unsigned int I_MakeRNGSeed() +{ + unsigned int seed; + int file; + + // Try reading from /dev/urandom first, then /dev/random, then + // if all else fails, use a crappy seed from time(). + seed = time(NULL); + file = open("/dev/urandom", O_RDONLY); + if (file < 0) + { + file = open("/dev/random", O_RDONLY); + } + if (file >= 0) + { + read(file, &seed, sizeof(seed)); + close(file); + } + return seed; +} + diff --git a/src/sdl/i_system.h b/src/sdl/i_system.h index 9036aac029..044443671f 100644 --- a/src/sdl/i_system.h +++ b/src/sdl/i_system.h @@ -64,6 +64,9 @@ extern void (*I_FreezeTime) (bool frozen); fixed_t I_GetTimeFrac (uint32 *ms); +// Return a seed value for the RNG. +unsigned int I_MakeRNGSeed(); + // // Called by D_DoomLoop, diff --git a/src/sfmt/LICENSE.txt b/src/sfmt/LICENSE.txt new file mode 100644 index 0000000000..decb299153 --- /dev/null +++ b/src/sfmt/LICENSE.txt @@ -0,0 +1,29 @@ +Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima +University. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the Hiroshima University nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/src/sfmt/SFMT-alti.h b/src/sfmt/SFMT-alti.h new file mode 100644 index 0000000000..cb17a40f8d --- /dev/null +++ b/src/sfmt/SFMT-alti.h @@ -0,0 +1,156 @@ +/** +* @file SFMT-alti.h +* +* @brief SIMD oriented Fast Mersenne Twister(SFMT) +* pseudorandom number generator +* +* @author Mutsuo Saito (Hiroshima University) +* @author Makoto Matsumoto (Hiroshima University) +* +* Copyright (C) 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima +* University. All rights reserved. +* +* The new BSD License is applied to this software. +* see LICENSE.txt +*/ + +#ifndef SFMT_ALTI_H +#define SFMT_ALTI_H + +inline static vector unsigned int vec_recursion(vector unsigned int a, + vector unsigned int b, + vector unsigned int c, + vector unsigned int d) + ALWAYSINLINE; + +/** +* This function represents the recursion formula in AltiVec and BIG ENDIAN. +* @param a a 128-bit part of the interal state array +* @param b a 128-bit part of the interal state array +* @param c a 128-bit part of the interal state array +* @param d a 128-bit part of the interal state array +* @return output +*/ +inline static vector unsigned int vec_recursion(vector unsigned int a, + vector unsigned int b, + vector unsigned int c, + vector unsigned int d) { + + const vector unsigned int sl1 = ALTI_SL1; + const vector unsigned int sr1 = ALTI_SR1; +#ifdef ONLY64 + const vector unsigned int mask = ALTI_MSK64; + const vector unsigned char perm_sl = ALTI_SL2_PERM64; + const vector unsigned char perm_sr = ALTI_SR2_PERM64; +#else + const vector unsigned int mask = ALTI_MSK; + const vector unsigned char perm_sl = ALTI_SL2_PERM; + const vector unsigned char perm_sr = ALTI_SR2_PERM; +#endif + vector unsigned int v, w, x, y, z; + x = vec_perm(a, (vector unsigned int)perm_sl, perm_sl); + v = a; + y = vec_sr(b, sr1); + z = vec_perm(c, (vector unsigned int)perm_sr, perm_sr); + w = vec_sl(d, sl1); + z = vec_xor(z, w); + y = vec_and(y, mask); + v = vec_xor(v, x); + z = vec_xor(z, y); + z = vec_xor(z, v); + return z; +} + +/** +* This function fills the internal state array with pseudorandom +* integers. +*/ +inline static void gen_rand_all(void) { + int i; + vector unsigned int r, r1, r2; + + r1 = sfmt[N - 2].s; + r2 = sfmt[N - 1].s; + for (i = 0; i < N - POS1; i++) { + r = vec_recursion(sfmt[i].s, sfmt[i + POS1].s, r1, r2); + sfmt[i].s = r; + r1 = r2; + r2 = r; + } + for (; i < N; i++) { + r = vec_recursion(sfmt[i].s, sfmt[i + POS1 - N].s, r1, r2); + sfmt[i].s = r; + r1 = r2; + r2 = r; + } +} + +/** +* This function fills the user-specified array with pseudorandom +* integers. +* +* @param array an 128-bit array to be filled by pseudorandom numbers. +* @param size number of 128-bit pesudorandom numbers to be generated. +*/ +inline static void gen_rand_array(w128_t *array, int size) { + int i, j; + vector unsigned int r, r1, r2; + + r1 = sfmt[N - 2].s; + r2 = sfmt[N - 1].s; + for (i = 0; i < N - POS1; i++) { + r = vec_recursion(sfmt[i].s, sfmt[i + POS1].s, r1, r2); + array[i].s = r; + r1 = r2; + r2 = r; + } + for (; i < N; i++) { + r = vec_recursion(sfmt[i].s, array[i + POS1 - N].s, r1, r2); + array[i].s = r; + r1 = r2; + r2 = r; + } + /* main loop */ + for (; i < size - N; i++) { + r = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2); + array[i].s = r; + r1 = r2; + r2 = r; + } + for (j = 0; j < 2 * N - size; j++) { + sfmt[j].s = array[j + size - N].s; + } + for (; i < size; i++) { + r = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2); + array[i].s = r; + sfmt[j++].s = r; + r1 = r2; + r2 = r; + } +} + +#ifndef ONLY64 +#if defined(__APPLE__) +#define ALTI_SWAP (vector unsigned char) \ + (4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11) +#else +#define ALTI_SWAP {4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11} +#endif +/** +* This function swaps high and low 32-bit of 64-bit integers in user +* specified array. +* +* @param array an 128-bit array to be swaped. +* @param size size of 128-bit array. +*/ +inline static void swap(w128_t *array, int size) { + int i; + const vector unsigned char perm = ALTI_SWAP; + + for (i = 0; i < size; i++) { + array[i].s = vec_perm(array[i].s, (vector unsigned int)perm, perm); + } +} +#endif + +#endif diff --git a/src/sfmt/SFMT-params.h b/src/sfmt/SFMT-params.h new file mode 100644 index 0000000000..1ca994f64c --- /dev/null +++ b/src/sfmt/SFMT-params.h @@ -0,0 +1,70 @@ +#ifndef SFMT_PARAMS_H +#define SFMT_PARAMS_H + +/*---------------------- + the parameters of SFMT + following definitions are in paramsXXXX.h file. + ----------------------*/ +/** the pick up position of the array. +#define POS1 122 +*/ + +/** the parameter of shift left as four 32-bit registers. +#define SL1 18 + */ + +/** the parameter of shift left as one 128-bit register. + * The 128-bit integer is shifted by (SL2 * 8) bits. +#define SL2 1 +*/ + +/** the parameter of shift right as four 32-bit registers. +#define SR1 11 +*/ + +/** the parameter of shift right as one 128-bit register. + * The 128-bit integer is shifted by (SL2 * 8) bits. +#define SR2 1 +*/ + +/** A bitmask, used in the recursion. These parameters are introduced + * to break symmetry of SIMD. +#define MSK1 0xdfffffefU +#define MSK2 0xddfecb7fU +#define MSK3 0xbffaffffU +#define MSK4 0xbffffff6U +*/ + +/** These definitions are part of a 128-bit period certification vector. +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0xc98e126aU +*/ + +#if MEXP == 607 + #include "SFMT-params607.h" +#elif MEXP == 1279 + #include "SFMT-params1279.h" +#elif MEXP == 2281 + #include "SFMT-params2281.h" +#elif MEXP == 4253 + #include "SFMT-params4253.h" +#elif MEXP == 11213 + #include "SFMT-params11213.h" +#elif MEXP == 19937 + #include "SFMT-params19937.h" +#elif MEXP == 44497 + #include "SFMT-params44497.h" +#elif MEXP == 86243 + #include "SFMT-params86243.h" +#elif MEXP == 132049 + #include "SFMT-params132049.h" +#elif MEXP == 216091 + #include "SFMT-params216091.h" +#else + #error "MEXP is not valid." + #undef MEXP +#endif + +#endif /* SFMT_PARAMS_H */ diff --git a/src/sfmt/SFMT-params11213.h b/src/sfmt/SFMT-params11213.h new file mode 100644 index 0000000000..244d3138f7 --- /dev/null +++ b/src/sfmt/SFMT-params11213.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS11213_H +#define SFMT_PARAMS11213_H + +#define POS1 68 +#define SL1 14 +#define SL2 3 +#define SR1 7 +#define SR2 3 +#define MSK1 0xeffff7fbU +#define MSK2 0xffffffefU +#define MSK3 0xdfdfbfffU +#define MSK4 0x7fffdbfdU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0xe8148000U +#define PARITY4 0xd0c7afa3U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12} + #define ALTI_SR2_PERM64 {13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12} +#endif /* For OSX */ +#define IDSTR "SFMT-11213:68-14-3-7-3:effff7fb-ffffffef-dfdfbfff-7fffdbfd" + +#endif /* SFMT_PARAMS11213_H */ diff --git a/src/sfmt/SFMT-params1279.h b/src/sfmt/SFMT-params1279.h new file mode 100644 index 0000000000..df538df99b --- /dev/null +++ b/src/sfmt/SFMT-params1279.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS1279_H +#define SFMT_PARAMS1279_H + +#define POS1 7 +#define SL1 14 +#define SL2 3 +#define SR1 5 +#define SR2 1 +#define MSK1 0xf7fefffdU +#define MSK2 0x7fefcfffU +#define MSK3 0xaff3ef3fU +#define MSK4 0xb5ffff7fU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0x20000000U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-1279:7-14-3-5-1:f7fefffd-7fefcfff-aff3ef3f-b5ffff7f" + +#endif /* SFMT_PARAMS1279_H */ diff --git a/src/sfmt/SFMT-params132049.h b/src/sfmt/SFMT-params132049.h new file mode 100644 index 0000000000..94e297eb55 --- /dev/null +++ b/src/sfmt/SFMT-params132049.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS132049_H +#define SFMT_PARAMS132049_H + +#define POS1 110 +#define SL1 19 +#define SL2 1 +#define SR1 21 +#define SR2 1 +#define MSK1 0xffffbb5fU +#define MSK2 0xfb6ebf95U +#define MSK3 0xfffefffaU +#define MSK4 0xcff77fffU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0xcb520000U +#define PARITY4 0xc7e91c7dU + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} + #define ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-132049:110-19-1-21-1:ffffbb5f-fb6ebf95-fffefffa-cff77fff" + +#endif /* SFMT_PARAMS132049_H */ diff --git a/src/sfmt/SFMT-params19937.h b/src/sfmt/SFMT-params19937.h new file mode 100644 index 0000000000..04708cdf3a --- /dev/null +++ b/src/sfmt/SFMT-params19937.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS19937_H +#define SFMT_PARAMS19937_H + +#define POS1 122 +#define SL1 18 +#define SL2 1 +#define SR1 11 +#define SR2 1 +#define MSK1 0xdfffffefU +#define MSK2 0xddfecb7fU +#define MSK3 0xbffaffffU +#define MSK4 0xbffffff6U +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0x13c9e684U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} + #define ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-19937:122-18-1-11-1:dfffffef-ddfecb7f-bffaffff-bffffff6" + +#endif /* SFMT_PARAMS19937_H */ diff --git a/src/sfmt/SFMT-params216091.h b/src/sfmt/SFMT-params216091.h new file mode 100644 index 0000000000..46c7303281 --- /dev/null +++ b/src/sfmt/SFMT-params216091.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS216091_H +#define SFMT_PARAMS216091_H + +#define POS1 627 +#define SL1 11 +#define SL2 3 +#define SR1 10 +#define SR2 1 +#define MSK1 0xbff7bff7U +#define MSK2 0xbfffffffU +#define MSK3 0xbffffa7fU +#define MSK4 0xffddfbfbU +#define PARITY1 0xf8000001U +#define PARITY2 0x89e80709U +#define PARITY3 0x3bd2b64bU +#define PARITY4 0x0c64b1e4U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-216091:627-11-3-10-1:bff7bff7-bfffffff-bffffa7f-ffddfbfb" + +#endif /* SFMT_PARAMS216091_H */ diff --git a/src/sfmt/SFMT-params2281.h b/src/sfmt/SFMT-params2281.h new file mode 100644 index 0000000000..ee2ebdfd2c --- /dev/null +++ b/src/sfmt/SFMT-params2281.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS2281_H +#define SFMT_PARAMS2281_H + +#define POS1 12 +#define SL1 19 +#define SL2 1 +#define SR1 5 +#define SR2 1 +#define MSK1 0xbff7ffbfU +#define MSK2 0xfdfffffeU +#define MSK3 0xf7ffef7fU +#define MSK4 0xf2f7cbbfU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0x41dfa600U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} + #define ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-2281:12-19-1-5-1:bff7ffbf-fdfffffe-f7ffef7f-f2f7cbbf" + +#endif /* SFMT_PARAMS2281_H */ diff --git a/src/sfmt/SFMT-params4253.h b/src/sfmt/SFMT-params4253.h new file mode 100644 index 0000000000..f391a7073e --- /dev/null +++ b/src/sfmt/SFMT-params4253.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS4253_H +#define SFMT_PARAMS4253_H + +#define POS1 17 +#define SL1 20 +#define SL2 1 +#define SR1 7 +#define SR2 1 +#define MSK1 0x9f7bffffU +#define MSK2 0x9fffff5fU +#define MSK3 0x3efffffbU +#define MSK4 0xfffff7bbU +#define PARITY1 0xa8000001U +#define PARITY2 0xaf5390a3U +#define PARITY3 0xb740b3f8U +#define PARITY4 0x6c11486dU + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} + #define ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-4253:17-20-1-7-1:9f7bffff-9fffff5f-3efffffb-fffff7bb" + +#endif /* SFMT_PARAMS4253_H */ diff --git a/src/sfmt/SFMT-params44497.h b/src/sfmt/SFMT-params44497.h new file mode 100644 index 0000000000..ddef00d83c --- /dev/null +++ b/src/sfmt/SFMT-params44497.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS44497_H +#define SFMT_PARAMS44497_H + +#define POS1 330 +#define SL1 5 +#define SL2 3 +#define SR1 9 +#define SR2 3 +#define MSK1 0xeffffffbU +#define MSK2 0xdfbebfffU +#define MSK3 0xbfbf7befU +#define MSK4 0x9ffd7bffU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0xa3ac4000U +#define PARITY4 0xecc1327aU + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12} + #define ALTI_SR2_PERM64 {13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12} +#endif /* For OSX */ +#define IDSTR "SFMT-44497:330-5-3-9-3:effffffb-dfbebfff-bfbf7bef-9ffd7bff" + +#endif /* SFMT_PARAMS44497_H */ diff --git a/src/sfmt/SFMT-params607.h b/src/sfmt/SFMT-params607.h new file mode 100644 index 0000000000..fc2be6da9d --- /dev/null +++ b/src/sfmt/SFMT-params607.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS607_H +#define SFMT_PARAMS607_H + +#define POS1 2 +#define SL1 15 +#define SL2 3 +#define SR1 13 +#define SR2 3 +#define MSK1 0xfdff37ffU +#define MSK2 0xef7f3f7dU +#define MSK3 0xff777b7dU +#define MSK4 0x7ff7fb2fU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0x5986f054U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12} + #define ALTI_SR2_PERM64 {13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12} +#endif /* For OSX */ +#define IDSTR "SFMT-607:2-15-3-13-3:fdff37ff-ef7f3f7d-ff777b7d-7ff7fb2f" + +#endif /* SFMT_PARAMS607_H */ diff --git a/src/sfmt/SFMT-params86243.h b/src/sfmt/SFMT-params86243.h new file mode 100644 index 0000000000..3b52b76811 --- /dev/null +++ b/src/sfmt/SFMT-params86243.h @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS86243_H +#define SFMT_PARAMS86243_H + +#define POS1 366 +#define SL1 6 +#define SL2 7 +#define SR1 19 +#define SR2 1 +#define MSK1 0xfdbffbffU +#define MSK2 0xbff7ff3fU +#define MSK3 0xfd77efffU +#define MSK4 0xbf9ff3ffU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0xe9528d85U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(25,25,25,25,3,25,25,25,7,0,1,2,11,4,5,6) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(7,25,25,25,25,25,25,25,15,0,1,2,3,4,5,6) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {25,25,25,25,3,25,25,25,7,0,1,2,11,4,5,6} + #define ALTI_SL2_PERM64 {7,25,25,25,25,25,25,25,15,0,1,2,3,4,5,6} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-86243:366-6-7-19-1:fdbffbff-bff7ff3f-fd77efff-bf9ff3ff" + +#endif /* SFMT_PARAMS86243_H */ diff --git a/src/sfmt/SFMT-sse2.h b/src/sfmt/SFMT-sse2.h new file mode 100644 index 0000000000..2eddc7e561 --- /dev/null +++ b/src/sfmt/SFMT-sse2.h @@ -0,0 +1,121 @@ +/** +* @file SFMT-sse2.h +* @brief SIMD oriented Fast Mersenne Twister(SFMT) for Intel SSE2 +* +* @author Mutsuo Saito (Hiroshima University) +* @author Makoto Matsumoto (Hiroshima University) +* +* @note We assume LITTLE ENDIAN in this file +* +* Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima +* University. All rights reserved. +* +* The new BSD License is applied to this software, see LICENSE.txt +*/ + +#ifndef SFMT_SSE2_H +#define SFMT_SSE2_H + +PRE_ALWAYS static __m128i mm_recursion(__m128i *a, __m128i *b, __m128i c, + __m128i d, __m128i mask) ALWAYSINLINE; + +/** +* This function represents the recursion formula. +* @param a a 128-bit part of the interal state array +* @param b a 128-bit part of the interal state array +* @param c a 128-bit part of the interal state array +* @param d a 128-bit part of the interal state array +* @param mask 128-bit mask +* @return output +*/ +PRE_ALWAYS static __m128i mm_recursion(__m128i *a, __m128i *b, + __m128i c, __m128i d, __m128i mask) { + __m128i v, x, y, z; + + x = _mm_load_si128(a); + y = _mm_srli_epi32(*b, SR1); + z = _mm_srli_si128(c, SR2); + v = _mm_slli_epi32(d, SL1); + z = _mm_xor_si128(z, x); + z = _mm_xor_si128(z, v); + x = _mm_slli_si128(x, SL2); + y = _mm_and_si128(y, mask); + z = _mm_xor_si128(z, x); + z = _mm_xor_si128(z, y); + return z; +} + +/** +* This function fills the internal state array with pseudorandom +* integers. +*/ +inline static void gen_rand_all(void) { + int i; + __m128i r, r1, r2, mask; + mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1); + + r1 = _mm_load_si128(&sfmt[N - 2].si); + r2 = _mm_load_si128(&sfmt[N - 1].si); + for (i = 0; i < N - POS1; i++) { + r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1].si, r1, r2, mask); + _mm_store_si128(&sfmt[i].si, r); + r1 = r2; + r2 = r; + } + for (; i < N; i++) { + r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1 - N].si, r1, r2, mask); + _mm_store_si128(&sfmt[i].si, r); + r1 = r2; + r2 = r; + } +} + +/** +* This function fills the user-specified array with pseudorandom +* integers. +* +* @param array an 128-bit array to be filled by pseudorandom numbers. +* @param size number of 128-bit pesudorandom numbers to be generated. +*/ +inline static void gen_rand_array(w128_t *array, int size) { + int i, j; + __m128i r, r1, r2, mask; + mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1); + + r1 = _mm_load_si128(&sfmt[N - 2].si); + r2 = _mm_load_si128(&sfmt[N - 1].si); + for (i = 0; i < N - POS1; i++) { + r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1].si, r1, r2, mask); + _mm_store_si128(&array[i].si, r); + r1 = r2; + r2 = r; + } + for (; i < N; i++) { + r = mm_recursion(&sfmt[i].si, &array[i + POS1 - N].si, r1, r2, mask); + _mm_store_si128(&array[i].si, r); + r1 = r2; + r2 = r; + } + /* main loop */ + for (; i < size - N; i++) { + r = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2, + mask); + _mm_store_si128(&array[i].si, r); + r1 = r2; + r2 = r; + } + for (j = 0; j < 2 * N - size; j++) { + r = _mm_load_si128(&array[j + size - N].si); + _mm_store_si128(&sfmt[j].si, r); + } + for (; i < size; i++) { + r = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2, + mask); + _mm_store_si128(&array[i].si, r); + _mm_store_si128(&sfmt[j++].si, r); + r1 = r2; + r2 = r; + } +} + +#endif diff --git a/src/sfmt/SFMT.cpp b/src/sfmt/SFMT.cpp new file mode 100644 index 0000000000..531c7e40e8 --- /dev/null +++ b/src/sfmt/SFMT.cpp @@ -0,0 +1,583 @@ +/** +* @file SFMT.c +* @brief SIMD oriented Fast Mersenne Twister(SFMT) +* +* @author Mutsuo Saito (Hiroshima University) +* @author Makoto Matsumoto (Hiroshima University) +* +* Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima +* University. All rights reserved. +* +* The new BSD License is applied to this software, see LICENSE.txt +*/ +#include +#include +#include "m_random.h" +#include "SFMT-params.h" + +#if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64) +#define BIG_ENDIAN64 1 +#endif +#if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64) +#define BIG_ENDIAN64 1 +#endif +#if defined(ONLY64) && !defined(BIG_ENDIAN64) +#if defined(__GNUC__) +#error "-DONLY64 must be specified with -DBIG_ENDIAN64" +#endif +#undef ONLY64 +#endif + +/** a parity check vector which certificate the period of 2^{MEXP} */ +static const DWORD parity[4] = { PARITY1, PARITY2, PARITY3, PARITY4 }; + +/*---------------- +STATIC FUNCTIONS +----------------*/ +inline static int idxof(int i); +inline static void rshift128(w128_t *out, w128_t const *in, int shift); +inline static void lshift128(w128_t *out, w128_t const *in, int shift); +inline static DWORD func1(DWORD x); +inline static DWORD func2(DWORD x); +#if defined(BIG_ENDIAN64) && !defined(ONLY64) +inline static void swap(w128_t *array, int size); +#endif + +// These SIMD versions WILL NOT work as-is. I'm not even sure SSE2 is +// safe to provide as a runtime option without significant changes to +// how the state is stored, since the VC++ docs warn that: +// Using variables of type __m128i will cause the compiler to generate +// the SSE2 movdqa instruction. This instruction does not cause a fault +// on Pentium III processors but will result in silent failure, with +// possible side effects caused by whatever instructions movdqa +// translates into on Pentium III processors. + +#if defined(HAVE_ALTIVEC) +#include "SFMT-alti.h" +#elif defined(HAVE_SSE2) +#include "SFMT-sse2.h" +#endif + +/** +* This function simulate a 64-bit index of LITTLE ENDIAN +* in BIG ENDIAN machine. +*/ +#ifdef ONLY64 +inline static int idxof(int i) { + return i ^ 1; +} +#else +inline static int idxof(int i) { + return i; +} +#endif +/** +* This function simulates SIMD 128-bit right shift by the standard C. +* The 128-bit integer given in in is shifted by (shift * 8) bits. +* This function simulates the LITTLE ENDIAN SIMD. +* @param out the output of this function +* @param in the 128-bit data to be shifted +* @param shift the shift value +*/ +#ifdef ONLY64 +inline static void rshift128(w128_t *out, w128_t const *in, int shift) { + QWORD th, tl, oh, ol; + + th = ((QWORD)in->u[2] << 32) | ((QWORD)in->u[3]); + tl = ((QWORD)in->u[0] << 32) | ((QWORD)in->u[1]); + + oh = th >> (shift * 8); + ol = tl >> (shift * 8); + ol |= th << (64 - shift * 8); + out->u[0] = (DWORD)(ol >> 32); + out->u[1] = (DWORD)ol; + out->u[2] = (DWORD)(oh >> 32); + out->u[3] = (DWORD)oh; +} +#else +inline static void rshift128(w128_t *out, w128_t const *in, int shift) { + QWORD th, tl, oh, ol; + + th = ((QWORD)in->u[3] << 32) | ((QWORD)in->u[2]); + tl = ((QWORD)in->u[1] << 32) | ((QWORD)in->u[0]); + + oh = th >> (shift * 8); + ol = tl >> (shift * 8); + ol |= th << (64 - shift * 8); + out->u[1] = (DWORD)(ol >> 32); + out->u[0] = (DWORD)ol; + out->u[3] = (DWORD)(oh >> 32); + out->u[2] = (DWORD)oh; +} +#endif +/** +* This function simulates SIMD 128-bit left shift by the standard C. +* The 128-bit integer given in in is shifted by (shift * 8) bits. +* This function simulates the LITTLE ENDIAN SIMD. +* @param out the output of this function +* @param in the 128-bit data to be shifted +* @param shift the shift value +*/ +#ifdef ONLY64 +inline static void lshift128(w128_t *out, w128_t const *in, int shift) { + QWORD th, tl, oh, ol; + + th = ((QWORD)in->u[2] << 32) | ((QWORD)in->u[3]); + tl = ((QWORD)in->u[0] << 32) | ((QWORD)in->u[1]); + + oh = th << (shift * 8); + ol = tl << (shift * 8); + oh |= tl >> (64 - shift * 8); + out->u[0] = (DWORD)(ol >> 32); + out->u[1] = (DWORD)ol; + out->u[2] = (DWORD)(oh >> 32); + out->u[3] = (DWORD)oh; +} +#else +inline static void lshift128(w128_t *out, w128_t const *in, int shift) { + QWORD th, tl, oh, ol; + + th = ((QWORD)in->u[3] << 32) | ((QWORD)in->u[2]); + tl = ((QWORD)in->u[1] << 32) | ((QWORD)in->u[0]); + + oh = th << (shift * 8); + ol = tl << (shift * 8); + oh |= tl >> (64 - shift * 8); + out->u[1] = (DWORD)(ol >> 32); + out->u[0] = (DWORD)ol; + out->u[3] = (DWORD)(oh >> 32); + out->u[2] = (DWORD)oh; +} +#endif + +/** +* This function represents the recursion formula. +* @param r output +* @param a a 128-bit part of the internal state array +* @param b a 128-bit part of the internal state array +* @param c a 128-bit part of the internal state array +* @param d a 128-bit part of the internal state array +*/ +#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2)) +#ifdef ONLY64 +inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c, + w128_t *d) { + w128_t x; + w128_t y; + + lshift128(&x, a, SL2); + rshift128(&y, c, SR2); + r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0] + ^ (d->u[0] << SL1); + r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1] + ^ (d->u[1] << SL1); + r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2] + ^ (d->u[2] << SL1); + r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3] + ^ (d->u[3] << SL1); +} +#else +inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c, + w128_t *d) { + w128_t x; + w128_t y; + + lshift128(&x, a, SL2); + rshift128(&y, c, SR2); + r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0] + ^ (d->u[0] << SL1); + r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1] + ^ (d->u[1] << SL1); + r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2] + ^ (d->u[2] << SL1); + r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3] + ^ (d->u[3] << SL1); +} +#endif +#endif + +#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2)) +/** +* This function fills the internal state array with pseudorandom +* integers. +*/ +void FRandom::GenRandAll() +{ + int i; + w128_t *r1, *r2; + + r1 = &sfmt.w128[SFMT::N - 2]; + r2 = &sfmt.w128[SFMT::N - 1]; + for (i = 0; i < SFMT::N - POS1; i++) { + do_recursion(&sfmt.w128[i], &sfmt.w128[i], &sfmt.w128[i + POS1], r1, r2); + r1 = r2; + r2 = &sfmt.w128[i]; + } + for (; i < SFMT::N; i++) { + do_recursion(&sfmt.w128[i], &sfmt.w128[i], &sfmt.w128[i + POS1 - SFMT::N], r1, r2); + r1 = r2; + r2 = &sfmt.w128[i]; + } +} + +/** +* This function fills the user-specified array with pseudorandom +* integers. +* +* @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) +{ + int i, j; + w128_t *r1, *r2; + + r1 = &sfmt.w128[SFMT::N - 2]; + r2 = &sfmt.w128[SFMT::N - 1]; + for (i = 0; i < SFMT::N - POS1; i++) { + do_recursion(&array[i], &sfmt.w128[i], &sfmt.w128[i + POS1], r1, r2); + r1 = r2; + r2 = &array[i]; + } + for (; i < SFMT::N; i++) { + do_recursion(&array[i], &sfmt.w128[i], &array[i + POS1 - SFMT::N], r1, r2); + r1 = r2; + r2 = &array[i]; + } + for (; i < size - SFMT::N; i++) { + do_recursion(&array[i], &array[i - SFMT::N], &array[i + POS1 - SFMT::N], r1, r2); + r1 = r2; + r2 = &array[i]; + } + for (j = 0; j < 2 * SFMT::N - size; j++) { + sfmt.w128[j] = array[j + size - SFMT::N]; + } + for (; i < size; i++, j++) { + do_recursion(&array[i], &array[i - SFMT::N], &array[i + POS1 - SFMT::N], r1, r2); + r1 = r2; + r2 = &array[i]; + sfmt.w128[j] = array[i]; + } +} +#endif + +#if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC) +inline static void swap(w128_t *array, int size) { + int i; + DWORD x, y; + + for (i = 0; i < size; i++) { + x = array[i].u[0]; + y = array[i].u[2]; + array[i].u[0] = array[i].u[1]; + array[i].u[2] = array[i].u[3]; + array[i].u[1] = x; + array[i].u[3] = y; + } +} +#endif +/** +* This function represents a function used in the initialization +* by init_by_array +* @param x 32-bit integer +* @return 32-bit integer +*/ +static DWORD func1(DWORD x) +{ + return (x ^ (x >> 27)) * (DWORD)1664525UL; +} + +/** +* This function represents a function used in the initialization +* by init_by_array +* @param x 32-bit integer +* @return 32-bit integer +*/ +static DWORD func2(DWORD x) +{ + return (x ^ (x >> 27)) * (DWORD)1566083941UL; +} + +/** +* This function certificate the period of 2^{MEXP} +*/ +void FRandom::PeriodCertification() +{ + int inner = 0; + int i, j; + DWORD work; + + for (i = 0; i < 4; i++) + inner ^= sfmt.u[idxof(i)] & parity[i]; + for (i = 16; i > 0; i >>= 1) + inner ^= inner >> i; + inner &= 1; + /* check OK */ + if (inner == 1) { + return; + } + /* check NG, and modification */ + for (i = 0; i < 4; i++) { + work = 1; + for (j = 0; j < 32; j++) { + if ((work & parity[i]) != 0) { + sfmt.u[idxof(i)] ^= work; + return; + } + work = work << 1; + } + } +} + +/*---------------- +PUBLIC FUNCTIONS +----------------*/ +/** +* This function returns the minimum size of array used for \b +* fill_array32() function. +* @return minimum size of array used for FillArray32() function. +*/ +int FRandom::GetMinArraySize32() +{ + return SFMT::N32; +} + +/** +* This function returns the minimum size of array used for \b +* fill_array64() function. +* @return minimum size of array used for FillArray64() function. +*/ +int FRandom::GetMinArraySize64() +{ + return SFMT::N64; +} + +#ifndef ONLY64 +/** +* This function generates and returns 32-bit pseudorandom number. +* init_gen_rand or init_by_array must be called before this function. +* @return 32-bit pseudorandom number +*/ +unsigned int FRandom::GenRand32() +{ + DWORD r; + + assert(initialized); + if (idx >= SFMT::N32) + { + GenRandAll(); + idx = 0; + } + r = sfmt.u[idx++]; + return r; +} +#endif +/** +* This function generates and returns 64-bit pseudorandom number. +* init_gen_rand or init_by_array must be called before this function. +* The function gen_rand64 should not be called after gen_rand32, +* unless an initialization is again executed. +* @return 64-bit pseudorandom number +*/ +QWORD FRandom::GenRand64() +{ +#if defined(BIG_ENDIAN64) && !defined(ONLY64) + DWORD r1, r2; +#else + QWORD r; +#endif + + assert(initialized); + assert(idx % 2 == 0); + + if (idx >= SFMT::N32) + { + GenRandAll(); + idx = 0; + } +#if defined(BIG_ENDIAN64) && !defined(ONLY64) + r1 = sfmt.u[idx]; + r2 = sfmt.u[idx + 1]; + idx += 2; + return ((QWORD)r2 << 32) | r1; +#else + r = sfmt.u64[idx / 2]; + idx += 2; + return r; +#endif +} + +#ifndef ONLY64 +/** +* This function generates pseudorandom 32-bit integers in the +* specified array[] by one call. The number of pseudorandom integers +* is specified by the argument size, which must be at least 624 and a +* multiple of four. The generation by this function is much faster +* than the following gen_rand function. +* +* For initialization, init_gen_rand or init_by_array must be called +* before the first call of this function. This function can not be +* used after calling gen_rand function, without initialization. +* +* @param array an array where pseudorandom 32-bit integers are filled +* by this function. The pointer to the array must be \b "aligned" +* (namely, must be a multiple of 16) in the SIMD version, since it +* refers to the address of a 128-bit integer. In the standard C +* version, the pointer is arbitrary. +* +* @param size the number of 32-bit pseudorandom integers to be +* generated. size must be a multiple of 4, and greater than or equal +* to (MEXP / 128 + 1) * 4. +* +* @note \b memalign or \b posix_memalign is available to get aligned +* memory. Mac OSX doesn't have these functions, but \b malloc of OSX +* returns the pointer to the aligned memory block. +*/ +void FRandom::FillArray32(DWORD *array, int size) +{ + assert(initialized); + assert(idx == SFMT::N32); + assert(size % 4 == 0); + assert(size >= SFMT::N32); + + GenRandArray((w128_t *)array, size / 4); + idx = SFMT::N32; +} +#endif + +/** +* This function generates pseudorandom 64-bit integers in the +* specified array[] by one call. The number of pseudorandom integers +* is specified by the argument size, which must be at least 312 and a +* multiple of two. The generation by this function is much faster +* than the following gen_rand function. +* +* For initialization, init_gen_rand or init_by_array must be called +* before the first call of this function. This function can not be +* used after calling gen_rand function, without initialization. +* +* @param array an array where pseudorandom 64-bit integers are filled +* by this function. The pointer to the array must be "aligned" +* (namely, must be a multiple of 16) in the SIMD version, since it +* refers to the address of a 128-bit integer. In the standard C +* version, the pointer is arbitrary. +* +* @param size the number of 64-bit pseudorandom integers to be +* generated. size must be a multiple of 2, and greater than or equal +* to (MEXP / 128 + 1) * 2 +* +* @note \b memalign or \b posix_memalign is available to get aligned +* memory. Mac OSX doesn't have these functions, but \b malloc of OSX +* returns the pointer to the aligned memory block. +*/ +void FRandom::FillArray64(QWORD *array, int size) +{ + assert(initialized); + assert(idx == SFMT::N32); + assert(size % 2 == 0); + assert(size >= SFMT::N64); + + GenRandArray((w128_t *)array, size / 2); + idx = SFMT::N32; + +#if defined(BIG_ENDIAN64) && !defined(ONLY64) + swap((w128_t *)array, size / 2); +#endif +} + +/** +* This function initializes the internal state array with a 32-bit +* integer seed. +* +* @param seed a 32-bit integer used as the seed. +*/ +void FRandom::InitGenRand(DWORD seed) +{ + int i; + + sfmt.u[idxof(0)] = seed; + for (i = 1; i < SFMT::N32; i++) + { + sfmt.u[idxof(i)] = 1812433253UL * (sfmt.u[idxof(i - 1)] + ^ (sfmt.u[idxof(i - 1)] >> 30)) + + i; + } + idx = SFMT::N32; + PeriodCertification(); +#ifndef NDEBUG + initialized = 1; +#endif +} + +/** +* This function initializes the internal state array, +* with an array of 32-bit integers used as the seeds +* @param init_key the array of 32-bit integers, used as a seed. +* @param key_length the length of init_key. +*/ +void FRandom::InitByArray(DWORD *init_key, int key_length) +{ + int i, j, count; + DWORD r; + int lag; + int mid; + int size = SFMT::N * 4; + + if (size >= 623) { + lag = 11; + } else if (size >= 68) { + lag = 7; + } else if (size >= 39) { + lag = 5; + } else { + lag = 3; + } + mid = (size - lag) / 2; + + memset(&sfmt, 0x8b, sizeof(sfmt)); + if (key_length + 1 > SFMT::N32) { + count = key_length + 1; + } else { + count = SFMT::N32; + } + r = func1(sfmt.u[idxof(0)] ^ sfmt.u[idxof(mid)] ^ sfmt.u[idxof(SFMT::N32 - 1)]); + sfmt.u[idxof(mid)] += r; + r += key_length; + sfmt.u[idxof(mid + lag)] += r; + sfmt.u[idxof(0)] = r; + + count--; + for (i = 1, j = 0; (j < count) && (j < key_length); j++) + { + r = func1(sfmt.u[idxof(i)] ^ sfmt.u[idxof((i + mid) % SFMT::N32)] ^ sfmt.u[idxof((i + SFMT::N32 - 1) % SFMT::N32)]); + sfmt.u[idxof((i + mid) % SFMT::N32)] += r; + r += init_key[j] + i; + sfmt.u[idxof((i + mid + lag) % SFMT::N32)] += r; + sfmt.u[idxof(i)] = r; + i = (i + 1) % SFMT::N32; + } + for (; j < count; j++) + { + r = func1(sfmt.u[idxof(i)] ^ sfmt.u[idxof((i + mid) % SFMT::N32)] ^ sfmt.u[idxof((i + SFMT::N32 - 1) % SFMT::N32)]); + sfmt.u[idxof((i + mid) % SFMT::N32)] += r; + r += i; + sfmt.u[idxof((i + mid + lag) % SFMT::N32)] += r; + sfmt.u[idxof(i)] = r; + i = (i + 1) % SFMT::N32; + } + for (j = 0; j < SFMT::N32; j++) + { + r = func2(sfmt.u[idxof(i)] + sfmt.u[idxof((i + mid) % SFMT::N32)] + sfmt.u[idxof((i + SFMT::N32 - 1) % SFMT::N32)]); + sfmt.u[idxof((i + mid) % SFMT::N32)] ^= r; + r -= i; + sfmt.u[idxof((i + mid + lag) % SFMT::N32)] ^= r; + sfmt.u[idxof(i)] = r; + i = (i + 1) % SFMT::N32; + } + + idx = SFMT::N32; + PeriodCertification(); +#ifndef NDEBUG + initialized = 1; +#endif +} diff --git a/src/sfmt/SFMT.h b/src/sfmt/SFMT.h new file mode 100644 index 0000000000..8fde00741e --- /dev/null +++ b/src/sfmt/SFMT.h @@ -0,0 +1,120 @@ +/** + * @file SFMT.h + * + * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom + * number generator + * + * @author Mutsuo Saito (Hiroshima University) + * @author Makoto Matsumoto (Hiroshima University) + * + * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima + * University. All rights reserved. + * + * The new BSD License is applied to this software. + * see LICENSE.txt + * + * @note We assume that your system has inttypes.h. If your system + * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t, + * and you have to define PRIu64 and PRIx64 in this file as follows: + * @verbatim + typedef unsigned int uint32_t + typedef unsigned long long uint64_t + #define PRIu64 "llu" + #define PRIx64 "llx" +@endverbatim + * uint32_t must be exactly 32-bit unsigned integer type (no more, no + * less), and uint64_t must be exactly 64-bit unsigned integer type. + * PRIu64 and PRIx64 are used for printf function to print 64-bit + * unsigned int and 64-bit unsigned int in hexadecimal format. + */ + +#ifndef SFMT_H +#define SFMT_H + +#ifndef PRIu64 + #if defined(_MSC_VER) || defined(__BORLANDC__) + #define PRIu64 "I64u" + #define PRIx64 "I64x" + #else + #define PRIu64 "llu" + #define PRIx64 "llx" + #endif +#endif + +#if defined(__GNUC__) +#define ALWAYSINLINE __attribute__((always_inline)) +#else +#define ALWAYSINLINE +#endif + +#if defined(_MSC_VER) + #if _MSC_VER >= 1200 + #define PRE_ALWAYS __forceinline + #else + #define PRE_ALWAYS inline + #endif +#else + #define PRE_ALWAYS inline +#endif + +/*------------------------------------------------------ + 128-bit SIMD data type for Altivec, SSE2 or standard C + ------------------------------------------------------*/ +#if defined(HAVE_ALTIVEC) + #if !defined(__APPLE__) + #include + #endif +/** 128-bit data structure */ +union w128_t { + vector unsigned int s; + DWORD u[4]; + QWORD u64[2]; +}; + +#elif defined(HAVE_SSE2) + #include + +/** 128-bit data structure */ +union w128_t { + __m128i si; + DWORD u[4]; + QWORD u64[2]; +}; + +#else + +/** 128-bit data structure */ +union w128_t { + DWORD u[4]; + QWORD u64[2]; +}; + +#endif + +/*----------------- + BASIC DEFINITIONS + -----------------*/ + +/** Mersenne Exponent. The period of the sequence + * is a multiple of 2^MEXP-1. */ +#if !defined(MEXP) + // [RH] The default MEXP for SFMT is 19937, but since that consumes + // quite a lot of space for state, and we're using lots of different + // RNGs, default to something smaller. + #define MEXP 607 +#endif + +namespace SFMT +{ +/** SFMT generator has an internal state array of 128-bit integers, + * and N is its size. */ +enum { N = MEXP / 128 + 1 }; +/** N32 is the size of internal state array when regarded as an array + * of 32-bit integers.*/ +enum { N32 = N * 4 }; +/** N64 is the size of internal state array when regarded as an array + * of 64-bit integers.*/ +enum { N64 = N * 2 }; +}; + +#endif diff --git a/src/version.h b/src/version.h index 46197ab5d7..ee925536ad 100644 --- a/src/version.h +++ b/src/version.h @@ -54,7 +54,7 @@ // Version identifier for network games. // Bump it every time you do a release unless you're certain you // didn't change anything that will affect sync. -#define NETGAMEVERSION 221 +#define NETGAMEVERSION 222 // Version stored in the ini's [LastRun] section. // Bump it if you made some configuration change that you want to @@ -75,7 +75,7 @@ // SAVESIG should match SAVEVER. // MINSAVEVER is the minimum level snapshot version that can be loaded. -#define MINSAVEVER 1452 +#define MINSAVEVER 1507 #if SVN_REVISION_NUMBER < MINSAVEVER // Never write a savegame with a version lower than what we need diff --git a/src/win32/i_system.cpp b/src/win32/i_system.cpp index 7972bf2e27..6f300446fd 100644 --- a/src/win32/i_system.cpp +++ b/src/win32/i_system.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,7 @@ #include #include #include +#include #define USE_WINDOWS_DWORD #include "hardware.h" @@ -564,7 +566,7 @@ void I_PrintStr (const char *cp) int bpos = 0; CHARRANGE selection; CHARRANGE endselection; - LONG lines_before, lines_after; + LONG lines_before = 0, lines_after; CHARFORMAT format; if (edit != NULL) @@ -876,10 +878,39 @@ FString I_GetSteamPath() return path; } -long long QueryPerfCounter() +// Return a random seed, preferably one with lots of entropy. +unsigned int I_MakeRNGSeed() { - LARGE_INTEGER counter; + unsigned int seed; - QueryPerformanceCounter(&counter); - return counter.QuadPart; + // If RtlGenRandom is available, use that to avoid increasing the + // working set by pulling in all of the crytographic API. + HMODULE advapi = GetModuleHandle("advapi32.dll"); + if (advapi != NULL) + { + BOOLEAN (APIENTRY *RtlGenRandom)(void *, ULONG) = + (BOOLEAN (APIENTRY *)(void *, ULONG))GetProcAddress(advapi, "SystemFunction036"); + if (RtlGenRandom != NULL) + { + if (RtlGenRandom(&seed, sizeof(seed))) + { + return seed; + } + } + } + + // Use the full crytographic API to produce a seed. If that fails, + // time() is used as a fallback. + HCRYPTPROV prov; + + if (!CryptAcquireContext(&prov, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) + { + return (unsigned int)time(NULL); + } + if (!CryptGenRandom(prov, sizeof(seed), (BYTE *)&seed)) + { + seed = (unsigned int)time(NULL); + } + CryptReleaseContext(prov, 0); + return seed; } diff --git a/src/win32/i_system.h b/src/win32/i_system.h index 090d2307cc..f268358d7d 100644 --- a/src/win32/i_system.h +++ b/src/win32/i_system.h @@ -68,6 +68,9 @@ extern void (*I_FreezeTime) (bool frozen); fixed_t I_GetTimeFrac (uint32 *ms); +// Return a seed value for the RNG. +unsigned int I_MakeRNGSeed(); + // // Called by D_DoomLoop, diff --git a/zdoom.vcproj b/zdoom.vcproj index e298ccd3b3..6fb652b8a9 100644 --- a/zdoom.vcproj +++ b/zdoom.vcproj @@ -6294,6 +6294,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +