Compare commits
17 Commits
a56f6ad9c2
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
e4893c150b | ||
|
05b9a22174 | ||
|
b8ce449e99 | ||
|
a6f0bf93d9 | ||
|
80aeb3fc92 | ||
|
f4afa5d7ea | ||
|
2708825924 | ||
|
546229e412 | ||
|
038172f593 | ||
|
c889708ba3 | ||
|
de621996ba | ||
70eb36015c | |||
b4259dfc90 | |||
b2bd93ce39 | |||
0aee9aee5b | |||
42ceef5c4f | |||
682a16e14f |
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
.cache
|
||||
.DS_Store
|
||||
compile_commands.json
|
||||
outputs
|
||||
xorshift
|
||||
xorshift.dSYM
|
30
aliases.h
Normal file
30
aliases.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef ALIASES_H
|
||||
#define ALIASES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define u8 uint8_t
|
||||
#define u16 uint16_t
|
||||
#define u32 uint32_t
|
||||
#define u64 uint64_t
|
||||
|
||||
#define i8 int8_t
|
||||
#define i16 int16_t
|
||||
#define i32 int32_t
|
||||
#define i64 int64_t
|
||||
|
||||
#define f32 float
|
||||
#define f64 double
|
||||
#define f128 long double
|
||||
|
||||
#define uptr uintptr_t
|
||||
#define iptr intptr_t
|
||||
|
||||
#define internal static
|
||||
#define persistent static
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define class_mem static
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !ALIASES_H
|
17
compile
Executable file
17
compile
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
|
||||
CC=clang
|
||||
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"
|
23
ppm.c
Normal file
23
ppm.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "ppm.h"
|
||||
#include "aliases.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void write_p6_ppm(const char *filepath, Image *img) {
|
||||
FILE *fp = fopen(filepath, "wb");
|
||||
if (!fp) {
|
||||
return;
|
||||
}
|
||||
|
||||
char header[1024] = {0};
|
||||
sprintf(header, "P6\n%llu %llu\n255\n", (unsigned long long)img->width,
|
||||
(unsigned long long)img->height);
|
||||
|
||||
u64 length = strlen(header);
|
||||
fwrite(&header, length, 1, fp);
|
||||
|
||||
u64 pixel_count = img->width * img->height;
|
||||
fwrite(img->data, sizeof(Pixel), pixel_count, fp);
|
||||
|
||||
fclose(fp);
|
||||
}
|
15
ppm.h
Normal file
15
ppm.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "aliases.h"
|
||||
|
||||
typedef struct {
|
||||
u8 r;
|
||||
u8 g;
|
||||
u8 b;
|
||||
} Pixel;
|
||||
|
||||
typedef struct {
|
||||
u64 width;
|
||||
u64 height;
|
||||
Pixel *data;
|
||||
} Image;
|
||||
|
||||
void write_p6_ppm(const char *filepath, Image *img);
|
156
xorshift.c
Normal file
156
xorshift.c
Normal file
@@ -0,0 +1,156 @@
|
||||
#include "aliases.h"
|
||||
#include "ppm.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef struct {
|
||||
u64 seed;
|
||||
} SplitMix64State;
|
||||
|
||||
typedef struct {
|
||||
u64 x;
|
||||
u64 y;
|
||||
u64 z;
|
||||
u64 w;
|
||||
} XOR256State;
|
||||
|
||||
typedef u64(RandGenFunc)(void *s);
|
||||
|
||||
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};
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
|
||||
|
||||
srand48(ts.tv_nsec);
|
||||
|
||||
u64 w = 1024;
|
||||
u64 h = 1024;
|
||||
u64 length = w * h;
|
||||
Pixel data[length];
|
||||
|
||||
Image img = {
|
||||
.width = w,
|
||||
.height = h,
|
||||
.data = data,
|
||||
};
|
||||
|
||||
test_xorshift_256(&img);
|
||||
test_xoshiro_256ss(&img);
|
||||
test_xoshiro_256p(&img);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
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) {
|
||||
XOR256State *state = (XOR256State *)s;
|
||||
|
||||
u64 t = state->x ^ (state->x << 11);
|
||||
|
||||
state->x = state->y;
|
||||
state->y = state->z;
|
||||
state->z = state->w;
|
||||
state->w = (state->w ^ (state->w >> 19)) ^ (t ^ (t >> 8));
|
||||
|
||||
return state->w;
|
||||
}
|
||||
|
||||
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) {
|
||||
XOR256State *state = (XOR256State *)s;
|
||||
|
||||
const u64 result = rol64(state->z * 5, 7) * 9;
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
u64 i = y * img->width + x;
|
||||
|
||||
u8 pixel = gen_rand(s) % UINT8_MAX;
|
||||
img->data[i] = (Pixel){
|
||||
.r = pixel,
|
||||
.g = pixel,
|
||||
.b = pixel,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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