[issue16166] Add PY_BYTE_ORDER macro to get endianess of platform

Christian Heimes report at bugs.python.org
Tue Oct 9 17:07:42 CEST 2012


Christian Heimes added the comment:

I think it's not enough as some platforms prefix/suffix the macros with dashes. The values must be compared to BYTE_ORDER iff the macro is defined. too. I've checked some machines of the Snakebite network and came up with this macro cascade:

/*
 * The endianess (byte order) can be defined in several ways. Some platforms
 * define either LITTLE_ENDIAN or BIG_ENDIAN while other platforms define
 * both and require comparison to BYTE_ORDER. Sometimes the macros are
 * prefixed or suffixed with one or two dashes, too. SPAM seems to be an
 * alias of __SPAM an all platforms that have SPAM.
 * The endian check can't be handled in a configure step as it would break
 * Apple's universal builds.
 */

#if defined(__BYTE_ORDER)
#  if defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
#    define PY_IS_BIG_ENDIAN 1
#  elif defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
#    define PY_IS_LITTLE_ENDIAN 1
#  else
#    error "__BYTE_ORDER not in (__BIG_ENDIAN, __LITTLE_ENDIAN)"
#  endif
#elif defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
#  define PY_IS_BIG_ENDIAN 1
#elif defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
#  define PY_IS_LITTLE_ENDIAN 1
/* and know the same block with just one dash as prefix */
#elif defined(_BYTE_ORDER)
#  if defined(_BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN
#    define PY_IS_BIG_ENDIAN 1
#  elif defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN
#    define PY_IS_LITTLE_ENDIAN 1
#  else
#    error "_BYTE_ORDER not in (_BIG_ENDIAN, _LITTLE_ENDIAN)"
#  endif
#elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
#  define PY_IS_BIG_ENDIAN 1
#elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
#  define PY_IS_LITTLE_ENDIAN 1
/* extra apple */
#elif defined(__APPLE__)
#  if defined(__BIG_ENDIAN__)
#    define PY_IS_BIG_ENDIAN 1
#  elif defined(__LITTLE_ENDIAN__)
#    define PY_IS_LITTLE_ENDIAN 1
#  else
#    error "__APPLE__ but neither __BIG_ENDIAN__ nor __LITTLE_ENDIAN__"
#  endif
/* Windows */
#elif defined(WIN32)
#  define PY_IS_LITTLE_ENDIAN 1
#else
#  error "Unable to detect endianess"
#endif

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16166>
_______________________________________


More information about the Python-bugs-list mailing list