mirror of
https://github.com/NeoFlock/neonucleus.git
synced 2025-09-24 09:03:32 +02:00
rng
This commit is contained in:
parent
9f073b91b5
commit
b7ca7f78c1
@ -128,10 +128,25 @@ typedef struct nn_Clock {
|
|||||||
nn_ClockProc *proc;
|
nn_ClockProc *proc;
|
||||||
} nn_Clock;
|
} nn_Clock;
|
||||||
|
|
||||||
|
typedef size_t nn_RngProc(void *userdata);
|
||||||
|
|
||||||
|
typedef struct nn_Rng {
|
||||||
|
void *userdata;
|
||||||
|
size_t maximum;
|
||||||
|
nn_RngProc *proc;
|
||||||
|
} nn_Rng;
|
||||||
|
|
||||||
|
size_t nn_rand(nn_Rng *rng);
|
||||||
|
// returns from 0 to 1 (inclusive)
|
||||||
|
double nn_randf(nn_Rng *rng);
|
||||||
|
// returns from 0 to 1 (exclusive)
|
||||||
|
double nn_randfe(nn_Rng *rng);
|
||||||
|
|
||||||
typedef struct nn_Context {
|
typedef struct nn_Context {
|
||||||
nn_Alloc allocator;
|
nn_Alloc allocator;
|
||||||
nn_LockManager lockManager;
|
nn_LockManager lockManager;
|
||||||
nn_Clock clock;
|
nn_Clock clock;
|
||||||
|
nn_Rng rng;
|
||||||
} nn_Context;
|
} nn_Context;
|
||||||
|
|
||||||
// TODO: write a bunch of utils so this *can* work on baremetal.
|
// TODO: write a bunch of utils so this *can* work on baremetal.
|
||||||
@ -140,6 +155,7 @@ typedef struct nn_Context {
|
|||||||
nn_Alloc nn_libcAllocator();
|
nn_Alloc nn_libcAllocator();
|
||||||
nn_Clock nn_libcRealTime();
|
nn_Clock nn_libcRealTime();
|
||||||
nn_LockManager nn_libcMutex();
|
nn_LockManager nn_libcMutex();
|
||||||
|
nn_Rng nn_libcRng();
|
||||||
nn_Context nn_libcContext();
|
nn_Context nn_libcContext();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -283,6 +299,7 @@ nn_Context *nn_getContext(nn_universe *universe);
|
|||||||
nn_Alloc *nn_getAllocator(nn_universe *universe);
|
nn_Alloc *nn_getAllocator(nn_universe *universe);
|
||||||
nn_Clock *nn_getClock(nn_universe *universe);
|
nn_Clock *nn_getClock(nn_universe *universe);
|
||||||
nn_LockManager *nn_getLockManager(nn_universe *universe);
|
nn_LockManager *nn_getLockManager(nn_universe *universe);
|
||||||
|
nn_Rng *nn_getRng(nn_universe *universe);
|
||||||
void nn_unsafeDeleteUniverse(nn_universe *universe);
|
void nn_unsafeDeleteUniverse(nn_universe *universe);
|
||||||
void *nn_queryUserdata(nn_universe *universe, const char *name);
|
void *nn_queryUserdata(nn_universe *universe, const char *name);
|
||||||
void nn_storeUserdata(nn_universe *universe, const char *name, void *data);
|
void nn_storeUserdata(nn_universe *universe, const char *name, void *data);
|
||||||
|
31
src/utils.c
31
src/utils.c
@ -61,11 +61,25 @@ nn_Alloc nn_libcAllocator() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t nni_rand(void *userdata) {
|
||||||
|
return rand();
|
||||||
|
}
|
||||||
|
|
||||||
|
nn_Rng nn_libcRng() {
|
||||||
|
srand(time(NULL));
|
||||||
|
return (nn_Rng) {
|
||||||
|
.userdata = NULL,
|
||||||
|
.maximum = RAND_MAX,
|
||||||
|
.proc = nni_rand,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
nn_Context nn_libcContext() {
|
nn_Context nn_libcContext() {
|
||||||
return (nn_Context) {
|
return (nn_Context) {
|
||||||
.allocator = nn_libcAllocator(),
|
.allocator = nn_libcAllocator(),
|
||||||
.clock = nn_libcRealTime(),
|
.clock = nn_libcRealTime(),
|
||||||
.lockManager = nn_libcMutex(),
|
.lockManager = nn_libcMutex(),
|
||||||
|
.rng = nn_libcRng(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -89,6 +103,23 @@ void nn_deallocStr(nn_Alloc *alloc, char *s) {
|
|||||||
nn_dealloc(alloc, s, strlen(s)+1);
|
nn_dealloc(alloc, s, strlen(s)+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t nn_rand(nn_Rng *rng) {
|
||||||
|
return rng->proc(rng->userdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns from 0 to 1 (inclusive)
|
||||||
|
double nn_randf(nn_Rng *rng) {
|
||||||
|
double x = nn_rand(rng);
|
||||||
|
return x / rng->maximum;
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns from 0 to 1 (exclusive)
|
||||||
|
double nn_randfe(nn_Rng *rng) {
|
||||||
|
double x = nn_rand(rng);
|
||||||
|
if(x >= rng->maximum) return 0;
|
||||||
|
return x / rng->maximum;
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef NN_BAREMETAL
|
#ifndef NN_BAREMETAL
|
||||||
|
|
||||||
#ifdef NN_POSIX
|
#ifdef NN_POSIX
|
||||||
|
Loading…
x
Reference in New Issue
Block a user