Skip to content

Instantly share code, notes, and snippets.

@skeeto
skeeto / primegaps.c
Created Apr 6, 2021
Prime gaps histogram animation
View primegaps.c
/* Prime gaps histogram animation
* $ cc -Ofast -o primegaps primegaps.c -lm
* $ ./primegaps | mpv --fps=60 --no-correct-pts -
* $ ./primegaps | x264 --fps=60 -o primegaps.mp4 /dev/stdin
* Ref: https://www.youtube.com/watch?v=SMsTXQYgbiQ
* This is free and unencumbered software released into the public domain.
*/
#include <limits.h>
#include <math.h>
#include <stdio.h>
@skeeto
skeeto / pi.c
Last active Apr 3, 2021
Monte Carlo Method pi estimators (higher precision)
View pi.c
/* Monte Carlo Method pi estimate via 128-bit integer math
* This is free and unencumbered software released into the public domain.
*/
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#define NTHREADS 8
#define REPORT 0x10000000
@skeeto
skeeto / iseven.c
Last active Apr 2, 2021
"Is Even" State Machine
View iseven.c
/* State machine accepting one byte at a time to determine if a string
* contains an even or odd integer (or neither). The initial state is
* zero. Input is terminated with a zero byte, rendering a final result.
* Leading zeros are invalid except for zero itself, and no whitespace
* is accepted. A leading + or - sign is valid.
*
* Returns the next state for most non-zero bytes, or the final result
* for a terminating zero byte. A result of -1 is an error (invalid
* input), 0 means odd, and 1 means even. Any other return value is a
* state (always positive).
View b32v16.c
/* b32v16: toy 32-bit block cipher using only 16-bit operations
*
* This toy Feistel cipher is 8 rounds of a computer-designed 16-bit
* integer permutation. The key is 128 bits split into eight 16-bit
* subkeys. Decryption is much slower than encryption and only the
* "forward" direction was considered in its design.
*
* Test vectors:
* key = 0000 0000 0000 0000 0000 0000 0000 0000
* pt = 00000000 00000001 00000002
@skeeto
skeeto / starwars.c
Created Mar 19, 2021
Star Wars cellular automaton
View starwars.c
/* Star Wars cellular automaton (rule 245/2/4)
* $ cc -O3 -fopenmp -o starwars starwars.c
* $ ./starwars | mpv --no-correct-pts --fps=15 --fs -
* $ ./starwars | x264 --frames=900 --fps=15 -o starwars.mp4 /dev/stdin
* Ref: https://www.conwaylife.com/wiki/OCA:Star_Wars
*/
#include <time.h>
#include <stdio.h>
#define W (1920/S)
@skeeto
skeeto / findmem.c
Last active Mar 14, 2021
Boyer-Moore-Horspool bytestring search state machine
View findmem.c
/* Boyer-Moore-Horspool bytestring search state machine
*
* Finds instances of a bytestring needle in an arbitrarily-large haystack
* given just a byte at a time. Makes no memory allocations and only
* requires scratch space the same size as the needle.
*
* This is free and unencumbered software released into the public domain.
*/
#include <stddef.h>
@skeeto
skeeto / kcs.c
Created Mar 13, 2021
Kansas City Standard encoder
View kcs.c
/* Kansas City Standard encoder
* Encodes standard input to an 8-bit PCM 22,050 Hz WAV on standard output.
* This is free and unencumbered software released into the public domain.
*/
#include <stdio.h>
#ifndef BAUD
# define BAUD 300 // 300, 600, or 1200
#endif
#ifndef STOPBITS
@skeeto
skeeto / glitch.c
Created Mar 12, 2021
Glitch video generator
View glitch.c
/* Glitch video generator
* $ cc -O3 -o glitch glitch.c
* $ convert in.png ppm:- | ./glitch | x264 --fps 6 -o out.mp4 /dev/stdin
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define VSHIFT 12
#define HSHIFT 30
@skeeto
skeeto / collide.c
Last active Feb 6, 2021
XXTEA block middle collider
View collide.c
/* XXTEA block middle collider
* Usage: $ cc -s -O3 -march=native -fopenmp collide.c && ./a.out
*/
#include <stdint.h>
#include <stdio.h>
#include <time.h>
static void
xxtea128_encrypt(const uint32_t k[4], uint32_t v[4])
{
@skeeto
skeeto / sleep.s
Created Jan 25, 2021
186-byte POSIX sleep(1) utility in Linux x86-64 assembly
View sleep.s
; 186-byte POSIX sleep(1) utility in Linux x86-64 assembly
; Usage: nasm sleep.s && chmod +x sleep
; Ref: https://redd.it/l4wfoo
; Ref: https://nullprogram.com/blog/2016/11/17/
; This is free and unencumbered software released into the public domain.
bits 64
db 0x7F, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00
db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
db 0x02, 0x00, 0x3E, 0x00, 0x01, 0x00, 0x00, 0x00
db 0x78, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00