From 9418f54ebc58fc3b2c98920a0f83afabf9a7f63f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 24 Jul 2021 20:39:40 -0500 Subject: [PATCH] [ox/std] Add support for proper std::bit_cast with C++20 enabled --- deps/ox/src/ox/std/bit.hpp | 26 +++++++++++++++++++++++++- deps/ox/src/ox/std/memops.hpp | 6 +++--- deps/ox/src/ox/std/types.hpp | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/deps/ox/src/ox/std/bit.hpp b/deps/ox/src/ox/std/bit.hpp index fd6b61b6..6f7c06f0 100644 --- a/deps/ox/src/ox/std/bit.hpp +++ b/deps/ox/src/ox/std/bit.hpp @@ -8,6 +8,10 @@ #pragma once +#if __cplusplus >= 202002L && defined(OX_USE_STDLIB) +#include +#endif + #include "memops.hpp" #include "types.hpp" #include "typetraits.hpp" @@ -16,7 +20,7 @@ namespace ox { template constexpr typename enable_if::type cbit_cast(From src) noexcept { - To dst; + To dst = {}; ox_memcpy(&dst, &src, sizeof(src)); return dst; } @@ -52,3 +56,23 @@ static_assert(onMask(3) == 7); static_assert(onMask(4) == 15); } + +namespace std { +#if __cplusplus >= 202002L && !defined(OX_USE_STDLIB) + +template +constexpr typename ox::enable_if::type bit_cast(From src) noexcept { + return __builtin_bitcast(src); +} + +#elif __cplusplus < 202002L + +template +typename ox::enable_if::type bit_cast(From src) noexcept { + To dst = {}; + memcpy(&dst, &src, sizeof(src)); + return dst; +} + +#endif +} diff --git a/deps/ox/src/ox/std/memops.hpp b/deps/ox/src/ox/std/memops.hpp index b361942e..3c9bd4a6 100644 --- a/deps/ox/src/ox/std/memops.hpp +++ b/deps/ox/src/ox/std/memops.hpp @@ -29,7 +29,7 @@ int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept; constexpr void *ox_memcpy(void *dest, const void *src, std::size_t size) noexcept { auto srcBuf = static_cast(src); auto dstBuf = static_cast(dest); - for (std::size_t i = 0; i < size; i++) { + for (std::size_t i = 0; i < size; ++i) { dstBuf[i] = static_cast(srcBuf[i]); } return dest; @@ -38,7 +38,7 @@ constexpr void *ox_memcpy(void *dest, const void *src, std::size_t size) noexcep constexpr void *ox_memmove(void *dest, const void *src, std::size_t size) noexcept { auto srcBuf = static_cast(src); auto dstBuf = static_cast(dest); - for (std::size_t i = 0; i < size; i++) { + for (std::size_t i = 0; i < size; ++i) { dstBuf[i] = static_cast(srcBuf[i]); } return dest; @@ -46,7 +46,7 @@ constexpr void *ox_memmove(void *dest, const void *src, std::size_t size) noexce constexpr void *ox_memset(void *ptr, int val, std::size_t size) noexcept { auto buf = static_cast(ptr); - for (std::size_t i = 0; i < size; i++) { + for (std::size_t i = 0; i < size; ++i) { buf[i] = static_cast(val); } return ptr; diff --git a/deps/ox/src/ox/std/types.hpp b/deps/ox/src/ox/std/types.hpp index d182429e..d64baada 100644 --- a/deps/ox/src/ox/std/types.hpp +++ b/deps/ox/src/ox/std/types.hpp @@ -64,6 +64,7 @@ namespace ox { template struct alignas(alignof(T)) AllocAlias { char buff[sz]; + constexpr AllocAlias() noexcept = default; };