Compare commits
	
		
			5 Commits
		
	
	
		
			a56f6ad9c2
			...
			b4259dfc90
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| b4259dfc90 | |||
| b2bd93ce39 | |||
| 0aee9aee5b | |||
| 42ceef5c4f | |||
| 682a16e14f | 
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | |||||||
|  | .cache | ||||||
|  | compile_commands.json | ||||||
|  | outputs | ||||||
|  | xorshift | ||||||
							
								
								
									
										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 | ||||||
							
								
								
									
										9
									
								
								compile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								compile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | #!/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
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								ppm.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | #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
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								ppm.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | #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
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								xorshift.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | |||||||
|  | #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; | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user