From 666ad5bfcf2d93a4cc5d3f8d77e58c615ec71f17 Mon Sep 17 00:00:00 2001 From: Teddy-Yangjiale <12411723@mail.sustech.edu.cn> Date: Tue, 19 May 2026 15:19:26 +0800 Subject: [PATCH] Merge pull request #29058 from Teddy-Yangjiale:rvv-opt RISC-V: add RVV convertScale paths for selected depth conversions #29058 ### Summary This PR extends the RISC-V RVV HAL `convertScale` implementation with optimized paths for selected `convertTo()` depth conversions: - `CV_32F -> CV_8U` - `CV_16U -> CV_32F` - `CV_16S -> CV_32F` The change is confined to `hal/riscv-rvv/src/core/convert_scale.cpp`. It does not modify the generic `convertTo()` implementation or affect dispatch for non-RISC-V targets. ### Rationale `Mat::convertTo()` reaches architecture-specific implementations through the HAL `convertScale` interface. Adding these paths in the RVV HAL keeps the optimization in the existing backend layer and avoids introducing RISC-V-specific branches into common OpenCV code. The selected conversions were chosen based on measured benefit and practical relevance. `CV_32F -> CV_8U` provides the largest speedup, while `CV_16U -> CV_32F` and `CV_16S -> CV_32F` provide consistent improvements for common image-processing data paths. ### Benchmark Tested on Banana Pi BPI-F3, an 8-core RISC-V board with RVV 1.0 support. Build and test configuration: - OpenCV `4.14.0-pre` - Release build - RVV HAL enabled - Compiler: `riscv64-unknown-linux-gnu-g++ 14.2.1` - Test binary: `opencv_perf_core` - Test filter: `*convertTo*` - Samples: `--perf_force_samples=20 --perf_min_samples=20` Benchmark command: ```bash ./opencv_perf_core \ --gtest_filter=*convertTo* \ --perf_force_samples=20 \ --perf_min_samples=20 ``` Each benchmark case was measured with 20 forced samples. The baseline/after comparison was repeated to check run-to-run stability. The optimized conversion pairs showed consistent improvements. Second-run comparison for the optimized pairs: ```text OPTIMIZED_PAIRS: 24 cases, gmean 2.6009x CV_32F -> CV_8U: 16.0718x CV_16U -> CV_32F: 1.2334x CV_16S -> CV_32F: 1.1376x ``` `CV_32F -> CV_8U` showed the largest improvement, with speedups in the `14.5x` to `18.8x` range across the tested image sizes, channel counts, and alpha values. ### Notes on Measurement The initial benchmark run showed noise in conversion pairs not touched by this PR, likely due to board-level variance such as frequency scaling, thermal state, or background scheduling. A second run showed untouched pairs close to neutral: ```text UNTOUCHED_OTHER: gmean 0.9941x ``` The optimized conversion pairs remained consistently faster across both runs, with no observed regression in the added RVV paths. ### 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/convert_scale.cpp | 89 ++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/hal/riscv-rvv/src/core/convert_scale.cpp b/hal/riscv-rvv/src/core/convert_scale.cpp index 8c5f83a677..bb0286354c 100644 --- a/hal/riscv-rvv/src/core/convert_scale.cpp +++ b/hal/riscv-rvv/src/core/convert_scale.cpp @@ -62,6 +62,79 @@ inline int convertScale_8U32F(const uchar* src, size_t src_step, uchar* dst, siz return CV_HAL_ERROR_OK; } +inline int convertScale_16U32F(const uchar* src, size_t src_step, uchar* dst, size_t dst_step, int width, int height, double alpha, double beta) +{ + int vlmax = __riscv_vsetvlmax_e32m8(); + auto vec_b = __riscv_vfmv_v_f_f32m8(beta, vlmax); + float a = alpha; + + for (int i = 0; i < height; i++) + { + const ushort* src_row = reinterpret_cast(src + i * src_step); + float* dst_row = reinterpret_cast(dst + i * dst_step); + int vl; + for (int j = 0; j < width; j += vl) + { + vl = __riscv_vsetvl_e16m4(width - j); + auto vec_src = __riscv_vle16_v_u16m4(src_row + j, vl); + auto vec_src_f32 = __riscv_vfwcvt_f(vec_src, vl); + auto vec_fma = __riscv_vfmadd(vec_src_f32, a, vec_b, vl); + __riscv_vse32_v_f32m8(dst_row + j, vec_fma, vl); + } + } + + return CV_HAL_ERROR_OK; +} + +inline int convertScale_16S32F(const uchar* src, size_t src_step, uchar* dst, size_t dst_step, int width, int height, double alpha, double beta) +{ + int vlmax = __riscv_vsetvlmax_e32m8(); + auto vec_b = __riscv_vfmv_v_f_f32m8(beta, vlmax); + float a = alpha; + + for (int i = 0; i < height; i++) + { + const short* src_row = reinterpret_cast(src + i * src_step); + float* dst_row = reinterpret_cast(dst + i * dst_step); + int vl; + for (int j = 0; j < width; j += vl) + { + vl = __riscv_vsetvl_e16m4(width - j); + auto vec_src = __riscv_vle16_v_i16m4(src_row + j, vl); + auto vec_src_f32 = __riscv_vfwcvt_f(vec_src, vl); + auto vec_fma = __riscv_vfmadd(vec_src_f32, a, vec_b, vl); + __riscv_vse32_v_f32m8(dst_row + j, vec_fma, vl); + } + } + + return CV_HAL_ERROR_OK; +} + +inline int convertScale_32F8U(const uchar* src, size_t src_step, uchar* dst, size_t dst_step, int width, int height, double alpha, double beta) +{ + int vlmax = __riscv_vsetvlmax_e32m8(); + auto vec_b = __riscv_vfmv_v_f_f32m8(beta, vlmax); + float a = alpha; + + for (int i = 0; i < height; i++) + { + const float* src_row = reinterpret_cast(src + i * src_step); + uchar* dst_row = dst + i * dst_step; + int vl; + for (int j = 0; j < width; j += vl) + { + vl = __riscv_vsetvl_e32m8(width - j); + auto vec_src = __riscv_vle32_v_f32m8(src_row + j, vl); + auto vec_fma = __riscv_vfmadd(vec_src, a, vec_b, vl); + auto vec_dst_u16 = __riscv_vfncvt_xu(vec_fma, vl); + auto vec_dst = __riscv_vnclipu(vec_dst_u16, 0, __RISCV_VXRM_RNU, vl); + __riscv_vse8_v_u8m2(dst_row + j, vec_dst, vl); + } + } + + return CV_HAL_ERROR_OK; +} + inline int convertScale_32F32F(const uchar* src, size_t src_step, uchar* dst, size_t dst_step, int width, int height, double alpha, double beta) { int vlmax = __riscv_vsetvlmax_e32m8(); @@ -102,9 +175,25 @@ int convertScale(const uchar* src, size_t src_step, uchar* dst, size_t dst_step, return convertScale_8U32F(src, src_step, dst, dst_step, width, height, alpha, beta); } return CV_HAL_ERROR_NOT_IMPLEMENTED; + case CV_16U: + switch (ddepth) + { + case CV_32F: + return convertScale_16U32F(src, src_step, dst, dst_step, width, height, alpha, beta); + } + return CV_HAL_ERROR_NOT_IMPLEMENTED; + case CV_16S: + switch (ddepth) + { + case CV_32F: + return convertScale_16S32F(src, src_step, dst, dst_step, width, height, alpha, beta); + } + return CV_HAL_ERROR_NOT_IMPLEMENTED; case CV_32F: switch (ddepth) { + case CV_8U: + return convertScale_32F8U(src, src_step, dst, dst_step, width, height, alpha, beta); case CV_32F: return convertScale_32F32F(src, src_step, dst, dst_step, width, height, alpha, beta); }