summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-10-06 13:44:59 +0000
committerDamien George <damien.p.george@gmail.com>2014-10-06 13:44:59 +0000
commita9bcd51dc7f05be0961f699f39d1856e78eaaaa6 (patch)
treeb2b106bcd916d9b1d731405c75f57ee17e4d54f5
parent5a04e2cca8255c2d4a1218d6ac0b38e67306206b (diff)
downloadmicropython-a9bcd51dc7f05be0961f699f39d1856e78eaaaa6.tar.gz
micropython-a9bcd51dc7f05be0961f699f39d1856e78eaaaa6.zip
py: Try to autodetect machine endianness when not defined by port.
-rw-r--r--py/mpconfig.h27
1 files changed, 20 insertions, 7 deletions
diff --git a/py/mpconfig.h b/py/mpconfig.h
index cf85533395..6056e20436 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -434,13 +434,26 @@ typedef double mp_float_t;
// mp_int_t value with most significant bit set
#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
-#if !defined(MP_ENDIANNESS_LITTLE) && !defined(MP_ENDIANNESS_BIG)
-// Just because most archs are such?
-#define MP_ENDIANNESS_LITTLE (1)
-#endif
-// Ensure we don't accidentally set both endiannesses
-#if MP_ENDIANNESS_BIG
-#define MP_ENDIANNESS_LITTLE (0)
+// Make sure both MP_ENDIANNESS_LITTLE and MP_ENDIANNESS_BIG are
+// defined and that they are the opposite of each other.
+#if defined(MP_ENDIANNESS_LITTLE)
+#define MP_ENDIANNESS_BIG (!MP_ENDIANNESS_LITTLE)
+#elif defined(MP_ENDIANNESS_BIG)
+#define MP_ENDIANNESS_LITTLE (!MP_ENDIANNESS_BIG)
+#else
+ // Endiannes not defined by port so try to autodetect it.
+ #if defined(__BYTE_ORDER__)
+ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ #define MP_ENDIANNESS_LITTLE (1)
+ #else
+ #define MP_ENDIANNESS_LITTLE (0)
+ #endif
+ #elif defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined (_BIG_ENDIAN)
+ #define MP_ENDIANNESS_LITTLE (0)
+ #else
+ #error endianness not defined and cannot detect it
+ #endif
+ #define MP_ENDIANNESS_BIG (!MP_ENDIANNESS_LITTLE)
#endif
// Make a pointer to RAM callable (eg set lower bit for Thumb code)