mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-07-15 03:37:09 +12:00
* Fix typo (#680) Co-authored-by: Noumi <139501014+noumidev@users.noreply.github.com> * More PTM stuff Co-Authored-By: Noumi <139501014+noumidev@users.noreply.github.com> * Make system language configurable * Fix building crypto++ for x64 target on Apple silicon MacOS * Attempt to switch to M1 runners again * Prevent selecting Vulkan renderer in Qt frontend and present a message * Libretro: Add system language option * Only enable audio by default on libretro for now * CMake: Bump version * Store configuration file in AppData root if not in working directory (#693) * Store configuration file in AppData root if not in working directory This fixes MacOS app bundles, as the emulator cannot write the config file into the app bundle. * Remove duplicate fs calls * I'm an idiot sandwich --------- Co-authored-by: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> * GL: Add usingGLES to driverInfo struct (#694) * Wayland fixes part 1 * Support GLES on desktop * Qt: Fix Wayland support Qt will only create a Wayland surface when show() is called on the main window and on the ScreenWidget. Thus, call the function before creating the GL context. Doesn't cause regressions on XWayland, untested in other platforms. Fixes #586 * No need to call screen->show() twice * Fix disabling Wayland & building on some distros (#700) * GLES: Properly stub out logic ops * Fix git versioning * Android_Build: Implement ccache (#703) * Android_Build: Implement ccache * Update Android_Build.yml * Update Android_Build.yml --------- Co-authored-by: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> * Removed dead Citra link in readme (#706) * CRO: Lighter icache flushes * Implement Luma icache SVCs * Add missing SVC logs * GPU: Add sw texture copies * Use vk::detail::DynamicLoader instead of vk::DynamicLoader (#710) * Use vk::detail::DynamicLoader instead of vk::DynamicLoader * Update renderer_vk.cpp * Vk: Fix typo * Vk: Lock CI runners to SDK version 1.3.301 temporarily * Vk: Fixing CI pt 2 * Vulkan: Fixing CI pt 3 * Vk: Fix typo * Temporarily give 80MB to all processes (#715) * Try to cross-compile Libretro core for arm64 (#717) * Try to cross-compile Libretro core for arm64 * Bonk * Update Hydra_Build.yml * [WIP] Libretro: Add audio support (#714) * Libretro: Add audio support * Adding audio interface part 1 * Audio device pt 2 * More audio device * More audio device * Morea uudi odevice * More audio device * More audio device * More audio device --------- Co-authored-by: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> * Libretro audio device: Fix frame count * Mark audio devices as final * Add toggle for libretro audio device (#719) * Very important work (#720) * Very important work * Most important fix * Add more HLE service calls for eshop (#721) * CI: Fix Vulkan SDK action (#723) * GPU registers: Fix writes to some registers ignoring the mask (#725) Co-authored-by: henry <23128103+atem2069@users.noreply.github.com> * OLED theme * OLED theme config fix (#736) Co-authored-by: smiRaphi <neogt404@gmail.com> * Adding Swedish translation * Fix Metal renderer compilation on iOS * [Core] Improve iOS compilation workflow * [Qt] Hook Swedish to UI * AppDataDocumentProvider: Typo (#740) * More iOS work * More iOS progress * More iOS work * AppDataDocumentProvider: Add missing ``COLUMN_FLAGS`` in the default document projectation (#741) Fixes unable to copy files from device to app's internal storage problem * More iOS work * ios: Simplify MTKView interface (still doesn't work though) * ios: Pass CAMetalLayer instead of void* to Obj-C++ bridging header * Fix bridging cast * FINALLY IOS GRAPHICS * ios: Remove printf spam * Metal: Reimplement some texture formats on iOS * metal: implement texture decoder * metal: check for format support * metal: implement texture swizzling * metal: remove unused texture functions * Shadergen types: Add Metal & MSL * Format * Undo submodule changes * Readme: Add Chonkystation 3 * Metal: Use std::unique_ptr for texture decode * AppDataDocumentProvider: Allow to remove documents (#744) * AppDataDocumentProvider: Allow to remove documents * Typo * Metal renderer fixes for iOS * iOS driver: Add doc comments * iOS: Add frontend & frontend build files (#746) * iOS: Add SwiftUI part to repo * Add iOS build script * Update SDL2 submodule * Fix iOS build script * CI: Update xcode tools for iOS * Update iOS_Build.yml * Update iOS build * Lower XCode version * A * Update project.pbxproj * Update iOS_Build.yml * Update iOS_Build.yml * Update build.sh * iOS: Fail on build error * iOS: Add file picker (#747) * iOS: Add file picker * Fix lock placement * Qt: Add runpog icon (#752) * Update discord-rpc submodule (#753) * Remove cryptoppwin submodule (#754) * Add optional texture hashing * Fix build on new Vk SDK (#757) Co-authored-by: Nadia Holmquist Pedersen <893884+nadiaholmquist@users.noreply.github.com> * CI: Use new Vulkan SDK --------- Co-authored-by: Noumi <139501014+noumidev@users.noreply.github.com> Co-authored-by: Thomas <thomas@thomasw.dev> Co-authored-by: Thomas <twvd@users.noreply.github.com> Co-authored-by: Daniel López Guimaraes <danielectra@outlook.com> Co-authored-by: Jonian Guveli <jonian@hardpixel.eu> Co-authored-by: Ishan09811 <156402647+Ishan09811@users.noreply.github.com> Co-authored-by: Auxy6858 <71662994+Auxy6858@users.noreply.github.com> Co-authored-by: Paris Oplopoios <parisoplop@gmail.com> Co-authored-by: henry <23128103+atem2069@users.noreply.github.com> Co-authored-by: smiRaphi <neogt404@gmail.com> Co-authored-by: smiRaphi <87574679+smiRaphi@users.noreply.github.com> Co-authored-by: Daniel Nylander <po@danielnylander.se> Co-authored-by: Samuliak <samuliak77@gmail.com> Co-authored-by: Albert <45282415+ggrtk@users.noreply.github.com> Co-authored-by: Nadia Holmquist Pedersen <893884+nadiaholmquist@users.noreply.github.com>
231 lines
7.6 KiB
C++
231 lines
7.6 KiB
C++
// mersenne.h - written and placed in public domain by Jeffrey Walton.
|
|
|
|
/// \file mersenne.h
|
|
/// \brief Class file for Mersenne Twister
|
|
/// \warning MersenneTwister is suitable for Monte-Carlo simulations, where uniformaly distributed
|
|
/// numbers are required quickly. It should not be used for cryptographic purposes.
|
|
/// \since Crypto++ 5.6.3
|
|
#ifndef CRYPTOPP_MERSENNE_TWISTER_H
|
|
#define CRYPTOPP_MERSENNE_TWISTER_H
|
|
|
|
#include "cryptlib.h"
|
|
#include "secblock.h"
|
|
#include "misc.h"
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
/// \brief Mersenne Twister class for Monte-Carlo simulations
|
|
/// \tparam K Magic constant
|
|
/// \tparam M Period parameter
|
|
/// \tparam N Size of the state vector
|
|
/// \tparam F Multiplier constant
|
|
/// \tparam S Initial seed
|
|
/// \details Provides the MersenneTwister implementation. The class is a header-only implementation.
|
|
/// \details You should reseed the generator after a fork() to avoid multiple generators
|
|
/// with the same internal state.
|
|
/// \warning MersenneTwister is suitable for simulations, where uniformaly distributed numbers are
|
|
/// required quickly. It should not be used for cryptographic purposes.
|
|
/// \sa MT19937, MT19937ar
|
|
/// \since Crypto++ 5.6.3
|
|
template <unsigned int K, unsigned int M, unsigned int N, unsigned int F, word32 S>
|
|
class MersenneTwister : public RandomNumberGenerator
|
|
{
|
|
public:
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return (S==5489 ? "MT19937ar" : (S==4537 ? "MT19937" : "MT19937x")); }
|
|
|
|
~MersenneTwister() {}
|
|
|
|
/// \brief Construct a Mersenne Twister
|
|
/// \param seed 32-bit seed
|
|
/// \details Defaults to template parameter S due to changing algorithm
|
|
/// parameters over time
|
|
MersenneTwister(word32 seed = S) : m_idx(N)
|
|
{
|
|
Reset(seed);
|
|
}
|
|
|
|
bool CanIncorporateEntropy() const {return true;}
|
|
|
|
/// \brief Update RNG state with additional unpredictable values
|
|
/// \param input the entropy to add to the generator
|
|
/// \param length the size of the input buffer
|
|
/// \details MersenneTwister uses the first 32-bits of <tt>input</tt> to reseed the
|
|
/// generator. If fewer bytes are provided, then the seed is padded with 0's.
|
|
void IncorporateEntropy(const byte *input, size_t length)
|
|
{
|
|
// Handle word32 size blocks
|
|
FixedSizeSecBlock<word32, 1> temp;
|
|
temp[0] = 0;
|
|
|
|
if (length > 4)
|
|
length = 4;
|
|
|
|
for (size_t i=0; i<length; ++i)
|
|
{
|
|
temp[0] <<= 8;
|
|
temp[0] = temp[0] | input[i];
|
|
}
|
|
|
|
Reset(temp[0]);
|
|
}
|
|
|
|
/// \brief Generate random array of bytes
|
|
/// \param output byte buffer
|
|
/// \param size length of the buffer, in bytes
|
|
/// \details Bytes are written to output in big endian order. If output length
|
|
/// is not a multiple of word32, then unused bytes are not accumulated for subsequent
|
|
/// calls to GenerateBlock. Rather, the unused tail bytes are discarded, and the
|
|
/// stream is continued at the next word32 boundary from the state array.
|
|
void GenerateBlock(byte *output, size_t size)
|
|
{
|
|
// Handle word32 size blocks
|
|
FixedSizeSecBlock<word32, 1> temp;
|
|
for (size_t i=0; i < size/4; i++, output += 4)
|
|
{
|
|
temp[0] = NextMersenneWord();
|
|
std::memcpy(output, temp+0, 4);
|
|
}
|
|
|
|
// No tail bytes
|
|
if (size%4 == 0)
|
|
return;
|
|
|
|
// Handle tail bytes
|
|
temp[0] = NextMersenneWord();
|
|
switch (size%4)
|
|
{
|
|
case 3: output[2] = CRYPTOPP_GET_BYTE_AS_BYTE(temp[0], 1); /* fall through */
|
|
case 2: output[1] = CRYPTOPP_GET_BYTE_AS_BYTE(temp[0], 2); /* fall through */
|
|
case 1: output[0] = CRYPTOPP_GET_BYTE_AS_BYTE(temp[0], 3); break;
|
|
|
|
default: CRYPTOPP_ASSERT(0);;
|
|
}
|
|
}
|
|
|
|
/// \brief Generate a random 32-bit word in the range min to max, inclusive
|
|
/// \return random 32-bit word in the range min to max, inclusive
|
|
/// \details If the 32-bit candidate is not within the range, then it is discarded
|
|
/// and a new candidate is used.
|
|
word32 GenerateWord32(word32 min=0, word32 max=0xffffffffL)
|
|
{
|
|
const word32 range = max-min;
|
|
if (range == 0xffffffffL)
|
|
return NextMersenneWord();
|
|
|
|
const int maxBits = BitPrecision(range);
|
|
word32 value;
|
|
|
|
do{
|
|
value = Crop(NextMersenneWord(), maxBits);
|
|
} while (value > range);
|
|
|
|
return value+min;
|
|
}
|
|
|
|
/// \brief Generate and discard n bytes
|
|
/// \param n the number of bytes to discard, rounded up to a <tt>word32</tt> size
|
|
/// \details If n is not a multiple of <tt>word32</tt>, then unused bytes are
|
|
/// not accumulated for subsequent calls to GenerateBlock. Rather, the unused
|
|
/// tail bytes are discarded, and the stream is continued at the next
|
|
/// <tt>word32</tt> boundary from the state array.
|
|
void DiscardBytes(size_t n)
|
|
{
|
|
for(size_t i=0; i < RoundUpToMultipleOf(n, 4U); i++)
|
|
NextMersenneWord();
|
|
}
|
|
|
|
protected:
|
|
|
|
void Reset(word32 seed)
|
|
{
|
|
m_idx = N;
|
|
|
|
m_state[0] = seed;
|
|
for (unsigned int i = 1; i < N+1; i++)
|
|
m_state[i] = word32(F * (m_state[i-1] ^ (m_state[i-1] >> 30)) + i);
|
|
}
|
|
|
|
/// \brief Returns the next 32-bit word from the state array
|
|
/// \return the next 32-bit word from the state array
|
|
/// \details fetches the next word frm the state array, performs bit operations on
|
|
/// it, and then returns the value to the caller.
|
|
word32 NextMersenneWord()
|
|
{
|
|
if (m_idx >= N) { Twist(); }
|
|
|
|
word32 temp = m_state[m_idx++];
|
|
|
|
temp ^= (temp >> 11);
|
|
temp ^= (temp << 7) & 0x9D2C5680; // 0x9D2C5680 (2636928640)
|
|
temp ^= (temp << 15) & 0xEFC60000; // 0xEFC60000 (4022730752)
|
|
|
|
return temp ^ (temp >> 18);
|
|
}
|
|
|
|
/// \brief Performs the twist operation on the state array
|
|
void Twist()
|
|
{
|
|
static const word32 magic[2]={0x0UL, K};
|
|
word32 kk, temp;
|
|
|
|
CRYPTOPP_ASSERT(N >= M);
|
|
for (kk=0;kk<N-M;kk++)
|
|
{
|
|
temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
|
|
m_state[kk] = m_state[kk+M] ^ (temp >> 1) ^ magic[temp & 0x1UL];
|
|
}
|
|
|
|
for (;kk<N-1;kk++)
|
|
{
|
|
temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF);
|
|
m_state[kk] = m_state[kk+(M-N)] ^ (temp >> 1) ^ magic[temp & 0x1UL];
|
|
}
|
|
|
|
temp = (m_state[N-1] & 0x80000000)|(m_state[0] & 0x7FFFFFFF);
|
|
m_state[N-1] = m_state[M-1] ^ (temp >> 1) ^ magic[temp & 0x1UL];
|
|
|
|
// Reset index
|
|
m_idx = 0;
|
|
|
|
// Wipe temp
|
|
SecureWipeArray(&temp, 1);
|
|
}
|
|
|
|
private:
|
|
|
|
/// \brief 32-bit word state array of size N
|
|
FixedSizeSecBlock<word32, N+1> m_state;
|
|
/// \brief the current index into the state array
|
|
word32 m_idx;
|
|
};
|
|
|
|
/// \brief Original MT19937 generator provided in the ACM paper.
|
|
/// \details MT19937 uses 4537 as default initial seed.
|
|
/// \details You should reseed the generator after a fork() to avoid multiple generators
|
|
/// with the same internal state.
|
|
/// \sa MT19937ar, <A HREF="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf">Mersenne twister:
|
|
/// a 623-dimensionally equidistributed uniform pseudo-random number generator</A>
|
|
/// \since Crypto++ 5.6.3
|
|
#if CRYPTOPP_DOXYGEN_PROCESSING
|
|
class MT19937 : public MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x10DCD /*69069*/, 4537> {};
|
|
#else
|
|
typedef MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x10DCD /*69069*/, 4537> MT19937;
|
|
#endif
|
|
|
|
/// \brief Updated MT19937 generator adapted to provide an array for initialization.
|
|
/// \details MT19937 uses 5489 as default initial seed. Use this generator when interoperating with C++11's
|
|
/// mt19937 class.
|
|
/// \details You should reseed the generator after a fork() to avoid multiple generators
|
|
/// with the same internal state.
|
|
/// \sa MT19937, <A HREF="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html">Mersenne Twister
|
|
/// with improved initialization</A>
|
|
/// \since Crypto++ 5.6.3
|
|
#if CRYPTOPP_DOXYGEN_PROCESSING
|
|
class MT19937ar : public MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x6C078965 /*1812433253*/, 5489> {};
|
|
#else
|
|
typedef MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x6C078965 /*1812433253*/, 5489> MT19937ar;
|
|
#endif
|
|
|
|
NAMESPACE_END
|
|
|
|
#endif // CRYPTOPP_MERSENNE_TWISTER_H
|