Skip to content

Commit adcb070

Browse files
Merge pull request #25307 from MaximMilashchenko:halrvv071
* added hal for cv_hal_cvtBGRtoBGR rvv 0.7.1
1 parent cbf3b11 commit adcb070

4 files changed

Lines changed: 153 additions & 0 deletions

File tree

3rdparty/hal_rvv/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION ${MIN_VER_CMAKE} FATAL_ERROR)
2+
3+
set(HAL_LIB_NAME "")
4+
5+
set(RVV_HAL_FOUND TRUE CACHE INTERNAL "")
6+
set(RVV_HAL_VERSION "0.0.1" CACHE INTERNAL "")
7+
set(RVV_HAL_LIBRARIES ${HAL_LIB_NAME} CACHE INTERNAL "")
8+
set(RVV_HAL_HEADERS "hal_rvv.hpp" CACHE INTERNAL "")
9+
set(RVV_HAL_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "")

3rdparty/hal_rvv/hal_rvv.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
5+
#ifndef OPENCV_HAL_RVV_HPP_INCLUDED
6+
#define OPENCV_HAL_RVV_HPP_INCLUDED
7+
8+
#include <riscv_vector.h>
9+
10+
#include "opencv2/core/hal/interface.h"
11+
12+
#ifndef CV_HAL_RVV_071_ENABLED
13+
# if defined(__GNUC__) && __GNUC__ == 10 && __GNUC_MINOR__ == 4 && defined(__THEAD_VERSION__) && defined(__riscv_v) && __riscv_v == 7000
14+
# define CV_HAL_RVV_071_ENABLED 1
15+
# else
16+
# define CV_HAL_RVV_071_ENABLED 0
17+
# endif
18+
#endif
19+
20+
#if CV_HAL_RVV_071_ENABLED
21+
#include "version/hal_rvv_071.hpp"
22+
#endif
23+
24+
#endif
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
5+
#ifndef OPENCV_HAL_RVV_071_HPP_INCLUDED
6+
#define OPENCV_HAL_RVV_071_HPP_INCLUDED
7+
8+
#include <limits>
9+
10+
namespace cv { namespace cv_hal_rvv {
11+
12+
#undef cv_hal_cvtBGRtoBGR
13+
#define cv_hal_cvtBGRtoBGR cv::cv_hal_rvv::cvtBGRtoBGR
14+
15+
static const unsigned char index_array_32 [32]
16+
{ 2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15, 18, 17, 16, 19, 22, 21, 20, 23, 26, 25, 24, 27, 30, 29, 28, 31 };
17+
18+
static const unsigned char index_array_24 [24]
19+
{ 2, 1, 0, 5, 4, 3, 8, 7, 6, 11, 10, 9, 14, 13, 12, 17, 16, 15, 20, 19, 18, 23, 22, 21 };
20+
21+
static void vBGRtoBGR(const unsigned char* src, unsigned char * dst, const unsigned char * index, int n, int scn, int dcn, int vsize_pixels, const int vsize)
22+
{
23+
vuint8m2_t vec_index = vle8_v_u8m2(index, vsize);
24+
25+
int i = 0;
26+
27+
for ( ; i <= n-vsize; i += vsize_pixels, src += vsize, dst += vsize)
28+
{
29+
vuint8m2_t vec_src = vle8_v_u8m2(src, vsize);
30+
vuint8m2_t vec_dst = vrgather_vv_u8m2(vec_src, vec_index, vsize);
31+
vse8_v_u8m2(dst, vec_dst, vsize);
32+
}
33+
34+
for ( ; i < n; i++, src += scn, dst += dcn )
35+
{
36+
unsigned char t0 = src[0], t1 = src[1], t2 = src[2];
37+
dst[2] = t0;
38+
dst[1] = t1;
39+
dst[0] = t2;
40+
if(dcn == 4)
41+
{
42+
unsigned char d = src[3];
43+
dst[3] = d;
44+
}
45+
}
46+
}
47+
48+
static void sBGRtoBGR(const unsigned char* src, unsigned char * dst, int n, int scn, int dcn, int bi)
49+
{
50+
for (int i = 0; i < n; i++, src += scn, dst += dcn)
51+
{
52+
unsigned char t0 = src[0], t1 = src[1], t2 = src[2];
53+
dst[bi ] = t0;
54+
dst[1] = t1;
55+
dst[bi^2] = t2;
56+
if(dcn == 4)
57+
{
58+
unsigned char d = scn == 4 ? src[3] : std::numeric_limits<unsigned char>::max();
59+
dst[3] = d;
60+
}
61+
}
62+
}
63+
64+
static int cvtBGRtoBGR(const unsigned char * src_data, size_t src_step, unsigned char * dst_data, size_t dst_step, int width, int height, int depth, int scn, int dcn, bool swapBlue)
65+
{
66+
if (depth != CV_8U)
67+
{
68+
return CV_HAL_ERROR_NOT_IMPLEMENTED;
69+
}
70+
71+
const int blueIdx = swapBlue ? 2 : 0;
72+
if (scn == dcn)
73+
{
74+
if (!swapBlue)
75+
{
76+
return CV_HAL_ERROR_NOT_IMPLEMENTED;
77+
}
78+
79+
const int vsize_pixels = 8;
80+
81+
if (scn == 4)
82+
{
83+
for (int i = 0; i < height; i++, src_data += src_step, dst_data += dst_step)
84+
{
85+
vBGRtoBGR(src_data, dst_data, index_array_32, width, scn, dcn, vsize_pixels, 32);
86+
}
87+
}
88+
else
89+
{
90+
for (int i = 0; i < height; i++, src_data += src_step, dst_data += dst_step)
91+
{
92+
vBGRtoBGR(src_data, dst_data, index_array_24, width, scn, dcn, vsize_pixels, 24);
93+
}
94+
}
95+
}
96+
else
97+
{
98+
for (int i = 0; i < height; i++, src_data += src_step, dst_data += dst_step)
99+
sBGRtoBGR(src_data, dst_data, width, scn, dcn, blueIdx);
100+
}
101+
102+
return CV_HAL_ERROR_OK;
103+
}
104+
105+
}}
106+
107+
#endif

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ OCV_OPTION(WITH_KLEIDICV "Use KleidiCV library for ARM platforms" OFF
265265
VISIBLE_IF (AARCH64 AND (ANDROID OR UNIX AND NOT IOS AND NOT XROS)))
266266
OCV_OPTION(WITH_NDSRVP "Use Andes RVP extension" (NOT CV_DISABLE_OPTIMIZATION)
267267
VISIBLE_IF RISCV)
268+
OCV_OPTION(WITH_HAL_RVV "Use HAL RVV optimizations" (NOT CV_DISABLE_OPTIMIZATION)
269+
VISIBLE_IF RISCV)
268270
OCV_OPTION(WITH_CPUFEATURES "Use cpufeatures Android library" ON
269271
VISIBLE_IF ANDROID
270272
VERIFY HAVE_CPUFEATURES)
@@ -994,6 +996,13 @@ if(WITH_NDSRVP)
994996
endif()
995997
endif()
996998

