Skip to content

Instantly share code, notes, and snippets.

@skeeto
skeeto / halvorsen.c
Last active Jul 23, 2021
Halvorsen system animation
View halvorsen.c
/* Halvorsen system animation
* $ cc -Ofast -o halvorsen halvorsen.c -lm
* $ ./halvorsen | mpv --no-correct-pts --fps=60 --fs -
* $ ./halvorsen | x264 --fps 60 --frames 3600 -o halvorsen.mp4 /dev/stdin
* This is free and unencumbered software released into the public domain.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@skeeto
skeeto / whitespace.c
Last active Jul 13, 2021
Encode/decode arbitrary input to/from whitespace
View whitespace.c
// Encode/decode arbitrary input to/from whitespace
// Ref: https://github.com/gregfoletta/whitespacer
// Ref: https://redd.it/oie8b1
// This is free and unencumbered software released into the public domain.
#include <stdio.h>
#include <string.h>
static unsigned char dbuf[1L<<14];
static unsigned char ebuf[4*sizeof(dbuf)];
static unsigned long long tr64[1L<<16];
View mix32.go
package main
import (
"encoding/binary"
"fmt"
"io"
"os"
)
// Mix32 is a quick-and-dirty 256-bit hash from 32-bit operations.
@skeeto
skeeto / abacaba.c
Last active May 24, 2021
ABACABA state machine
View abacaba.c
/* ABACABA state machine
* Ref: https://redd.it/njxq95
* This is free and unencumbered software released into the public domain.
*/
/* Compute the next ABACABA state. The initial state is zero, and halt
* is indicated by returning to the zero state.
*
* The state is a 31-bit quantity where bits 0-24 are a bitstack, bits
* 25-29 are the stack size, and bit 30 is the recursion direction.
@skeeto
skeeto / Benchmark.java
Created Apr 23, 2021
EnumTest benchmark
View Benchmark.java
/* EnumSet benchmark
* $ javac Benchmark.java
* $ java -ea Benchmark
* Ref: https://nullprogram.com/blog/2021/04/23/
*/
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
@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)