Skip to content

Instantly share code, notes, and snippets.

@skeeto
skeeto / 2pow.c
Last active October 29, 2023 15:45
2^1000000
enum {
BASE = 1000000000, // nine decimals per limb
NUMCAP = 1<<16, // capacity just over a half million decimals
};
typedef struct {
int *digits;
int len;
} num;
@skeeto
skeeto / intset.c
Last active October 26, 2023 14:13
Hash trie iterator example (integer set)
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define assert(c) while (!(c)) *(volatile int *)0 = 0
#define new(a, t) (t *)alloc(a, sizeof(t))
typedef struct {
char *beg, *end;
@skeeto
skeeto / solve.c
Created October 10, 2023 17:13
Transaction counter solver
// Transaction counter
//
// Input: Null-terminated null-terminated string array. Each string has
// three non-negative integers under 1e9: "sender-id receiver-id amount"
// The amount field is unused.
//
// Output: A null-terminated, numerically sorted null-terminated string
// array of ids that exceeded a given transaction count threshold.
//
// Ref: https://old.reddit.com/r/C_Programming/comments/174dwcb
@skeeto
skeeto / write_test.go
Created September 28, 2023 20:49
io.Writer to a buffer benchmarks
// $ go test -bench=.
// goos: windows
// goarch: amd64
// pkg: x
// cpu: 12th Gen Intel(R) Core(TM) i9-12900
// BenchmarkFixed-24 21196731 54.78 ns/op
// BenchmarkVariable-24 10045690 117.7 ns/op
// BenchmarkMappedCold-24 14038420 93.53 ns/op
// BenchmarkMappedWarm-24 21380121 53.21 ns/op
// BenchmarkBytes-24 19775221 82.31 ns/op
@skeeto
skeeto / icosphere.c
Last active September 15, 2023 23:43
// $ eval cc icosphere.c $(pkg-config --cflags --libs sdl2) -lopengl32
// Ref: http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html
// Ref: https://old.reddit.com/r/C_Programming/comments/16ionwe
#include <math.h>
#include <stdint.h>
#include <string.h>
#define new(h, t, n) (t *)alloc(h, sizeof(t), _Alignof(t), n)
typedef struct {
@skeeto
skeeto / bench.c
Created September 14, 2023 02:05
tree benchmarks
// https://old.reddit.com/r/C_Programming/comments/16ho5ql/implementing_and_simplifying_treap_in_c/
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define new(h, t) (t *)alloc(h, sizeof(t), _Alignof(t))
#define assert(c) while (!(c)) __builtin_trap()
@skeeto
skeeto / GNUmakefile
Created September 9, 2023 18:19
Quake2 with w64devkit
CC = cc
CFLAGS = -w -g3 -DC_ONLY -fcommon -DGAME_HARD_LINKED
all: quake2.exe ref_soft.dll
game_obj = \
game/g_ai.o game/g_chase.o game/g_cmds.o game/g_combat.o game/g_func.o \
game/g_items.o game/g_main.o game/g_misc.o game/g_monster.o game/g_phys.o \
game/g_save.o game/g_spawn.o game/g_svcmds.o game/g_target.o \
game/g_trigger.o game/g_turret.o game/g_utils.o game/g_weapon.o \
@skeeto
skeeto / svg.c
Last active September 19, 2023 12:00
SVG generator example
// SVG generator example
// $ cc -DDEMO -o svg svg.c
// $ ./svg >demo.svg
// Large demo:
// $ cc -DBIGDEMO -o svg svg.c
// $ ./svg >bigdemo.svg
// This is free and unencumbered software released into the public domain.
#include <stddef.h>
#include <stdint.h>
@skeeto
skeeto / rand16.c
Last active September 7, 2023 23:55
Arlet Ottens's 16 byte random generator
// Arlet Ottens's 16 byte random generator
// $ cc -O3 -o rand16 rand16.c
// $ ./rand16 | RNG_test stdin32
// https://github.com/Arlet/pseudo-random-number-generator/blob/main/avr/rand16.S
#include <stdint.h>
#include <stdio.h>
uint32_t rand16(uint8_t s[16])
{
uint8_t t, c, r[32];
@skeeto
skeeto / go.mod
Last active August 25, 2023 08:38 — forked from lorenzotinfena/sort.go
Efficient (but ugly) radix sort implementation for uint32
module x
go 1.21.0