vita-toolchain

git clone https://git.neptards.moe/neptards/vita-toolchain.git
Log | Files | Refs | README | LICENSE

endian-utils.h (2777B)


      1 #ifndef ENDIAN_UTILS_H
      2 #define ENDIAN_UTILS_H
      3 
      4 #ifndef __MINGW32__
      5 
      6 #ifdef __APPLE__
      7 
      8 #include <machine/endian.h>
      9 #include <libkern/OSByteOrder.h>
     10 
     11 #define htobe16(x) OSSwapHostToBigInt16(x)
     12 #define htole16(x) OSSwapHostToLittleInt16(x)
     13 #define be16toh(x) OSSwapBigToHostInt16(x)
     14 #define le16toh(x) OSSwapLittleToHostInt16(x)
     15  
     16 #define htobe32(x) OSSwapHostToBigInt32(x)
     17 #define htole32(x) OSSwapHostToLittleInt32(x)
     18 #define be32toh(x) OSSwapBigToHostInt32(x)
     19 #define le32toh(x) OSSwapLittleToHostInt32(x)
     20  
     21 #define htobe64(x) OSSwapHostToBigInt64(x)
     22 #define htole64(x) OSSwapHostToLittleInt64(x)
     23 #define be64toh(x) OSSwapBigToHostInt64(x)
     24 #define le64toh(x) OSSwapLittleToHostInt64(x)
     25 
     26 #define __BYTE_ORDER    BYTE_ORDER
     27 #define __BIG_ENDIAN    BIG_ENDIAN
     28 #define __LITTLE_ENDIAN LITTLE_ENDIAN
     29 #define __PDP_ENDIAN    PDP_ENDIAN
     30 
     31 #else
     32 
     33 #ifdef USE_BUNDLED_ENDIAN_H
     34 
     35 // Taken from FreeBSD
     36 #define	bswap16(x) (uint16_t) \
     37 	((x >> 8) | (x << 8))
     38 
     39 #define	bswap32(x) (uint32_t) \
     40 	((x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24))
     41 
     42 #define	bswap64(x) (uint64_t) \
     43 	((x >> 56) | ((x >> 40) & 0xff00) | ((x >> 24) & 0xff0000) | \
     44 	((x >> 8) & 0xff000000) | ((x << 8) & ((uint64_t)0xff << 32)) | \
     45 	((x << 24) & ((uint64_t)0xff << 40)) | \
     46 	((x << 40) & ((uint64_t)0xff << 48)) | ((x << 56)))
     47 
     48 /*
     49  * Host to big endian, host to little endian, big endian to host, and little
     50  * endian to host byte order functions as detailed in byteorder(9).
     51  */
     52 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
     53 #define	htobe16(x)	bswap16((uint16_t)(x))
     54 #define	htobe32(x)	bswap32((uint32_t)(x))
     55 #define	htobe64(x)	bswap64((uint64_t)(x))
     56 #define	htole16(x)	((uint16_t)(x))
     57 #define	htole32(x)	((uint32_t)(x))
     58 #define	htole64(x)	((uint64_t)(x))
     59 
     60 #define	be16toh(x)	bswap16((uint16_t)(x))
     61 #define	be32toh(x)	bswap32((uint32_t)(x))
     62 #define	be64toh(x)	bswap64((uint64_t)(x))
     63 #define	le16toh(x)	((uint16_t)(x))
     64 #define	le32toh(x)	((uint32_t)(x))
     65 #define	le64toh(x)	((uint64_t)(x))
     66 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
     67 #define	htobe16(x)	((uint16_t)(x))
     68 #define	htobe32(x)	((uint32_t)(x))
     69 #define	htobe64(x)	((uint64_t)(x))
     70 #define	htole16(x)	bswap16((uint16_t)(x))
     71 #define	htole32(x)	bswap32((uint32_t)(x))
     72 #define	htole64(x)	bswap64((uint64_t)(x))
     73 
     74 #define	be16toh(x)	((uint16_t)(x))
     75 #define	be32toh(x)	((uint32_t)(x))
     76 #define	be64toh(x)	((uint64_t)(x))
     77 #define	le16toh(x)	bswap16((uint16_t)(x))
     78 #define	le32toh(x)	bswap32((uint32_t)(x))
     79 #define	le64toh(x)	bswap64((uint64_t)(x))
     80 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
     81 
     82 #else
     83 #include <endian.h>
     84 #endif
     85 
     86 #endif
     87 
     88 #else
     89 /* Todo: make these definitions work on BE systems! */
     90 # define htole32(value) (value)
     91 # define htole16(value) (value)
     92 
     93 #define le32toh(value) htole32(value)
     94 #define le16toh(value) htole16(value)
     95 #endif
     96 
     97 #endif