Merge branch 'endianness' into 'master'

Utility function for little-endian <-> big-endian conversion

See merge request OpenMW/openmw!516
pull/593/head
Petr Mikheev 4 years ago
commit 66559e8827

@ -0,0 +1,82 @@
#ifndef COMPONENTS_MISC_ENDIANNESS_H
#define COMPONENTS_MISC_ENDIANNESS_H
#include <cstdint>
namespace Misc
{
// Two-way conversion little-endian <-> big-endian
template <typename T>
void swapEndiannessInplace(T& v)
{
static_assert(std::is_arithmetic_v<T>);
static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8);
if constexpr (sizeof(T) == 2)
{
uint16_t& v16 = *reinterpret_cast<uint16_t*>(&v);
v16 = (v16 >> 8) | (v16 << 8);
}
if constexpr (sizeof(T) == 4)
{
uint32_t& v32 = *reinterpret_cast<uint32_t*>(&v);
v32 = (v32 >> 24) | ((v32 >> 8) & 0xff00) | ((v32 & 0xff00) << 8) || v32 << 24;
}
if constexpr (sizeof(T) == 8)
{
uint64_t& v64 = *reinterpret_cast<uint64_t*>(&v);
v64 = (v64 >> 56) | ((v64 & 0x00ff'0000'0000'0000) >> 40) | ((v64 & 0x0000'ff00'0000'0000) >> 24)
| ((v64 & 0x0000'00ff'0000'0000) >> 8) | ((v64 & 0x0000'0000'ff00'0000) << 8)
| ((v64 & 0x0000'0000'00ff'0000) << 24) | ((v64 & 0x0000'0000'0000'ff00) << 40) | (v64 << 56);
}
}
#ifdef _WIN32
constexpr bool IS_LITTLE_ENDIAN = true;
constexpr bool IS_BIG_ENDIAN = false;
#else
constexpr bool IS_LITTLE_ENDIAN = __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__;
constexpr bool IS_BIG_ENDIAN = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
#endif
// Usage: swapEndiannessInplaceIf<IS_BIG_ENDIAN>(v) - native to little-endian or back
// swapEndiannessInplaceIf<IS_LITTLE_ENDIAN>(v) - native to big-endian or back
template <bool C, typename T>
void swapEndiannessInplaceIf(T& v)
{
static_assert(std::is_arithmetic_v<T>);
static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8);
if constexpr (C)
swapEndiannessInplace(v);
}
template <typename T>
T toLittleEndian(T v)
{
swapEndiannessInplaceIf<IS_BIG_ENDIAN>(v);
return v;
}
template <typename T>
T fromLittleEndian(T v)
{
swapEndiannessInplaceIf<IS_BIG_ENDIAN>(v);
return v;
}
template <typename T>
T toBigEndian(T v)
{
swapEndiannessInplaceIf<IS_LITTLE_ENDIAN>(v);
return v;
}
template <typename T>
T fromBigEndian(T v)
{
swapEndiannessInplaceIf<IS_LITTLE_ENDIAN>(v);
return v;
}
}
#endif // COMPONENTS_MISC_ENDIANNESS_H

