Common: Move byte swapping utilities into their own header

This moves all the byte swapping utilities into a header named Swap.h.

A dedicated header is much more preferable here due to the size of the
code itself. In general usage throughout the codebase, CommonFuncs.h was
generally only included for these functions anyway. These being in their
own header avoids dumping the lesser used utilities into scope. As well
as providing a localized area for more utilities related to byte
swapping in the future (should they be needed). This also makes it nicer
to identify which files depend on the byte swapping utilities in
particular.

Since this is a completely new header, moving the code uncovered a few
indirect includes, as well as making some other inclusions unnecessary.
This commit is contained in:
Lioncash
2017-03-03 14:43:52 -05:00
parent 51e932b4ec
commit 552c0d8404
88 changed files with 347 additions and 296 deletions

View File

@ -4,12 +4,7 @@
#pragma once
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#endif
#include <cstddef>
#include <cstring>
#include <string>
#include "Common/CommonTypes.h"
@ -29,13 +24,6 @@ constexpr size_t ArraySize(T (&arr)[N])
#ifndef _WIN32
#include <errno.h>
#ifdef __linux__
#include <byteswap.h>
#elif defined __FreeBSD__
#include <sys/endian.h>
#endif
// go to debugger mode
#define Crash() \
{ \
@ -103,153 +91,3 @@ __declspec(dllimport) void __stdcall DebugBreak(void);
// This function might change the error code.
// Defined in Misc.cpp.
std::string GetLastErrorMsg();
namespace Common
{
inline u8 swap8(u8 _data)
{
return _data;
}
inline u32 swap24(const u8* _data)
{
return (_data[0] << 16) | (_data[1] << 8) | _data[2];
}
#if defined(ANDROID) || defined(__OpenBSD__)
#undef swap16
#undef swap32
#undef swap64
#endif
#ifdef _WIN32
inline u16 swap16(u16 _data)
{
return _byteswap_ushort(_data);
}
inline u32 swap32(u32 _data)
{
return _byteswap_ulong(_data);
}
inline u64 swap64(u64 _data)
{
return _byteswap_uint64(_data);
}
#elif __linux__
inline u16 swap16(u16 _data)
{
return bswap_16(_data);
}
inline u32 swap32(u32 _data)
{
return bswap_32(_data);
}
inline u64 swap64(u64 _data)
{
return bswap_64(_data);
}
#elif __APPLE__
inline __attribute__((always_inline)) u16 swap16(u16 _data)
{
return OSSwapInt16(_data);
}
inline __attribute__((always_inline)) u32 swap32(u32 _data)
{
return OSSwapInt32(_data);
}
inline __attribute__((always_inline)) u64 swap64(u64 _data)
{
return OSSwapInt64(_data);
}
#elif __FreeBSD__
inline u16 swap16(u16 _data)
{
return bswap16(_data);
}
inline u32 swap32(u32 _data)
{
return bswap32(_data);
}
inline u64 swap64(u64 _data)
{
return bswap64(_data);
}
#else
// Slow generic implementation.
inline u16 swap16(u16 data)
{
return (data >> 8) | (data << 8);
}
inline u32 swap32(u32 data)
{
return (swap16(data) << 16) | swap16(data >> 16);
}
inline u64 swap64(u64 data)
{
return ((u64)swap32(data) << 32) | swap32(data >> 32);
}
#endif
inline u16 swap16(const u8* data)
{
u16 value;
std::memcpy(&value, data, sizeof(u16));
return swap16(value);
}
inline u32 swap32(const u8* data)
{
u32 value;
std::memcpy(&value, data, sizeof(u32));
return swap32(value);
}
inline u64 swap64(const u8* data)
{
u64 value;
std::memcpy(&value, data, sizeof(u64));
return swap64(value);
}
template <int count>
void swap(u8*);
template <>
inline void swap<1>(u8* data)
{
}
template <>
inline void swap<2>(u8* data)
{
const u16 value = swap16(data);
std::memcpy(data, &value, sizeof(u16));
}
template <>
inline void swap<4>(u8* data)
{
const u32 value = swap32(data);
std::memcpy(data, &value, sizeof(u32));
}
template <>
inline void swap<8>(u8* data)
{
const u64 value = swap64(data);
std::memcpy(data, &value, sizeof(u64));
}
template <typename T>
inline T FromBigEndian(T data)
{
static_assert(std::is_arithmetic<T>::value, "function only makes sense with arithmetic types");
swap<sizeof(data)>(reinterpret_cast<u8*>(&data));
return data;
}
} // Namespace Common