aboutsummaryrefslogtreecommitdiff
path: root/base.h
blob: f841328dd44328e90952955edbcfde28eacfa876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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