Skip to content

Instantly share code, notes, and snippets.

View m1lkweed's full-sized avatar

m1lkweed

View GitHub Profile
#include <stdio.h>
#define SIMD_SSE 0x1
#define SIMD_SSE2 0x2
#define SIMD_SSE3 0x4
#define SIMD_SSE4_1 0x8
#define SIMD_SSE4_2 0x10
#define SIMD_AVX 0x20
#define SIMD_AVX2 0x40
#define SIMD_AVX512F 0x80
@m1lkweed
m1lkweed / memclear.c
Created April 7, 2022 12:45
Guarantee a call to memset, even when optimizing
#include <stddef.h>
static inline void *memclear(void *dest, size_t len){
asm inline volatile("call memset"
:
:"r"(dest),
"r"(0),
"r"(len)
:
);
@m1lkweed
m1lkweed / fatptr.c
Last active May 14, 2022 04:27
Pointers that store array size data, x86_64 only; not recommended for use.
#include <inttypes.h>
#define IS_ARRAY(x) _Generic(&x, default: 1, typeof(*x)**: 0)
#define COUNT_OF(x) _Generic(&x, default: (sizeof(x) / sizeof(0[x])), typeof(*x)**: 0)
#define array_to_fatptr(x) ((fatptr_t){.addr = x, .n_elems = (int16_t)COUNT_OF(x)})
#define fatptr_to_ptr(x, type) ((type*)(intptr_t)x.addr)
typedef union fatptr_t{
void *ptr;
@m1lkweed
m1lkweed / preserve_dead_code.c
Created May 19, 2022 11:35
Preserve dead code during compilation
// works on all architectures and optimization levels in GCC and clang
// Dead code is optimized like running code and may be removed if the
// compiler can prove that no branches will ever reach it.
#define preserve_dead_code(body) ({ \
__label__ label; \
asm goto(""::::label); \
if(0)label:{body} \
&&label; \
})
@m1lkweed
m1lkweed / asm_sizeof.c
Last active May 19, 2022 18:58
Evaluate a body of code and return its size
// Works in GCC and clang, produces correct results at all optimization levels
// returns the contents of the instruction pointer
#define get_pc() ({ \
register void *temp; \
asm volatile("call .+5\n\t" \
"pop %0" \
:"=r"(temp) \
); \
temp; \
@m1lkweed
m1lkweed / on_stack.c
Last active June 26, 2022 04:46
Returns true for addresses on the stack
[[gnu::noinline]] _Bool on_stack(void *addr){
char stack_check[1];
return (addr >= (void*)&stack_check[1]); // stack_check[1] is OoB but it produces better assembly
}
@m1lkweed
m1lkweed / get_pos.c
Created July 14, 2022 16:26
Get the position of the tty cursor
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int get_pos(int *col, int *line){
char buf[30]={0};
int ret, i, pow;
char ch;
*line = 0;
*col = 0;
@m1lkweed
m1lkweed / is_real_tty.c
Last active July 15, 2022 03:37
Returns true when running under a TTY, false under a VT
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <linux/kd.h>
#include <sys/ioctl.h>
static int is_a_console(int fd){
char arg = 0;
@m1lkweed
m1lkweed / sizeof.c
Created July 26, 2022 05:20
sizeof implemented as a macro
#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));
@m1lkweed
m1lkweed / rand_double.c
Last active September 8, 2022 04:51
Generates a random-enough double in the range [0, 1)
#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;