Skip to content

Instantly share code, notes, and snippets.

// https://old.reddit.com/r/C_Programming/comments/1l1hxeu
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define S(s) (Str){s, sizeof(s)-1}
typedef struct {
@skeeto
skeeto / demo.c
Last active June 1, 2025 20:33
N3037 showcase
// Requires GNU C23: GCC >= 15 or Clang >= 21
#define affirm(c) while (!(c)) __builtin_unreachable()
#define lenof(a) (iz)(sizeof(a) / sizeof(*(a)))
#define new(a, n, t) (t *)alloc(a, n, sizeof(t), _Alignof(t))
#define maxof(t) ((t)-1<1 ? (((t)1<<(sizeof(t)*8-2))-1)*2+1 : (t)-1)
#define S(s) (Str){(u8 *)s, lenof(s)-1}
typedef unsigned char u8;
typedef int i32;
@skeeto
skeeto / svg.c
Created May 29, 2025 00:12
SVG color lookup tables
#include <stdint.h>
// Return the RGB value of the named color. Does not validate that the
// input matches a real color name.
int32_t svg2rgb(char *s)
{
static int32_t t[1024] = {
[ 0]=0x9acd32, [ 4]=0xa9a9a9, [ 16]=0x808080, [ 18]=0xda70d6,
[ 21]=0x40e0d0, [ 26]=0xffe4b5, [ 27]=0xf5fffa, [ 29]=0xa0522d,
[ 38]=0xfaf0e6, [ 41]=0x2f4f4f, [ 43]=0x6a5acd, [ 55]=0xb22222,
@skeeto
skeeto / build.sh
Created May 22, 2025 19:39
Staz Wasm
#!/bin/sh
set -ex
clang --target=wasm32 -nostdlib -O2 -fno-builtin -Wl,--no-entry -o staz.wasm wasm.c
@skeeto
skeeto / dualmix128.md
Created May 5, 2025 14:52
Asking GPT 4.1 about a PRNG
@skeeto
skeeto / queue.c
Created April 12, 2025 14:21
Single consumer, single producer circular buffer using Win32 SRW
// Single consumer, single producer circular buffer using Win32 SRW
// Ref: https://github.com/vibhav950/cbuf
// Ref: https://old.reddit.com/r/C_Programming/comments/1jwnlrf
// Ref: https://nullprogram.com/blog/2024/10/03/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
typedef uint8_t u8;
typedef int32_t b32;
@skeeto
skeeto / path_open.txt
Created April 5, 2025 14:57
Question via Illume
!profile claude
!user
Please give me an example of using WASI `path_open` to open a file for writing. In particular, I don't know where to get the `dirfd` parameter.
!assistant
# Example of using WASI `path_open` to open a file for writing
The WASI `path_open` function requires a directory file descriptor (`dirfd`) as its first parameter because WASI uses a capability-based security model. Here's how to use it:
@skeeto
skeeto / example.c
Last active March 17, 2025 22:34
Robust Lock?
// Robust Lock: if a "thread" exits while holding a lock, it is unlocked
// linux $ cc -pthread example.c
// w64dk $ cc example.c -lntdll
// Ref: https://old.reddit.com/r/C_Programming/comments/1jd82ux
// Ref: https://github.com/cozis/timestamp_lock
// This is free and unencumbered software released into the public domain.
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
// https://old.reddit.com/r/C_Programming/comments/1iljkn2
// https://github.com/jamesnolanverran/dmap
#include "dmap.c"
#include <string.h>
#define KEYLEN 8
#define RUNS 10
#define KEYS 1000000
static void encode(char *key, int x)