Skip to content

Instantly share code, notes, and snippets.

View louisswarren's full-sized avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / asm_diff.diff
Last active February 13, 2024 08:21
Type-checked units in C
--- units.asm 2023-06-18 12:02:32.313569952 +1200
+++ units_unsafe.asm 2023-06-18 12:02:32.313569952 +1200
@@ -1,7 +1,7 @@
-metres_from_feet(feet):
+metres_from_feet(double):
divsd xmm0, QWORD PTR .LC0[rip]
ret
-feet_from_metres(metres):
+feet_from_metres(double):
mulsd xmm0, QWORD PTR .LC0[rip]
@louisswarren
louisswarren / easyarg.py
Created February 5, 2024 08:23
Map command line arguments to function arguments in python
import sys
def foo(*args, **kwargs):
argstr = list(map(repr, args))
kwargstr = [f'{key}={repr(value)}' for key, value in kwargs.items()]
print("foo(", ", ".join(argstr + kwargstr), ")", sep="")
def easyarg(f):
opts = {}
for i in range(1, len(sys.argv)):
@louisswarren
louisswarren / graphs.mac
Last active January 7, 2024 14:46
Graphs in maxima
anyp(pred, seq) := lreduce(lambda([P, x], P or pred(x)), seq, false);
allp(pred, seq) := lreduce(lambda([P, x], P and pred(x)), seq, true);
walklength(seq) := (length(seq)-1)/2;
walkverts(seq) := makelist(seq[2*i-1], i, 1, walklength(seq)+1);
walkedges(seq) := makelist(seq[2*i], i, 1, walklength(seq));
closedp(seq) := is(first(seq) = last(seq));
vertexset(G) := first(G);
edgeset(G) := second(G);
@louisswarren
louisswarren / Makefile
Last active December 24, 2023 02:05
Fullwidth text conversion without libc
CFLAGS = -ffreestanding -fno-stack-protector
LDFLAGS = -nostdlib -static
fullwidth-freestanding: fullwidth-freestanding.o
fullwidth-freestanding.o: fullwidth-freestanding.c
@louisswarren
louisswarren / conn4.c
Last active December 14, 2023 08:09
Simulate connect 4
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define ENABLE_MOVE_COUNTER
struct game {
uint8_t col[2][7];
@louisswarren
louisswarren / Makefile
Last active September 3, 2023 03:10
PSX emulation
CFLAGS = -Wall -Warray-bounds=2 -Wcast-align=strict -Wcast-qual -Wconversion -Wno-sign-conversion -Wdangling-else -Wdate-time -Wdouble-promotion -Wextra -Wfloat-conversion -Wformat-overflow=2 -Wformat-signedness -Wformat-truncation=2 -Wformat=2 -Winit-self -Wjump-misses-init -Wlogical-op -Wmissing-include-dirs -Wnested-externs -Wnull-dereference -Wpacked -Wpedantic -Wredundant-decls -Wshadow -Wshift-negative-value -Wshift-overflow=2 -Wstrict-aliasing -Wstrict-overflow=2 -Wstrict-prototypes -Wstringop-overflow=4 -Wstringop-truncation -Wswitch-default -Wswitch-enum -Wuninitialized -Wunsafe-loop-optimizations -Wunused -Wuse-after-free=3 -Wwrite-strings -fanalyzer -fmax-errors=2 -pedantic-errors
.PHONY: default
default: test
.PHONY: test
test: xods
./$< < /dev/null | diff expected.log - | head -n40
xods.o: xods.c instructions.h
@louisswarren
louisswarren / tabulate.py
Last active September 3, 2023 01:00
Tabulation in python, in place of spreadsheets
class NotApplicable:
def __repr__(self):
return 'NA'
def __str__(self):
return ''
def __add__(self, other): return self
def __sub__(self, other): return self
def __mul__(self, other): return self
@louisswarren
louisswarren / fib.py
Last active August 11, 2023 11:19
How many steps does it take to compute the fibbonacci sequence recursively?
def fib_recursive(n, _mem={1: (1, 1), 2: (1, 1)}):
if n not in _mem:
a, x = fib_recursive(n - 2)
b, y = fib_recursive(n - 1)
_mem[n] = (a + b), (x + y + 1)
return _mem[n]
for i in range(1, 101):
f, n = fib_recursive(i)
print(f"{i:>3}: {f:>21} ({n:>21} steps)")
@louisswarren
louisswarren / omp.c
Created August 4, 2023 06:40 — forked from milesrout/omp.c
// Hello
@louisswarren
louisswarren / Makefile
Last active June 12, 2023 08:39
ctypes example
.PHONY: default
default: out.png
%.png: %.ppm
convert $< $@
out.ppm: draw.py image.so
python3 $< > $@
image.so: image.c