This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[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" | |
: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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__)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NaN boxing in C | |
// (c)m1lkweed 2022 | |
// GPLv3+ | |
#include <math.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <stdbool.h> | |
#define MASK_TYPE (0x0007000000000000UL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)) + \ |