start: c Random subclass (jrand48_r())

Fortune for cpprng's current commit: Curse − 凶
lib
Avril 3 years ago
parent 7bd7964d98
commit 5ac7fc5e11

@ -4,8 +4,8 @@
PROJECT=rng
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
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)

@ -0,0 +1,39 @@
#include <stdlib.h>
#include <string.h>
#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);
}

@ -0,0 +1,8 @@
#include <rng.h>
#include "crand.h"
namespace rng
{
}

@ -0,0 +1,25 @@
#ifndef _CRAND_H
#define _CRAND_H
#include <common.h>
#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
Loading…
Cancel
Save