From 7cc35104d4c4510bdbf67eaa5bd35e84dfb5ae24 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sun, 1 Nov 2026 21:21:16 +0100 Subject: [PATCH] fix(math): fix calculate_offset --- include/cenet/math/tensor_shape.hpp | 34 ++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/include/cenet/math/tensor_shape.hpp b/include/cenet/math/tensor_shape.hpp index 0b0a6d6..8fce8c9 100644 --- a/include/cenet/math/tensor_shape.hpp +++ b/include/cenet/math/tensor_shape.hpp @@ -76,10 +76,12 @@ public: template std::size_t calculate_offset(const std::tuple& offs) const { - static_assert(sizeof...(Offs) > 0); - static_assert(sizeof...(Offs) <= sizeof...(Dims)); + constexpr std::size_t off_count = sizeof...(Offs); - return calculate_offset_impl(offs); + static_assert(off_count > 0); + static_assert(Start + off_count <= sizeof...(Dims)); + + return calculate_offset_impl(offs) * trailing_product(); } private: template @@ -90,6 +92,15 @@ private: return (calculate_offset_impl(offs) * get()) + std::get(offs); } } + + template + std::size_t trailing_product() const { + if constexpr (I == sizeof...(Dims)) { + return 1; + } else { + return get() * trailing_product(); + } + } }; template @@ -171,10 +182,12 @@ public: template std::size_t calculate_offset(const std::tuple& offs) const { - static_assert(sizeof...(Offs) > 0); - static_assert(sizeof...(Offs) <= sizeof...(Dims)); + constexpr std::size_t off_count = sizeof...(Offs); - return calculate_offset_impl(offs); + static_assert(off_count > 0); + static_assert(Start + off_count <= sizeof...(Dims)); + + return calculate_offset_impl(offs) * trailing_product(); } private: template @@ -185,6 +198,15 @@ private: return (calculate_offset_impl(offs) * get()) + std::get(offs); } } + + template + std::size_t trailing_product() const { + if constexpr (I == sizeof...(Dims)) { + return 1; + } else { + return get() * trailing_product(); + } + } }; namespace detail {