Files
opencv-MIRROR/modules/core/src/stat.simd.hpp
Madan mohan Manokar 579a505734 Merge pull request #29335 from amd:fast_norm_simd
core: SIMD optimizations for norm, distance and Hamming APIs. (Improves ORB & BRISK) #29335

Generic universal-intrinsic kernels (benefit all SIMD backends: NEON, AVX2, AVX-512, etc.), plus enabling wider dispatch for the norm module.

- hal::normHamming: cached-pointer dispatch (resolve once, no per-call dispatch chain or trace region) + vector popcount path. cv::norm(NORM_HAMMING) and binary-descriptor matching (BFMatcher ORB/BRISK/FREAK via cv::batchDistance).
- hal::normL2Sqr_ / normL1_: direct inlinable kernels with single-vector tail (cv::batchDistance / BFMatcher float L2/L1, cv::kmeans).
- cv::norm masked NORM_INF: deinterleave SIMD for multichannel + back-step tail.
- cv::norm(src1, src2, type, mask): SIMD masked norm-diff; INF is one templated kernel for all element types, plus uchar L1/L2 and int L1 kernels.
- Enable AVX512_SKX/AVX512_ICL dispatch for the norm module.
- features2d: add BFMatcher knnMatch perf tests (float L2/L1, binary Hamming).
- ORB and BRISK performance improved
### 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
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2026-07-01 15:48:47 +03:00

151 lines
3.8 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) 2026, Advanced Micro Devices, Inc., all rights reserved.
#include "opencv2/core/hal/intrin.hpp"
namespace cv { namespace hal {
extern const uchar popCountTable[256];
typedef int (*NormHammingFunc)(const uchar*, int);
typedef int (*NormHammingDiffFunc)(const uchar*, const uchar*, int);
CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN
// forward declarations
int normHamming(const uchar* a, int n);
int normHamming(const uchar* a, const uchar* b, int n);
NormHammingFunc getNormHammingFunc();
NormHammingDiffFunc getNormHammingDiffFunc();
#ifndef CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
#if CV_AVX2
static inline int _mm256_extract_epi32_(__m256i reg, const int i)
{
CV_DECL_ALIGNED(32) int reg_data[8];
CV_DbgAssert(0 <= i && i < 8);
_mm256_store_si256((__m256i*)reg_data, reg);
return reg_data[i];
}
#endif
int normHamming(const uchar* a, int n)
{
CV_AVX_GUARD;
int i = 0;
int result = 0;
#if (CV_SIMD || CV_SIMD_SCALABLE)
{
v_uint64 t = vx_setzero_u64();
for (; i <= n - VTraits<v_uint8>::vlanes(); i += VTraits<v_uint8>::vlanes())
t = v_add(t, v_popcount(v_reinterpret_as_u64(vx_load(a + i))));
result = (int)v_reduce_sum(t);
vx_cleanup();
}
#endif
#if CV_POPCNT
{
# if defined CV_POPCNT_U64
for(; i <= n - 8; i += 8)
{
uint64_t val;
std::memcpy(&val, a + i, sizeof(val));
result += (int)CV_POPCNT_U64(val);
}
# endif
for(; i <= n - 4; i += 4)
{
uint32_t val;
std::memcpy(&val, a + i, sizeof(val));
result += CV_POPCNT_U32(val);
}
}
#endif
#if CV_ENABLE_UNROLLED
for(; i <= n - 4; i += 4)
{
result += popCountTable[a[i]] + popCountTable[a[i+1]] +
popCountTable[a[i+2]] + popCountTable[a[i+3]];
}
#endif
for(; i < n; i++)
{
result += popCountTable[a[i]];
}
return result;
}
int normHamming(const uchar* a, const uchar* b, int n)
{
CV_AVX_GUARD;
int i = 0;
int result = 0;
#if (CV_SIMD || CV_SIMD_SCALABLE)
{
v_uint64 t = vx_setzero_u64();
for (; i <= n - VTraits<v_uint8>::vlanes(); i += VTraits<v_uint8>::vlanes())
t = v_add(t, v_popcount(v_reinterpret_as_u64(v_xor(vx_load(a + i), vx_load(b + i)))));
result += (int)v_reduce_sum(t);
}
#endif
#if CV_POPCNT
{
# if defined CV_POPCNT_U64
for(; i <= n - 8; i += 8)
{
uint64_t val_a, val_b;
std::memcpy(&val_a, a + i, sizeof(val_a));
std::memcpy(&val_b, b + i, sizeof(val_b));
result += (int)CV_POPCNT_U64(val_a ^ val_b);
}
# endif
for(; i <= n - 4; i += 4)
{
uint32_t val_a, val_b;
std::memcpy(&val_a, a + i, sizeof(val_a));
std::memcpy(&val_b, b + i, sizeof(val_b));
result += (int)CV_POPCNT_U32(val_a ^ val_b);
}
}
#endif
#if CV_ENABLE_UNROLLED
for(; i <= n - 4; i += 4)
{
result += popCountTable[a[i] ^ b[i]] + popCountTable[a[i+1] ^ b[i+1]] +
popCountTable[a[i+2] ^ b[i+2]] + popCountTable[a[i+3] ^ b[i+3]];
}
#endif
for(; i < n; i++)
{
result += popCountTable[a[i] ^ b[i]];
}
return result;
}
NormHammingFunc getNormHammingFunc()
{
NormHammingFunc f = &normHamming; // disambiguate the (a,n) overload
return f;
}
NormHammingDiffFunc getNormHammingDiffFunc()
{
NormHammingDiffFunc f = &normHamming; // disambiguate the (a,b,n) overload
return f;
}
#endif // CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
CV_CPU_OPTIMIZATION_NAMESPACE_END
}} //cv::hal