diff --git a/include/bx/bx.h b/include/bx/bx.h index 4877f56..79eff13 100644 --- a/include/bx/bx.h +++ b/include/bx/bx.h @@ -214,6 +214,10 @@ namespace bx template constexpr bool isPowerOf2(Ty _a); + /// Returns a value of type To by reinterpreting the object representation of From. + template + constexpr To bit_cast(const From& value) noexcept; + /// Copy memory block. /// /// @param _dst Destination pointer. diff --git a/include/bx/inline/bx.inl b/include/bx/inline/bx.inl index 80f7194..46c6c7e 100644 --- a/include/bx/inline/bx.inl +++ b/include/bx/inline/bx.inl @@ -147,4 +147,14 @@ namespace bx return _a && !(_a & (_a - 1) ); } + template + inline constexpr To bit_cast(const From& value) noexcept + { + BX_STATIC_ASSERT(sizeof(To) == sizeof(From), "To and From must be the same size."); + BX_STATIC_ASSERT(isTriviallyConstructible(), "Destination target must be trivially constructible."); + To result; + bx::memCopy(&result, &value, sizeof(To)); + return result; + } + } // namespace bx