Skip to content

Commit 9630376

Browse files
committed
py/mpconfig.h: Be stricter when autodetecting machine endianness.
This patch changes 2 things in the endianness detection: 1. Don't assume that __BYTE_ORDER__ not being __ORDER_LITTLE_ENDIAN__ means that the machine is big endian, so add an explicit check that this macro is indeed __ORDER_BIG_ENDIAN__ (same with __BYTE_ORDER, __LITTLE_ENDIAN and __BIG_ENDIAN). A machine could have PDP endianness. 2. Remove the checks which base their autodetection decision on whether any little or big endian macros are defined (eg __LITTLE_ENDIAN__ or __BIG_ENDIAN__). Just because a system defines these does not mean it has that endianness. See issue adafruit#3760.
1 parent 7541be5 commit 9630376

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

py/mpconfig.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,29 +1256,26 @@ typedef double mp_float_t;
12561256
#elif defined(MP_ENDIANNESS_BIG)
12571257
#define MP_ENDIANNESS_LITTLE (!MP_ENDIANNESS_BIG)
12581258
#else
1259-
// Endiannes not defined by port so try to autodetect it.
1259+
// Endianness not defined by port so try to autodetect it.
12601260
#if defined(__BYTE_ORDER__)
12611261
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
12621262
#define MP_ENDIANNESS_LITTLE (1)
1263-
#else
1263+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
12641264
#define MP_ENDIANNESS_LITTLE (0)
12651265
#endif
1266-
#elif defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN)
1267-
#define MP_ENDIANNESS_LITTLE (1)
1268-
#elif defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined (_BIG_ENDIAN)
1269-
#define MP_ENDIANNESS_LITTLE (0)
12701266
#else
12711267
#include <endian.h>
12721268
#if defined(__BYTE_ORDER)
12731269
#if __BYTE_ORDER == __LITTLE_ENDIAN
12741270
#define MP_ENDIANNESS_LITTLE (1)
1275-
#else
1271+
#elif __BYTE_ORDER == __BIG_ENDIAN
12761272
#define MP_ENDIANNESS_LITTLE (0)
12771273
#endif
1278-
#else
1279-
#error endianness not defined and cannot detect it
12801274
#endif
12811275
#endif
1276+
#ifndef MP_ENDIANNESS_LITTLE
1277+
#error endianness not defined and cannot detect it
1278+
#endif
12821279
#define MP_ENDIANNESS_BIG (!MP_ENDIANNESS_LITTLE)
12831280
#endif
12841281

0 commit comments

Comments
 (0)