damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

pendian_detect.h (3719B)


      1 /*
      2  * Uses various known flags to decide endianness and defines:
      3  *
      4  * __LITTLE_ENDIAN__ or __BIG_ENDIAN__ if not already defined
      5  *
      6  * and also defines
      7  *
      8  * __BYTE_ORDER__ to either __ORDER_LITTLE_ENDIAN__ or
      9  * __ORDER_BIG_ENDIAN__ if not already defined
     10  *
     11  * If none of these could be set, __UNKNOWN_ENDIAN__ is defined,
     12  * which is not a known flag. If __BYTE_ORDER__ is defined but
     13  * not big or little endian, __UNKNOWN_ENDIAN__ is also defined.
     14  *
     15  * Note: Some systems define __BYTE_ORDER without __ at the end
     16  * - this will be mapped to to __BYTE_ORDER__.
     17  */
     18 
     19 #ifndef PENDIAN_DETECT
     20 #define PENDIAN_DETECT
     21 
     22 #ifdef __cplusplus
     23 extern "C" {
     24 #endif
     25 
     26 #ifndef __ORDER_LITTLE_ENDIAN__
     27 #define __ORDER_LITTLE_ENDIAN__ 1234
     28 #endif
     29 
     30 #ifndef __ORDER_BIG_ENDIAN__
     31 #define __ORDER_BIG_ENDIAN__ 4321
     32 #endif
     33 
     34 #ifdef __BYTE_ORDER__
     35 
     36 #if defined(__LITTLE_ENDIAN__) && __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
     37 #error __LITTLE_ENDIAN__ inconsistent with __BYTE_ORDER__
     38 #endif
     39 
     40 #if defined(__BIG_ENDIAN__) && __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
     41 #error __BIG_ENDIAN__ inconsistent with __BYTE_ORDER__
     42 #endif
     43 
     44 #else /* __BYTE_ORDER__ */
     45 
     46 
     47 #if                                                                         \
     48   defined(__LITTLE_ENDIAN__) ||                                             \
     49   (defined(__BYTE_ORDER) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN) ||       \
     50   defined(__ARMEL__) || defined(__THUMBEL__) ||                             \
     51   defined(__AARCH64EL__) ||                                                 \
     52   (defined(_MSC_VER) && defined(_M_ARM)) ||                                 \
     53   defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) ||           \
     54   defined(_M_X64) || defined(_M_IX86) || defined(_M_I86) ||                 \
     55   defined(__i386__) || defined(__alpha__) ||                                \
     56   defined(__ia64) || defined(__ia64__) ||                                   \
     57   defined(_M_IA64) || defined(_M_ALPHA) ||                                  \
     58   defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) ||            \
     59   defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) ||            \
     60   defined(__bfin__)
     61 
     62 #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
     63 
     64 #endif
     65 
     66 #if                                                                         \
     67   defined (__BIG_ENDIAN__) ||                                               \
     68   (defined(__BYTE_ORDER) && __BYTE_ORDER == __ORDER_BIG_ENDIAN) ||          \
     69   defined(__ARMEB__) || defined(THUMBEB__) || defined (__AARCH64EB__) ||    \
     70   defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) ||           \
     71   defined(__sparc) || defined(__sparc__) ||                                 \
     72   defined(_POWER) || defined(__powerpc__) || defined(__ppc__) ||            \
     73   defined(__hpux) || defined(__hppa) || defined(__s390__)
     74 
     75 #define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
     76 
     77 #endif
     78 
     79 #endif /* __BYTE_ORDER__ */
     80 
     81 #ifdef __BYTE_ORDER__
     82 
     83 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
     84 
     85 #ifndef __LITTLE_ENDIAN__
     86 #define __LITTLE_ENDIAN__ 1
     87 #endif
     88 
     89 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
     90 
     91 #ifndef __BIG_ENDIAN__
     92 #define __BIG_ENDIAN__ 1
     93 #endif
     94 
     95 #else
     96 
     97 /*
     98  * Custom extension - we only define __BYTE_ORDER__ if known big or little.
     99  * User code that understands __BYTE_ORDER__ may also assume unkown if
    100  * it is not defined by now - this will allow other endian formats than
    101  * big or little when supported by compiler.
    102  */
    103 #ifndef __UNKNOWN_ENDIAN__
    104 #define __UNKNOWN_ENDIAN__ 1
    105 #endif
    106 
    107 #endif
    108 #endif /* __BYTE_ORDER__ */
    109 
    110 #if defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__)
    111 #error conflicting definitions of __LITTLE_ENDIAN__ and __BIG_ENDIAN__
    112 #endif
    113 
    114 #ifdef __cplusplus
    115 }
    116 #endif
    117 
    118 #endif /* PENDIAN_DETECT */