
This article was written by Yuantao Feng of the OpenCV China Team.
What is RISC-V and RVV 1.0?
RISC-V (pronounced “risk-five”) is an open standard instruction set architecture (ISA) based on the principles of reduced instruction set computing (RISC). Unlike proprietary ISAs such as Intel’s x86 or ARM’s architecture, RISC-V is free to use and modify, enabling companies and researchers to design custom processors without licensing fees or restrictions. This openness has led to growing adoption across academia, startups, and major industry players, such as Xiangshan from Chinese Academy of Sciences, KeyStone from SpacemiT and XuanTie from T-head.
One of the most important standard extensions for high-performance computing on RISC-V is the RISC-V Vector Extension (RVV). Version 1.0 of RVV has been ratified back in 2023 and it defines a scalable vector architecture designed to support data-level parallelism, similar in spirit to Intel’s AVX or ARM’s NEON. However, RVV is length-agnostic, meaning the same code can run on vector units of varying widths—from 128 bits to 1024 bits or more—without recompilation. SpacemiT Key Stone K1 was one of the first RISC-V CPUs with the support of RVV 1.0 (256-bit).
OpenCV has been supporting length-agnostic RVV since 2022 via OpenCV’s Universal Intrinsics. The Universal Intrinsics is an abstraction set of basic operations (load, store, add, sub, …) intended to simplify vectorization of code across different platforms with a compromise of ultimate performance gain.
HAL in OpenCV and HAL riscv-rvv
HAL (Hardware Abstraction Layer) in OpenCV has been through a long-time development. It starts with Intel IPP, an image processing library designed for x86-64 platform with consideration of certain performance-critical and hardware-dependent functions. Then Carotene for ARM was added to squeeze more performance on ARM CPUs. To unleash the full power of RISC-V CPUs with RVV 1.0, we now introduce HAL riscv-rvv since OpenCV 4.12.
With the help from SpacemiT, Chinese Academy of Sciences and OpenCV China, we were able to release HAL riscv-rvv with basically all available HAL replacement functions implemented in module core and imgproc, covering the following 119 widely and frequently used functions:
- Basic math: div, exp, log, sqrt, norm, meanStdDev …
- Transformation: transpose, flip, split, merge …
- Image processing: cvtColor, filters, median blur, resize, warpAffine, warpPerspective, remap …
Check here for a detailed list. More functions are yet to come.
Performance gain of HAL riscv-rvv
With OpenCV’s own performance testings, we can see how much riscv-rvv acceleration reaches. Tests were done on SpacemiT MUSE Pi with SpacemiT KeyStone K1, a 8-core RISC-V CPU supporting 256-bit RVV 1.0. The code was built with SpacemiT’s toolchain 1.0.5. HAL riscv-rvv can be built by default with multithreading support, but to showcase the potential capabilities of a core with RVV 1.0 support, the below benchmarks show the single-core performance uplift of HAL riscv-rvv at an image size of 1920*1080.


The benchmark scores can be various between each operation and compiler. A smaller performance gain from a compiler can due to its already-well-optimized base. A bigger score truly showcases the full utilization of RVV 1.0 can lead to dramatic performance improvement. The mean uplift across different operations exceeds 200%.
Get and Test It!
Currently OpenCV does not have official releases of binary packages for RISC-V platforms. For users who already own a SpacemiT product, they can cross compile from source on a x86-64 host with CMake and toolchains to enable HAL riscv-rvv:
Cross compilation with GCC
bash
# Get latest OpenCV
git clone https://github.com/opencv/opencv
# Set path to toolchain directory and opencv’s toolchain files for cross compilation
TOOLCHAIN_DIR=/path/to/spacemit-toolchain-linux-glibc-x86_64-v1.0.5
TOOLCHAIN_FILE_GCC=$(pwd)/opencv/platforms/linux/riscv64-gcc.toolchain.cmake
# GCC
BUILD_DIR=build-gcc
cmake -G Ninja -B ${BUILD_DIR} \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR}/install \
-DCMAKE_C_COMPILER=${TOOLCHAIN_DIR}/bin/riscv64-unknown-linux-gnu-gcc \
-DCMAKE_CXX_COMPILER=${TOOLCHAIN_DIR}/bin/riscv64-unknown-linux-gnu-g++ \
-DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE_GCC} \
-DCPU_BASELINE=RVV -DCPU_BASELINE_REQUIRE=RVV \
-DRISCV_RVV_SCALABLE=ON opencv
cmake --build build-gcc --target install -j10
Cross compilation with Clang
bash
# Get latest OpenCV
git clone https://github.com/opencv/opencv
# Set path to toolchain directory and opencv’s toolchain files for cross compilation
TOOLCHAIN_DIR=/path/to/spacemit-toolchain-linux-glibc-x86_64-v1.0.5
TOOLCHAIN_FILE_CLANG=$(pwd)//opencv/platforms/linux/riscv64-clang.toolchain.cmake
# Clang
BUILD_DIR=build-clang
cmake -G Ninja -B ${BUILD_DIR} \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR}/install \
-DRISCV_CLANG_BUILD_ROOT=${TOOLCHAIN_DIR} \
-DRISCV_GCC_INSTALL_ROOT=${TOOLCHAIN_DIR} \
-DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE_CLANG} \
-DCPU_BASELINE=RVV -DCPU_BASELINE_REQUIRE=RVV \
-DRISCV_RVV_SCALABLE=ON opencv
cmake --build build-gcc --target install -j10
cmake --build build-clang --target install -j10
Don’t hesitate to file issues If you encounter problems building or using OpenCV with HAL risc-rvv. We welcome your contributions to make HAL riscv-rvv better!
—
Yuantao Feng of the OpenCV China Team
5K+ Learners
Join Free VLM Bootcamp3 Hours of Learning