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 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 |
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 <stddef.h> | |
static inline void *memclear(void *dest, size_t len){ | |
asm inline volatile("call memset" | |
: | |
:"r"(dest), | |
"r"(0), | |
"r"(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
#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; |
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
// Must be compiled with optimizations enabled, all options except -O0 are supported | |
#include <stddef.h> | |
#include <inttypes.h> | |
// Injects a function in-between the current function and its caller, | |
// injections are visited in reverse order, see example. | |
[[gnu::always_inline]] static inline void inject(void *addr){ | |
asm("call .+5"); | |
void * volatile *p = __builtin_frame_address(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
// 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; \ |
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
// 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; \ | |
}) |
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
[[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 | |
} |
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> | |
#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; |
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> | |
#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; |
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 <unistd.h> // STDIN_FILENO | |
#include <termios.h> //tcsetattr/tcgetattr | |
#include <sys/ioctl.h> // ioctl | |
int kbhit(){ | |
struct termios term; | |
tcgetattr(STDIN_FILENO, &term); | |
struct termios term2 = term; | |
term2.c_lflag &= ~ICANON; // disable canonical mode | |
tcsetattr(STDIN_FILENO, TCSANOW, &term2); |
OlderNewer