From ba5c0a4579ece5d53c009a14d00e683e70b982f4 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 9 May 2025 18:29:52 +0100 Subject: Initial implementation --- base.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 base.h (limited to 'base.h') diff --git a/base.h b/base.h new file mode 100644 index 0000000..f841328 --- /dev/null +++ b/base.h @@ -0,0 +1,78 @@ +/* Copyright (C) 2025 Aryadev Chavali + + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for + * details. + + * You may distribute and modify this code under the terms of the GNU General + * Public License Version 2, which you should have received a copy of along with + * this program. If not, please go to . + + * Created: 2025-03-31 + * Description: Base + */ + +#ifndef BASE_H +#define BASE_H + +#include +#include +#include +#include +#include + +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; +static_assert(sizeof(f32) == sizeof(u32)); +static_assert(sizeof(f64) == sizeof(u64)); + +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define NTH_BYTE(X, N) (((X) >> (8 * N)) & ((1 << 8) - 1)) +#define ARR_SIZE(XS) (sizeof(XS) / sizeof((XS)[0])) + +#define TODO(MSG) (assert(false && MSG)); + +#ifndef DEBUG +#define DEBUG 2 +#endif + +static inline void debug(char *fmt, ...) +{ +#if DEBUG > 1 + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +#endif +} + +static inline void info(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +} + +static inline void print_bits(u64 w) +{ + for (u64 i = 8 * sizeof(w); i > 0; --i) + { + printf("%c", ((w >> (i - 1)) & 1) == 1 ? '1' : '0'); + } + printf("\n"); +} + +#endif -- cgit v1.2.3-13-gbd6f