Compare commits

...

2 Commits

Author SHA1 Message Date
Abdelrahman Said
a6f0bf93d9 Create single state struct and add utility function for initialising it 2024-03-22 12:06:34 +00:00
Abdelrahman Said
80aeb3fc92 Update compile script to run the generators 2024-03-22 12:06:16 +00:00
2 changed files with 27 additions and 28 deletions

@ -6,4 +6,12 @@ CFLAGS="-g -Wall -Werror "
XORSHIFT_SRC="xorshift.c ppm.c"
XORSHIFT_OUT=xorshift
echo "Clean outputs dir..."
rm -rf "./outputs/*"
echo -e "\nBuild xorshift generators..."
(set -x ; $CC $CFLAGS $XORSHIFT_SRC -o $XORSHIFT_OUT)
echo -e "\nTest xorshift generators..."
./xorshift
echo "Generated"

@ -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) {