aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 02:11:35 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 02:11:35 +0000
commit08fcd58893b5ea754ebddbf63b2745dde34d4830 (patch)
tree6e121100ec1aceccd2a6032c90bf11d1dd16ef8f /src/utils.h
downloadlab-01-08fcd58893b5ea754ebddbf63b2745dde34d4830.tar.gz
publishHEADmaster
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..d5a1242
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,65 @@
1#ifndef INCLUDED_UTILS_H
2#define INCLUDED_UTILS_H
3
4#include <stdlib.h>
5#include <stdio.h>
6#include <stdbool.h>
7#include <inttypes.h>
8#include <assert.h>
9
10#define MALLOC malloc
11#define REALLOC realloc
12#ifdef DEBUG
13#define FREE(PTR) ({free (PTR); PTR = NULL;})
14#define ASSERT assert
15#else
16#define FREE free
17#define ASSERT
18#endif
19
20#define CEIL_INT_DIV(A, B) ((A + (B - 1))/B)
21// #define CEIL_INT_DIV(A, B) (1 + (A - 1)/B) // A != 0
22
23#define MOD(A, B) (((A % B) + B) % B)
24
25#define SWAP(A, B) \
26 ({ \
27 typeof (A) tmp = A; \
28 A = B; \
29 B = tmp; \
30 })
31
32#define MIN(A, B) \
33 ({ \
34 typeof (A) a = A; \
35 typeof (B) b = B; \
36 a < b ? a : b; \
37 }) \
38
39inline void
40setbit (uint8_t *bytes, size_t index, bool value)
41{
42 uint8_t *byte = &bytes[index >> 3];
43 uint8_t offset = index & ((1 << 3) - 1);
44 uint8_t mask = ~(1 << offset);
45 *byte = *byte & mask | value << offset;
46}
47
48inline bool
49getbit (uint8_t *bytes, size_t index)
50{
51 uint8_t byte = bytes[index >> 3];
52 uint8_t offset = index & ((1 << 3) - 1);
53 uint8_t mask = 1 << offset;
54 return (byte & mask) >> offset;
55}
56
57inline void
58prompt (char *label, char *fmt, void *where)
59{
60 printf ("%s: ", label);
61 scanf (fmt, where);
62 while (getchar () != '\n');
63}
64
65#endif