Porting to Intel IV – W00t!

I got a major part of my application working on the Intel Mac.

Here are a few macros I came up with to handle the byte ordering issues when reading & writing binary data from a file:

#define W(w) ((unsigned short)w)
#define WSWAP(w) (W((W(w) << 8 ) | (W(w) >> 8 )))
#define DW(d) ((unsigned long)d)
#define DWSWAP(d) ((DW(d) >> 24) | ((DW(d) >> 8 ) & 0xff00) | ((DW(d) & 0xff00) << 8 ) | (DW(d) << 24))

#if defined(__LITTLE_ENDIAN__)
#define LITTLE_ENDIAN_WORD(w) W(w)
#define LITTLE_ENDIAN_DWORD(d) DW(d)
#define BIG_ENDIAN_WORD(w) WSWAP(w)
#define BIG_ENDIAN_DWORD(d) DWSWAP(d)
#else
#define LITTLE_ENDIAN_WORD(w) WSWAP(w)
#define LITTLE_ENDIAN_DWORD(d) DWSWAP(d)
#define BIG_ENDIAN_WORD(w) W(w)
#define BIG_ENDIAN_DWORD(d) DW(d)
#endif

Comments are closed.