Compare commits
5 Commits
f4afa5d7ea
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
e4893c150b | ||
|
05b9a22174 | ||
|
b8ce449e99 | ||
|
a6f0bf93d9 | ||
|
80aeb3fc92 |
8
compile
8
compile
@@ -6,4 +6,12 @@ CFLAGS="-g -Wall -Werror "
|
||||
XORSHIFT_SRC="xorshift.c ppm.c"
|
||||
XORSHIFT_OUT=xorshift
|
||||
|
||||
echo "Cleaning outputs dir..."
|
||||
rm -rf "./outputs/*"
|
||||
|
||||
echo -e "\nBuilding xorshift generators..."
|
||||
(set -x ; $CC $CFLAGS $XORSHIFT_SRC -o $XORSHIFT_OUT)
|
||||
|
||||
echo -e "\nTesting xorshift generators..."
|
||||
./xorshift
|
||||
echo "Generated"
|
||||
|
83
xorshift.c
83
xorshift.c
@@ -5,18 +5,15 @@
|
||||
#include <time.h>
|
||||
|
||||
typedef struct {
|
||||
u64 x;
|
||||
u64 y;
|
||||
u64 z;
|
||||
u64 w;
|
||||
} XORShift256;
|
||||
u64 seed;
|
||||
} SplitMix64State;
|
||||
|
||||
typedef struct {
|
||||
u64 x;
|
||||
u64 y;
|
||||
u64 z;
|
||||
u64 w;
|
||||
} XOShiRo256SS;
|
||||
} XOR256State;
|
||||
|
||||
typedef u64(RandGenFunc)(void *s);
|
||||
|
||||
@@ -24,8 +21,12 @@ 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);
|
||||
u64 split_mix_64(SplitMix64State *state);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
struct timespec ts = {0};
|
||||
@@ -45,27 +46,20 @@ 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;
|
||||
}
|
||||
|
||||
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);
|
||||
write_p6_ppm("./outputs/xorshift256.ppm", img);
|
||||
}
|
||||
|
||||
u64 xorshift_256_generate(void *s) {
|
||||
XORShift256 *state = (XORShift256 *)s;
|
||||
XOR256State *state = (XOR256State *)s;
|
||||
|
||||
u64 t = state->x ^ (state->x << 11);
|
||||
|
||||
@@ -78,18 +72,13 @@ 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);
|
||||
write_p6_ppm("./outputs/xoshiro256ss.ppm", img);
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -105,8 +94,42 @@ 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() {
|
||||
SplitMix64State sm64 = {.seed = lrand48()};
|
||||
|
||||
return (XOR256State){
|
||||
.x = split_mix_64(&sm64),
|
||||
.y = split_mix_64(&sm64),
|
||||
.z = split_mix_64(&sm64),
|
||||
.w = split_mix_64(&sm64),
|
||||
};
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -121,3 +144,13 @@ void write_image_data(Image *img, RandGenFunc *gen_rand, void *s) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
u64 split_mix_64(SplitMix64State *state) {
|
||||
state->seed += 0x9E3779B97f4A7C15;
|
||||
|
||||
u64 result = state->seed;
|
||||
result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9;
|
||||
result = (result ^ (result >> 27)) * 0x94D049BB133111EB;
|
||||
|
||||
return result ^ (result >> 31);
|
||||
}
|
||||
|
Reference in New Issue
Block a user