2020-08-01 01:49:07 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-08-03 06:07:32 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdint>
|
2020-08-07 04:09:15 +01:00
|
|
|
#include <bit>
|
2020-08-01 01:49:07 +01:00
|
|
|
|
2020-08-03 06:07:32 +01:00
|
|
|
namespace Feather
|
2020-08-01 01:49:07 +01:00
|
|
|
{
|
2020-08-03 06:07:32 +01:00
|
|
|
enum class Endian
|
|
|
|
{
|
|
|
|
Little,
|
|
|
|
Big
|
|
|
|
};
|
2020-08-01 01:49:07 +01:00
|
|
|
|
2020-08-03 06:07:32 +01:00
|
|
|
// TODO: Make this constexpr
|
|
|
|
inline uint8_t ReverseBytes(uint8_t n) { return n; }
|
2020-08-04 02:26:45 +01:00
|
|
|
#ifdef __GNUC__
|
2020-08-03 06:07:32 +01:00
|
|
|
inline uint16_t ReverseBytes(uint16_t n) { return __builtin_bswap16(n); }
|
|
|
|
inline uint32_t ReverseBytes(uint32_t n) { return __builtin_bswap32(n); }
|
2020-08-04 02:26:45 +01:00
|
|
|
inline uint64_t ReverseBytes(uint64_t n) { return __builtin_bswap64(n); }
|
|
|
|
#else
|
2020-08-03 06:07:32 +01:00
|
|
|
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); }
|
2020-08-04 02:26:45 +01:00
|
|
|
#endif
|
2020-08-01 01:49:07 +01:00
|
|
|
|
2020-08-03 06:07:32 +01:00
|
|
|
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))); }
|
2020-08-04 02:38:00 +01:00
|
|
|
|
2020-08-07 04:09:15 +01:00
|
|
|
template <typename J, typename T>
|
2020-08-07 04:12:10 +01:00
|
|
|
inline T ReverseBytesBitcast(T n)
|
|
|
|
{
|
|
|
|
static_assert(sizeof(T) == sizeof(J));
|
|
|
|
return std::bit_cast<T>(ReverseBytes(std::bit_cast<J>(n)));
|
|
|
|
}
|
2020-08-07 04:09:15 +01:00
|
|
|
|
2020-08-07 04:09:49 +01:00
|
|
|
inline float ReverseBytes(float n) { return ReverseBytesBitcast<uint32_t>(n); }
|
2020-08-07 04:09:15 +01:00
|
|
|
inline double ReverseBytes(double n) { return ReverseBytesBitcast<uint64_t>(n); }
|
2020-08-03 06:07:32 +01:00
|
|
|
}
|