From f016c728f56da1c876a2d40623a11ad173f81bff Mon Sep 17 00:00:00 2001 From: Yuantao Feng Date: Thu, 15 May 2025 14:57:05 +0800 Subject: [PATCH] Merge pull request #27315 from fengyuentau:4x/hal/riscv-rvv/refactor_functab_elemsize python3 "/opencv/platforms/android/build_sdk.py" --build_doc --config "/opencv/platforms/android/default.config.py" --sdk_path "$ANDROID_HOME" --ndk_path "$ANDROID_NDK_HOME" /build | tee /build/build-log.txt python3 "/opencv/platforms/android/build_java_shared_aar.py" --offline --ndk_location="$ANDROID_NDK_HOME" --cmake_location=$(dirname $(dirname $(which cmake))) /build/OpenCV-android-sdk hal/riscv-rvv: make use of function tab in copyToMasked and CV_ELEM_SIZE1 in place of elem_size_tab #27315 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- hal/riscv-rvv/src/core/copy_mask.cpp | 67 +++++++--------------------- hal/riscv-rvv/src/core/dotprod.cpp | 13 ++---- hal/riscv-rvv/src/core/norm.cpp | 13 ++---- hal/riscv-rvv/src/core/norm_diff.cpp | 15 ++----- 4 files changed, 25 insertions(+), 83 deletions(-) diff --git a/hal/riscv-rvv/src/core/copy_mask.cpp b/hal/riscv-rvv/src/core/copy_mask.cpp index dd49cfdeab..8bde41759a 100644 --- a/hal/riscv-rvv/src/core/copy_mask.cpp +++ b/hal/riscv-rvv/src/core/copy_mask.cpp @@ -105,62 +105,25 @@ int copyToMasked(const uchar *src_data, size_t src_step, uchar *dst_data, size_t return CV_HAL_ERROR_NOT_IMPLEMENTED; } - CopyToMaskedFunc func = nullptr; - switch (depth) { - case CV_8U: {} - case CV_8S: switch (cn) { - case 1: func = copyToMasked_e8c1; break; - case 2: func = copyToMasked_e16c1; break; - case 3: func = copyToMasked_e8c3; break; - case 4: func = copyToMasked_e32c1; break; - case 6: func = copyToMasked_e16c3; break; - case 8: func = copyToMasked_e64c1; break; - default: func = nullptr; - }; break; - case CV_16U: {} - case CV_16S: switch (cn) { - case 1: func = copyToMasked_e16c1; break; - case 2: func = copyToMasked_e32c1; break; - case 3: func = copyToMasked_e16c3; break; - case 4: func = copyToMasked_e64c1; break; - case 6: func = copyToMasked_e32c3; break; - case 8: func = copyToMasked_e64c2; break; - default: func = nullptr; break; - }; break; - case CV_32S: {} - case CV_32F: switch (cn) { - case 1: func = copyToMasked_e32c1; break; - case 2: func = copyToMasked_e64c1; break; - case 3: func = copyToMasked_e32c3; break; - case 4: func = copyToMasked_e64c2; break; - case 6: func = copyToMasked_e64c3; break; - case 8: func = copyToMasked_e64c4; break; - default: func = nullptr; break; - }; break; - case CV_64F: switch (cn) { - case 1: func = copyToMasked_e64c1; break; - case 2: func = copyToMasked_e64c2; break; - case 3: func = copyToMasked_e64c3; break; - case 4: func = copyToMasked_e64c4; break; - default: func = nullptr; break; - }; break; - default: func = nullptr; - } - + static CopyToMaskedFunc tab[] = { + 0, copyToMasked_e8c1, copyToMasked_e16c1, copyToMasked_e8c3, + copyToMasked_e32c1, 0, copyToMasked_e16c3, 0, + copyToMasked_e64c1, 0, 0, 0, + copyToMasked_e32c3, 0, 0, 0, + copyToMasked_e64c2, 0, 0, 0, + 0, 0, 0, 0, + copyToMasked_e64c3, 0, 0, 0, + 0, 0, 0, 0, + copyToMasked_e64c4 + }; + CopyToMaskedFunc func = tab[CV_ELEM_SIZE(type)]; if (func == nullptr) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } - static const size_t elem_size_tab[CV_DEPTH_MAX] = { - sizeof(uchar), sizeof(schar), - sizeof(ushort), sizeof(short), - sizeof(int), sizeof(float), - sizeof(int64_t), 0, - }; - CV_Assert(elem_size_tab[depth]); - - bool src_continuous = (src_step == width * elem_size_tab[depth] * cn || (src_step != width * elem_size_tab[depth] * cn && height == 1)); - bool dst_continuous = (dst_step == width * elem_size_tab[depth] * cn || (dst_step != width * elem_size_tab[depth] * cn && height == 1)); + int elem_size1 = CV_ELEM_SIZE1(type); + bool src_continuous = (src_step == width * elem_size1 * cn || (src_step != width * elem_size1 * cn && height == 1)); + bool dst_continuous = (dst_step == width * elem_size1 * cn || (dst_step != width * elem_size1 * cn && height == 1)); bool mask_continuous = (mask_step == static_cast(width)); size_t nplanes = 1; int _width = width, _height = height; diff --git a/hal/riscv-rvv/src/core/dotprod.cpp b/hal/riscv-rvv/src/core/dotprod.cpp index 2630ca198d..6c896a59e1 100644 --- a/hal/riscv-rvv/src/core/dotprod.cpp +++ b/hal/riscv-rvv/src/core/dotprod.cpp @@ -190,16 +190,9 @@ int dotprod(const uchar *a_data, size_t a_step, const uchar *b_data, size_t b_st return CV_HAL_ERROR_NOT_IMPLEMENTED; } - static const size_t elem_size_tab[CV_DEPTH_MAX] = { - sizeof(uchar), sizeof(schar), - sizeof(ushort), sizeof(short), - sizeof(int), sizeof(float), - sizeof(int64_t), 0, - }; - CV_Assert(elem_size_tab[depth]); - - bool a_continuous = (a_step == width * elem_size_tab[depth] * cn); - bool b_continuous = (b_step == width * elem_size_tab[depth] * cn); + int elem_size1 = CV_ELEM_SIZE1(type); + bool a_continuous = (a_step == width * elem_size1 * cn); + bool b_continuous = (b_step == width * elem_size1 * cn); size_t nplanes = 1; size_t len = width * height; if (!a_continuous || !b_continuous) { diff --git a/hal/riscv-rvv/src/core/norm.cpp b/hal/riscv-rvv/src/core/norm.cpp index b2deb3f4fc..e07b34049a 100644 --- a/hal/riscv-rvv/src/core/norm.cpp +++ b/hal/riscv-rvv/src/core/norm.cpp @@ -999,15 +999,8 @@ int norm(const uchar* src, size_t src_step, const uchar* mask, size_t mask_step, }, }; - static const size_t elem_size_tab[CV_DEPTH_MAX] = { - sizeof(uchar), sizeof(schar), - sizeof(ushort), sizeof(short), - sizeof(int), sizeof(float), - sizeof(int64_t), 0, - }; - CV_Assert(elem_size_tab[depth]); - - bool src_continuous = (src_step == width * elem_size_tab[depth] * cn || (src_step != width * elem_size_tab[depth] * cn && height == 1)); + int elem_size1 = CV_ELEM_SIZE1(type); + bool src_continuous = (src_step == width * elem_size1 * cn || (src_step != width * elem_size1 * cn && height == 1)); bool mask_continuous = (mask_step == static_cast(width)); size_t nplanes = 1; size_t size = width * height; @@ -1030,7 +1023,7 @@ int norm(const uchar* src, size_t src_step, const uchar* mask, size_t mask_step, res.d = 0; if ((norm_type == NORM_L1 && depth <= CV_16S) || ((norm_type == NORM_L2 || norm_type == NORM_L2SQR) && depth <= CV_8S)) { - const size_t esz = elem_size_tab[depth] * cn; + const size_t esz = elem_size1 * cn; const int total = (int)size; const int intSumBlockSize = (norm_type == NORM_L1 && depth <= CV_8S ? (1 << 23) : (1 << 15))/cn; const int blockSize = std::min(total, intSumBlockSize); diff --git a/hal/riscv-rvv/src/core/norm_diff.cpp b/hal/riscv-rvv/src/core/norm_diff.cpp index f136be108a..adf2555694 100644 --- a/hal/riscv-rvv/src/core/norm_diff.cpp +++ b/hal/riscv-rvv/src/core/norm_diff.cpp @@ -1111,16 +1111,9 @@ int normDiff(const uchar* src1, size_t src1_step, const uchar* src2, size_t src2 }, }; - static const size_t elem_size_tab[CV_DEPTH_MAX] = { - sizeof(uchar), sizeof(schar), - sizeof(ushort), sizeof(short), - sizeof(int), sizeof(float), - sizeof(int64_t), 0, - }; - CV_Assert(elem_size_tab[depth]); - - bool src_continuous = (src1_step == width * elem_size_tab[depth] * cn || (src1_step != width * elem_size_tab[depth] * cn && height == 1)); - src_continuous &= (src2_step == width * elem_size_tab[depth] * cn || (src2_step != width * elem_size_tab[depth] * cn && height == 1)); + int elem_size1 = CV_ELEM_SIZE1(type); + bool src_continuous = (src1_step == width * elem_size1 * cn || (src1_step != width * elem_size1 * cn && height == 1)); + src_continuous &= (src2_step == width * elem_size1 * cn || (src2_step != width * elem_size1 * cn && height == 1)); bool mask_continuous = (mask_step == static_cast(width)); size_t nplanes = 1; size_t size = width * height; @@ -1143,7 +1136,7 @@ int normDiff(const uchar* src1, size_t src1_step, const uchar* src2, size_t src2 res.d = 0; if ((norm_type == NORM_L1 && depth <= CV_16S) || ((norm_type == NORM_L2 || norm_type == NORM_L2SQR) && depth <= CV_8S)) { - const size_t esz = elem_size_tab[depth] * cn; + const size_t esz = elem_size1 * cn; const int total = (int)size; const int intSumBlockSize = (norm_type == NORM_L1 && depth <= CV_8S ? (1 << 23) : (1 << 15))/cn; const int blockSize = std::min(total, intSumBlockSize);