From 08cb8d6a585dd552b69a488d9db1be123f01467a Mon Sep 17 00:00:00 2001 From: tech kaczynski Date: Tue, 22 Sep 2026 22:38:57 +1000 Subject: initial commit. --- common.h | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 common.h (limited to 'common.h') diff --git a/common.h b/common.h new file mode 100644 index 0000000..a33e3c5 --- /dev/null +++ b/common.h @@ -0,0 +1,111 @@ +#ifndef COMMON_H +#define COMMON_H + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "ext/hashmap.h" +#include "ext/fuzzy_match.h" +#include "ext/tinyfiledialogs/tinyfiledialogs.h" + +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define CLAMP(x, lo, hi) ((x) < (lo) ? (lo) : ((x) > (hi) ? (hi) : (x))) + +#define BIT(x) (1ULL << (x)) +#define FLAG_SET(n, f) ((n) |= (f)) +#define FLAG_CLEAR(n, f) ((n) &= ~(f)) +#define FLAG_TOGGLE(n, f) ((n) ^= (f)) +#define FLAG_CHECK(n, f) ((n) & (f)) + +#define BYTES_TO_KB(b) ((b) / 1024) +#define BYTES_TO_MB(b) ((b) / (1024 * 1024)) +#define KILOBYTES(kb) ((kb) * 1024) +#define MEGABYTES(mb) ((mb) * 1024 * 1024) + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + +typedef float f32; +typedef double f64; +typedef const char *cstr; + +typedef u8 Flags8; +typedef u32 Flags32; +typedef u64 Flags64; + +typedef Rectangle Rect; +typedef Vector2 Vec2; +typedef Vector3 Vec3; + +/* POSIX/LINUX SPECIFIC PERFORMANCE MEASURE STUFF, DELETE LATER */ +#include + +static inline double +timespec_seconds(struct timespec start, struct timespec end) { + return (double)(end.tv_sec - start.tv_sec) + + (double)(end.tv_nsec - start.tv_nsec) / 1000000000.0; +} + +#define PERF(...) \ + do { \ + struct timespec perf_start_; \ + struct timespec perf_end_; \ + double perf_elapsed_; \ + \ + clock_gettime(CLOCK_MONOTONIC, &perf_start_); \ + __VA_ARGS__; \ + clock_gettime(CLOCK_MONOTONIC, &perf_end_); \ + \ + perf_elapsed_ = timespec_seconds(perf_start_, perf_end_); \ + fprintf(stderr, "%s: %.9f s (%.3f ms)\n", #__VA_ARGS__, perf_elapsed_, \ + perf_elapsed_ * 1000.0); \ + } while (0) + +#define PERF_N(count, ...) \ + do { \ + struct timespec perf_start_; \ + struct timespec perf_end_; \ + double perf_elapsed_; \ + size_t perf_count_ = (count); \ + \ + clock_gettime(CLOCK_MONOTONIC, &perf_start_); \ + for (size_t perf_i_ = 0; perf_i_ < perf_count_; perf_i_++) { \ + __VA_ARGS__; \ + } \ + clock_gettime(CLOCK_MONOTONIC, &perf_end_); \ + \ + perf_elapsed_ = timespec_seconds(perf_start_, perf_end_); \ + fprintf(stderr, \ + "%s:\n" \ + "\t%zu calls\n" \ + "\t%.9f s total\n" \ + "\t%.3f ns per call\n", \ + #__VA_ARGS__, perf_count_, perf_elapsed_, \ + perf_elapsed_ * 1000000000.0 / (double)perf_count_); \ + } while (0) + +#endif -- cgit v1.2.3