999+
if(WITH_HAL_RVV)
1000+
ocv_debug_message(STATUS "Enable HAL RVV acceleration")
1001+
if(NOT ";${OpenCV_HAL};" MATCHES ";halrvv;")
1002+
set(OpenCV_HAL "halrvv;${OpenCV_HAL}")
1003+
endif()
1004+
endif()
1005+
9971006
foreach(hal ${OpenCV_HAL})
9981007
if(hal STREQUAL "carotene")
9991008
if(";${CPU_BASELINE_FINAL};" MATCHES ";NEON;")
@@ -1023,6 +1032,10 @@ foreach(hal ${OpenCV_HAL})
10231032
else()
10241033
message(STATUS "NDSRVP: Andes GNU Toolchain DSP extension is not open, disabling ndsrvp...")
10251034
endif()
1035+
elseif(hal STREQUAL "halrvv")
1036+
add_subdirectory(3rdparty/hal_rvv/)
1037+
ocv_hal_register(RVV_HAL_LIBRARIES RVV_HAL_HEADERS RVV_HAL_INCLUDE_DIRS)
1038+
list(APPEND OpenCV_USED_HAL "HAL RVV (ver ${RVV_HAL_VERSION})")
10261039
elseif(hal STREQUAL "openvx")
10271040
add_subdirectory(3rdparty/openvx)
10281041
ocv_hal_register(OPENVX_HAL_LIBRARIES OPENVX_HAL_HEADERS OPENVX_HAL_INCLUDE_DIRS)

0 commit comments

Comments
 (0)