Add xoshiro256p generator

This commit is contained in:
Abdelrahman Said 2024-03-22 12:21:03 +00:00
parent b8ce449e99
commit 05b9a22174

View File

@ -5,10 +5,10 @@
#include <time.h> #include <time.h>
typedef struct { typedef struct {
u64 x; u64 x;
u64 y; u64 y;
u64 z; u64 z;
u64 w; u64 w;
} XOR256State; } XOR256State;
typedef u64(RandGenFunc)(void *s); typedef u64(RandGenFunc)(void *s);
@ -17,6 +17,8 @@ void test_xorshift_256(Image *img);
u64 xorshift_256_generate(void *s); u64 xorshift_256_generate(void *s);
void test_xoshiro_256ss(Image *img); void test_xoshiro_256ss(Image *img);
u64 xoshiro_256ss_generate(void *s); u64 xoshiro_256ss_generate(void *s);
void test_xoshiro_256p(Image *img);
u64 xoshiro_256p_generate(void *s);
u64 rol64(u64 x, u64 bits); u64 rol64(u64 x, u64 bits);
XOR256State init_xor_256_state(); XOR256State init_xor_256_state();
void write_image_data(Image *img, RandGenFunc *gen_rand, void *s); 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); test_xorshift_256(&img);
write_p6_ppm("./outputs/xorshift256.ppm", &img);
test_xoshiro_256ss(&img); test_xoshiro_256ss(&img);
write_p6_ppm("./outputs/xoshiro256ss.ppm", &img); test_xoshiro_256p(&img);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -50,6 +50,7 @@ int main(int argc, char *argv[]) {
void test_xorshift_256(Image *img) { void test_xorshift_256(Image *img) {
XOR256State state = init_xor_256_state(); XOR256State state = init_xor_256_state();
write_image_data(img, xorshift_256_generate, (void *)&state); write_image_data(img, xorshift_256_generate, (void *)&state);
write_p6_ppm("./outputs/xorshift256.ppm", img);
} }
u64 xorshift_256_generate(void *s) { u64 xorshift_256_generate(void *s) {
@ -68,6 +69,7 @@ u64 xorshift_256_generate(void *s) {
void test_xoshiro_256ss(Image *img) { void test_xoshiro_256ss(Image *img) {
XOR256State state = init_xor_256_state(); XOR256State state = init_xor_256_state();
write_image_data(img, xoshiro_256ss_generate, (void *)&state); write_image_data(img, xoshiro_256ss_generate, (void *)&state);
write_p6_ppm("./outputs/xoshiro256ss.ppm", img);
} }
u64 xoshiro_256ss_generate(void *s) { u64 xoshiro_256ss_generate(void *s) {
@ -87,15 +89,38 @@ u64 xoshiro_256ss_generate(void *s) {
return result; 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)); } u64 rol64(u64 x, u64 bits) { return (x << bits) | (x >> (64 - bits)); }
XOR256State init_xor_256_state() { XOR256State init_xor_256_state() {
return (XOR256State){ return (XOR256State){
.x = lrand48(), .x = lrand48(),
.y = lrand48(), .y = lrand48(),
.z = lrand48(), .z = lrand48(),
.w = lrand48(), .w = lrand48(),
}; };
} }
void write_image_data(Image *img, RandGenFunc *gen_rand, void *s) { void write_image_data(Image *img, RandGenFunc *gen_rand, void *s) {