2017-07-15 11:24:08 +02:00
|
|
|
/*****************************************************************************
|
2019-03-17 08:16:15 +01:00
|
|
|
* Copyright (c) 2014-2019 OpenRCT2 developers
|
2018-06-15 14:07:34 +02:00
|
|
|
*
|
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
*
|
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
|
|
|
*****************************************************************************/
|
2017-07-15 11:24:08 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../common.h"
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
template<size_t size> struct ByteSwapT
|
|
|
|
{
|
|
|
|
};
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
template<> struct ByteSwapT<1>
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2020-02-17 11:43:47 +01:00
|
|
|
using UIntType = uint8_t;
|
2018-06-20 17:28:51 +02:00
|
|
|
static uint8_t SwapBE(uint8_t value)
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
template<> struct ByteSwapT<2>
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2020-02-17 11:43:47 +01:00
|
|
|
using UIntType = uint16_t;
|
2018-06-20 17:28:51 +02:00
|
|
|
static uint16_t SwapBE(uint16_t value)
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
return static_cast<uint16_t>((value << 8) | (value >> 8));
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
template<> struct ByteSwapT<4>
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2020-02-17 11:43:47 +01:00
|
|
|
using UIntType = uint32_t;
|
2018-06-20 17:28:51 +02:00
|
|
|
static uint32_t SwapBE(uint32_t value)
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
return static_cast<uint32_t>(
|
|
|
|
((value << 24) | ((value << 8) & 0x00FF0000) | ((value >> 8) & 0x0000FF00) | (value >> 24)));
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-02 05:18:36 +01:00
|
|
|
template<> struct ByteSwapT<8>
|
|
|
|
{
|
2020-02-17 11:43:47 +01:00
|
|
|
using UIntType = uint64_t;
|
2019-01-02 05:18:36 +01:00
|
|
|
static uint64_t SwapBE(uint64_t value)
|
|
|
|
{
|
|
|
|
value = (value & 0x00000000FFFFFFFF) << 32 | (value & 0xFFFFFFFF00000000) >> 32;
|
|
|
|
value = (value & 0x0000FFFF0000FFFF) << 16 | (value & 0xFFFF0000FFFF0000) >> 16;
|
|
|
|
value = (value & 0x00FF00FF00FF00FF) << 8 | (value & 0xFF00FF00FF00FF00) >> 8;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
template<typename T> static T ByteSwapBE(const T& value)
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2020-02-17 11:43:47 +01:00
|
|
|
using ByteSwap = ByteSwapT<sizeof(T)>;
|
2019-08-26 15:15:44 +02:00
|
|
|
typename ByteSwap::UIntType result = ByteSwap::SwapBE(reinterpret_cast<const typename ByteSwap::UIntType&>(value));
|
|
|
|
return *reinterpret_cast<T*>(&result);
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|