diff options
| author | 2026-11-08 02:11:35 +0000 | |
|---|---|---|
| committer | 2026-11-08 02:11:35 +0000 | |
| commit | 08fcd58893b5ea754ebddbf63b2745dde34d4830 (patch) | |
| tree | 6e121100ec1aceccd2a6032c90bf11d1dd16ef8f /src/utils.h | |
| download | lab-01-08fcd58893b5ea754ebddbf63b2745dde34d4830.tar.gz | |
Diffstat (limited to 'src/utils.h')
| -rw-r--r-- | src/utils.h | 65 |
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 | |||
| 39 | inline void | ||
| 40 | setbit (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 | |||
| 48 | inline bool | ||
| 49 | getbit (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 | |||
| 57 | inline void | ||
| 58 | prompt (char *label, char *fmt, void *where) | ||
| 59 | { | ||
| 60 | printf ("%s: ", label); | ||
| 61 | scanf (fmt, where); | ||
| 62 | while (getchar () != '\n'); | ||
| 63 | } | ||
| 64 | |||
| 65 | #endif | ||
