Fix minor warnings and get rid of 'portable_endian_bswap.h' (in house
impl instead)
This commit is contained in:
117
external/portable_endian_bswap.h
vendored
117
external/portable_endian_bswap.h
vendored
@@ -1,117 +0,0 @@
|
||||
// "License": Public Domain
|
||||
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
|
||||
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
|
||||
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
|
||||
// an example on how to get the endian conversion functions on different platforms.
|
||||
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
#define bswap_16(x) (((x) & 0xFF00u) >> 8) | (((x) & 0x00FFu) << 8)
|
||||
#define bswap_32(x) (((x) & 0xFF000000u) >> 24u) | (((x) & 0x00FF0000u) >> 8u) | (((x) & 0x0000FF00u) << 8u) | (((x) & 0x000000FFu) << 24u)
|
||||
#define bswap_64(x) (((x) & 0xFF00000000000000u) >> 56u) | (((x) & 0x00FF000000000000u) >> 40u) | (((x) & 0x0000FF0000000000u) >> 24u) | (((x) & 0x000000FF00000000u) >> 8u) | (((x) & 0x00000000FF000000u) << 8u) | (((x) & 0x0000000000FF0000u) << 24u) | (((x) & 0x000000000000FF00u) << 40u) | (((x) & 0x00000000000000FFu) << 56u)
|
||||
|
||||
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
|
||||
|
||||
# define __WINDOWS__
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) || defined(__CYGWIN__)
|
||||
|
||||
# include <endian.h>
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
|
||||
# include <libkern/OSByteOrder.h>
|
||||
|
||||
# define htobe16(x) OSSwapHostToBigInt16(x)
|
||||
# define htole16(x) OSSwapHostToLittleInt16(x)
|
||||
# define be16toh(x) OSSwapBigToHostInt16(x)
|
||||
# define le16toh(x) OSSwapLittleToHostInt16(x)
|
||||
|
||||
# define htobe32(x) OSSwapHostToBigInt32(x)
|
||||
# define htole32(x) OSSwapHostToLittleInt32(x)
|
||||
# define be32toh(x) OSSwapBigToHostInt32(x)
|
||||
# define le32toh(x) OSSwapLittleToHostInt32(x)
|
||||
|
||||
# define htobe64(x) OSSwapHostToBigInt64(x)
|
||||
# define htole64(x) OSSwapHostToLittleInt64(x)
|
||||
# define be64toh(x) OSSwapBigToHostInt64(x)
|
||||
# define le64toh(x) OSSwapLittleToHostInt64(x)
|
||||
|
||||
# define __BYTE_ORDER BYTE_ORDER
|
||||
# define __BIG_ENDIAN BIG_ENDIAN
|
||||
# define __LITTLE_ENDIAN LITTLE_ENDIAN
|
||||
# define __PDP_ENDIAN PDP_ENDIAN
|
||||
|
||||
#elif defined(__OpenBSD__)
|
||||
|
||||
# include <sys/endian.h>
|
||||
|
||||
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
|
||||
# include <sys/endian.h>
|
||||
|
||||
# define be16toh(x) betoh16(x)
|
||||
# define le16toh(x) letoh16(x)
|
||||
|
||||
# define be32toh(x) betoh32(x)
|
||||
# define le32toh(x) letoh32(x)
|
||||
|
||||
# define be64toh(x) betoh64(x)
|
||||
# define le64toh(x) letoh64(x)
|
||||
|
||||
#elif defined(__WINDOWS__)
|
||||
|
||||
# if BYTE_ORDER == LITTLE_ENDIAN
|
||||
|
||||
# define htobe16(x) bswap_16(x)
|
||||
# define htole16(x) (x)
|
||||
# define be16toh(x) bswap_16(x)
|
||||
# define le16toh(x) (x)
|
||||
|
||||
# define htobe32(x) bswap_32(x)
|
||||
# define htole32(x) (x)
|
||||
# define be32toh(x) bswap_32(x)
|
||||
# define le32toh(x) (x)
|
||||
|
||||
# define htobe64(x) bswap_64(x)
|
||||
# define htole64(x) (x)
|
||||
# define be64toh(x) bswap_64(x)
|
||||
# define le64toh(x) (x)
|
||||
|
||||
# elif BYTE_ORDER == BIG_ENDIAN
|
||||
|
||||
/* that would be xbox 360 */
|
||||
# define htobe16(x) (x)
|
||||
# define htole16(x) bswap_16(x)
|
||||
# define be16toh(x) (x)
|
||||
# define le16toh(x) bswap_16(x)
|
||||
|
||||
# define htobe32(x) (x)
|
||||
# define htole32(x) bswap_32(x)
|
||||
# define be32toh(x) (x)
|
||||
# define le32toh(x) bswap_32(x)
|
||||
|
||||
# define htobe64(x) (x)
|
||||
# define htole64(x) bswap_64(x)
|
||||
# define be64toh(x) (x)
|
||||
# define le64toh(x) bswap_64(x)
|
||||
|
||||
# else
|
||||
|
||||
# error byte order not supported
|
||||
|
||||
# endif
|
||||
|
||||
# define __BYTE_ORDER BYTE_ORDER
|
||||
# define __BIG_ENDIAN BIG_ENDIAN
|
||||
# define __LITTLE_ENDIAN LITTLE_ENDIAN
|
||||
# define __PDP_ENDIAN PDP_ENDIAN
|
||||
|
||||
#else
|
||||
|
||||
# error platform not supported
|
||||
|
||||
#endif
|
||||
@@ -6,15 +6,15 @@
|
||||
|
||||
enum class CompositorCategory { Windows, MacOS, XCB, Wayland };
|
||||
|
||||
static inline CompositorCategory GetOSCompositorCategory() {
|
||||
static CompositorCategory GetOSCompositorCategory() {
|
||||
const QString platform_name = QGuiApplication::platformName();
|
||||
if (platform_name == QStringLiteral("windows"))
|
||||
return CompositorCategory::Windows;
|
||||
else if (platform_name == QStringLiteral("xcb"))
|
||||
if (platform_name == QStringLiteral("xcb"))
|
||||
return CompositorCategory::XCB;
|
||||
else if (platform_name == QStringLiteral("wayland") || platform_name == QStringLiteral("wayland-egl"))
|
||||
if (platform_name == QStringLiteral("wayland") || platform_name == QStringLiteral("wayland-egl"))
|
||||
return CompositorCategory::Wayland;
|
||||
else if (platform_name == QStringLiteral("cocoa") || platform_name == QStringLiteral("ios"))
|
||||
if (platform_name == QStringLiteral("cocoa") || platform_name == QStringLiteral("ios"))
|
||||
return CompositorCategory::MacOS;
|
||||
|
||||
Util::error("Unknown Qt platform!");
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <log.hpp>
|
||||
#include <portable_endian_bswap.h>
|
||||
#include <byteswap.hpp>
|
||||
|
||||
namespace Util {
|
||||
template <typename T>
|
||||
static FORCE_INLINE const std::vector<u8> &IntegralToBuffer(const T &val) {
|
||||
static FORCE_INLINE std::vector<u8> IntegralToBuffer(const T &val) {
|
||||
std::vector<u8> ret{};
|
||||
ret.resize(sizeof(T));
|
||||
|
||||
@@ -17,11 +17,11 @@ static FORCE_INLINE const std::vector<u8> &IntegralToBuffer(const T &val) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static FORCE_INLINE T ReadAccess(const u8 *data, u32 index) {
|
||||
static FORCE_INLINE T ReadAccess(const u8 *data, const u32 index) {
|
||||
if constexpr (sizeof(T) == 8) {
|
||||
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
|
||||
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
|
||||
T result = ((T)hi << 32) | (T)lo;
|
||||
const T result = static_cast<T>(hi) << 32 | static_cast<T>(lo);
|
||||
return result;
|
||||
} else {
|
||||
return *reinterpret_cast<const T *>(&data[index]);
|
||||
@@ -29,11 +29,11 @@ static FORCE_INLINE T ReadAccess(const u8 *data, u32 index) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static FORCE_INLINE T ReadAccess(const std::vector<u8> &data, u32 index) {
|
||||
static FORCE_INLINE T ReadAccess(const std::vector<u8> &data, const u32 index) {
|
||||
if constexpr (sizeof(T) == 8) {
|
||||
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
|
||||
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
|
||||
T result = ((T)hi << 32) | (T)lo;
|
||||
const T result = (static_cast<T>(hi) << 32) | static_cast<T>(lo);
|
||||
return result;
|
||||
} else {
|
||||
return *reinterpret_cast<const T *>(&data[index]);
|
||||
@@ -41,11 +41,11 @@ static FORCE_INLINE T ReadAccess(const std::vector<u8> &data, u32 index) {
|
||||
}
|
||||
|
||||
template <typename T, size_t Size>
|
||||
static FORCE_INLINE T ReadAccess(const std::array<u8, Size> &data, u32 index) {
|
||||
static FORCE_INLINE T ReadAccess(const std::array<u8, Size> &data, const u32 index) {
|
||||
if constexpr (sizeof(T) == 8) {
|
||||
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
|
||||
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
|
||||
T result = ((T)hi << 32) | (T)lo;
|
||||
const T result = static_cast<T>(hi) << 32 | static_cast<T>(lo);
|
||||
return result;
|
||||
} else {
|
||||
return *reinterpret_cast<const T *>(&data[index]);
|
||||
@@ -53,10 +53,10 @@ static FORCE_INLINE T ReadAccess(const std::array<u8, Size> &data, u32 index) {
|
||||
}
|
||||
|
||||
template <typename T, size_t Size>
|
||||
static FORCE_INLINE void WriteAccess(std::array<u8, Size> &data, u32 index, T val) {
|
||||
static FORCE_INLINE void WriteAccess(std::array<u8, Size> &data, const u32 index, const T val) {
|
||||
if constexpr (sizeof(T) == 8) {
|
||||
u32 hi = val >> 32;
|
||||
u32 lo = val;
|
||||
const u32 hi = val >> 32;
|
||||
const u32 lo = val;
|
||||
|
||||
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
|
||||
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
|
||||
@@ -66,10 +66,10 @@ static FORCE_INLINE void WriteAccess(std::array<u8, Size> &data, u32 index, T va
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static FORCE_INLINE void WriteAccess(std::vector<u8> &data, u32 index, T val) {
|
||||
static FORCE_INLINE void WriteAccess(std::vector<u8> &data, const u32 index, const T val) {
|
||||
if constexpr (sizeof(T) == 8) {
|
||||
u32 hi = val >> 32;
|
||||
u32 lo = val;
|
||||
const u32 hi = val >> 32;
|
||||
const u32 lo = val;
|
||||
|
||||
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
|
||||
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
|
||||
@@ -79,10 +79,10 @@ static FORCE_INLINE void WriteAccess(std::vector<u8> &data, u32 index, T val) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static FORCE_INLINE void WriteAccess(u8 *data, u32 index, T val) {
|
||||
static FORCE_INLINE void WriteAccess(u8 *data, const u32 index, const T val) {
|
||||
if constexpr (sizeof(T) == 8) {
|
||||
u32 hi = val >> 32;
|
||||
u32 lo = val;
|
||||
const u32 hi = val >> 32;
|
||||
const u32 lo = val;
|
||||
|
||||
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
|
||||
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
|
||||
@@ -93,43 +93,41 @@ static FORCE_INLINE void WriteAccess(u8 *data, u32 index, T val) {
|
||||
|
||||
FORCE_INLINE void SwapBuffer32(std::vector<u8> &data) {
|
||||
for (size_t i = 0; i < data.size(); i += 4) {
|
||||
u32 original = *(u32 *)&data[i];
|
||||
*(u32 *)&data[i] = bswap_32(original);
|
||||
const u32 original = *reinterpret_cast<u32 *>(&data[i]);
|
||||
*reinterpret_cast<u32 *>(&data[i]) = bswap(original);
|
||||
}
|
||||
}
|
||||
|
||||
FORCE_INLINE void SwapBuffer16(std::vector<u8> &data) {
|
||||
for (size_t i = 0; i < data.size(); i += 2) {
|
||||
u16 original = *(u16 *)&data[i];
|
||||
*(u16 *)&data[i] = bswap_16(original);
|
||||
const u16 original = *reinterpret_cast<u16 *>(&data[i]);
|
||||
*reinterpret_cast<u16 *>(&data[i]) = bswap(original);
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t Size>
|
||||
FORCE_INLINE void SwapBuffer32(std::array<u8, Size> &data) {
|
||||
for (size_t i = 0; i < Size; i += 4) {
|
||||
u32 original = *(u32 *)&data[i];
|
||||
*(u32 *)&data[i] = bswap_32(original);
|
||||
const u32 original = *static_cast<u32 *>(&data[i]);
|
||||
*static_cast<u32 *>(&data[i]) = bswap(original);
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t Size>
|
||||
FORCE_INLINE void SwapBuffer16(std::array<u8, Size> &data) {
|
||||
for (size_t i = 0; i < Size; i += 2) {
|
||||
u16 original = *(u16 *)&data[i];
|
||||
*(u16 *)&data[i] = bswap_16(original);
|
||||
const u16 original = *static_cast<u16 *>(&data[i]);
|
||||
*static_cast<u16 *>(&data[i]) = bswap(original);
|
||||
}
|
||||
}
|
||||
|
||||
FORCE_INLINE u32 crc32(u32 crc, const u8 *buf, size_t len) {
|
||||
FORCE_INLINE u32 crc32(u32 crc, const u8 *buf, const size_t len) {
|
||||
static u32 table[256];
|
||||
static int have_table = 0;
|
||||
u32 rem;
|
||||
u8 octet;
|
||||
|
||||
if (have_table == 0) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
rem = i;
|
||||
u32 rem = i;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if (rem & 1) {
|
||||
rem >>= 1;
|
||||
@@ -144,18 +142,20 @@ FORCE_INLINE u32 crc32(u32 crc, const u8 *buf, size_t len) {
|
||||
|
||||
crc = ~crc;
|
||||
for (int i = 0; i < len; i++) {
|
||||
octet = buf[i]; /* Cast to unsigned octet. */
|
||||
const u8 octet = buf[i]; /* Cast to unsigned octet. */
|
||||
crc = (crc >> 8) ^ table[(crc & 0xff) ^ octet];
|
||||
}
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
FORCE_INLINE void *aligned_alloc(size_t alignment, size_t size) { return _aligned_malloc(size, alignment); }
|
||||
FORCE_INLINE void *aligned_alloc(const size_t alignment, const size_t size) { return _aligned_malloc(size, alignment); }
|
||||
|
||||
FORCE_INLINE void aligned_free(void *ptr) { _aligned_free(ptr); }
|
||||
#else
|
||||
FORCE_INLINE void *aligned_alloc(size_t alignment, size_t size) { return std::aligned_alloc(alignment, size); }
|
||||
FORCE_INLINE void *aligned_alloc(const size_t alignment, const size_t size) {
|
||||
return std::aligned_alloc(alignment, size);
|
||||
}
|
||||
|
||||
FORCE_INLINE void aligned_free(void *ptr) { std::free(ptr); }
|
||||
#endif
|
||||
|
||||
22
src/utils/byteswap.hpp
Normal file
22
src/utils/byteswap.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <common.hpp>
|
||||
|
||||
template <typename T>
|
||||
T bswap(T);
|
||||
|
||||
template <>
|
||||
inline u16 bswap(const u16 x) {
|
||||
return (x & 0xFF00u) >> 8 | (x & 0x00FFu) << 8;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline u32 bswap(const u32 x) {
|
||||
return (x & 0xFF000000u) >> 24u | (x & 0x00FF0000u) >> 8u | (x & 0x0000FF00u) << 8u | (x & 0x000000FFu) << 24u;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline u64 bswap(const u64 x) {
|
||||
return (x & 0xFF00000000000000u) >> 56u | (x & 0x00FF000000000000u) >> 40u | (x & 0x0000FF0000000000u) >> 24u |
|
||||
(x & 0x000000FF00000000u) >> 8u | (x & 0x00000000FF000000u) << 8u | (x & 0x0000000000FF0000u) << 24u |
|
||||
(x & 0x000000000000FF00u) << 40u | (x & 0x00000000000000FFu) << 56u;
|
||||
}
|
||||
Reference in New Issue
Block a user