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> | |
#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){ |
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
// 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, \ |
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 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(){ |
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 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){ |
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 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.) - \ |
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> | |
// 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 |
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)) + \ |
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
// (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
// 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){ |
NewerOlder