Initial commit

This commit is contained in:
Gericom
2025-11-22 17:21:45 +01:00
commit 5d6f67c612
517 changed files with 63025 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
#include "common.h"
#include "LinearCongruentialGenerator.h"
u32 LinearCongruentialGenerator::Next()
{
_state = _state * 0x5D588B656C078965LL + 0x269EC3LL;
return _state >> 32;
}

View 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;
};

View 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;
};

View 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;
};