1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-10-05 00:27:30 +02:00
openrw/rwlib/source/rw/bit_cast.cpp
Filip Gawin a60bc20585 Fix misaligned memory(UB)
X86 is able to deal with
misaligned memory, but it can hurt perf.
Other arch like for example mips
is not able to digest it.

So in order of portability we should get
rid of this UB.
2018-01-13 00:49:17 +00:00

17 lines
407 B
C++

#ifndef _LIBRW_BIT_CAST_CPP_
#define _LIBRW_BIT_CAST_CPP_
//Based on https://gist.github.com/socantre/3472964
#include <cstring> // memcpy
#include <type_traits> // is_trivially_copyable
#include "rw/defines.hpp" // RW_ASSERT
template <class Dest, class Source>
inline Dest bit_cast(Source const &source) {
Dest dest = Dest{};
std::memcpy(&dest, &source, sizeof(Dest));
return dest;
}
#endif