Skip to content

Instantly share code, notes, and snippets.

View garettbass's full-sized avatar
🤓

Garett Bass garettbass

🤓
View GitHub Profile
@garettbass
garettbass / link.cpp
Created December 23, 2019 19:15
A simple embedded link and list
@garettbass
garettbass / clang_sections.c
Last active June 23, 2020 20:28
Enumerating section symbols with clang/gcc (https://repl.it/@garettbass/clangsections)
#include <stdio.h>
#define CONCAT(A, B) _CONCAT(A, B)
#define _CONCAT(A, B) A##B
#define TOSTRING(...) _TOSTRING(__VA_ARGS__)
#define _TOSTRING(...) #__VA_ARGS__
#ifdef SECTION_DEFINITIONS
#define _section_declaration_definition(SECTION) section_definition(SECTION)
@garettbass
garettbass / msvc_sections.c
Last active December 11, 2019 18:37
Enumerating section symbols with MSVC (https://rextester.com/TOUNK25484)
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
#include <stdio.h>
#define CONCAT(A, B) _CONCAT(A, B)
#define _CONCAT(A, B) A##B
#define TOSTRING(...) _TOSTRING(__VA_ARGS__)
#define _TOSTRING(...) #__VA_ARGS__
@garettbass
garettbass / bitset.h
Last active May 6, 2021 16:45
A generic-length bitset in C
#pragma once
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
//------------------------------------------------------------------------------
typedef uint64_t bitset_block_t;
@garettbass
garettbass / stlcost.cpp
Created August 11, 2019 18:15
stlcost.cpp
/*
c++ --version; c++ -std=c++14 -g stlcost.cpp && ./a.out && rm ./a.out
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
iteration 0 -----------------------------------------
malloc():
@garettbass
garettbass / swap.c
Created July 15, 2019 19:22
A simple in-place swap macro for C
#define swap(a, b) do {\
typedef struct { char data[sizeof(a) == sizeof(b) ? sizeof(a) : -1]; } swap;\
swap* const aa = (swap*)&(a);\
swap* const bb = (swap*)&(b);\
swap const cc = *aa; *aa = *bb; *bb = cc;\
} while(0)
@garettbass
garettbass / static_hash.h
Last active May 30, 2019 18:26
Compile time fnv1a 32- and 64-bit hashing of up to 15 characters in C
#pragma once
//------------------------------------------------------------------------------
#define FNV1A32_BASIS (2166136261u)
#define FNV1A32_PRIME (16777619u)
#define FNV1A64_BASIS (14695981039346656037ull)
#define FNV1A64_PRIME (1099511628211ull)
@garettbass
garettbass / array.h
Last active May 30, 2019 23:27
Some utilities for C arrays
#pragma once
#include <assert.h>
#define must_be_array(a)\
_Pragma("GCC diagnostic push")\
_Pragma("GCC diagnostic error \"-Wincompatible-pointer-types\"")\
((void)sizeof(((char(*)(typeof(a[0])(*)[sizeof(a)/sizeof(a[0])]))0)(&a)))\
_Pragma("GCC diagnostic pop")\
// returns the length of the provided array
@garettbass
garettbass / echo.h
Last active August 4, 2019 01:45
echo(expr) prints the expression and its value
#pragma once
#ifdef __cplusplus
#include <cstdio>
static inline int
_echo(const char* x, bool v)
{ return printf("%s: %s", x, v?"true":"false"); }
@garettbass
garettbass / seq.h
Last active May 29, 2019 16:32
Simple sequence manipulation
#pragma once
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#if defined(_WIN32)
#include <malloc.h>