I want to have a macro that tells me the endianness of the machine I'm compiling for, because I expect to compile for different architectures, so I can do
if LITTLE_ENDIAN{ //... }else{ //... }
in places that I do network or low-level stuff.
Obviously I want the macro to add no instructions, but with this macro that I made
static const union { unsigned char bytes[4]; uint32_t value; } _endianMacroHelper = { { 0, 1, 2, 3 } }; #define BIG_ENDIAN (_endianMacroHelper.value == 0x00010203) #define LITTLE_ENDIAN (_endianMacroHelper.value == 0x03020100)
even with the optimize -o2 arguments for the compiler, it generates a compare instruction in the 'if's.
So I guess the way to go is to more explicitly set the endianness macro based on some other compiler macros, or macros set by my compile options?
Another question I have is how to target multiple OS's and instruction sets. Like, AFAIK, Windows supports x86 and x64. I'm not sure about the instruction sets of Linux and Mac but I think there are multiple ones that are common. So, I assume for each OS I should have different executables for the different common instruction sets, and in my code I should treat OS-specific and instruction-set-specific stuff orthogonally. But endianness would depend on the instruction set, because each instruction set supports only one endianness? I'm making a game targetting PC only (Windows, Linux and Mac, I guess). This might be a stupid question, but what instruction sets should I support, on each OS? I guess that brings the question, what's the oldest version to support for each OS. And there might not be a clear answer, but I just don't know much about Linux and Mac...