diff options
| author | steven-na <noreply.github@stvnc.dev> | 2026-09-07 18:11:21 -0700 |
|---|---|---|
| committer | steven-na <noreply.github@stvnc.dev> | 2026-09-07 18:11:21 -0700 |
| commit | 81caeb5ffc0748273157bf26c3299dfee9c12a6d (patch) | |
| tree | 05ad674efd0f08d205868c61f063605c43ed302e /src/binary.h | |
| parent | fd475232ee12089f817f237cc755fe4444ec483b (diff) | |
Utility functions
Diffstat (limited to 'src/binary.h')
| -rw-r--r-- | src/binary.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/binary.h b/src/binary.h new file mode 100644 index 0000000..139d2bf --- /dev/null +++ b/src/binary.h @@ -0,0 +1,28 @@ +#pragma once + +#include "common.h" +#include "log.h" +#include "string.h" + +// Binary representation of u64 +strng_t *strng_u64_bin(smrt_arena_t *arena, u64 n); + +static inline strng_view_t uint_to_binsv_impl(smrt_arena_t *arena, u64 n, u8 bits) { + strng_t *b = strng_u64_bin(arena, n); + strng_view_t svb = sv_from(b); + sv_set_len_right(&svb, bits); + return svb; +} + +// Converts n (u8-u64) to a strng_view_t representing the 0s and 1s +#define UINT_TO_BINSV(arena, n) uint_to_binsv_impl(arena, n, \ + _Generic(n, u8: 8, u16: 16, u32: 32, default: 64)) + +#define LOG_UINT_BINREP(label, n) \ + do { \ + smrta_temp_t _macro_t = QUICK_SCRATCH; \ + auto _macro_n = (n); \ + strng_view_t _macro_svb = UINT_TO_BINSV(_macro_t.arena, _macro_n); \ + log_debug(label": 0b%.*s", SV_FMT(_macro_svb)); \ + QUICK_SCRATCH_E(_macro_t); \ + } while (0) |