From 878c944ebf2783533cf10009c1f0409085e33df0 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 31 Jan 2020 19:43:23 -0600 Subject: [PATCH] [ox/std] Add bit_cast --- deps/ox/src/ox/mc/intops.hpp | 1 + deps/ox/src/ox/std/bit.hpp | 12 ++++++++++++ deps/ox/src/ox/std/typetraits.hpp | 5 +---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/deps/ox/src/ox/mc/intops.hpp b/deps/ox/src/ox/mc/intops.hpp index 7ff41694..029747b2 100644 --- a/deps/ox/src/ox/mc/intops.hpp +++ b/deps/ox/src/ox/mc/intops.hpp @@ -9,6 +9,7 @@ #pragma once #include +#include #include #include diff --git a/deps/ox/src/ox/std/bit.hpp b/deps/ox/src/ox/std/bit.hpp index 616249d4..61da610d 100644 --- a/deps/ox/src/ox/std/bit.hpp +++ b/deps/ox/src/ox/std/bit.hpp @@ -8,10 +8,19 @@ #pragma once +#include "memops.hpp" #include "types.hpp" +#include "typetraits.hpp" namespace ox { +template +constexpr typename enable_if::type bit_cast(From src) noexcept { + To dst; + memcpy(&dst, &src, sizeof(src)); + return dst; +} + template [[nodiscard]] constexpr T rotl(T i, int shift) noexcept { constexpr auto bits = sizeof(i) * 8; @@ -27,6 +36,9 @@ template return out; } +template +constexpr auto MaxValue = onMask(); + static_assert(onMask(1) == 1); static_assert(onMask(2) == 3); static_assert(onMask(3) == 7); diff --git a/deps/ox/src/ox/std/typetraits.hpp b/deps/ox/src/ox/std/typetraits.hpp index 46765550..d3932b8a 100644 --- a/deps/ox/src/ox/std/typetraits.hpp +++ b/deps/ox/src/ox/std/typetraits.hpp @@ -8,7 +8,7 @@ #pragma once -#include "bit.hpp" +#include "types.hpp" #if __has_include() @@ -27,9 +27,6 @@ constexpr bool is_union_v = __is_union(T); namespace ox { -template -constexpr auto MaxValue = onMask(); - template struct integral_constant {