aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
blob: aec95d2adb5eaad051b743c898deb26eeb11ac3f (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
#ifndef INCLUDED_UTILS_H
#define INCLUDED_UTILS_H

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

#define UNUSED(X) ((void) (X))
#define UNREACHABLE() ASSERT (!"reached the unreachable...")
// #define TODO(STR) fprintf (stderr, "%s:%d: warning: TODO "STR"\n", __FILE__, __LINE__)
#define TODO(...)

#define ASSERT assert
#define REALLOC realloc
#define FREE free

#define ARRAY_LEN(array) (sizeof (array)/sizeof (*array))
#define ARRAY_GET(array, index) \
    (ASSERT(index >= 0), ASSERT(index < ARRAY_LEN(array)), array[index])

// inline void
// setbit (uint8_t *bytes, size_t index, bool value)
// {
//   uint8_t *byte = &bytes[index >> 3];
//   uint8_t offset = index & ((1 << 3) - 1);
//   uint8_t mask = ~(1 << offset);
//   *byte = *byte & mask | value << offset;
// }

static inline void
setbit (uint8_t *bytes, size_t index)
{
  uint8_t *byte = &bytes[index/8];
  uint8_t offset = index & 7;
  *byte |= 1 << offset;
}

static inline bool
getbit (uint8_t *bytes, size_t index)
{
  uint8_t byte = bytes[index/8];
  uint8_t offset = index & 7;
  uint8_t mask = 1 << offset;
  return (byte & mask) >> offset;
}

#endif