From ce53d686f534aa680425f680577e4665ef903a4f Mon Sep 17 00:00:00 2001 From: stderr64 Date: Thu, 8 Oct 2026 21:39:15 +0300 Subject: Made state array size and shift value for state selection configurable Now the state array size and the shift value for state selection are confirutable through the Makefile or through environment variables STATE_SIZE and SHIFT_VALUE --- Makefile | 6 ++++-- structs.h | 2 +- xorshift.c | 11 ++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 4ffc5a4..a91be1c 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,15 @@ CC=gcc OUTPUT_MULTIPLIER=0x6ab64cc1 +STATE_SIZE=32U +SHIFT_VALUE=27U rel: mkdir -p ./build - $(CC) -Wall -Werror -pedantic -O2 -march=x86-64-v3 -mavx2 -std=gnu23 -DOUTPUT_MULTIPLIER=${OUTPUT_MULTIPLIER} -shared -fPIC ./xorshift.c -o ./build/libxorshift.so + $(CC) -Wall -Werror -pedantic -O2 -march=x86-64-v3 -mavx2 -std=gnu23 -DSHIFT_VALUE=${SHIFT_VALUE} -DSTATE_SIZE=${STATE_SIZE} -DOUTPUT_MULTIPLIER=${OUTPUT_MULTIPLIER} -shared -fPIC ./xorshift.c -o ./build/libxorshift.so debug: mkdir -p ./build - $(CC) -Wall -Werror -pedantic -O2 -march=x86-64-v3 -mavx2 -std=gnu23 -ggdb -DOUTPUT_MULTIPLIER=${OUTPUT_MULTIPLIER} -shared -fPIC ./xorshift.c -o ./build/libxorshift.so + $(CC) -Wall -Werror -pedantic -O2 -march=x86-64-v3 -mavx2 -std=gnu23 -ggdb -DSHIFT_VALUE=${SHIFT_VALUE} -DSTATE_SIZE=${STATE_SIZE} -DOUTPUT_MULTIPLIER=${OUTPUT_MULTIPLIER} -shared -fPIC ./xorshift.c -o ./build/libxorshift.so clean: rm -rf ./build diff --git a/structs.h b/structs.h index 34c923b..c39abeb 100644 --- a/structs.h +++ b/structs.h @@ -1,6 +1,6 @@ struct rng_state{ size_t state_idx; - uint32_t states[16]; + uint32_t states[STATE_SIZE]; uint32_t gen_period_ctr; uint8_t period_end_ctr; }; diff --git a/xorshift.c b/xorshift.c index ac4d087..528f3db 100644 --- a/xorshift.c +++ b/xorshift.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "structs.h" @@ -16,23 +17,23 @@ __attribute__((visibility("default"))) bool initialize_states(){ assert( (sizeof(uint32_t) * CHAR_BIT) == 32 ); memset( (void*)&global_rng_state, 0, sizeof(struct rng_state) ); ssize_t state_bytes_read = 0; - if ( (state_bytes_read = getrandom((void*)&global_rng_state.states, 16 * sizeof(uint32_t), GRND_NONBLOCK)) == -1 ) + if ( (state_bytes_read = getrandom((void*)&global_rng_state.states, STATE_SIZE * sizeof(uint32_t), GRND_NONBLOCK)) == -1 ) return false; - if ( (size_t)state_bytes_read < (16 * sizeof(uint32_t)) ){ + if ( (size_t)state_bytes_read < (STATE_SIZE * sizeof(uint32_t)) ){ errno = EIO; return false; } - global_rng_state.state_idx = global_rng_state.states[7] >> 28; + global_rng_state.state_idx = global_rng_state.states[(uint32_t)(floor(STATE_SIZE / 2))] >> SHIFT_VALUE; errno = 0; return true; } __attribute__((visibility("default"))) uint32_t get_random_output(){ size_t use_state = global_rng_state.state_idx; - global_rng_state.state_idx = global_rng_state.states[use_state] >> 28; + global_rng_state.state_idx = global_rng_state.states[use_state] >> SHIFT_VALUE; if ( global_rng_state.gen_period_ctr >= 0xffffffff ){ global_rng_state.period_end_ctr++; - if ( global_rng_state.period_end_ctr >= 16 ){ + if ( global_rng_state.period_end_ctr >= STATE_SIZE ){ assert( initialize_states() == true ); global_rng_state.period_end_ctr = 0; } -- cgit v1.3.1-9-g1d36