Skip to content

Instantly share code, notes, and snippets.

@depp
depp / !README.md
Created December 1, 2022 01:12
1-bit Ordered Dithering in WebGL

Dithering

Shows how to get a 1-bit dithering effect in WebGL.

I disclaim copyright on this code, feel free to use it without attribution.

The TypeScript file shows how the texture is generated. The GLSL code shows how it is used in a fragment shader.

@depp
depp / !Readme.md
Created November 13, 2022 07:45
Divide by 240

Fast Division

We want to calculate x/240, and to do that, we calculate (x*34953)>>23. This only works for sufficiently small values of x.

The value of 34953 looks like some kind of magic constant. It is really just ceil((1 << 23)/240)—the value 1/240, scaled up by 2^23. As you use larger scaling factors (above 1<<23), the value becomes more accurate, but you need more bits.

GCC can do this optimization for you under certain circumstances. GCC will convert x * 34953 to the same sequence of shifts and adds, if you are targeting an old processor like a 68000.

@depp
depp / NumToString.c
Last active October 16, 2022 08:08
NumToString
/*
Copyright 2022 Dietrich Epp
This file is released under the terms of the Mozilla Public License, version
2.0.
Fast conversion from binary to decimal! This was originally written for a GBA
project. Note that it's fast if you compile it as ARM code, but if you compile
it as Thumb code, it will be significantly slower. The reason is because the
division operation can be optimized out in ARM. In ARM, GCC will replace the
@depp
depp / Makefile
Last active October 2, 2022 04:50
Parser
CFLAGS += -O2 -Wall -Wextra -std=c17 -D_DEFAULT_SOURCE
scanner: scanner.o
$(CC) -o $@ $^
scanner.c: scanner.l
flex -o$@ $<
clean:
rm -f scanner.o scanner.c scanner
.PHONY: clean
@depp
depp / scanner.l
Created October 2, 2022 04:21
Simple scanner
%{
#include <stdio.h>
#include <stdlib.h>
enum {
End,
Comma,
Equals,
Semicolon,
Number,
@depp
depp / MacReader.c
Created April 10, 2022 15:23
Classic Mac file reading
// Location of the application: volume & directory, or 0 if unknown.
static short gAppVolRefNum;
static long gAppParID;
// Get an FSSpec pointing to a file with the given name.
static OSErr GetDataFile(FSSpec *spec, const char *filename) {
ProcessSerialNumber psn = {0, kCurrentProcess};
ProcessInfoRec info = {0};
FSSpec app_spec;
Str255 pname;
@depp
depp / problem.cpp
Last active December 21, 2021 19:04
A Faster Solution
// Faster solution for:
// http://www.boyter.org/2017/03/golang-solution-faster-equivalent-java-solution/
// With threading.
// g++ -std=c++11 -Wall -Wextra -O3 -pthread
// On my computer (i5-6600K 3.50 GHz 4 cores), takes about ~160 ms after the CPU
// has warmed up, or ~80 ms if the CPU is cold (due to Turbo Boost).
// How it works: Start by generating a list of losing states -- states where the
// game can end in one turn. Generate a new list of states by running the game
@depp
depp / ctl.py
Created November 10, 2020 06:21
Nintendo 64 CTL Dumper
"""Dump a Nintendo 64 .ctl file to text.
Usage: python3 ctl.py <file.ctl>
"""
import argparse
import struct
import sys
SPACE = ' ' * 80
def pr(indent, *msg):
@depp
depp / Makefile
Created June 25, 2021 14:24
Injection Demo
all: inject.dylib main
inject.dylib: inject.c
cc -dynamiclib $^ -o $@
main: main.c
cc $^ -o $@

Sampling Uniformly in a Sphere

  • Method 1: rejection sampling: sample uniformly in a cube, then reject points outside the unit sphere.

  • Method 2: polar sampling: use polar coordinates to directly generate points uniformly in the sphere

There are two implementations here. One in JavaScript, one in C. Which method is faster is different depending on the language!

Results (JavaScript)