Skip to content

Instantly share code, notes, and snippets.

View m1lkweed's full-sized avatar

m1lkweed

View GitHub Profile
@m1lkweed
m1lkweed / memtools.c
Created May 4, 2024 02:57
Handy memory-manipulation functions that don't exist in the standard
#include <stdint.h>
#include <string.h>
// Copy a byte pattern into memory, repeating src if dest_len > src_len
void *memwrap(void *dest, const void *src, size_t dest_len, size_t src_len){
if((dest == NULL) || (src == NULL) || (dest_len == 0U) ||
(src_len == 0U) || (dest == src)){
goto return_early;
}
if(dest_len <= src_len){
@m1lkweed
m1lkweed / un_builtin.c
Last active April 26, 2024 14:05
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(x)*){}), typeof(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, \
@m1lkweed
m1lkweed / map.c
Created March 12, 2024 19:33
Type-generic one-line `map` macro in GNU C
#define map(f,a,...) _Pragma("push_macro(\"map\")")_Pragma("pop_macro(\"map\")")f(a)__VA_OPT__(,map(f,__VA_ARGS__))
// Example
#include <stdio.h>
int square(int a){
return a * a;
}
int main(){
@m1lkweed
m1lkweed / hyperloglog.c
Created February 29, 2024 07:28
HyperLogLog in C
// (c)m1lkweed 2024 GPLv3+
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h> // for rand()
uint64_t hash(void *in_data, size_t len){
char *data = in_data;
uint64_t h = 0x100;
for(size_t i = 0; i < len; ++i){
@m1lkweed
m1lkweed / py_range.c
Last active March 3, 2024 07:42
Type-generic `range()` macro in GNU C, inspired by Python
#define COUNTOF(x) (sizeof(x) / sizeof*(x))
#define NEEDED_SIZE_HELPER(start, stop, step, X) ( \
((((start) > (stop))?(start):(stop)) - \
(((start) < (stop))?(start):(stop))) / ((step) X) \
)
#define NEEDED_SIZE(start, stop, step) ( \
NEEDED_SIZE_HELPER(start, stop, step,) + \
((NEEDED_SIZE_HELPER(start, stop, step, + 0.) - \
@m1lkweed
m1lkweed / for_each.c
Last active April 3, 2024 21:53
`for each` macro in standard C that works on all types
#include <stdio.h>
// Enhances `for` to automatically iterate over an array. Item cannot be used to modify the contents of array.
#if __STDC__ < 202311L
#define each(item, ...) \
(int no_loops = 1, break_p = 2; (no_loops >= 1) && (break_p == 2);) \
for(typeof(*(__VA_ARGS__)) *foreach_p = (__VA_ARGS__), *foreach_p2 = foreach_p, (item) = {}; \
(foreach_p < &((foreach_p2)[sizeof(__VA_ARGS__) / sizeof(*(__VA_ARGS__))])) && \
((break_p = (break_p == 2)?0:break_p), no_loops); ++foreach_p) \
if((__builtin_memcpy(&(item), foreach_p, sizeof(item))), 0){}else
#else
@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)) + \
@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 / 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);
}
// 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){