diff options
| author | 2026-11-08 05:23:31 +0000 | |
|---|---|---|
| committer | 2026-11-08 05:24:55 +0000 | |
| commit | 3f36aca66e1ac6c872300059e8db691a473614d4 (patch) | |
| tree | 48c65f463c487d281c7c4091c7ba01dd4a2ff8f1 /src/utils.h | |
| download | lab-02-3f36aca66e1ac6c872300059e8db691a473614d4.tar.gz | |
Diffstat (limited to 'src/utils.h')
| -rw-r--r-- | src/utils.h | 48 |
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 | |||
| 31 | static inline void | ||
| 32 | setbit (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 | |||
| 39 | static inline bool | ||
| 40 | getbit (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 | ||
