| 1 | /* @title: Memory Sizes and Unit Conversions */ |
| 2 | #pragma once |
| 3 | #include <stddef.h> |
| 4 | |
| 5 | /* Binary (IEC) units: powers of 1024 */ |
| 6 | #define KIB(x) ((size_t) (x) << 10) |
| 7 | #define MIB(x) ((size_t) (x) << 20) |
| 8 | #define GIB(x) ((size_t) (x) << 30) |
| 9 | #define TIB(x) ((size_t) (x) << 40) |
| 10 | |
| 11 | /* Decimal (SI) units: powers of 1000 */ |
| 12 | #define KB(x) ((size_t) (x) * 1000ULL) |
| 13 | #define MB(x) ((size_t) (x) * 1000000ULL) |
| 14 | #define GB(x) ((size_t) (x) * 1000000000ULL) |
| 15 | #define TB(x) ((size_t) (x) * 1000000000000ULL) |
| 16 | |
| 17 | static inline size_t to_bits(size_t bytes) { |
| 18 | return bytes * 8; |
| 19 | } |
| 20 | |
| 21 | static inline size_t to_bytes(size_t bits) { |
| 22 | return (bits + 7) / 8; |
| 23 | } |
| 24 | |