FeatherMC/src/util/ByteUtil.h

33 lines
1.1 KiB
C++

#pragma once
#include <cstdlib>
#include <cstdint>
namespace Feather
{
enum class Endian
{
Little,
Big
};
// TODO: Make this constexpr
inline uint8_t ReverseBytes(uint8_t n) { return n; }
#ifdef __GNUC__
inline uint16_t ReverseBytes(uint16_t n) { return __builtin_bswap16(n); }
inline uint32_t ReverseBytes(uint32_t n) { return __builtin_bswap32(n); }
inline uint64_t ReverseBytes(uint64_t n) { return __builtin_bswap64(n); }
#else
inline uint16_t ReverseBytes(uint16_t n) { return _byteswap_ushort(n); }
inline uint32_t ReverseBytes(uint32_t n) { return _byteswap_ulong (n); }
inline uint64_t ReverseBytes(uint64_t n) { return _byteswap_uint64(n); }
#endif
inline int8_t ReverseBytes(int8_t n) { return n; }
inline int16_t ReverseBytes(int16_t n) { return int16_t(ReverseBytes(uint16_t(n))); }
inline int32_t ReverseBytes(int32_t n) { return int32_t(ReverseBytes(uint32_t(n))); }
inline int64_t ReverseBytes(int64_t n) { return int64_t(ReverseBytes(uint64_t(n))); }
// FIXME: this maybe very wrong
inline double ReverseBytes(double d) { return (double)ReverseBytes((uint64_t)d); }
}