diff --git a/Makefile b/Makefile index 3ed44ee..bbdf4d1 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,8 @@ PROJECT=rng AUTHOR=Avril (Flanchan) -SRC_C = $(wildcard src/*.c) -SRC_CXX = $(wildcard src/*.cpp) +SRC_C = $(wildcard src/*.c) $(wildcard src/rng/*.c) +SRC_CXX = $(wildcard src/*.cpp) $(wildcard src/rng/*.cpp) INCLUDE=include @@ -59,7 +59,7 @@ debug: | dirs $(PROJECT)-debug # Targets dirs: - @mkdir -p obj/c{,xx}/src + @mkdir -p obj/c{,xx}/src{,/rng} obj/c/%.o: %.c $(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS) diff --git a/src/rng/crand.c b/src/rng/crand.c new file mode 100644 index 0000000..164b94d --- /dev/null +++ b/src/rng/crand.c @@ -0,0 +1,39 @@ +#include +#include + +#include "crand.h" + +struct jr_state +{ + long result; + struct drand48_data data; + union { + unsigned short xsubi[3]; + unsigned int xsubh; + } st; +}; + +void _jr_seed(struct jr_state* restrict state, unsigned int with) +{ + state->st.xsubh = with & JR_MAX; + seed48_r(state->st.xsubi, &state->data); +} + +long _jr_proc(struct jr_state* restrict state) +{ + jrand48_r(state->st.xsubi, &state->data, &state->result); + return state->result; +} + +struct jr_state* _jr_alloc() +{ + struct jr_state* bx = aligned_alloc(_Alignof(struct jr_state), sizeof(struct jr_state)); + memset(bx, 0, sizeof(struct jr_state)); + + return bx; +} + +void _jr_free(struct jr_state* restrict state) +{ + free(state); +} diff --git a/src/rng/crand.cpp b/src/rng/crand.cpp new file mode 100644 index 0000000..45fb0ef --- /dev/null +++ b/src/rng/crand.cpp @@ -0,0 +1,8 @@ +#include + +#include "crand.h" + +namespace rng +{ + +} diff --git a/src/rng/crand.h b/src/rng/crand.h new file mode 100644 index 0000000..3bcb130 --- /dev/null +++ b/src/rng/crand.h @@ -0,0 +1,25 @@ +#ifndef _CRAND_H +#define _CRAND_H + +#include + +#define JR_MAX (UINT32_MAX >> 8) + +#ifdef __cplusplus +extern "C" { +#endif + +struct jr_state; + +//TODO: make all these functions visibility("internal") + +void _jr_seed(struct jr_state* restrict state, unsigned int with); +long _jr_proc(struct jr_state* restrict state); +struct jr_state* _jr_alloc(); +void _jr_free(struct jr_state* restrict state); + +#ifdef __cplusplus +} +#endif + +#endif