From 46e6d61803a88deb21a31e481e4d7f3030e38da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 16 Nov 2022 18:11:14 +0100 Subject: [PATCH] Math: make {un,}pack{Half,}Into() and castInto() assertions clearer. Say which of the two is non-contiguous, whether the source or the destination. --- src/Magnum/Math/PackingBatch.cpp | 36 +++++++++++++++-------- src/Magnum/Math/Test/PackingBatchTest.cpp | 33 +++++++++++++++++---- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/Magnum/Math/PackingBatch.cpp b/src/Magnum/Math/PackingBatch.cpp index 57a59798e..b75246a68 100644 --- a/src/Magnum/Math/PackingBatch.cpp +++ b/src/Magnum/Math/PackingBatch.cpp @@ -38,8 +38,10 @@ namespace { template inline void unpackUnsignedIntoImplementation(const Corrade::Containers::StridedArrayView2D& src, const Corrade::Containers::StridedArrayView2D& dst) { CORRADE_ASSERT(src.size() == dst.size(), "Math::unpackInto(): wrong destination size, got" << dst.size() << "but expected" << src.size(), ); - CORRADE_ASSERT(src.template isContiguous<1>() && dst.isContiguous<1>(), - "Math::unpackInto(): second view dimension is not contiguous", ); + CORRADE_ASSERT(src.template isContiguous<1>(), + "Math::unpackInto(): second source view dimension is not contiguous", ); + CORRADE_ASSERT(dst.isContiguous<1>(), + "Math::unpackInto(): second destination view dimension is not contiguous", ); /** @todo SSE/NEON implementations exploiting second dimension size/stride, contiguity etc */ @@ -77,8 +79,10 @@ namespace { template inline void unpackSignedIntoImplementation(const Corrade::Containers::StridedArrayView2D& src, const Corrade::Containers::StridedArrayView2D& dst) { CORRADE_ASSERT(src.size() == dst.size(), "Math::unpackInto(): wrong destination size, got" << dst.size() << "but expected" << src.size(), ); - CORRADE_ASSERT(src.template isContiguous<1>() && dst.isContiguous<1>(), - "Math::unpackInto(): second view dimension is not contiguous", ); + CORRADE_ASSERT(src.template isContiguous<1>(), + "Math::unpackInto(): second source view dimension is not contiguous", ); + CORRADE_ASSERT(dst.isContiguous<1>(), + "Math::unpackInto(): second destination view dimension is not contiguous", ); /** @todo SSE/NEON implementations exploiting second dimension size/stride, contiguity etc */ @@ -119,8 +123,10 @@ namespace { template inline void packIntoImplementation(const Corrade::Containers::StridedArrayView2D& src, const Corrade::Containers::StridedArrayView2D& dst) { CORRADE_ASSERT(src.size() == dst.size(), "Math::packInto(): wrong destination size, got" << dst.size() << "but expected" << src.size(), ); - CORRADE_ASSERT(src.isContiguous<1>() && dst.template isContiguous<1>(), - "Math::packInto(): second view dimension is not contiguous", ); + CORRADE_ASSERT(src.isContiguous<1>(), + "Math::packInto(): second source view dimension is not contiguous", ); + CORRADE_ASSERT(dst.template isContiguous<1>(), + "Math::packInto(): second destination view dimension is not contiguous", ); /** @todo SSE/NEON implementations exploiting second dimension size/stride, contiguity etc */ @@ -167,8 +173,10 @@ namespace { template inline void castIntoImplementation(const Corrade::Containers::StridedArrayView2D& src, const Corrade::Containers::StridedArrayView2D& dst) { CORRADE_ASSERT(src.size() == dst.size(), "Math::castInto(): wrong destination size, got" << dst.size() << "but expected" << src.size(), ); - CORRADE_ASSERT(src.template isContiguous<1>() && dst.template isContiguous<1>(), - "Math::castInto(): second view dimension is not contiguous", ); + CORRADE_ASSERT(src.template isContiguous<1>(), + "Math::castInto(): second source view dimension is not contiguous", ); + CORRADE_ASSERT(dst.template isContiguous<1>(), + "Math::castInto(): second destination view dimension is not contiguous", ); /** @todo SSE/NEON implementations exploiting second dimension size/stride, contiguity etc */ @@ -401,8 +409,10 @@ static_assert(sizeof(HalfBaseTable) + sizeof(HalfShiftTable) == 1536, void unpackHalfInto(const Corrade::Containers::StridedArrayView2D& src, const Corrade::Containers::StridedArrayView2D& dst) { CORRADE_ASSERT(src.size() == dst.size(), "Math::unpackHalfInto(): wrong destination size, got" << dst.size() << "but expected" << src.size(), ); - CORRADE_ASSERT(src.isContiguous<1>() && dst.isContiguous<1>(), - "Math::unpackHalfInto(): second view dimension is not contiguous", ); + CORRADE_ASSERT(src.isContiguous<1>(), + "Math::unpackHalfInto(): second source view dimension is not contiguous", ); + CORRADE_ASSERT(dst.isContiguous<1>(), + "Math::unpackHalfInto(): second destination view dimension is not contiguous", ); /* Caching values to avoid inline function calls in debug builds */ const char* srcPtr = reinterpret_cast(src.data()); @@ -426,8 +436,10 @@ void unpackHalfInto(const Corrade::Containers::StridedArrayView2D& src, const Corrade::Containers::StridedArrayView2D& dst) { CORRADE_ASSERT(src.size() == dst.size(), "Math::packHalfInto(): wrong destination size, got" << dst.size() << "but expected" << src.size(), ); - CORRADE_ASSERT(src.isContiguous<1>() && dst.isContiguous<1>(), - "Math::packHalfInto(): second view dimension is not contiguous", ); + CORRADE_ASSERT(src.isContiguous<1>(), + "Math::packHalfInto(): second source view dimension is not contiguous", ); + CORRADE_ASSERT(dst.isContiguous<1>(), + "Math::packHalfInto(): second destination view dimension is not contiguous", ); /* Caching values to avoid inline function calls in debug builds */ const char* srcPtr = reinterpret_cast(src.data()); diff --git a/src/Magnum/Math/Test/PackingBatchTest.cpp b/src/Magnum/Math/Test/PackingBatchTest.cpp index 87f5a0c98..77a2adcc9 100644 --- a/src/Magnum/Math/Test/PackingBatchTest.cpp +++ b/src/Magnum/Math/Test/PackingBatchTest.cpp @@ -145,6 +145,7 @@ typedef Math::Vector2 Vector2; typedef Math::Vector2 Vector2ui; typedef Math::Vector2 Vector2i; typedef Math::Vector3 Vector3; +typedef Math::Vector4 Vector4us; typedef Math::Vector4 Vector4; void PackingBatchTest::unpackUnsignedByte() { @@ -641,12 +642,18 @@ template void PackingBatchTest::assertionsPackUnpack() { CORRADE_SKIP_IF_NO_ASSERT(); Math::Vector2 data[2]{}; + Math::Vector4 dataNonContiguous[2]{}; Vector2 resultWrongCount[1]{}; + Vector2 result[2]{}; Vector3 resultWrongVectorSize[2]{}; Vector4 resultNonContiguous[2]{}; auto src = Corrade::Containers::arrayCast<2, T>( Corrade::Containers::arrayView(data)); + auto srcNonContiguous = Corrade::Containers::arrayCast<2, T>( + Corrade::Containers::arrayView(dataNonContiguous)).every({1, 2}); + auto dst = Corrade::Containers::arrayCast<2, Float>( + Corrade::Containers::arrayView(result)); auto dstWrongCount = Corrade::Containers::arrayCast<2, Float>( Corrade::Containers::arrayView(resultWrongCount)); auto dstWrongVectorSize = Corrade::Containers::arrayCast<2, Float>( @@ -659,28 +666,38 @@ template void PackingBatchTest::assertionsPackUnpack() { unpackInto(src, dstWrongCount); unpackInto(src, dstWrongVectorSize); unpackInto(src, dstNotContiguous); + unpackInto(srcNonContiguous, dst); packInto(dstWrongCount, src); packInto(dstWrongVectorSize, src); packInto(dstNotContiguous, src); + packInto(dst, srcNonContiguous); CORRADE_COMPARE(out.str(), "Math::unpackInto(): wrong destination size, got {1, 2} but expected {2, 2}\n" "Math::unpackInto(): wrong destination size, got {2, 3} but expected {2, 2}\n" - "Math::unpackInto(): second view dimension is not contiguous\n" + "Math::unpackInto(): second destination view dimension is not contiguous\n" + "Math::unpackInto(): second source view dimension is not contiguous\n" "Math::packInto(): wrong destination size, got {2, 2} but expected {1, 2}\n" "Math::packInto(): wrong destination size, got {2, 2} but expected {2, 3}\n" - "Math::packInto(): second view dimension is not contiguous\n"); + "Math::packInto(): second source view dimension is not contiguous\n" + "Math::packInto(): second destination view dimension is not contiguous\n"); } void PackingBatchTest::assertionsPackUnpackHalf() { CORRADE_SKIP_IF_NO_ASSERT(); Vector2us data[2]{}; + Vector4us dataNonContiguous[2]{}; + Vector2 result[2]{}; Vector2 resultWrongCount[1]{}; Vector3 resultWrongVectorSize[2]{}; Vector4 resultNonContiguous[2]{}; auto src = Corrade::Containers::arrayCast<2, UnsignedShort>( Corrade::Containers::arrayView(data)); + auto srcNonContiguous = Corrade::Containers::arrayCast<2, UnsignedShort>( + Corrade::Containers::arrayView(dataNonContiguous)).every({1, 2}); + auto dst = Corrade::Containers::arrayCast<2, Float>( + Corrade::Containers::arrayView(result)); auto dstWrongCount = Corrade::Containers::arrayCast<2, Float>( Corrade::Containers::arrayView(resultWrongCount)); auto dstWrongVectorSize = Corrade::Containers::arrayCast<2, Float>( @@ -693,16 +710,20 @@ void PackingBatchTest::assertionsPackUnpackHalf() { unpackHalfInto(src, dstWrongCount); unpackHalfInto(src, dstWrongVectorSize); unpackHalfInto(src, dstNotContiguous); + unpackHalfInto(srcNonContiguous, dst); packHalfInto(dstWrongCount, src); packHalfInto(dstWrongVectorSize, src); packHalfInto(dstNotContiguous, src); + packHalfInto(dst, srcNonContiguous); CORRADE_COMPARE(out.str(), "Math::unpackHalfInto(): wrong destination size, got {1, 2} but expected {2, 2}\n" "Math::unpackHalfInto(): wrong destination size, got {2, 3} but expected {2, 2}\n" - "Math::unpackHalfInto(): second view dimension is not contiguous\n" + "Math::unpackHalfInto(): second destination view dimension is not contiguous\n" + "Math::unpackHalfInto(): second source view dimension is not contiguous\n" "Math::packHalfInto(): wrong destination size, got {2, 2} but expected {1, 2}\n" "Math::packHalfInto(): wrong destination size, got {2, 2} but expected {2, 3}\n" - "Math::packHalfInto(): second view dimension is not contiguous\n"); + "Math::packHalfInto(): second source view dimension is not contiguous\n" + "Math::packHalfInto(): second destination view dimension is not contiguous\n"); } template void PackingBatchTest::assertionsCast() { @@ -735,10 +756,10 @@ template void PackingBatchTest::assertionsCast() { CORRADE_COMPARE(out.str(), "Math::castInto(): wrong destination size, got {1, 2} but expected {2, 2}\n" "Math::castInto(): wrong destination size, got {2, 3} but expected {2, 2}\n" - "Math::castInto(): second view dimension is not contiguous\n" + "Math::castInto(): second destination view dimension is not contiguous\n" "Math::castInto(): wrong destination size, got {2, 2} but expected {1, 2}\n" "Math::castInto(): wrong destination size, got {2, 2} but expected {2, 3}\n" - "Math::castInto(): second view dimension is not contiguous\n"); + "Math::castInto(): second source view dimension is not contiguous\n"); } }}}}