Skip to content

Instantly share code, notes, and snippets.

View m1lkweed's full-sized avatar

m1lkweed

View GitHub Profile
@m1lkweed
m1lkweed / py_else.c
Last active May 24, 2024 06:31
Porting python's `for`/`else` to C with no `setjmp`.
#include <stdio.h>
#define py_for(...) for(int loop_flag = 2, break_called = 0; loop_flag && !break_called; --loop_flag)if((loop_flag == 2) && !break_called)for(__VA_ARGS__, break_called = 0)if((break_called = 1), 0){}else
#define py_whl(...) py_for(;__VA_ARGS__;(void)0)
int main(){
py_for(int i = 0; i < 5; ++i){
printf("%d\n", i);
}else{
puts("egg");
@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 May 24, 2024 06:31
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, \
@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 May 26, 2024 04:46
`for each` macro in standard C that works on all types
#include <stdio.h>
// Enhances `for` to automatically iterate over an array.
#define each(item, ...) (int foreach_break_called_p = 2; (foreach_break_called_p == 2);) \
for(extern void *(memcpy)(void *restrict, const void *restrict, typeof(sizeof 0)); (foreach_break_called_p == 2);) \
for(typeof(*(__VA_ARGS__)) *foreach_curr_ptr = (__VA_ARGS__), *foreach_base_ptr = foreach_curr_ptr, (item) = {}; \
(foreach_curr_ptr < &((foreach_base_ptr)[sizeof(__VA_ARGS__) / sizeof(*(__VA_ARGS__))])) && \
((foreach_break_called_p = (foreach_break_called_p == 2)?0:foreach_break_called_p), 1); \
(memcpy)(foreach_curr_ptr++, &(item), sizeof(item))) if(((memcpy)(&(item), foreach_curr_ptr, sizeof(item))), 0){}else
int main(){
@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){