mirror of
https://github.com/LNH-team/pico-launcher.git
synced 2026-06-02 09:06:54 +02:00
Initial commit
This commit is contained in:
8
arm9/source/rng/LinearCongruentialGenerator.cpp
Normal file
8
arm9/source/rng/LinearCongruentialGenerator.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "common.h"
|
||||
#include "LinearCongruentialGenerator.h"
|
||||
|
||||
u32 LinearCongruentialGenerator::Next()
|
||||
{
|
||||
_state = _state * 0x5D588B656C078965LL + 0x269EC3LL;
|
||||
return _state >> 32;
|
||||
}
|
||||
20
arm9/source/rng/LinearCongruentialGenerator.h
Normal file
20
arm9/source/rng/LinearCongruentialGenerator.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include "RandomGenerator.h"
|
||||
|
||||
class LinearCongruentialGenerator : public RandomGenerator
|
||||
{
|
||||
public:
|
||||
explicit LinearCongruentialGenerator(u64 seed)
|
||||
: _state(seed) { }
|
||||
|
||||
void Seed(u64 seed) override
|
||||
{
|
||||
_state = seed;
|
||||
}
|
||||
|
||||
protected:
|
||||
u32 Next() override;
|
||||
|
||||
private:
|
||||
u64 _state;
|
||||
};
|
||||
38
arm9/source/rng/RandomGenerator.h
Normal file
38
arm9/source/rng/RandomGenerator.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
|
||||
class RandomGenerator
|
||||
{
|
||||
template<class> friend class ThreadSafeRandomGenerator;
|
||||
|
||||
public:
|
||||
virtual void Seed(u64 seed) = 0;
|
||||
|
||||
u32 NextU32()
|
||||
{
|
||||
return Next();
|
||||
}
|
||||
|
||||
u32 NextU32(u32 maxPlusOne)
|
||||
{
|
||||
return ((u64)Next() * maxPlusOne) >> 32;
|
||||
}
|
||||
|
||||
u32 NextU32(u32 min, u32 maxPlusOne)
|
||||
{
|
||||
return NextU32(maxPlusOne - min) + min;
|
||||
}
|
||||
|
||||
s32 NextS32()
|
||||
{
|
||||
return Next();
|
||||
}
|
||||
|
||||
u32 NextS32(s32 min, s32 maxPlusOne)
|
||||
{
|
||||
return NextU32(maxPlusOne - min) + min;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual u32 Next() = 0;
|
||||
};
|
||||
41
arm9/source/rng/ThreadSafeRandomGenerator.h
Normal file
41
arm9/source/rng/ThreadSafeRandomGenerator.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <libtwl/rtos/rtosMutex.h>
|
||||
#include "RandomGenerator.h"
|
||||
|
||||
template <class TGenerator>
|
||||
class ThreadSafeRandomGenerator : public RandomGenerator
|
||||
{
|
||||
public:
|
||||
template <typename ...Args>
|
||||
explicit ThreadSafeRandomGenerator(Args && ...args)
|
||||
: _generator(std::forward<Args>(args)...)
|
||||
{
|
||||
rtos_createMutex(&_mutex);
|
||||
}
|
||||
|
||||
void Seed(u64 seed) override
|
||||
{
|
||||
rtos_lockMutex(&_mutex);
|
||||
{
|
||||
_generator.Seed(seed);
|
||||
}
|
||||
rtos_unlockMutex(&_mutex);
|
||||
}
|
||||
|
||||
protected:
|
||||
u32 Next() override
|
||||
{
|
||||
u32 result;
|
||||
rtos_lockMutex(&_mutex);
|
||||
{
|
||||
result = ((RandomGenerator&)_generator).Next();
|
||||
}
|
||||
rtos_unlockMutex(&_mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
rtos_mutex_t _mutex;
|
||||
TGenerator _generator;
|
||||
};
|
||||
Reference in New Issue
Block a user