Endianness check at compile time

Started by
4 comments, last by mattnewport 17 years ago
Hi! I am looking for a convinient way of checking endianness at compile time (using C++). Using boost/detail/endian.hpp might be a portable choice. I'd want to know if there exists a constant expression I could use as a template paramter or another way of hacking this together without using dirty preprocessor macros. -Riku
Advertisement
Quote:...compile time ... without using dirty preprocessor macros

In short No.
This is the only way to do it at compile time, runtime is easy but is no good for templates. Boost uses the macros to do this.
Just to make a note, while most processors are locked into their endian-ness, there are procs out there that can be switched between little and big endian. So a runtime check would be the only way to know what mode they were currently running in.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Quote:Original post by Rattrap
Just to make a note, while most processors are locked into their endian-ness, there are procs out there that can be switched between little and big endian. So a runtime check would be the only way to know what mode they were currently running in.


Can you give an example of such a processor ?
Quote:Original post by nmi
Quote:Original post by Rattrap
Just to make a note, while most processors are locked into their endian-ness, there are procs out there that can be switched between little and big endian. So a runtime check would be the only way to know what mode they were currently running in.


Can you give an example of such a processor ?


See bi-endian hardware for a whole list. Also even if that wasn't the case there would be nothing wrong in having the compiler and compiled executable running on completely different architectures (it isn't common though).
Quote:Original post by CTar
See bi-endian hardware for a whole list. Also even if that wasn't the case there would be nothing wrong in having the compiler and compiled executable running on completely different architectures (it isn't common though).


It actually is pretty common to cross compile (have the compiler running on a different architecture to the target). Most console development happens this way. Anyone developing a game for the GameCube, PS2, Xbox 360 or PS3 for example will be doing this since the development tools run on an x86 PC and the game executable will be running on MIPS, PowerPC or an SPU depending on the system. The PowerPC based platforms have opposite endianness to the PC. Developing for mobile phones, PDAs or embedded devices will also typically involve cross compiling.

Game Programming Blog: www.mattnewport.com/blog

This topic is closed to new replies.

Advertisement