aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:23:31 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:24:55 +0000
commit3f36aca66e1ac6c872300059e8db691a473614d4 (patch)
tree48c65f463c487d281c7c4091c7ba01dd4a2ff8f1 /src/utils.h
downloadlab-02-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..aec95d2
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,48 @@
1#ifndef INCLUDED_UTILS_H
2#define INCLUDED_UTILS_H
3
4#include <stdlib.h>
5#include <stdint.h>
6#include <stdbool.h>
7#include <assert.h>
8
9#define UNUSED(X) ((void) (X))
10#define UNREACHABLE() ASSERT (!"reached the unreachable...")
11// #define TODO(STR) fprintf (stderr, "%s:%d: warning: TODO "STR"\n", __FILE__, __LINE__)
12#define TODO(...)
13
14#define ASSERT assert
15#define REALLOC realloc
16#define FREE free
17
18#define ARRAY_LEN(array) (sizeof (array)/sizeof (*array))
19#define ARRAY_GET(array, index) \
20 (ASSERT(index >= 0), ASSERT(index < ARRAY_LEN(array)), array[index])
21
22// inline void
23// setbit (uint8_t *bytes, size_t index, bool value)
24// {
25// uint8_t *byte = &bytes[index >> 3];
26// uint8_t offset = index & ((1 << 3) - 1);
27// uint8_t mask = ~(1 << offset);
28// *byte = *byte & mask | value << offset;
29// }
30
31static inline void
32setbit (uint8_t *bytes, size_t index)
33{
34 uint8_t *byte = &bytes[index/8];
35 uint8_t offset = index & 7;
36 *byte |= 1 << offset;
37}
38
39static inline bool
40getbit (uint8_t *bytes, size_t index)
41{
42 uint8_t byte = bytes[index/8];
43 uint8_t offset = index & 7;
44 uint8_t mask = 1 << offset;
45 return (byte & mask) >> offset;
46}
47
48#endif