RandyGaul / cute_headers

Collection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
4.33k stars 269 forks source link

CUTE_ASEPRITE: support byte swapping for big endian hosts #385

Closed mgerhardy closed 5 months ago

mgerhardy commented 5 months ago

When using SDL you could e.g. use

#define CUTE_ASEPRITE_BE_SWAP16 SDL_SwapLE16
#define CUTE_ASEPRITE_BE_SWAP32 SDL_SwapLE32
#define CUTE_ASEPRITE_BE_SWAP64 SDL_SwapLE64

or the gcc built-ins like __builtin_swap32, ... to perform the byte swapping

RandyGaul commented 5 months ago

This shouldn't be necessary since the code reads in one byte at a time, thanks for the idea though!

mgerhardy commented 5 months ago

Sorry, I suppose I don't understand. Why shouldn't this be needed? You are reading byte by byte and bit shifting the value into the little endian representation.

    value = (*p)[0];
    value |= (((uint16_t)((*p)[1])) << 8);

as far as I know this is incorrect for big endian systems and should get swapped.

RandyGaul commented 5 months ago

Thanks for asking, the bitshifting is platform independent. If you study the C standard you can see that the bitshifting itself will place the bytes into the correct endian format. The strategy here is to intentionally only read in only one byte and rely on the bitshifting to handle reading-endianness.

mgerhardy commented 5 months ago

Thanks a lot for clarification