Repository: opencv/opencv
File: hal/riscv-rvv/src/features2d/fast.cpp (C++)

Code before:
#include "rvv_hal.hpp"
#include "common.hpp"
#include <cfloat>

namespace cv { namespace rvv_hal { namespace features2d {

using RVV_VECTOR_TYPE = vuint8m4_t;


// Since uint16_t range is 0 to 65535, row stride should be less than 65535/6 = 10922
inline void makeOffsets(int16_t pixel[], vuint16m2_t& v_offset, int64_t row_stride, int patternSize)
{
    uint16_t pixel_u[25];
    memset(pixel_u, 0, sizeof(uint16_t) * 25);

    switch(patternSize) {
    case 16:
        // set min element (pixel[9] = -1 + row_stride * -3) as the base addr
        pixel_u[0] = 1 + row_stride * 6;
        pixel_u[1] = 2 + row_stride * 6;
        pixel_u[2] = 3 + row_stride * 5;
        pixel_u[3] = 4 + row_stride * 4;
        pixel_u[4] = 4 + row_stride * 3;
        pixel_u[5] = 4 + row_stride * 2;
        pixel_u[6] = 3 + row_stride * 1;
        pixel_u[7] = 2 + row_stride * 0;
        pixel_u[8] = 1 + row_stride * 0;
        pixel_u[9] = 0 + row_stride * 0;
        pixel_u[10] = -1 + row_stride * 1;
        pixel_u[11] = -2 + row_stride * 2;
        pixel_u[12] = -2 + row_stride * 3;
        pixel_u[13] = -2 + row_stride * 4;
        pixel_u[14] = -1 + row_stride * 5;
        pixel_u[15] = 0 + row_stride * 6;

        for (int i = 16; i < 25; i++)
        {
            pixel_u[i] = pixel_u[i - 16];
        }

Review comment (suggestion):
Memset is redundant if patternSize == 16. I propose to move to `default` branch.

Code after:
#include "rvv_hal.hpp"
#include "common.hpp"
#include <cfloat>

namespace cv { namespace rvv_hal { namespace features2d {

static inline uint8_t cornerScore(const uint8_t* ptr, const int* pixel)
{
    constexpr int K = 8, N = 16 + K + 1;
    int v = ptr[0];
    int16_t d[32] = {0};
    for (int k = 0; k < N; k++)
        d[k] = (int16_t)(v - ptr[pixel[k]]);
    auto vlenb = __riscv_vlenb();
    switch (vlenb) {
        #define CV_RVV_HAL_FAST_CORNERSOCRE16_CASE(lmul) \
            size_t vl = __riscv_vsetvl_e16m##lmul(N); \
            vint16m##lmul##_t vd = __riscv_vle16_v_i16m##lmul(d, vl); \
            vint16m##lmul##_t q0 = __riscv_vmv_v_x_i16m##lmul((int16_t)(-1000), vl); \
            vint16m##lmul##_t q1 = __riscv_vmv_v_x_i16m##lmul((int16_t)(1000), vl); \
            vint16m##lmul##_t vds = vd, ak0 = vd, bk0 = vd; \
            for (int i = 0; i < 8; i++) { \
                vds = __riscv_vslide1down(vds, 0, vl); \
                ak0 = __riscv_vmin(ak0, vds, vl); \
                bk0 = __riscv_vmax(bk0, vds, vl); \
            } \
            q0 = __riscv_vmax(q0, __riscv_vmin(ak0, vd, vl), vl); \
            q1 = __riscv_vmin(q1, __riscv_vmax(bk0, vd, vl), vl); \
            vds = __riscv_vslide1down(vds, 0, vl); \
            q0 = __riscv_vmax(q0, __riscv_vmin(ak0, vds, vl), vl); \
            q1 = __riscv_vmin(q1, __riscv_vmax(bk0, vds, vl), vl); \
            q0 = __riscv_vmax(q0, __riscv_vrsub(q1, 0, vl), vl); \
            return (uint8_t)(__riscv_vmv_x(__riscv_vredmax(q0, __riscv_vmv_s_x_i16m1(0, vl), vl)) - 1);
        case 16: { // 128-bit
            CV_RVV_HAL_FAST_CORNERSOCRE16_CASE(4)
        } break;
        case 32: { // 256-bit
            CV_RVV_HAL_FAST_CORNERSOCRE16_CASE(2)
        } break;