[ox/std] Add bit_cast
This commit is contained in:
parent
d9b363330b
commit
878c944ebf
1
deps/ox/src/ox/mc/intops.hpp
vendored
1
deps/ox/src/ox/mc/intops.hpp
vendored
@ -9,6 +9,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ox/std/assert.hpp>
|
#include <ox/std/assert.hpp>
|
||||||
|
#include <ox/std/bit.hpp>
|
||||||
#include <ox/std/byteswap.hpp>
|
#include <ox/std/byteswap.hpp>
|
||||||
#include <ox/std/memops.hpp>
|
#include <ox/std/memops.hpp>
|
||||||
|
|
||||||
|
12
deps/ox/src/ox/std/bit.hpp
vendored
12
deps/ox/src/ox/std/bit.hpp
vendored
@ -8,10 +8,19 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "memops.hpp"
|
||||||
#include "types.hpp"
|
#include "types.hpp"
|
||||||
|
#include "typetraits.hpp"
|
||||||
|
|
||||||
namespace ox {
|
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>
|
template<typename T>
|
||||||
[[nodiscard]] constexpr T rotl(T i, int shift) noexcept {
|
[[nodiscard]] constexpr T rotl(T i, int shift) noexcept {
|
||||||
constexpr auto bits = sizeof(i) * 8;
|
constexpr auto bits = sizeof(i) * 8;
|
||||||
@ -27,6 +36,9 @@ template<typename T>
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr auto MaxValue = onMask<T>();
|
||||||
|
|
||||||
static_assert(onMask<int>(1) == 1);
|
static_assert(onMask<int>(1) == 1);
|
||||||
static_assert(onMask<int>(2) == 3);
|
static_assert(onMask<int>(2) == 3);
|
||||||
static_assert(onMask<int>(3) == 7);
|
static_assert(onMask<int>(3) == 7);
|
||||||
|
5
deps/ox/src/ox/std/typetraits.hpp
vendored
5
deps/ox/src/ox/std/typetraits.hpp
vendored
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "bit.hpp"
|
#include "types.hpp"
|
||||||
|
|
||||||
#if __has_include(<type_traits>)
|
#if __has_include(<type_traits>)
|
||||||
|
|
||||||
@ -27,9 +27,6 @@ constexpr bool is_union_v = __is_union(T);
|
|||||||
|
|
||||||
namespace ox {
|
namespace ox {
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr auto MaxValue = onMask<T>();
|
|
||||||
|
|
||||||
template<class T, T v>
|
template<class T, T v>
|
||||||
struct integral_constant {
|
struct integral_constant {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user