diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-16 09:01:36 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-01-16 09:01:36 +0100 |
commit | 30d4c8e5c26aa89a64998ef1da350d4d80359237 (patch) | |
tree | e19c909675d703dc9837202ee8ca2ced1d98a20c /serialize | |
parent | 07716595e0c26ba6d19ee6c498280508ca5aa6c2 (diff) |
cw
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/packbits-read.cpp | 4 | ||||
-rw-r--r-- | serialize/packbits-read.hpp | 15 | ||||
-rw-r--r-- | serialize/packbits-write.cpp | 23 | ||||
-rw-r--r-- | serialize/packbits-write.hpp | 20 | ||||
-rw-r--r-- | serialize/world-impl.hpp | 13 |
5 files changed, 46 insertions, 29 deletions
diff --git a/serialize/packbits-read.cpp b/serialize/packbits-read.cpp index f21a0de9..6b4a13d4 100644 --- a/serialize/packbits-read.cpp +++ b/serialize/packbits-read.cpp @@ -3,11 +3,11 @@ namespace floormat { -using namespace floormat::detail_Pack_input; +using namespace floormat::Pack; namespace { -template<std::unsigned_integral T, size_t N> constexpr inline T lowbits = (T{1} << N)-T{1}; +template<std::unsigned_integral T, size_t N> constexpr inline T lowbits = N == sizeof(T)*8 ? (T)-1 : (T{1} << N)-T{1}; static_assert(!input<uint32_t, 3>{65535}.check_zero()); static_assert(input<uint32_t, 30>{65535}.advance<16>() == 0); diff --git a/serialize/packbits-read.hpp b/serialize/packbits-read.hpp index dc21d7ee..6f5dd883 100644 --- a/serialize/packbits-read.hpp +++ b/serialize/packbits-read.hpp @@ -5,7 +5,7 @@ #include <utility> #include "compat/assert.hpp" -namespace floormat::detail_Pack_input { +namespace floormat::Pack { template<std::unsigned_integral T, size_t N> struct input_bits final @@ -114,10 +114,17 @@ constexpr void read_(Place&, input<T, Left>, std::index_sequence<Is...>, empty_p template<std::unsigned_integral T, size_t... Ns> using make_pack = empty_pack_tuple<input_bits<T, Ns>...>; -} // namespace floormat::detail_Pack_input +} // namespace floormat::Pack -namespace floormat::pack { +namespace floormat { +template<std::unsigned_integral T, size_t... Sizes> +constexpr T pack_write(const std::tuple<Pack::output_field<T, Sizes>...>& tuple) +{ + constexpr size_t nbits = sizeof(T)*8; + return Pack::write_(tuple, Pack::output<T, nbits, nbits>{T{0}}, make_reverse_index_sequence<sizeof...(Sizes)>{}); +} +constexpr uint8_t pack_write(const std::tuple<>&) = delete; -} // namespace floormat::pack +} // namespace floormat diff --git a/serialize/packbits-write.cpp b/serialize/packbits-write.cpp index 045d6283..5db0fa1b 100644 --- a/serialize/packbits-write.cpp +++ b/serialize/packbits-write.cpp @@ -1,6 +1,10 @@ #include "packbits-write.hpp" -namespace floormat::detail_Pack_output { +namespace floormat { + +namespace { + +using namespace floormat::Pack; using u32 = uint32_t; using u16 = uint16_t; @@ -22,18 +26,21 @@ static_assert(write_( output<u32, 32, 32>{0}, make_reverse_index_sequence<3>{}) == 0b000101110); -static_assert(write(std::tuple{f32<2>{0b10}, f32<3>{0b011}, f32<3>{0b01}}) == 0b00101110); -//static_assert(write(std::tuple{f32<2>{0b10}, f32<3>{0b1011}, f32<3>{0b001}}) == 0b000101110); -static_assert(write(std::tuple{f8<2>{0b10}, f8<3>{0b011}, f8<3>{0b01}}) == 0b00101110); -//static_assert(write(std::tuple{f8<2>{0b10}, f8<3>{0b011}, f8<4>{0b01}}) == 0b00101110); -//static_assert(write(std::tuple{}) == 0); +static_assert(pack_write(std::tuple{f32<2>{0b10}, f32<3>{0b011}, f32<3>{0b01}}) == 0b00101110); +//static_assert(pack_write(std::tuple{f32<2>{0b10}, f32<3>{0b1011}, f32<3>{0b001}}) == 0b000101110); +static_assert(pack_write(std::tuple{f8<2>{0b10}, f8<3>{0b011}, f8<3>{0b01}}) == 0b00101110); +//static_assert(pack_write(std::tuple{f8<2>{0b10}, f8<3>{0b011}, f8<4>{0b01}}) == 0b00101110); +//static_assert(pack_write(std::tuple{}) == 0); +static_assert(pack_write(std::tuple{f8<1>{0b1}, f8<3>{0b101}, f8<2>{0b10}}) == 0b101011); #if 0 // check disasembly u32 foo1(u32 a, u32 b, u32 c); u32 foo1(u32 a, u32 b, u32 c) { - return write(std::tuple{f32<2>{a}, f32<3>{b}, f32<3>{c}}); + return pack_write(std::tuple{f32<2>{a}, f32<3>{b}, f32<3>{c}}); } #endif -} // namespace floormat::detail_Pack_output +} // namespace + +} // namespace floormat diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp index 09ec820e..5a8eae52 100644 --- a/serialize/packbits-write.hpp +++ b/serialize/packbits-write.hpp @@ -1,11 +1,12 @@ #pragma once #include "compat/assert.hpp" +#include "compat/reverse-index-sequence.hpp" #include <type_traits> #include <bit> #include <concepts> #include <tuple> -namespace floormat::detail_Pack_output { +namespace floormat::Pack { template<std::unsigned_integral T, size_t CAPACITY, size_t LEFT> struct output @@ -26,11 +27,6 @@ struct output_field T value; }; -template <size_t... Is> -constexpr std::index_sequence<sizeof...(Is)-1u-Is...> reverse_index_sequence(std::index_sequence<Is...>); -template <size_t N> -using make_reverse_index_sequence = decltype(reverse_index_sequence(std::make_index_sequence<N>{})); - template<typename T> struct is_output_field : std::bool_constant<false> {}; template<std::unsigned_integral T, size_t N> struct is_output_field<output_field<T, N>> : std::bool_constant<true> { static_assert(N > 0); }; @@ -57,13 +53,17 @@ constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple&, output<T, Capacity, Left> return st.value; } +} // namespace floormat::Pack + +namespace floormat { + template<std::unsigned_integral T, size_t... Sizes> -constexpr T write(const std::tuple<output_field<T, Sizes>...>& tuple) +constexpr T pack_write(const std::tuple<Pack::output_field<T, Sizes>...>& tuple) { constexpr size_t nbits = sizeof(T)*8; - return write_(tuple, output<T, nbits, nbits>{T{0}}, make_reverse_index_sequence<sizeof...(Sizes)>{}); + return Pack::write_(tuple, Pack::output<T, nbits, nbits>{T{0}}, make_reverse_index_sequence<sizeof...(Sizes)>{}); } -constexpr uint8_t write(const std::tuple<>&) = delete; +constexpr uint8_t pack_write(const std::tuple<>&) = delete; -} // namespace floormat::detail_Pack_output +} // namespace floormat diff --git a/serialize/world-impl.hpp b/serialize/world-impl.hpp index 5efd9bbc..ce4d9a8c 100644 --- a/serialize/world-impl.hpp +++ b/serialize/world-impl.hpp @@ -9,7 +9,7 @@ #include "src/object-type.hpp" #include <bit> #include <cstdio> -#include <limits> +#include <concepts> /* protocol changelog: * 1) Initial version. @@ -44,9 +44,12 @@ using atlasid = uint16_t; using chunksiz = uint16_t; using proto_t = uint16_t; -namespace { +template<typename T> struct int_traits; + +template<std::unsigned_integral T> struct int_traits<T> { static constexpr T max = T(-1); }; +template<std::signed_integral T> struct int_traits<T> { static constexpr T max = T(-1)&~(T(1) << sizeof(T)*8-1); }; -template<typename T> constexpr inline T int_max = std::numeric_limits<T>::max(); +namespace { #define file_magic ".floormat.save" @@ -67,8 +70,8 @@ constexpr inline auto pass_mask = (1 << pass_mode_BITS)-1; template<typename T, size_t N, size_t off> constexpr inline auto highbits = (T(1) << N)-1 << sizeof(T)*8-N-off; -template<size_t N, typename T = uint8_t> -constexpr T lowbits = T((T{1} << N)-T{1}); +template<size_t N, std::unsigned_integral T = uint8_t> +constexpr T lowbits = N == sizeof(T)*8 ? (T)-1 : T((T{1} << N)-T{1}); constexpr inline uint8_t meta_short_scenery_bit = highbits<uint8_t, 1, 0>; constexpr inline uint8_t meta_rotation_bits = highbits<uint8_t, rotation_BITS, 1>; |