divinity76 / cacdrive

harddrive emulator using cloudatcost's "cloud storage" as a storage backend.
The Unlicense
9 stars 4 forks source link

better endian functions #8

Open divinity76 opened 5 years ago

divinity76 commented 5 years ago

should do something like this instead imo

// ntohll => network to host long long => big endian to host endian, 64bit => BETOHE64
// ntohl => network to host long => big endian to host endian, 32bit => BETOHE32
// htonll => host to network long long => host endian to big endian, 64bit => HETOBE64
// htonl => host to network long => host endian to big endian, 32 bit => HETOBE32

#if !defined(HETOBE16)
#if !defined(__BYTE_ORDER__)
#error Failed to detect byte order!
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
// host endian to big endian 64bit
#define HETOBE64(x) (x)
// host endian to little endian 64bit
#define HETOLE64(x) __bswap_constant_64(x)
// host endian to big endian 32bit
#define HETOBE32(x) (x)
// host endian to little endian 32bit
#define HETOLE32(x) __bswap_constant_32(x)
// host endian to big endian 16bit
#define HETOBE16(x) (x)
// host endian to little endian 16bit
#define HETOLE16(x) __bswap_constant_16(x)
// little endian to host endian 64bit
#define LETOHE64(x) __bswap_constant_64(x)
// big endian to host endian 64bit
#define BETOHE64(x) (x)
// little endian to host endian 32bit
#define LETOHE32(x) __bswap_constant_32(x)
// big endian to host endian 32bit
#define BETOHE32(x) (x)
// little endian to host endian 16bit
#define LETOHE16(x) __bswap_constant_16(x)
// big endian to host endian 16bit
#define BETOHE16(x) (x)
#else
#if __BYTE_ORDER == __LITTLE_ENDIAN
// host endian to big endian 64bit
#define HETOBE64(x) __bswap_constant_64(x)
// host endian to little endian 64bit
#define HETOLE64(x) (x)
// host endian to big endian 32bit
#define HETOBE32(x) __bswap_constant_32(x)
// host endian to little endian 32bit
#define HETOLE32(x) (x)
// host endian to big endian 16bit
#define HETOBE16(x) __bswap_constant_16(x)
// host endian to little endian 16bit
#define HETOLE16(x) (x)
// little endian to host endian 64bit
#define LETOHE64(x) (x)
// big endian to host endian 64bit
#define BETOHE64(x) __bswap_constant_64(x)
// little endian to host endian 32bit
#define LETOHE32(x) (x)
// big endian to host endian 32bit
#define BETOHE32(x) __bswap_constant_32(x)
// little endian to host endian 16bit
#define LETOHE16(x) (x)
// big endian to host endian 16bit
#define BETOHE16(x) __bswap_constant_16(x)
#else
#error Failed to detect byte order! appears to be neither big endian nor little endian..
#endif
#endif
#endif