I am writing a tool to port games written in C/C++ from the 24bit eZ80 to Windows/Linux. One of the issues is that int24_t
doesn’t exist on x86/x64.
typedef uint32_t uint24_t
doesn’t always work, especially with packed data and incrementing uint24_t*
pointers. I tried _BitInt(24)
in Clang C23, but I then found out that sizeof(_BitInt(24))
is 4 bytes, with -Wcast-align
stating that the alignment for _BitInt(24)
was also 4. I tried _ExtInt(24)
but it also had a size of 4 bytes.
So far, I have only found two solutions where int24_t is exactly 3 bytes packed.
typedef struct int24_t {
uint8_t n[3];
} int24_t;
The first being a struct. But this requires a lot of refactoring in the code, as int x = y + a * b
on the eZ80 would become int24_t x = ADD_INT24(y, MUL_INT24(a, b))
, which is not as readable.
class int24_t {
private:
uint8_t n[3];
};
The other solution I found was to use a C++ class, which is also exactly 3 bytes in Clang and GCC. Since this allows for operator overloading, expressions can be written normally int24_t x = y + a * b
. However, since C and C++ are different languages, compiling C as C++ might not always work.
Are there any ways to have a packed int24_t
type in C?