mirror of
https://github.com/opencv/opencv.git
synced 2026-11-27 20:07:05 -06:00
RVV: add fused HAL implementation for rotate - #30057 ## Pull Request Readiness Checklist See details at [https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request](https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request) - [x] I agree to contribute to the project under the Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on code under GPL or another license that is incompatible with OpenCV. - [x] The PR is proposed to the proper branch (`4.x`). - [x] Accuracy tests are included and performance results are provided below. ## Summary This PR adds an RVV HAL implementation for `cv_hal_rotate90` and fuses the transpose and flip operations used by 90-degree and 270-degree rotations into a single vectorized pass. The previous path composes transpose and flip operations, which requires an intermediate image and additional memory traffic. The new RVV path loads several source rows, builds RVV vector tuples, and writes directly to the final rotated positions with strided segmented stores. ## Implementation details - Registers `cv_hal_rotate90` in the RISC-V RVV core HAL. - Processes 1-, 2-, and 8-byte elements in blocks of eight source rows using `vssseg8e8`, `vssseg8e16`, and `vssseg8e64`. - Processes 4-byte elements in blocks of four source rows using `vssseg4e32`. - Uses positive or negative destination strides to implement clockwise and counter-clockwise rotation without a separate flip pass. - Uses dynamic VL and strided vector stores for remaining rows and non-multiple tails. - Supports padded/non-contiguous source rows through `src_step` and `dst_step`. - Returns `CV_HAL_ERROR_NOT_IMPLEMENTED` for aliased input/output and unsupported element sizes, preserving the existing OpenCV fallback behavior. - Keeps the existing RVV flip path for 180-degree rotation when safe. - For 1-byte 180-degree rotation with `VLMAX > 256`, uses 16-bit gather indices with `vrgatherei16` to avoid index wraparound in the existing 8-bit gather path. - Includes compatibility helpers for Clang versions earlier than 18 that do not provide the required vector tuple creation intrinsics. The directly accelerated total element sizes are 1, 2, 4, and 8 bytes. Other sizes, including common 3- and 6-byte pixel formats, fall back to the existing implementation. ## Test environment ```text Build type: Release Compiler: GCC 14.2.0 ISA flags: -march=rv64gcv RISCV_RVV_SCALABLE: ON CPU_BASELINE_REQUIRE: RVV QEMU: 8.2.2, RVV 1.0 ``` ## Accuracy tests The new `Core_Rotate.OddSizesAndPaddedRows` test and the existing randomized rotate test passed under QEMU RVV at VLEN 128, 256, 512, and 1024. ## Performance tests Performance was measured with `opencv_perf_core` under the environment described above. Detailed performance results are provided in the comment below. These measurements are intended to show the relative difference between identical baseline and optimized builds under QEMU. Absolute performance should be confirmed on RISC-V hardware.
197 lines
8.0 KiB
C++
197 lines
8.0 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
//
|
|
// Copyright (C) 2025, SpaceMIT Inc., all rights reserved.
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
#include "rvv_hal.hpp"
|
|
|
|
#include "rvv_vcreate.hpp"
|
|
|
|
namespace cv { namespace rvv_hal { namespace core {
|
|
|
|
#if CV_HAL_RVV_1P0_ENABLED
|
|
|
|
static void transpose2d_8u(const uchar *src_data, size_t src_step, uchar *dst_data, size_t dst_step, int src_width, int src_height) {
|
|
auto transpose_8u_8xVl = [](const uchar *src, size_t sstep, uchar *dst, size_t dstep, const int vl) {
|
|
auto v0 = __riscv_vle8_v_u8m1(src, vl);
|
|
auto v1 = __riscv_vle8_v_u8m1(src + sstep, vl);
|
|
auto v2 = __riscv_vle8_v_u8m1(src + 2 * sstep, vl);
|
|
auto v3 = __riscv_vle8_v_u8m1(src + 3 * sstep, vl);
|
|
auto v4 = __riscv_vle8_v_u8m1(src + 4 * sstep, vl);
|
|
auto v5 = __riscv_vle8_v_u8m1(src + 5 * sstep, vl);
|
|
auto v6 = __riscv_vle8_v_u8m1(src + 6 * sstep, vl);
|
|
auto v7 = __riscv_vle8_v_u8m1(src + 7 * sstep, vl);
|
|
vuint8m1x8_t v = __riscv_vcreate_v_u8m1x8(v0, v1, v2, v3, v4, v5, v6, v7);
|
|
__riscv_vssseg8e8(dst, dstep, v, vl);
|
|
};
|
|
|
|
int h = 0, w = 0;
|
|
for (; h <= src_height - 8; h += 8) {
|
|
const uchar *src = src_data + h * src_step;
|
|
uchar *dst = dst_data + h;
|
|
int vl;
|
|
for (w = 0; w < src_width; w += vl) {
|
|
vl = __riscv_vsetvl_e8m1(src_width - w);
|
|
transpose_8u_8xVl(src + w, src_step, dst + w * dst_step, dst_step, vl);
|
|
}
|
|
}
|
|
for (; h < src_height; h++) {
|
|
const uchar *src = src_data + h * src_step;
|
|
uchar *dst = dst_data + h;
|
|
int vl;
|
|
for (w = 0; w < src_width; w += vl) {
|
|
vl = __riscv_vsetvl_e8m8(src_width - w);
|
|
auto v = __riscv_vle8_v_u8m8(src + w, vl);
|
|
__riscv_vsse8(dst + w * dst_step, dst_step, v, vl);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void transpose2d_16u(const uchar *src_data, size_t src_step, uchar *dst_data, size_t dst_step, int src_width, int src_height) {
|
|
auto transpose_16u_8xVl = [](const ushort *src, size_t sstep, ushort *dst, size_t dstep, const int vl) {
|
|
auto v0 = __riscv_vle16_v_u16m1(src, vl);
|
|
auto v1 = __riscv_vle16_v_u16m1(src + sstep, vl);
|
|
auto v2 = __riscv_vle16_v_u16m1(src + 2 * sstep, vl);
|
|
auto v3 = __riscv_vle16_v_u16m1(src + 3 * sstep, vl);
|
|
auto v4 = __riscv_vle16_v_u16m1(src + 4 * sstep, vl);
|
|
auto v5 = __riscv_vle16_v_u16m1(src + 5 * sstep, vl);
|
|
auto v6 = __riscv_vle16_v_u16m1(src + 6 * sstep, vl);
|
|
auto v7 = __riscv_vle16_v_u16m1(src + 7 * sstep, vl);
|
|
vuint16m1x8_t v = __riscv_vcreate_v_u16m1x8(v0, v1, v2, v3, v4, v5, v6, v7);
|
|
__riscv_vssseg8e16(dst, dstep, v, vl);
|
|
};
|
|
|
|
size_t src_step_base = src_step / sizeof(ushort);
|
|
size_t dst_step_base = dst_step / sizeof(ushort);
|
|
|
|
int h = 0, w = 0;
|
|
for (; h <= src_height - 8; h += 8) {
|
|
const ushort *src = (const ushort*)(src_data) + h * src_step_base;
|
|
ushort *dst = (ushort*)(dst_data) + h;
|
|
int vl;
|
|
for (w = 0; w < src_width; w += vl) {
|
|
vl = __riscv_vsetvl_e16m1(src_width - w);
|
|
transpose_16u_8xVl(src + w, src_step_base, dst + w * dst_step_base, dst_step, vl);
|
|
}
|
|
}
|
|
for (; h < src_height; h++) {
|
|
const ushort *src = (const ushort*)(src_data) + h * src_step_base;
|
|
ushort *dst = (ushort*)(dst_data) + h;
|
|
int vl;
|
|
for (w = 0; w < src_width; w += vl) {
|
|
vl = __riscv_vsetvl_e16m8(src_width - w);
|
|
auto v = __riscv_vle16_v_u16m8(src + w, vl);
|
|
__riscv_vsse16(dst + w * dst_step_base, dst_step, v, vl);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void transpose2d_32s(const uchar *src_data, size_t src_step, uchar *dst_data, size_t dst_step, int src_width, int src_height) {
|
|
auto transpose_32s_4xVl = [](const int *src, size_t sstep, int *dst, size_t dstep, const int vl) {
|
|
auto v0 = __riscv_vle32_v_i32m1(src, vl);
|
|
auto v1 = __riscv_vle32_v_i32m1(src + sstep, vl);
|
|
auto v2 = __riscv_vle32_v_i32m1(src + 2 * sstep, vl);
|
|
auto v3 = __riscv_vle32_v_i32m1(src + 3 * sstep, vl);
|
|
vint32m1x4_t v = __riscv_vcreate_v_i32m1x4(v0, v1, v2, v3);
|
|
__riscv_vssseg4e32(dst, dstep, v, vl);
|
|
};
|
|
|
|
size_t src_step_base = src_step / sizeof(int);
|
|
size_t dst_step_base = dst_step / sizeof(int);
|
|
|
|
int h = 0, w = 0;
|
|
for (; h <= src_height - 4; h += 4) {
|
|
const int *src = (const int*)(src_data) + h * src_step_base;
|
|
int *dst = (int*)(dst_data) + h;
|
|
int vl;
|
|
for (w = 0; w < src_width; w += vl) {
|
|
vl = __riscv_vsetvl_e32m1(src_width - w);
|
|
transpose_32s_4xVl(src + w, src_step_base, dst + w * dst_step_base, dst_step, vl);
|
|
}
|
|
}
|
|
for (; h < src_height; h++) {
|
|
const int *src = (const int*)(src_data) + h * src_step_base;
|
|
int *dst = (int*)(dst_data) + h;
|
|
int vl;
|
|
for (w = 0; w < src_width; w += vl) {
|
|
vl = __riscv_vsetvl_e32m8(src_width - w);
|
|
auto v = __riscv_vle32_v_i32m8(src + w, vl);
|
|
__riscv_vsse32(dst + w * dst_step_base, dst_step, v, vl);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void transpose2d_32sC2(const uchar *src_data, size_t src_step, uchar *dst_data, size_t dst_step, int src_width, int src_height) {
|
|
auto transpose_64s_8xVl = [](const int64_t *src, size_t sstep, int64_t *dst, size_t dstep, const int vl) {
|
|
auto v0 = __riscv_vle64_v_i64m1(src, vl);
|
|
auto v1 = __riscv_vle64_v_i64m1(src + sstep, vl);
|
|
auto v2 = __riscv_vle64_v_i64m1(src + 2 * sstep, vl);
|
|
auto v3 = __riscv_vle64_v_i64m1(src + 3 * sstep, vl);
|
|
auto v4 = __riscv_vle64_v_i64m1(src + 4 * sstep, vl);
|
|
auto v5 = __riscv_vle64_v_i64m1(src + 5 * sstep, vl);
|
|
auto v6 = __riscv_vle64_v_i64m1(src + 6 * sstep, vl);
|
|
auto v7 = __riscv_vle64_v_i64m1(src + 7 * sstep, vl);
|
|
vint64m1x8_t v = __riscv_vcreate_v_i64m1x8(v0, v1, v2, v3, v4, v5, v6, v7);
|
|
__riscv_vssseg8e64(dst, dstep, v, vl);
|
|
};
|
|
|
|
size_t src_step_base = src_step / sizeof(int64_t);
|
|
size_t dst_step_base = dst_step / sizeof(int64_t);
|
|
|
|
int h = 0, w = 0;
|
|
for (; h <= src_height - 8; h += 8) {
|
|
const int64_t *src = (const int64_t*)(src_data) + h * src_step_base;
|
|
int64_t *dst = (int64_t*)(dst_data) + h;
|
|
int vl;
|
|
for (w = 0; w < src_width; w += vl) {
|
|
vl = __riscv_vsetvl_e64m1(src_width - w);
|
|
transpose_64s_8xVl(src + w, src_step_base, dst + w * dst_step_base, dst_step, vl);
|
|
}
|
|
}
|
|
for (; h < src_height; h++) {
|
|
const int64_t *src = (const int64_t*)(src_data) + h * src_step_base;
|
|
int64_t *dst = (int64_t*)(dst_data) + h;
|
|
int vl;
|
|
for (w = 0; w < src_width; w += vl) {
|
|
vl = __riscv_vsetvl_e64m8(src_width - w);
|
|
auto v = __riscv_vle64_v_i64m8(src + w, vl);
|
|
__riscv_vsse64(dst + w * dst_step_base, dst_step, v, vl);
|
|
}
|
|
}
|
|
}
|
|
|
|
using Transpose2dFunc = void (*)(const uchar*, size_t, uchar*, size_t, int, int);
|
|
int transpose2d(const uchar* src_data, size_t src_step, uchar* dst_data, size_t dst_step,
|
|
int src_width, int src_height, int element_size) {
|
|
if (src_data == dst_data || element_size <= 0 ||
|
|
src_step % element_size != 0 || dst_step % element_size != 0) {
|
|
return CV_HAL_ERROR_NOT_IMPLEMENTED;
|
|
}
|
|
|
|
static Transpose2dFunc tab[] = {
|
|
0, transpose2d_8u, transpose2d_16u, 0,
|
|
transpose2d_32s, 0, 0, 0,
|
|
transpose2d_32sC2, 0, 0, 0,
|
|
0, 0, 0, 0,
|
|
0, 0, 0, 0,
|
|
0, 0, 0, 0,
|
|
0, 0, 0, 0,
|
|
0, 0, 0, 0,
|
|
0
|
|
};
|
|
Transpose2dFunc func = element_size <= 32 ? tab[element_size] : nullptr;
|
|
if (!func) {
|
|
return CV_HAL_ERROR_NOT_IMPLEMENTED;
|
|
}
|
|
|
|
func(src_data, src_step, dst_data, dst_step, src_width, src_height);
|
|
|
|
return CV_HAL_ERROR_OK;
|
|
}
|
|
|
|
#endif // CV_HAL_RVV_1P0_ENABLED
|
|
|
|
}}} // cv::rvv_hal::core
|