diff options
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/packbits-impl.cpp | 11 | ||||
-rw-r--r-- | serialize/packbits-impl.hpp | 13 | ||||
-rw-r--r-- | serialize/packbits-read.cpp | 19 | ||||
-rw-r--r-- | serialize/packbits-read.hpp | 16 | ||||
-rw-r--r-- | serialize/packbits-write.cpp | 2 | ||||
-rw-r--r-- | serialize/packbits-write.hpp | 14 |
6 files changed, 57 insertions, 18 deletions
diff --git a/serialize/packbits-impl.cpp b/serialize/packbits-impl.cpp new file mode 100644 index 00000000..b7a4eefc --- /dev/null +++ b/serialize/packbits-impl.cpp @@ -0,0 +1,11 @@ +#include "packbits-impl.hpp" +#include "compat/exception.hpp" + +namespace floormat::Pack_impl { + +void throw_on_read_nonzero() noexcept(false) +{ + throw std::runtime_error{"extra bits in pack_read()"}; +} + +} // namespace floormat::Pack_impl diff --git a/serialize/packbits-impl.hpp b/serialize/packbits-impl.hpp new file mode 100644 index 00000000..3e57ea75 --- /dev/null +++ b/serialize/packbits-impl.hpp @@ -0,0 +1,13 @@ +#pragma once + +namespace floormat::Pack_impl { + +[[noreturn]] void throw_on_read_nonzero() noexcept(false); +[[noreturn]] void throw_on_write_input_bit_overflow() noexcept(false); + +template<size_t... Ns> struct expand_sum; +template<size_t N, size_t... Ns> struct expand_sum<N, Ns...> { static constexpr size_t value = N + expand_sum<Ns...>::value; }; +template<> struct expand_sum<> { static constexpr size_t value = 0; }; +template<size_t... Ns> constexpr inline size_t sum = expand_sum<Ns...>::value; + +} // namespace floormat::Pack_impl diff --git a/serialize/packbits-read.cpp b/serialize/packbits-read.cpp index 6b4a13d4..9980ca63 100644 --- a/serialize/packbits-read.cpp +++ b/serialize/packbits-read.cpp @@ -1,12 +1,29 @@ #include "packbits-read.hpp" #include "compat/assert.hpp" +#include "compat/exception.hpp" + +namespace floormat::Pack_impl { + +void throw_on_read_nonzero() noexcept(false) +{ + throw std::runtime_error{"extra bits in pack_read()"}; +} + +} // namespace floormat::Pack_impl namespace floormat { -using namespace floormat::Pack; +using namespace floormat::Pack_impl; namespace { +static_assert(sum<1, 2, 3, 4, 5> == 6*(6-1)/2); +static_assert(sum<5, 10, 15> == 30); + +using u32 = uint32_t; +using u16 = uint16_t; +using u8 = uint8_t; + 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()); diff --git a/serialize/packbits-read.hpp b/serialize/packbits-read.hpp index 6f5dd883..2ea50848 100644 --- a/serialize/packbits-read.hpp +++ b/serialize/packbits-read.hpp @@ -1,11 +1,13 @@ #pragma once +#include "packbits-impl.hpp" +#include "compat/reverse-index-sequence.hpp" #include <type_traits> #include <concepts> #include <tuple> #include <utility> #include "compat/assert.hpp" -namespace floormat::Pack { +namespace floormat::Pack_impl { template<std::unsigned_integral T, size_t N> struct input_bits final @@ -105,7 +107,8 @@ constexpr void read_(Place& p, input<T, Left> st, std::index_sequence<I, Is...>, template<std::unsigned_integral T, typename Place, size_t Left> constexpr void read_(Place&, input<T, Left> st, std::index_sequence<>, empty_pack_tuple<>) { - fm_assert(st.check_zero()); + if (!st.check_zero()) [[unlikely]] + throw_on_read_nonzero(); } template<std::unsigned_integral T, typename Place, size_t Left, size_t... Is, typename... Sizes> @@ -114,17 +117,10 @@ 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::Pack +} // namespace floormat::Pack_impl 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 diff --git a/serialize/packbits-write.cpp b/serialize/packbits-write.cpp index 5db0fa1b..f485a72e 100644 --- a/serialize/packbits-write.cpp +++ b/serialize/packbits-write.cpp @@ -4,7 +4,7 @@ namespace floormat { namespace { -using namespace floormat::Pack; +using namespace floormat::Pack_impl; using u32 = uint32_t; using u16 = uint16_t; diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp index 5a8eae52..ca18cfbb 100644 --- a/serialize/packbits-write.hpp +++ b/serialize/packbits-write.hpp @@ -1,12 +1,12 @@ #pragma once -#include "compat/assert.hpp" +#include "packbits-impl.hpp" #include "compat/reverse-index-sequence.hpp" #include <type_traits> #include <bit> #include <concepts> #include <tuple> -namespace floormat::Pack { +namespace floormat::Pack_impl { template<std::unsigned_integral T, size_t CAPACITY, size_t LEFT> struct output @@ -42,7 +42,9 @@ constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple& tuple, output<T, Capacity, static_assert(N <= Left); T x = std::get<I>(tuple).value; - fm_assert((size_t)std::bit_width(x) <= N); + + if (!((size_t)std::bit_width(x) <= N)) [[unlikely]] + throw_on_write_input_bit_overflow(); T value = T(T(st.value << N) | x); return write_(tuple, output<T, Capacity, Left - N>{value}, std::index_sequence<Is...>{}); } @@ -53,15 +55,15 @@ constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple&, output<T, Capacity, Left> return st.value; } -} // namespace floormat::Pack +} // namespace floormat::Pack_impl namespace floormat { template<std::unsigned_integral T, size_t... Sizes> -constexpr T pack_write(const std::tuple<Pack::output_field<T, Sizes>...>& tuple) +constexpr T pack_write(const std::tuple<Pack_impl::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)>{}); + return Pack_impl::write_(tuple, Pack_impl::output<T, nbits, nbits>{T{0}}, make_reverse_index_sequence<sizeof...(Sizes)>{}); } constexpr uint8_t pack_write(const std::tuple<>&) = delete; |