Create single state struct and add utility function for initialising it
This commit is contained in:
parent
80aeb3fc92
commit
a6f0bf93d9
47
xorshift.c
47
xorshift.c
@ -5,18 +5,11 @@
|
||||
#include <time.h>
|
||||
|
||||
typedef struct {
|
||||
u64 x;
|
||||
u64 y;
|
||||
u64 z;
|
||||
u64 w;
|
||||
} XORShift256;
|
||||
|
||||
typedef struct {
|
||||
u64 x;
|
||||
u64 y;
|
||||
u64 z;
|
||||
u64 w;
|
||||
} XOShiRo256SS;
|
||||
u64 x;
|
||||
u64 y;
|
||||
u64 z;
|
||||
u64 w;
|
||||
} XOR256State;
|
||||
|
||||
typedef u64(RandGenFunc)(void *s);
|
||||
|
||||
@ -25,6 +18,7 @@ u64 xorshift_256_generate(void *s);
|
||||
void test_xoshiro_256ss(Image *img);
|
||||
u64 xoshiro_256ss_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);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@ -54,18 +48,12 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
void test_xorshift_256(Image *img) {
|
||||
XORShift256 state = {
|
||||
.x = lrand48(),
|
||||
.y = lrand48(),
|
||||
.z = lrand48(),
|
||||
.w = lrand48(),
|
||||
};
|
||||
|
||||
XOR256State state = init_xor_256_state();
|
||||
write_image_data(img, xorshift_256_generate, (void *)&state);
|
||||
}
|
||||
|
||||
u64 xorshift_256_generate(void *s) {
|
||||
XORShift256 *state = (XORShift256 *)s;
|
||||
XOR256State *state = (XOR256State *)s;
|
||||
|
||||
u64 t = state->x ^ (state->x << 11);
|
||||
|
||||
@ -78,18 +66,12 @@ u64 xorshift_256_generate(void *s) {
|
||||
}
|
||||
|
||||
void test_xoshiro_256ss(Image *img) {
|
||||
XOShiRo256SS state = {
|
||||
.x = lrand48(),
|
||||
.y = lrand48(),
|
||||
.z = lrand48(),
|
||||
.w = lrand48(),
|
||||
};
|
||||
|
||||
XOR256State state = init_xor_256_state();
|
||||
write_image_data(img, xoshiro_256ss_generate, (void *)&state);
|
||||
}
|
||||
|
||||
u64 xoshiro_256ss_generate(void *s) {
|
||||
XOShiRo256SS *state = (XOShiRo256SS *)s;
|
||||
XOR256State *state = (XOR256State *)s;
|
||||
|
||||
const u64 result = rol64(state->z * 5, 7) * 9;
|
||||
const u64 t = state->z << 17;
|
||||
@ -107,6 +89,15 @@ u64 xoshiro_256ss_generate(void *s) {
|
||||
|
||||
u64 rol64(u64 x, u64 bits) { return (x << bits) | (x >> (64 - bits)); }
|
||||
|
||||
XOR256State init_xor_256_state() {
|
||||
return (XOR256State){
|
||||
.x = lrand48(),
|
||||
.y = lrand48(),
|
||||
.z = lrand48(),
|
||||
.w = lrand48(),
|
||||
};
|
||||
}
|
||||
|
||||
void write_image_data(Image *img, RandGenFunc *gen_rand, void *s) {
|
||||
for (u64 y = 0; y < img->height; ++y) {
|
||||
for (u64 x = 0; x < img->width; ++x) {
|
||||
|
Loading…
Reference in New Issue
Block a user