Skip to content

Commit 3fbaad3

Browse files
authored
Merge pull request opencv#26624 from hanliutong:rvv-mean
Add RISC-V HAL implementation for meanStdDev opencv#26624 `meanStdDev` benefits from the Universal Intrinsic backend of RVV, but we also found that the performance on the `8UC4` type is worse than the scalar version when there is a mask, and there is no optimization implementation on `32FC1`. This patch implements `meanStdDev` function in RVV_HAL using native intrinsic, significantly optimizing the performance for `8UC1`, `8UC4` and `32FC1`. This patch is tested on BPI-F3 for both gcc 14.2 and clang 19.1. ``` $ opencv_test_core --gtest_filter="*MeanStdDev*" $ opencv_perf_core --gtest_filter="Size_MatType_meanStdDev* ``` ![1734077611879](https://github.com/user-attachments/assets/71c85c9d-1db1-470d-81d1-bf546e27ad86) ### 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 - [ ] 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
1 parent 7c0c9e1 commit 3fbaad3

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed

3rdparty/hal_rvv/hal_rvv.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#if defined(__riscv_v) && __riscv_v == 1000000
2323
#include "hal_rvv_1p0/merge.hpp" // core
24+
#include "hal_rvv_1p0/mean.hpp" // core
2425
#endif
2526

