mirror of
https://github.com/rwengine/openrw.git
synced 2024-10-05 08:37:20 +02:00
a60bc20585
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.
17 lines
407 B
C++
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
|