Skip to content

Instantly share code, notes, and snippets.

// $ cc -g3 -fsanitize=thread,undefined test.c
//
// No data races detected because the mutex is unnecessary. To verify TSan
// is indeed working, modify newqueue to initialize improperly, such as
// setting a zero field to non-zero. This will cause a data race.
#define lenof(a) (int)(sizeof(a) / sizeof(*(a)))
typedef struct {
int count;
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define S(s) (Str){s, sizeof(s)-1}
#define new(a, n, t) (t *)alloc(a, n, sizeof(t), _Alignof(t))
global _start
_start:
mov rdi, rsp
xor r10d, r10d
lea r13, [rel nl]
push r12
push rbp
push rbx
mov rbx, rdi
sub rsp, 1040
// 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 27, 2025 16:34
N3037 showcase
// Requires GNU C23: GCC >= 15 or Clang >= 21
#include <stddef.h>
#include <string.h>
#define affirm(c) while (!(c)) 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}
@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;