Asked by:
Detect target architecture endianess (in preprocessor time)
-
Hi everyone. I want to write some multiplataform/architecure code. In the *nix world there exists some #defines (BYTE_ORDER generally defined by sys/types.h inclusion). There exists something like this in the Windows world?
Thanks in advance.
Best regards
Question
All replies
-
-
PabloGentle wrote:
Hi everyone. I want to write some multiplataform/architecure code. In the *nix world there exists some #defines (BYTE_ORDER
generally defined by sys/types.h inclusion). There exists something like this in the Windows world?Does Windows actually run on any big-endian CPU? I doubt it (but don't know for sure).
Igor Tandetnik
-
IA64 is bi-endian, but Windows itself still uses its little-endian mode.Does Windows actually run on any big-endian CPU? I doubt it (but don't know for sure).
Answering policy: see profile.
- Proposed as answer by Helen Zhao Tuesday, March 13, 2012 8:38 AM
-
-
Thank to all for the responses. I can't use a define like the suggested by Pierre because I need to use it to make others conditionals defines. I'll clarify with an example (OpenBSD, others *nixes take similar approaches):
In sys/endian.h :
.......
#define _LITTLE_ENDIAN 1234
#define _BIG_ENDIAN 4321.......
In /sys/arch/sparc64/include/endian.h
........
#define _BYTE_ORDER _BIG_ENDIAN
........
In /sys/arch/i386/include/endian.h
........
#define _BYTE_ORDER _LITTLE_ENDIAN
........
In my code:
#if BYTE_ORDER == LITTLE_ENDIAN
#define SOME_MACRO some endianess dependent code
#else
#define SOME_MACRO some (different) endianess dependent code#endif
If there is not some similar think in windows I plan to use (According to Igor/David, in practice, windows always run in little endian archs (IA-64 is bi-endian but Windows use it in little *) ):
#ifdef _WIN32
#define _LITTLE_ENDIAN 1234
#define BYTE_ORDER LITTLE_ENDIAN
#endif
Thoughts?
* In IA-64 the processor starts in a predefined endianess mode an the OS set his operation mode is some early stage?
Thanks in advance!
Best regards!
-
Thank to all for the responses. I can't use a define like the suggested by Pierre because I need to use it to make others conditionals defines. I'll clarify with an example (OpenBSD, others *nixes take similar approaches):
In sys/endian.h :
.......
#define _LITTLE_ENDIAN 1234
#define _BIG_ENDIAN 4321.......
In /sys/arch/sparc64/include/endian.h
........
#define _BYTE_ORDER _BIG_ENDIAN
........
In /sys/arch/i386/include/endian.h
........
#define _BYTE_ORDER _LITTLE_ENDIAN
........
In my code:
#if BYTE_ORDER == LITTLE_ENDIAN
#define SOME_MACRO some endianess dependent code
#else
#define SOME_MACRO some (different) endianess dependent code#endif
If there is not some similar think in windows I plan to use (According to Igor/David, in practice, windows always run in little endian archs (IA-64 is bi-endian but Windows use it in little *) ):
#ifdef _WIN32
#define _LITTLE_ENDIAN 1234
#define BYTE_ORDER LITTLE_ENDIAN
#endif
Thoughts?
Well, you could have adapted Pierre's macro test easily enough, but testing for _WIN32 is probably sufficient.
Answering policy: see profile.

