Skip to content

Instantly share code, notes, and snippets.

@jserv
jserv / amei.c
Created March 16, 2026 09:08
aMEI face decoder
/* Amei face decoder: gcc -O2 amei.c -lm && ./a.out > amei.png */
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define foreach(a, d) for (int a = 0; a < d; a++)
#define A(v) ((v) < 0 ? -(v) : (v))
#define C8(v) ((v) < 0 ? 0 : (v) > 255 ? 255 : (v))
static const uint32_t ct[] = { /* Streaming PNG: 16-entry nibble CRC */
0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4,
@jserv
jserv / setup.sh
Created February 21, 2026 16:16
Run Lotus 1-2-3 on macOS + Apple Silicon
#!/usr/bin/env bash
# setup.sh - Bootstrap Lotus 1-2-3 on macOS arm64 via Blink
# Authored by Jim Huang <jserv@ccns.ncku.edu.tw>
#
# Downloads the prebuilt i386 binary from GitHub, builds Blink,
# populates the sysroot from Ubuntu archive i386 .debs, validates the layout,
# and generates run-123.sh. No SSH/scp needed -- everything from public URLs.
#
set -euo pipefail
@jserv
jserv / main.c
Created November 17, 2024 18:53
Object-Oriented Programming (OOP) in C: Render pixel effects using SDL2
/*
* This program demonstrates a simple Object-Oriented Programming (OOP)
* approach in C using function pointers and structures. It renders pixel
* effects onto an RGBA32 buffer using SDL2 for window management and display.
*
* Supported effects:
* - Bitwise operations pattern
* - Mandelbrot fractal visualization
*
* Key controls:
@jserv
jserv / main.c
Created November 17, 2024 17:23
Conway's Game of Life using SDL2 in C99
/*
* Conway's Game of Life using SDL2 in C99
*
* The grid size is dynamically scaled, and the cells are displayed in two
* states: dead and alive. The application uses a double-buffered grid to
* update the cell states based on the classic rules of Conway's Game of Life:
* - Any live cell with fewer than two live neighbors dies (underpopulation).
* - Any live cell with two or three live neighbors lives on to the next
* generation.
* - Any live cell with more than three live neighbors dies (overpopulation).
@jserv
jserv / readers.c
Last active March 24, 2022 02:31
user-level threads using clone system call (incomplete)
#if !defined(__x86_64__)
#error "This program only works for x86_64"
#endif
#define _GNU_SOURCE
#include <errno.h>
#include <limits.h>
#include <linux/futex.h>
#include <sched.h>
#include <signal.h>
@jserv
jserv / Makefile
Created August 20, 2021 12:05
lock-free hashmap
.PHONY: all clean
TARGET = test-hashmap
all: $(TARGET)
include common.mk
CFLAGS = -I.
CFLAGS += -O2 -g
CFLAGS += -std=gnu11 -Wall
@jserv
jserv / Makefile
Created August 20, 2021 11:49
Scheduler Plugin for Linux Kernel
obj-m += proc_queue.o
obj-m += proc_sched.o
obj-m += proc_set.o
PWD := $(shell pwd)
KERNELDIR ?= /lib/modules/`uname -r`/build
PWD := $(shell pwd)
all:
@jserv
jserv / norcu.patch
Created August 19, 2021 02:09
No RCU
diff --git a/fs/Kconfig b/fs/Kconfig
index c229f82..515f76d 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -4,6 +4,12 @@
menu "File systems"
+config DCACHE_NO_RCU
+ def_bool y
@jserv
jserv / Makefile
Last active August 1, 2022 07:26
Lock-free multiple-producer (MP) /multiple-consumer (MC) ring buffer (**incomplete**)
CC = gcc
CFLAGS = -O2 -g -Wall -I.
CFLAGS += -fsanitize=thread
LDFLAGS = -fsanitize=thread
all: lfring
# Control the build verbosity
ifeq ("$(VERBOSE)","1")
Q :=
@jserv
jserv / Makefile
Created August 13, 2021 10:26
Synthesize events for select/poll/epoll (**incomplete***)
MODULENAME := vpoll
obj-m += $(MODULENAME).o
$(MODULENAME)-y += module.o
KERNELDIR ?= /lib/modules/`uname -r`/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
gcc -Wall -o user user.c