From 7d7dc94af87d45f32044259a151a97b1db4b2bcc Mon Sep 17 00:00:00 2001 From: pbkx <93405617+pbkx@users.noreply.github.com> Date: Tue, 13 Oct 2026 20:42:48 -0700 Subject: [PATCH] fix aliased CV_64FC2 GEMM destination --- modules/core/src/matmul.dispatch.cpp | 2 +- modules/core/test/test_math.cpp | 31 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/modules/core/src/matmul.dispatch.cpp b/modules/core/src/matmul.dispatch.cpp index 37a2b86b91..5507df3a6f 100644 --- a/modules/core/src/matmul.dispatch.cpp +++ b/modules/core/src/matmul.dispatch.cpp @@ -421,7 +421,7 @@ void gemm(InputArray matA, InputArray matB, double alpha, CV_Assert( type == CV_64FC2 ); hal::gemm64fc(A.ptr(), A.step, B.ptr(), B.step, alpha, C.ptr(), C.step, beta, - D.ptr(), D.step, + DProxyPtr->ptr(), DProxyPtr->step, a_size.height, a_size.width, DProxyPtr->cols, flags); } diff --git a/modules/core/test/test_math.cpp b/modules/core/test/test_math.cpp index ac4df3d937..27f5904ea0 100644 --- a/modules/core/test/test_math.cpp +++ b/modules/core/test/test_math.cpp @@ -3043,6 +3043,37 @@ TEST(Core_CrossProduct, accuracy) { Core_CrossProductTest test; test.safe_run(); TEST(Core_Determinant, accuracy) { Core_DetTest test; test.safe_run(); } TEST(Core_DotProduct, accuracy) { Core_DotProductTest test; test.safe_run(); } TEST(Core_GEMM, accuracy) { Core_GEMMTest test; test.safe_run(); } + +TEST(Core_GEMM, inplace_complex) +{ + const Mat a64 = (Mat_(2, 2) << Vec2d(1, 2), Vec2d(2, -1), + Vec2d(-3, 1), Vec2d(4, 2)); + const Mat b64 = (Mat_(2, 2) << Vec2d(3, 4), Vec2d(-2, 1), + Vec2d(1, -3), Vec2d(2, 5)); + const Mat expected64 = (Mat_(2, 2) << Vec2d(-6, 3), Vec2d(5, 5), + Vec2d(-3, -19), Vec2d(3, 19)); + const int types[] = {CV_32FC2, CV_64FC2}; + for (int i = 0; i < 2; ++i) + { + SCOPED_TRACE(types[i]); + Mat a, b, expected, actual; + a64.convertTo(a, types[i]); + b64.convertTo(b, types[i]); + expected64.convertTo(expected, types[i]); + + gemm(a, b, 1, Mat(), 0, actual); + EXPECT_EQ(0., cvtest::norm(actual, expected, NORM_INF)) << "out-of-place"; + + Mat aliasA = a.clone(); + gemm(aliasA, b, 1, Mat(), 0, aliasA); + EXPECT_EQ(0., cvtest::norm(aliasA, expected, NORM_INF)) << "destination aliases A"; + + Mat aliasB = b.clone(); + gemm(a, aliasB, 1, Mat(), 0, aliasB); + EXPECT_EQ(0., cvtest::norm(aliasB, expected, NORM_INF)) << "destination aliases B"; + } +} + TEST(Core_Invert, accuracy) { Core_InvertTest test; test.safe_run(); } TEST(Core_Mahalanobis, accuracy) { Core_MahalanobisTest test; test.safe_run(); } TEST(Core_MulTransposed, accuracy) { Core_MulTransposedTest test; test.safe_run(); }