Compare commits
No commits in common. "b4259dfc90c802092859e9a1077e4fae14b7d738" and "a56f6ad9c2288e9a2318ad0eca9eef1ff1283abd" have entirely different histories.
b4259dfc90
...
a56f6ad9c2
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,4 +0,0 @@
|
|||||||
.cache
|
|
||||||
compile_commands.json
|
|
||||||
outputs
|
|
||||||
xorshift
|
|
30
aliases.h
30
aliases.h
@ -1,30 +0,0 @@
|
|||||||
#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
|
|
9
compile
9
compile
@ -1,9 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
CC=clang
|
|
||||||
CFLAGS="-g -Wall -Werror "
|
|
||||||
|
|
||||||
XORSHIFT_SRC="xorshift.c ppm.c"
|
|
||||||
XORSHIFT_OUT=xorshift
|
|
||||||
|
|
||||||
(set -x ; $CC $CFLAGS $XORSHIFT_SRC -o $XORSHIFT_OUT)
|
|
22
ppm.c
22
ppm.c
@ -1,22 +0,0 @@
|
|||||||
#include "ppm.h"
|
|
||||||
#include "aliases.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
void write_p6_ppm(const char *filepath, u64 width, u64 height, Pixel *data) {
|
|
||||||
FILE *fp = fopen(filepath, "wb");
|
|
||||||
if (!fp) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
char header[1024] = {0};
|
|
||||||
sprintf(header, "P6\n%lu %lu\n255\n", width, height);
|
|
||||||
|
|
||||||
u64 length = strlen(header);
|
|
||||||
fwrite(&header, length, 1, fp);
|
|
||||||
|
|
||||||
u64 pixel_count = width * height;
|
|
||||||
fwrite(data, sizeof(Pixel), pixel_count, fp);
|
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
}
|
|
9
ppm.h
9
ppm.h
@ -1,9 +0,0 @@
|
|||||||
#include "aliases.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
u8 r;
|
|
||||||
u8 g;
|
|
||||||
u8 b;
|
|
||||||
} Pixel;
|
|
||||||
|
|
||||||
void write_p6_ppm(const char *filepath, u64 width, u64 height, Pixel *data);
|
|
63
xorshift.c
63
xorshift.c
@ -1,63 +0,0 @@
|
|||||||
#include "aliases.h"
|
|
||||||
#include "ppm.h"
|
|
||||||
#include <bits/time.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
u64 x;
|
|
||||||
u64 y;
|
|
||||||
u64 z;
|
|
||||||
u64 w;
|
|
||||||
} XORShiftBasic;
|
|
||||||
|
|
||||||
u64 xor_shift_basic_generate(XORShiftBasic *state);
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
struct timespec ts = {0};
|
|
||||||
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
|
|
||||||
|
|
||||||
srand48(ts.tv_nsec);
|
|
||||||
|
|
||||||
XORShiftBasic state = {
|
|
||||||
.x = lrand48(),
|
|
||||||
.y = lrand48(),
|
|
||||||
.z = lrand48(),
|
|
||||||
.w = lrand48(),
|
|
||||||
};
|
|
||||||
|
|
||||||
u64 w = 1024;
|
|
||||||
u64 h = 1024;
|
|
||||||
u64 length = w * h;
|
|
||||||
|
|
||||||
Pixel data[length];
|
|
||||||
for (u64 y = 0; y < h; ++y) {
|
|
||||||
for (u64 x = 0; x < w; ++x) {
|
|
||||||
u64 i = y * w + x;
|
|
||||||
|
|
||||||
u8 pixel = xor_shift_basic_generate(&state) % UINT8_MAX;
|
|
||||||
data[i] = (Pixel){
|
|
||||||
.r = pixel,
|
|
||||||
.g = pixel,
|
|
||||||
.b = pixel,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
write_p6_ppm("./outputs/xorshift.ppm", w, h, data);
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 xor_shift_basic_generate(XORShiftBasic *state) {
|
|
||||||
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;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user