Skip to content

Instantly share code, notes, and snippets.

View m1lkweed's full-sized avatar

m1lkweed

View GitHub Profile
@m1lkweed
m1lkweed / better_container_of.c
Last active September 17, 2022 02:47
A drop-in replacement for the linux container_of macro that doesn't require GNU extensions
#define offsetof(type, member) ((size_t)&((type*)0)->member)
#define container_of(ptr, type, member) (typeof(type)*)((char*)ptr - offsetof(typeof(type), member))
#include <stdio.h>
int main(){
struct foo{
int a;
int b;
}bar;
@m1lkweed
m1lkweed / sizeof.c
Created July 26, 2022 05:20
sizeof implemented as a macro
#define sizeof(x) ((size_t)(((typeof(x)*)0)+1))
#include <stdio.h>
struct foo{
char a,b,c,d,e;
};
int main(){
printf("%zu\n", sizeof(int));
@m1lkweed
m1lkweed / zero_redzone.c
Last active November 2, 2022 19:22
Leaf function that sets all bytes in the redzone to 0
[[gnu::naked, gnu::noinline]] void zero_redzone(){
__asm volatile("endbr64\n\t"
"lfence\n\t"
"mov $0x10, %%eax\n\t"
"pushq $0x0\n\t"
"dec %%eax\n\t"
"jne .-4\n\t"
"addq $128, %%rsp\n\t"
"ret"
:
@m1lkweed
m1lkweed / rand_double.c
Last active September 8, 2022 04:51
Generates a random-enough double in the range [0, 1)
#include <stdint.h>
static uint64_t simple_rnd_state = 0xcafef00dd15ea5e5u;
// returns a random 64-bit value
uint64_t simple_rng(void){
uint64_t x = simple_rnd_state;
simple_rnd_state = x * 6364136223846793005u;
x ^= x >> 22;
return x;
@m1lkweed
m1lkweed / optimizers.c
Last active March 26, 2023 09:31
A few macros that can help with optimizing code and speed
#if defined __GNUC__
// x is the likely statement, such as (a < 5).
// The optional second value is the probability that
// x is true, represented as a double from 0.0 to 1.0
#define likely(x, ...) (__builtin_expect ## __VA_OPT__(_with_probability)((x), 1, ##__VA_ARGS__))
// x is the unlikely statement, such as (a < 5).
// The optional second value is the probability that
// x is false, represented as a double from 0.0 to 1.0
#define unlikely(x, ...) (__builtin_expect ## __VA_OPT__(_with_probability)((x), 0, ##__VA_ARGS__))
@m1lkweed
m1lkweed / xsetjmp.c
Last active November 20, 2023 02:35
Assembly-free setjmp in C. Compile with `-znoexecstack` on any optimization level
#pragma once
// (c) M1lkweed, 2022-2023
// Released as GPLv3+
// Differences from normal setjmp:
// 1.) volatile is not required to prevent clobbering
// 2.) sizeof(xjmp_buf) is 8
// 3.) xlongjmp can be used more than once per buffer
// 4.) reading the return value of xsetjmp is not UB
// 5.) all of the points also apply to the sig*jmp family
// The following codes and algorithms have all been patented at some point, and some still are.
#define MICROSOFT_PATENTED_OPERATOR(x, y) (&(x) != &(y))
// The address-wise ISNOT operator is patent-protected for BASIC compilers
// by Microsoft until 2024. US Patent #20040230959
// https://image-ppubs.uspto.gov/dirsearch-public/print/downloadPdf/20040230959
typedef ptrdiff_t selfptr_t;
static inline selfptr_t set_selfptr(selfptr_t *ptr, void *addr){
@m1lkweed
m1lkweed / always_assert.c
Created February 17, 2023 20:23
Macro that tries to check assertions at runtime and compile-time
// (c) M1lkweed, 2022-2023
// GPLv3+
#include <assert.h>
#define ALWAYS_ASSERT(cond, ...) ({static_assert(__builtin_constant_p((cond))?(cond):1,##__VA_ARGS__);assert((cond),##__VA_ARGS__);})
int main(int argc, char**){
ALWAYS_ASSERT(argc > 2);
}
@m1lkweed
m1lkweed / nan_box.c
Created June 23, 2023 05:09
A quick and dirty nan boxing implementation
// NaN boxing in C
// (c)m1lkweed 2022
// GPLv3+
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#define MASK_TYPE (0x0007000000000000UL)
@m1lkweed
m1lkweed / date_parse.c
Created January 7, 2024 22:01
Compile-time date parser, converts `__DATE__` to ints
#include <stdio.h>
#define pp_current_build_year (((__DATE__[7] - '0') * 1000) + ((__DATE__[8] - '0') * 100) + ((__DATE__[9] - '0') * 10) + (__DATE__[10] - '0'))
#define pp_current_build_day ((((__DATE__[4] - ' ') % 16) * 10) + (__DATE__[5] - '0'))
#define MONTH_CMP(b) ((__DATE__[0] == (b)[0]) && (__DATE__[1] == (b)[1]) && (__DATE__[2] == (b)[2]))
#define pp_current_build_month ( \
(MONTH_CMP("Jan")*( 1)) + \
(MONTH_CMP("Feb")*( 2)) + \
(MONTH_CMP("Mar")*( 3)) + \
(MONTH_CMP("Apr")*( 4)) + \