From 05b9a221748938b832acd509257ebd931049cab0 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Fri, 22 Mar 2024 12:21:03 +0000 Subject: [PATCH] Add xoshiro256p generator --- xorshift.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/xorshift.c b/xorshift.c index 4e53edb..db4c918 100644 --- a/xorshift.c +++ b/xorshift.c @@ -5,10 +5,10 @@ #include typedef struct { - u64 x; - u64 y; - u64 z; - u64 w; + u64 x; + u64 y; + u64 z; + u64 w; } XOR256State; typedef u64(RandGenFunc)(void *s); @@ -17,6 +17,8 @@ void test_xorshift_256(Image *img); u64 xorshift_256_generate(void *s); void test_xoshiro_256ss(Image *img); u64 xoshiro_256ss_generate(void *s); +void test_xoshiro_256p(Image *img); +u64 xoshiro_256p_generate(void *s); u64 rol64(u64 x, u64 bits); XOR256State init_xor_256_state(); void write_image_data(Image *img, RandGenFunc *gen_rand, void *s); @@ -39,10 +41,8 @@ int main(int argc, char *argv[]) { }; test_xorshift_256(&img); - write_p6_ppm("./outputs/xorshift256.ppm", &img); - test_xoshiro_256ss(&img); - write_p6_ppm("./outputs/xoshiro256ss.ppm", &img); + test_xoshiro_256p(&img); return EXIT_SUCCESS; } @@ -50,6 +50,7 @@ int main(int argc, char *argv[]) { void test_xorshift_256(Image *img) { XOR256State state = init_xor_256_state(); write_image_data(img, xorshift_256_generate, (void *)&state); + write_p6_ppm("./outputs/xorshift256.ppm", img); } u64 xorshift_256_generate(void *s) { @@ -68,6 +69,7 @@ u64 xorshift_256_generate(void *s) { void test_xoshiro_256ss(Image *img) { XOR256State state = init_xor_256_state(); write_image_data(img, xoshiro_256ss_generate, (void *)&state); + write_p6_ppm("./outputs/xoshiro256ss.ppm", img); } u64 xoshiro_256ss_generate(void *s) { @@ -87,15 +89,38 @@ u64 xoshiro_256ss_generate(void *s) { return result; } +void test_xoshiro_256p(Image *img) { + XOR256State state = init_xor_256_state(); + write_image_data(img, xoshiro_256p_generate, (void *)&state); + write_p6_ppm("./outputs/xoshiro256p.ppm", img); +} + +u64 xoshiro_256p_generate(void *s) { + XOR256State *state = (XOR256State *)s; + + const u64 result = state->w + state->x; + const u64 t = state->z << 17; + + state->y ^= state->w; + state->x ^= state->z; + state->z ^= state->y; + state->w ^= state->x; + + state->y ^= t; + state->x = rol64(state->x, 45); + + return result; +} + u64 rol64(u64 x, u64 bits) { return (x << bits) | (x >> (64 - bits)); } XOR256State init_xor_256_state() { - return (XOR256State){ + return (XOR256State){ .x = lrand48(), .y = lrand48(), .z = lrand48(), .w = lrand48(), - }; + }; } void write_image_data(Image *img, RandGenFunc *gen_rand, void *s) {