none
Detect target architecture endianess (in preprocessor time)

    Question

  • 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

    Tuesday, March 06, 2012 2:58 AM

All replies

  • You can use something like

    #define IS_BIG_ENDIAN (*(WORD *)"\0\x2" == 0x200)
    

    Tuesday, March 06, 2012 3:50 AM
  • 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

    Tuesday, March 06, 2012 5:32 AM
  • Does Windows actually run on any big-endian CPU? I doubt it (but don't  know for sure).

    IA64 is bi-endian, but Windows itself still uses its little-endian mode.

    Answering policy: see profile.

    • Proposed as answer by Helen Zhao Tuesday, March 13, 2012 8:38 AM
    Tuesday, March 06, 2012 9:25 AM
  • You can use something like

    #define IS_BIG_ENDIAN (*(WORD *)"\0\x2" == 0x200)


    Nice idea which unfortunately is incorrect.  Name the macro IS_LITTLE_ENDIAN (or negate the condition) and it will be correct.

    Answering policy: see profile.

    Tuesday, March 06, 2012 9:27 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!

    Tuesday, March 06, 2012 1:15 PM
  • 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.

    Tuesday, March 06, 2012 3:18 PM