[ox/std] Add bit_cast

This commit is contained in:
Gary Talent 2020-01-31 19:43:23 -06:00
parent d9b363330b
commit 878c944ebf
3 changed files with 14 additions and 4 deletions

View File

@ -9,6 +9,7 @@
#pragma once
#include <ox/std/assert.hpp>
#include <ox/std/bit.hpp>
#include <ox/std/byteswap.hpp>
#include <ox/std/memops.hpp>

View File

@ -8,10 +8,19 @@
#pragma once
#include "memops.hpp"
#include "types.hpp"
#include "typetraits.hpp"
namespace ox {
template<typename To, typename From>
constexpr typename enable_if<sizeof(To) == sizeof(From), To>::type bit_cast(From src) noexcept {
To dst;
memcpy(&dst, &src, sizeof(src));
return dst;
}
template<typename T>
[[nodiscard]] constexpr T rotl(T i, int shift) noexcept {
constexpr auto bits = sizeof(i) * 8;
@ -27,6 +36,9 @@ template<typename T>
return out;
}
template<typename T>
constexpr auto MaxValue = onMask<T>();
static_assert(onMask<int>(1) == 1);
static_assert(onMask<int>(2) == 3);
static_assert(onMask<int>(3) == 7);

View File

@ -8,7 +8,7 @@
#pragma once
#include "bit.hpp"
#include "types.hpp"
#if __has_include(<type_traits>)
@ -27,9 +27,6 @@ constexpr bool is_union_v = __is_union(T);
namespace ox {
template<typename T>
constexpr auto MaxValue = onMask<T>();
template<class T, T v>
struct integral_constant {