2627
#endif
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
#ifndef OPENCV_HAL_RVV_MEANSTDDEV_HPP_INCLUDED
5+
#define OPENCV_HAL_RVV_MEANSTDDEV_HPP_INCLUDED
6+
7+
#include <riscv_vector.h>
8+
9+
namespace cv { namespace cv_hal_rvv {
10+
11+
#undef cv_hal_meanStdDev
12+
#define cv_hal_meanStdDev cv::cv_hal_rvv::meanStdDev
13+
14+
inline int meanStdDev_8UC1(const uchar* src_data, size_t src_step, int width, int height,
15+
double* mean_val, double* stddev_val, uchar* mask, size_t mask_step);
16+
inline int meanStdDev_8UC4(const uchar* src_data, size_t src_step, int width, int height,
17+
double* mean_val, double* stddev_val, uchar* mask, size_t mask_step);
18+
inline int meanStdDev_32FC1(const uchar* src_data, size_t src_step, int width, int height,
19+
double* mean_val, double* stddev_val, uchar* mask, size_t mask_step);
20+
21+
inline int meanStdDev(const uchar* src_data, size_t src_step, int width, int height,
22+
int src_type, double* mean_val, double* stddev_val, uchar* mask, size_t mask_step) {
23+
switch (src_type)
24+
{
25+
case CV_8UC1:
26+
return meanStdDev_8UC1(src_data, src_step, width, height, mean_val, stddev_val, mask, mask_step);
27+
case CV_8UC4:
28+
return meanStdDev_8UC4(src_data, src_step, width, height, mean_val, stddev_val, mask, mask_step);
29+
case CV_32FC1:
30+
return meanStdDev_32FC1(src_data, src_step, width, height, mean_val, stddev_val, mask, mask_step);
31+
default:
32+
return CV_HAL_ERROR_NOT_IMPLEMENTED;
33+
}
34+
}
35+
36+
inline int meanStdDev_8UC1(const uchar* src_data, size_t src_step, int width, int height,
37+
double* mean_val, double* stddev_val, uchar* mask, size_t mask_step) {
38+
int nz = 0;
39+
int vlmax = __riscv_vsetvlmax_e64m8();
40+
vuint64m8_t vec_sum = __riscv_vmv_v_x_u64m8(0, vlmax);
41+
vuint64m8_t vec_sqsum = __riscv_vmv_v_x_u64m8(0, vlmax);
42+
if (mask) {
43+
for (int i = 0; i < height; ++i) {
44+
const uchar* src_row = src_data + i * src_step;
45+
const uchar* mask_row = mask + i * mask_step;
46+
int j = 0, vl;
47+
for ( ; j < width; j += vl) {
48+
vl = __riscv_vsetvl_e8m1(width - j);
49+
auto vec_pixel_u8 = __riscv_vle8_v_u8m1(src_row + j, vl);
50+
auto vmask_u8 = __riscv_vle8_v_u8m1(mask_row+j, vl);
51+
auto vec_pixel = __riscv_vzext_vf4(vec_pixel_u8, vl);
52+
auto vmask = __riscv_vmseq_vx_u8m1_b8(vmask_u8, 1, vl);
53+
vec_sum = __riscv_vwaddu_wv_u64m8_tumu(vmask, vec_sum, vec_sum, vec_pixel, vl);
54+
vec_sqsum = __riscv_vwmaccu_vv_u64m8_tumu(vmask, vec_sqsum, vec_pixel, vec_pixel, vl);
55+
nz += __riscv_vcpop_m_b8(vmask, vl);
56+
}
57+
}
58+
} else {
59+
for (int i = 0; i < height; i++) {
60+
const uchar* src_row = src_data + i * src_step;
61+
int j = 0, vl;
62+
for ( ; j < width; j += vl) {
63+
vl = __riscv_vsetvl_e8m1(width - j);
64+
auto vec_pixel_u8 = __riscv_vle8_v_u8m1(src_row + j, vl);
65+
auto vec_pixel = __riscv_vzext_vf4(vec_pixel_u8, vl);
66+
vec_sum = __riscv_vwaddu_wv_u64m8_tu(vec_sum, vec_sum, vec_pixel, vl);
67+
vec_sqsum = __riscv_vwmaccu_vv_u64m8_tu(vec_sqsum, vec_pixel, vec_pixel, vl);
68+
}
69+
}
70+
nz = height * width;
71+
}
72+
if (nz == 0) {
73+
if (mean_val) *mean_val = 0.0;
74+
if (stddev_val) *stddev_val = 0.0;
75+
return CV_HAL_ERROR_OK;
76+
}
77+
auto zero = __riscv_vmv_s_x_u64m1(0, vlmax);
78+
auto vec_red = __riscv_vmv_v_x_u64m1(0, vlmax);
79+
auto vec_reddev = __riscv_vmv_v_x_u64m1(0, vlmax);
80+
vec_red = __riscv_vredsum(vec_sum, zero, vlmax);
81+
vec_reddev = __riscv_vredsum(vec_sqsum, zero, vlmax);
82+
double sum = __riscv_vmv_x(vec_red);
83+
double mean = sum / nz;
84+
if (mean_val) {
85+
*mean_val = mean;
86+
}
87+
if (stddev_val) {
88+
double sqsum = __riscv_vmv_x(vec_reddev);
89+
double variance = std::max((sqsum / nz) - (mean * mean), 0.0);
90+
double stddev = std::sqrt(variance);
91+
*stddev_val = stddev;
92+
}
93+
return CV_HAL_ERROR_OK;
94+
}
95+
96+
inline int meanStdDev_8UC4(const uchar* src_data, size_t src_step, int width, int height,
97+
double* mean_val, double* stddev_val, uchar* mask, size_t mask_step) {
98+
int nz = 0;
99+
int vlmax = __riscv_vsetvlmax_e64m8();
100+
vuint64m8_t vec_sum = __riscv_vmv_v_x_u64m8(0, vlmax);
101+
vuint64m8_t vec_sqsum = __riscv_vmv_v_x_u64m8(0, vlmax);
102+
if (mask) {
103+
for (int i = 0; i < height; ++i) {
104+
const uchar* src_row = src_data + i * src_step;
105+
const uchar* mask_row = mask + i * mask_step;
106+
int j = 0, jm = 0, vl, vlm;
107+
for ( ; j < width*4; j += vl, jm += vlm) {
108+
vl = __riscv_vsetvl_e8m1(width*4 - j);
109+
vlm = __riscv_vsetvl_e8mf4(width - jm);
110+
auto vec_pixel_u8 = __riscv_vle8_v_u8m1(src_row + j, vl);
111+
auto vmask_u8mf4 = __riscv_vle8_v_u8mf4(mask_row + jm, vlm);
112+
auto vmask_u32 = __riscv_vzext_vf4(vmask_u8mf4, vlm);
113+
// 0 -> 0000; 1 -> 1111
114+
vmask_u32 = __riscv_vmul(vmask_u32, 0b00000001000000010000000100000001, vlm);
115+
auto vmask_u8 = __riscv_vreinterpret_u8m1(vmask_u32);
116+
auto vec_pixel = __riscv_vzext_vf4(vec_pixel_u8, vl);
117+
auto vmask = __riscv_vmseq_vx_u8m1_b8(vmask_u8, 1, vl);
118+
vec_sum = __riscv_vwaddu_wv_u64m8_tumu(vmask, vec_sum, vec_sum, vec_pixel, vl);
119+
vec_sqsum = __riscv_vwmaccu_vv_u64m8_tumu(vmask, vec_sqsum, vec_pixel, vec_pixel, vl);
120+
nz += __riscv_vcpop_m_b8(vmask, vl);
121+
}
122+
nz /= 4;
123+
}
124+
} else {
125+
for (int i = 0; i < height; i++) {
126+
const uchar* src_row = src_data + i * src_step;
127+
int j = 0, vl;
128+
for ( ; j < width*4; j += vl) {
129+
vl = __riscv_vsetvl_e8m1(width*4 - j);
130+
auto vec_pixel_u8 = __riscv_vle8_v_u8m1(src_row + j, vl);
131+
auto vec_pixel = __riscv_vzext_vf4(vec_pixel_u8, vl);
132+
vec_sum = __riscv_vwaddu_wv_u64m8_tu(vec_sum, vec_sum, vec_pixel, vl);
133+
vec_sqsum = __riscv_vwmaccu_vv_u64m8_tu(vec_sqsum, vec_pixel, vec_pixel, vl);
134+
}
135+
}
136+
nz = height * width;
137+
}
138+
if (nz == 0) {
139+
if (mean_val) *mean_val = 0.0;
140+
if (stddev_val) *stddev_val = 0.0;
141+
return CV_HAL_ERROR_OK;
142+
}
143+
uint64_t s[256], sq[256], sum[4] = {0}, sqsum[4] = {0};
144+
__riscv_vse64(s, vec_sum, vlmax);
145+
__riscv_vse64(sq, vec_sqsum, vlmax);
146+
for (int i = 0; i < vlmax; ++i)
147+
{
148+
sum[i % 4] += s[i];
149+
sqsum[i % 4] += sq[i];
150+
}
151+
if (mean_val) {
152+
mean_val[0] = (double)sum[0] / nz;
153+
mean_val[1] = (double)sum[1] / nz;
154+
mean_val[2] = (double)sum[2] / nz;
155+
mean_val[3] = (double)sum[3] / nz;
156+
}
157+
if (stddev_val) {
158+
stddev_val[0] = std::sqrt(std::max(((double)sqsum[0] / nz) - (mean_val[0] * mean_val[0]), 0.0));
159+
stddev_val[1] = std::sqrt(std::max(((double)sqsum[1] / nz) - (mean_val[1] * mean_val[1]), 0.0));
160+
stddev_val[2] = std::sqrt(std::max(((double)sqsum[2] / nz) - (mean_val[2] * mean_val[2]), 0.0));
161+
stddev_val[3] = std::sqrt(std::max(((double)sqsum[3] / nz) - (mean_val[3] * mean_val[3]), 0.0));
162+
}
163+
return CV_HAL_ERROR_OK;
164+
}
165+
166+
inline int meanStdDev_32FC1(const uchar* src_data, size_t src_step, int width, int height,
167+
double* mean_val, double* stddev_val, uchar* mask, size_t mask_step) {
168+
int nz = 0;
169+
int vlmax = __riscv_vsetvlmax_e64m4();
170+
vfloat64m4_t vec_sum = __riscv_vfmv_v_f_f64m4(0, vlmax);
171+
vfloat64m4_t vec_sqsum = __riscv_vfmv_v_f_f64m4(0, vlmax);
172+
src_step /= sizeof(float);
173+
if (mask) {
174+
for (int i = 0; i < height; ++i) {
175+
const float* src_row0 = reinterpret_cast<const float*>(src_data) + i * src_step;
176+
const uchar* mask_row = mask + i * mask_step;
177+
int j = 0, vl;
178+
for ( ; j < width; j += vl) {
179+
vl = __riscv_vsetvl_e32m2(width - j);
180+
auto vec_pixel = __riscv_vle32_v_f32m2(src_row0 + j, vl);
181+
auto vmask_u8 = __riscv_vle8_v_u8mf2(mask_row + j, vl);
182+
auto vmask_u32 = __riscv_vzext_vf4(vmask_u8, vl);
183+
auto vmask = __riscv_vmseq_vx_u32m2_b16(vmask_u32, 1, vl);
184+
vec_sum = __riscv_vfwadd_wv_f64m4_tumu(vmask, vec_sum, vec_sum, vec_pixel, vl);
185+
vec_sqsum = __riscv_vfwmacc_vv_f64m4_tumu(vmask, vec_sqsum, vec_pixel, vec_pixel, vl);
186+
nz += __riscv_vcpop_m_b16(vmask, vl);
187+
}
188+
}
189+
} else {
190+
for (int i = 0; i < height; i++) {
191+
const float* src_row0 = reinterpret_cast<const float*>(src_data) + i * src_step;
192+
int j = 0, vl;
193+
for ( ; j < width; j += vl) {
194+
vl = __riscv_vsetvl_e32m2(width - j);
195+
auto vec_pixel = __riscv_vle32_v_f32m2(src_row0 + j, vl);
196+
vec_sum = __riscv_vfwadd_wv_f64m4_tu(vec_sum, vec_sum, vec_pixel, vl);
197+
vec_sqsum = __riscv_vfwmacc_vv_f64m4_tu(vec_sqsum, vec_pixel, vec_pixel, vl);
198+
}
199+
}
200+
nz = height * width;
201+
}
202+
if (nz == 0) {
203+
if (mean_val) *mean_val = 0.0;
204+
if (stddev_val) *stddev_val = 0.0;
205+
return CV_HAL_ERROR_OK;
206+
}
207+
auto zero = __riscv_vfmv_v_f_f64m1(0, vlmax);
208+
auto vec_red = __riscv_vfmv_v_f_f64m1(0, vlmax);
209+
auto vec_reddev = __riscv_vfmv_v_f_f64m1(0, vlmax);
210+
vec_red = __riscv_vfredusum(vec_sum, zero, vlmax);
211+
vec_reddev = __riscv_vfredusum(vec_sqsum, zero, vlmax);
212+
double sum = __riscv_vfmv_f(vec_red);
213+
double mean = sum / nz;
214+
if (mean_val) {
215+
*mean_val = mean;
216+
}
217+
if (stddev_val) {
218+
double sqsum = __riscv_vfmv_f(vec_reddev);
219+
double variance = std::max((sqsum / nz) - (mean * mean), 0.0);
220+
double stddev = std::sqrt(variance);
221+
*stddev_val = stddev;
222+
}
223+
return CV_HAL_ERROR_OK;
224+
}
225+
226+
}}
227+
228+
#endif

0 commit comments

Comments
 (0)