Misc:
Moved parts of log into their own header to start tracing stuff all over the place Added random number generators
This commit is contained in:
parent
6761cc0718
commit
bc933dcafd
7
TODO
Normal file
7
TODO
Normal file
|
@ -0,0 +1,7 @@
|
|||
- [ ] Fix string_eq
|
||||
- [ ] Move function signatures to the *.h file
|
||||
- [ ] Sorting stuff
|
||||
- [x] Random Libraries
|
||||
- [x] Mersenne Twister
|
||||
- [ ] add in u64 and f64 generation proper
|
||||
- [ ]
|
|
@ -1,15 +1,19 @@
|
|||
#include "log.h"
|
||||
|
||||
#include "types/bool.h"
|
||||
#include "types/int.h"
|
||||
|
||||
#include "types/float.h"
|
||||
#include "types/string/string.h"
|
||||
#include "types/string/string.c"
|
||||
|
||||
#include "types/sys_id.h"
|
||||
#include "types/services.h"
|
||||
|
||||
#include "ecalls.c"
|
||||
|
||||
#include "services/log.c"
|
||||
#include "services/log_service.c"
|
||||
#include "services/csprng_service.c"
|
||||
|
||||
|
||||
#include "random/random.c"
|
||||
#include "arguments.c"
|
||||
#include "process.c"
|
|
@ -4,6 +4,7 @@ typedef struct {
|
|||
} Argument;
|
||||
|
||||
Argument get_arguments() {
|
||||
// TODO read arguments from registers
|
||||
Argument args[0];
|
||||
|
||||
return (Argument) {
|
||||
|
|
5
src/ableos_std/log.h
Normal file
5
src/ableos_std/log.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#define error(MSG) inner_log(0, (MSG), __FILE__, __LINE__)
|
||||
#define warn(MSG) inner_log(1, (MSG), __FILE__, __LINE__)
|
||||
#define info(MSG) inner_log(2, (MSG), __FILE__, __LINE__)
|
||||
#define debug(MSG) inner_log(3, (MSG), __FILE__, __LINE__)
|
||||
#define trace(MSG) inner_log(4, (MSG), __FILE__, __LINE__)
|
63
src/ableos_std/random/mersenne_twister/lib.c
Normal file
63
src/ableos_std/random/mersenne_twister/lib.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include "lib.h"
|
||||
|
||||
#define UPPER_MASK 0x80000000
|
||||
#define LOWER_MASK 0x7fffffff
|
||||
#define TEMPERING_MASK_B 0x9d2c5680
|
||||
#define TEMPERING_MASK_C 0xefc60000
|
||||
|
||||
|
||||
inline static void mt_seedRand(MTRand* rand, unsigned long seed) {
|
||||
rand->mt[0] = seed & 0xffffffff;
|
||||
for(rand->index=1; rand->index<STATE_VECTOR_LENGTH; rand->index++) {
|
||||
rand->mt[rand->index] = (6069 * rand->mt[rand->index-1]) & 0xffffffff;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates a new random number generator from a given seed.
|
||||
*/
|
||||
MTRand mersenne_twister_seed_rand(u32 seed) {
|
||||
MTRand rand;
|
||||
trace("Seeding a mersenne_twister");
|
||||
mt_seedRand(&rand, seed);
|
||||
return rand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a pseudo-randomly generated u32.
|
||||
*/
|
||||
u32 mt_generate_u32(MTRand* rand) {
|
||||
u32 y;
|
||||
static u32 mag[2] = {0x0, 0x9908b0df}; /* mag[x] = x * 0x9908b0df for x = 0,1 */
|
||||
if(rand->index >= STATE_VECTOR_LENGTH || rand->index < 0) {
|
||||
/* generate STATE_VECTOR_LENGTH words at a time */
|
||||
int kk;
|
||||
if(rand->index >= STATE_VECTOR_LENGTH+1 || rand->index < 0) {
|
||||
mt_seedRand(rand, 4357);
|
||||
}
|
||||
for(kk=0; kk<STATE_VECTOR_LENGTH-STATE_VECTOR_M; kk++) {
|
||||
y = (rand->mt[kk] & UPPER_MASK) | (rand->mt[kk+1] & LOWER_MASK);
|
||||
rand->mt[kk] = rand->mt[kk+STATE_VECTOR_M] ^ (y >> 1) ^ mag[y & 0x1];
|
||||
}
|
||||
for(; kk<STATE_VECTOR_LENGTH-1; kk++) {
|
||||
y = (rand->mt[kk] & UPPER_MASK) | (rand->mt[kk+1] & LOWER_MASK);
|
||||
rand->mt[kk] = rand->mt[kk+(STATE_VECTOR_M-STATE_VECTOR_LENGTH)] ^ (y >> 1) ^ mag[y & 0x1];
|
||||
}
|
||||
y = (rand->mt[STATE_VECTOR_LENGTH-1] & UPPER_MASK) | (rand->mt[0] & LOWER_MASK);
|
||||
rand->mt[STATE_VECTOR_LENGTH-1] = rand->mt[STATE_VECTOR_M-1] ^ (y >> 1) ^ mag[y & 0x1];
|
||||
rand->index = 0;
|
||||
}
|
||||
y = rand->mt[rand->index++];
|
||||
y ^= (y >> 11);
|
||||
y ^= (y << 7) & TEMPERING_MASK_B;
|
||||
y ^= (y << 15) & TEMPERING_MASK_C;
|
||||
y ^= (y >> 18);
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a pseudo-randomly generated float 64 in the range [0..1].
|
||||
*/
|
||||
f64 mt_generate_f64_0_to_1(MTRand* rand) {
|
||||
return((f64)mt_generate_u32(rand) / (u32)0xffffffff);
|
||||
}
|
12
src/ableos_std/random/mersenne_twister/lib.h
Normal file
12
src/ableos_std/random/mersenne_twister/lib.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#define STATE_VECTOR_LENGTH 624
|
||||
/* changes to STATE_VECTOR_LENGTH also require changes to this */
|
||||
#define STATE_VECTOR_M 397
|
||||
|
||||
typedef struct tagMTRand {
|
||||
unsigned long mt[STATE_VECTOR_LENGTH];
|
||||
int index;
|
||||
} MTRand;
|
||||
|
||||
MTRand mersenne_twister_seed_rand(unsigned long seed);
|
||||
u32 mt_generate_u32(MTRand* rand);
|
||||
f64 mt_generate_f64_0_to_1(MTRand* rand);
|
1
src/ableos_std/random/random.c
Normal file
1
src/ableos_std/random/random.c
Normal file
|
@ -0,0 +1 @@
|
|||
#include "mersenne_twister/lib.c"
|
6
src/ableos_std/services/csprng_service.c
Normal file
6
src/ableos_std/services/csprng_service.c
Normal file
|
@ -0,0 +1,6 @@
|
|||
// Use this u64 as a seed to your random number generator
|
||||
u64 get_u64(){
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -33,9 +33,3 @@ void inner_log(Level level, const char* message, const char* file, int line) {
|
|||
|
||||
printf("%s [%s:%d]: %s\r\n", lvl_str, file, line, message);
|
||||
}
|
||||
|
||||
#define error(MSG) inner_log(0, (MSG), __FILE__, __LINE__)
|
||||
#define warn(MSG) inner_log(1, (MSG), __FILE__, __LINE__)
|
||||
#define info(MSG) inner_log(2, (MSG), __FILE__, __LINE__)
|
||||
#define debug(MSG) inner_log(3, (MSG), __FILE__, __LINE__)
|
||||
#define trace(MSG) inner_log(4, (MSG), __FILE__, __LINE__)
|
2
src/ableos_std/types/float.h
Normal file
2
src/ableos_std/types/float.h
Normal file
|
@ -0,0 +1,2 @@
|
|||
typedef float f32;
|
||||
typedef double f64;
|
|
@ -59,11 +59,14 @@ PS2Response scancode_command(PS2ScancodeSet scancode) {
|
|||
return Resend;
|
||||
}
|
||||
|
||||
|
||||
// Replacement for the PS/2 Keyboard Driver on ableOS
|
||||
int main() {
|
||||
Argument args = get_arguments();
|
||||
|
||||
MTRand a = mersenne_twister_seed_rand(10);
|
||||
f64 ac = mt_generate_f64_0_to_1(&a);
|
||||
printf("%f\n\r", ac);
|
||||
|
||||
int b = string_eq(args.value, from_c_str("x86-64"));
|
||||
if (b == 1) {
|
||||
info("running on X86");
|
||||
|
|
Loading…
Reference in a new issue