No more autoconf for endianess detection
autoconf is annoying to work with, and I think that programs that rely excessively on it for cross platform compatibility have issues of their own. Sometimes you really have no choice though.
Fortunately I recently discovered one place where I now do have a choice where I didn't before. This little code snippet can be optimized by gcc at compile time into a constant expression. That means that gcc realizes there is only one possible result and it uses that result in place of actually running the code in the function. Here is the code snippet:
inline bool is_little_endian()
{
const union {
::std::tr1::uint32_t tval;
unsigned char tchar[4];
} testunion = { 0x11223344ul };
return testunion.tchar[0] == 0x44u;
}
This is guaranteed to work on C99 systems, and, as I said, gcc is capable of recognizing it as a constant expression. This also means that if you have code like this:
if (is_little_endian()) {
do_something();
} else {
do_something_else();
}
gcc will be smart enough to see that only one branch of that if will ever be taken and optimize the other completely out of your code.
Normally you'd want to use autoconf for this so you will have a preprocessor macro that will elide the code for you. The fact gcc can optimize this well means you don't have to do that to get efficient code.
