Skip to content

Commit a9bcd51

Browse files
committed
py: Try to autodetect machine endianness when not defined by port.
1 parent 5a04e2c commit a9bcd51

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

py/mpconfig.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,26 @@ typedef double mp_float_t;
434434
// mp_int_t value with most significant bit set
435435
#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
436436

437-
#if !defined(MP_ENDIANNESS_LITTLE) && !defined(MP_ENDIANNESS_BIG)
438-
// Just because most archs are such?
439-
#define MP_ENDIANNESS_LITTLE (1)
440-
#endif
441-
// Ensure we don't accidentally set both endiannesses
442-
#if MP_ENDIANNESS_BIG
443-
#define MP_ENDIANNESS_LITTLE (0)
437+
// Make sure both MP_ENDIANNESS_LITTLE and MP_ENDIANNESS_BIG are
438+
// defined and that they are the opposite of each other.
439+
#if defined(MP_ENDIANNESS_LITTLE)
440+
#define MP_ENDIANNESS_BIG (!MP_ENDIANNESS_LITTLE)
441+
#elif defined(MP_ENDIANNESS_BIG)
442+
#define MP_ENDIANNESS_LITTLE (!MP_ENDIANNESS_BIG)
443+
#else
444+
// Endiannes not defined by port so try to autodetect it.
445+
#if defined(__BYTE_ORDER__)
446+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
447+
#define MP_ENDIANNESS_LITTLE (1)
448+
#else
449+
#define MP_ENDIANNESS_LITTLE (0)
450+
#endif
451+
#elif defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined (_BIG_ENDIAN)
452+
#define MP_ENDIANNESS_LITTLE (0)
453+
#else
454+
#error endianness not defined and cannot detect it
455+
#endif
456+
#define MP_ENDIANNESS_BIG (!MP_ENDIANNESS_LITTLE)
444457
#endif
445458

446459
// Make a pointer to RAM callable (eg set lower bit for Thumb code)

0 commit comments

Comments
 (0)