diff options
Diffstat (limited to 'base.h')
-rw-r--r-- | base.h | 78 |
1 files changed, 78 insertions, 0 deletions
@@ -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 <https://www.gnu.org/licenses/>. + + * Created: 2025-03-31 + * Description: Base + */ + +#ifndef BASE_H +#define BASE_H + +#include <assert.h> +#include <stdarg.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> + +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 |