Skip to content

Instantly share code, notes, and snippets.

@imaami
imaami / rounding.c
Last active June 10, 2026 21:30
Faithfully replicating a bad way to do rounding
/*
* gcc -O2 -march=native -mtune=native -flto=auto rounding.c -o rounding -lm
*/
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
@m1lkweed
m1lkweed / audit_trail.c
Last active April 7, 2026 22:08
"audit trail" debugging concept for self-documenting programs
/* Whether you want to catch bugs in production
* or just want to help hackers interface with
* your program, sometimes it's nice to have
* your binary explain what it's doing in a
* human-readable way. Audit trails are just
* no-op functions that help you document your
* binary for the sake of future debuggers.
* in gdb:
* b audit_trail
* commands
@BlobTheKat
BlobTheKat / a.h
Last active October 31, 2026 01:25
a.h - C11 Atomics & threads complement library
/**
* Atomics & threads complement library
* Matthew Reiner, 2026
* Available under the MIT license
* Targeting: Windows, Linux, FreeBSD, NetBSD, OpenBSD, MacOS
* Implements the following:
* - Wait/Notify functionality for simple types up to 64 bit
* - Arbitrary condition wait
* - Fine-grained memory ordering
* - Thread management
@m1lkweed
m1lkweed / octets.c
Last active February 2, 2026 04:35
Array of unicode half/quarter/quadrant/eighth/octet characters and braille characters as strings, sanely indexed for terminal pseudographics
// Not sure this can be copyrighted, but if so:
// (c) m1lkweed, 2026, licensed under cc0 where required
// Dedicated to the public domain
typeof(const char[5]) pixels[256] = {
"⠀", "𜺨", "𜺫", "🮂", "𜴀", "▘", "𜴁", "𜴂", "𜴃", "𜴄", "▝", "𜴅", "𜴆", "𜴇", "𜴈", "▀",
"𜴉", "𜴊", "𜴋", "𜴌", "🯦", "𜴍", "𜴎", "𜴏", "𜴐", "𜴑", "𜴒", "𜴓", "𜴔", "𜴕", "𜴖", "𜴗",
"𜴘", "𜴙", "𜴚", "𜴛", "𜴜", "𜴝", "𜴞", "𜴟", "🯧", "𜴠", "𜴡", "𜴢", "𜴣", "𜴤", "𜴥", "𜴦",
"𜴧", "𜴨", "𜴩", "𜴪", "𜴫", "𜴬", "𜴭", "𜴮", "𜴯", "𜴰", "𜴱", "𜴲", "𜴳", "𜴴", "𜴵", "🮅",
"𜺣", "𜴶", "𜴷", "𜴸", "𜴹", "𜴺", "𜴻", "𜴼", "𜴽", "𜴾", "𜴿", "𜵀", "𜵁", "𜵂", "𜵃", "𜵄",
"▖", "𜵅", "𜵆", "𜵇", "𜵈", "▌", "𜵉", "𜵊", "𜵋", "𜵌", "▞", "𜵍", "𜵎", "𜵏", "𜵐", "▛",
@GameDroit
GameDroit / list.c
Last active March 7, 2025 00:53
Curtain Collections List
#include "list.h"
List list_instance(size_t type_size) {
return (List){
.data = malloc(0),
.size = 0,
.type_size = type_size
};
}
@m1lkweed
m1lkweed / for_else.c
Last active July 15, 2025 18:42
Porting python's `for`/`else` to C with no `setjmp`.
#include <stdio.h>
#define py_for(...) _Pragma("GCC diagnostic push")_Pragma("GCC diagnostic ignored \"-Wdangling-else\"") \
for(struct{int count; bool broke; bool once; bool copy;}py_for = {}; \
(py_for.count < 2) && !py_for.broke; ++py_for.count)if(!py_for.count)for(__VA_ARGS__) \
if(py_for.broke){break;}else for(py_for.once = true; (py_for.copy = py_for.broke), \
(py_for.broke = py_for.once), py_for.once && !py_for.copy; py_for.broke = py_for.once = false) \
_Pragma("GCC diagnostic pop")
#define py_whl(...) py_for(;__VA_ARGS__;)
@Eczbek
Eczbek / resources.md
Last active January 27, 2026 14:39
C++ resources and a rearrangement of `learncpp.com`
@eisenwave
eisenwave / pointers.md
Last active June 30, 2026 18:13
How pointers in C++ actually work

How pointers in C++ actually work

Abstract: This document teaches you from the very basics to advanced features such as std::launder how pointers in C++ work. It is aimed at developers of any skill level. However, it links to the C++ standard so that advanced readers can verify the information and investigate further.

Motivation: Most tutorials on pointers are aimed at beginners, and present a simplified model. Some tutorials perpetuate an explanation of pointers that is solely based on their implementation (pointer = memory address). This tutorial aims to provide a comprehensive explanation of pointers that is in line with how they actually work from a language perspective.

@m1lkweed
m1lkweed / un_builtin.c
Last active January 17, 2025 12:56
Replacements for various gcc builtins using c23's `typeof` and c11's `_Generic`
// Compares types of both parameters. Returns true if the types are compatible, otherwise false.
// Parameter y may not be a variably-modified type.
// Designed to be a drop-in replacement for gcc's __builtin_types_compatible_p
#define types_compatible_p(x, y) _Generic(((typeof_unqual(x)*){}), typeof_unqual(y)*:1, default:0)
// Cast the bits of arg to type. Parameter type must be a type and must not be a literal or object with that type.
// Designed to be compatible with g++'s __builtin_bit_cast
#define bit_cast(type, ...) ( \
union{typeof(__VA_ARGS__) in; typeof(type) out; \
int enforce_type:_Generic((int(*)(int(type)))0, \
@eisenwave
eisenwave / case_against_almost_always_auto.md
Last active October 2, 2026 15:18
The case against Almost Always `auto` (AAA)

The case against Almost Always auto (AAA)

Introduction

I've been writing C++ for half a decade now, and auto has always been a great source of discomfort to me. Whenever I came back to a past project that makes extensive use of it, I found myself confused, and first had to look at all the types before I could make sense of it.

Similarly, I've worked as an intern at a company that had a AAA policy for its code base. Whenever something didn't work, and I had to debug some code, half the time was spent just looking up types.