@ -7,7 +7,7 @@ namespace Nif
osg::Quat NIFStream::getQuaternion()
{
float f[4];
readLittleEndianBufferOfType<4, float,uint32_t>(inp, (float*)&f);
readLittleEndianBufferOfType<4, float>(inp, (float*)&f);
osg::Quat quat;
quat.w() = f[0];
quat.x() = f[1];

@ -9,6 +9,7 @@
#include <vector>
#include <components/files/constrainedfilestream.hpp>
#include <components/misc/endianness.hpp>
#include <osg/Vec3f>
#include <osg/Vec4f>
@ -22,61 +23,30 @@ namespace Nif
class NIFFile;
/*
readLittleEndianBufferOfType: This template should only be used with non POD data types
readLittleEndianBufferOfType: This template should only be used with arithmetic types
*/
template <uint32_t numInstances, typename T, typename IntegerT> inline void readLittleEndianBufferOfType(Files::IStreamPtr &pIStream, T* dest)
template <uint32_t numInstances, typename T> inline void readLittleEndianBufferOfType(Files::IStreamPtr &pIStream, T* dest)
{
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
pIStream->read((char*)dest, numInstances * sizeof(T));
#else
uint8_t* destByteBuffer = (uint8_t*)dest;
pIStream->read((char*)dest, numInstances * sizeof(T));
/*
Due to the loop iterations being known at compile time,
this nested loop will most likely be unrolled
For example, for 2 instances of a 4 byte data type, you should get the below result
*/
union {
IntegerT i;
T t;
} u;
for (uint32_t i = 0; i < numInstances; i++)
{
u = { 0 };
for (uint32_t byte = 0; byte < sizeof(T); byte++)
u.i |= (((IntegerT)destByteBuffer[i * sizeof(T) + byte]) << (byte * 8));
dest[i] = u.t;
}
#endif
if constexpr (Misc::IS_BIG_ENDIAN)
for (uint32_t i = 0; i < numInstances; i++)
Misc::swapEndiannessInplace(dest[i]);
}
/*
readLittleEndianDynamicBufferOfType: This template should only be used with non POD data types
readLittleEndianDynamicBufferOfType: This template should only be used with arithmetic types
*/
template <typename T, typename IntegerT> inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr &pIStream, T* dest, uint32_t numInstances)
template <typename T> inline void readLittleEndianDynamicBufferOfType(Files::IStreamPtr &pIStream, T* dest, uint32_t numInstances)
{
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
pIStream->read((char*)dest, numInstances * sizeof(T));
#else
uint8_t* destByteBuffer = (uint8_t*)dest;
pIStream->read((char*)dest, numInstances * sizeof(T));
union {
IntegerT i;
T t;
} u;
for (uint32_t i = 0; i < numInstances; i++)
{
u.i = 0;
for (uint32_t byte = 0; byte < sizeof(T); byte++)
u.i |= ((IntegerT)destByteBuffer[i * sizeof(T) + byte]) << (byte * 8);
dest[i] = u.t;
}
#endif
if constexpr (Misc::IS_BIG_ENDIAN)
for (uint32_t i = 0; i < numInstances; i++)
Misc::swapEndiannessInplace(dest[i]);
}
template<typename type, typename IntegerT> type inline readLittleEndianType(Files::IStreamPtr &pIStream)
template<typename type> type inline readLittleEndianType(Files::IStreamPtr &pIStream)
{
type val;
readLittleEndianBufferOfType<1,type,IntegerT>(pIStream, (type*)&val);
readLittleEndianBufferOfType<1, type>(pIStream, (type*)&val);
return val;
}
@ -95,59 +65,59 @@ public:
char getChar()
{
return readLittleEndianType<char,char>(inp);
return readLittleEndianType<char>(inp);
}
short getShort()
{
return readLittleEndianType<short,short>(inp);
return readLittleEndianType<short>(inp);
}
unsigned short getUShort()
{
return readLittleEndianType<unsigned short,unsigned short>(inp);
return readLittleEndianType<unsigned short>(inp);
}
int getInt()
{
return readLittleEndianType<int,int>(inp);
return readLittleEndianType<int>(inp);
}
unsigned int getUInt()
{
return readLittleEndianType<unsigned int,unsigned int>(inp);
return readLittleEndianType<unsigned int>(inp);
}
float getFloat()
{
return readLittleEndianType<float,uint32_t>(inp);
return readLittleEndianType<float>(inp);
}
osg::Vec2f getVector2()
{
osg::Vec2f vec;
readLittleEndianBufferOfType<2,float,uint32_t>(inp, (float*)&vec._v[0]);
readLittleEndianBufferOfType<2,float>(inp, (float*)&vec._v[0]);
return vec;
}
osg::Vec3f getVector3()
{
osg::Vec3f vec;
readLittleEndianBufferOfType<3, float,uint32_t>(inp, (float*)&vec._v[0]);
readLittleEndianBufferOfType<3, float>(inp, (float*)&vec._v[0]);
return vec;
}
osg::Vec4f getVector4()
{
osg::Vec4f vec;
readLittleEndianBufferOfType<4, float,uint32_t>(inp, (float*)&vec._v[0]);
readLittleEndianBufferOfType<4, float>(inp, (float*)&vec._v[0]);
return vec;
}
Matrix3 getMatrix3()
{
Matrix3 mat;
readLittleEndianBufferOfType<9, float,uint32_t>(inp, (float*)&mat.mValues);
readLittleEndianBufferOfType<9, float>(inp, (float*)&mat.mValues);
return mat;
}
@ -181,14 +151,14 @@ public:
///Read in a string of the length specified in the file
std::string getSizedString()
{
size_t size = readLittleEndianType<uint32_t,uint32_t>(inp);
size_t size = readLittleEndianType<uint32_t>(inp);
return getSizedString(size);
}
///Specific to Bethesda headers, uses a byte for length
std::string getExportString()
{
size_t size = static_cast<size_t>(readLittleEndianType<uint8_t,uint8_t>(inp));
size_t size = static_cast<size_t>(readLittleEndianType<uint8_t>(inp));
return getSizedString(size);
}
@ -203,58 +173,58 @@ public:
void getChars(std::vector<char> &vec, size_t size)
{
vec.resize(size);
readLittleEndianDynamicBufferOfType<char,char>(inp, vec.data(), size);
readLittleEndianDynamicBufferOfType<char>(inp, vec.data(), size);
}
void getUChars(std::vector<unsigned char> &vec, size_t size)
{
vec.resize(size);
readLittleEndianDynamicBufferOfType<unsigned char,unsigned char>(inp, vec.data(), size);
readLittleEndianDynamicBufferOfType<unsigned char>(inp, vec.data(), size);
}
void getUShorts(std::vector<unsigned short> &vec, size_t size)
{
vec.resize(size);
readLittleEndianDynamicBufferOfType<unsigned short,unsigned short>(inp, vec.data(), size);
readLittleEndianDynamicBufferOfType<unsigned short>(inp, vec.data(), size);
}
void getFloats(std::vector<float> &vec, size_t size)
{
vec.resize(size);
readLittleEndianDynamicBufferOfType<float,uint32_t>(inp, vec.data(), size);
readLittleEndianDynamicBufferOfType<float>(inp, vec.data(), size);
}
void getInts(std::vector<int> &vec, size_t size)
{
vec.resize(size);
readLittleEndianDynamicBufferOfType<int,int>(inp, vec.data(), size);
readLittleEndianDynamicBufferOfType<int>(inp, vec.data(), size);
}
void getUInts(std::vector<unsigned int> &vec, size_t size)
{
vec.resize(size);
readLittleEndianDynamicBufferOfType<unsigned int,unsigned int>(inp, vec.data(), size);
readLittleEndianDynamicBufferOfType<unsigned int>(inp, vec.data(), size);
}
void getVector2s(std::vector<osg::Vec2f> &vec, size_t size)
{
vec.resize(size);
/* The packed storage of each Vec2f is 2 floats exactly */
readLittleEndianDynamicBufferOfType<float,uint32_t>(inp,(float*)vec.data(), size*2);
readLittleEndianDynamicBufferOfType<float>(inp,(float*)vec.data(), size*2);
}
void getVector3s(std::vector<osg::Vec3f> &vec, size_t size)
{
vec.resize(size);
/* The packed storage of each Vec3f is 3 floats exactly */
readLittleEndianDynamicBufferOfType<float,uint32_t>(inp, (float*)vec.data(), size*3);
readLittleEndianDynamicBufferOfType<float>(inp, (float*)vec.data(), size*3);
}
void getVector4s(std::vector<osg::Vec4f> &vec, size_t size)
{
vec.resize(size);
/* The packed storage of each Vec4f is 4 floats exactly */
readLittleEndianDynamicBufferOfType<float,uint32_t>(inp, (float*)vec.data(), size*4);
readLittleEndianDynamicBufferOfType<float>(inp, (float*)vec.data(), size*4);
}
void getQuaternions(std::vector<osg::Quat> &quat, size_t size)

Loading…
Cancel
Save