mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 15:23:05 +04:00
Merge pull request #23980 from hanliutong:rewrite-core
Rewrite Universal Intrinsic code by using new API: Core module. #23980 The goal of this PR is to match and modify all SIMD code blocks guarded by `CV_SIMD` macro in the `opencv/modules/core` folder and rewrite them by using the new Universal Intrinsic API. The patch is almost auto-generated by using the [rewriter](https://github.com/hanliutong/rewriter), related PR #23885. Most of the files have been rewritten, but I marked this PR as draft because, the `CV_SIMD` macro also exists in the following files, and the reasons why they are not rewrited are: 1. ~~code design for fixed-size SIMD (v_int16x8, v_float32x4, etc.), need to manually rewrite.~~ Rewrited - ./modules/core/src/stat.simd.hpp - ./modules/core/src/matrix_transform.cpp - ./modules/core/src/matmul.simd.hpp 2. Vector types are wrapped in other class/struct, that are not supported by the compiler in variable-length backends. Can not be rewrited directly. - ./modules/core/src/mathfuncs_core.simd.hpp ```cpp struct v_atan_f32 { explicit v_atan_f32(const float& scale) { ... } v_float32 compute(const v_float32& y, const v_float32& x) { ... } ... v_float32 val90; // sizeless type can not used in a class v_float32 val180; v_float32 val360; v_float32 s; }; ``` 3. The API interface does not support/does not match - ./modules/core/src/norm.cpp Use `v_popcount`, ~~waiting for #23966~~ Fixed - ./modules/core/src/has_non_zero.simd.hpp Use illegal Universal Intrinsic API: For float type, there is no logical operation `|`. Further discussion needed ```cpp /** @brief Bitwise OR Only for integer types. */ template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator|(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b); template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n>& operator|=(v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b); ``` ```cpp #if CV_SIMD typedef v_float32 v_type; const v_type v_zero = vx_setzero_f32(); constexpr const int unrollCount = 8; int step = v_type::nlanes * unrollCount; int len0 = len & -step; const float* srcSimdEnd = src+len0; int countSIMD = static_cast<int>((srcSimdEnd-src)/step); while(!res && countSIMD--) { v_type v0 = vx_load(src); src += v_type::nlanes; v_type v1 = vx_load(src); src += v_type::nlanes; .... src += v_type::nlanes; v0 |= v1; //Illegal ? .... //res = v_check_any(((v0 | v4) != v_zero));//beware : (NaN != 0) returns "false" since != is mapped to _CMP_NEQ_OQ and not _CMP_NEQ_UQ res = !v_check_all(((v0 | v4) == v_zero)); } v_cleanup(); #endif ``` ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [ ] I agree to contribute to the project under Apache 2 License. - [ ] 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 - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
+19
-69
@@ -274,22 +274,21 @@ template<typename T> struct VBLAS
|
||||
{
|
||||
int dot(const T*, const T*, int, T*) const { return 0; }
|
||||
int givens(T*, T*, int, T, T) const { return 0; }
|
||||
int givensx(T*, T*, int, T, T, T*, T*) const { return 0; }
|
||||
};
|
||||
|
||||
#if CV_SIMD
|
||||
#if CV_SIMD // TODO: enable for CV_SIMD_SCALABLE_64F
|
||||
template<> inline int VBLAS<float>::dot(const float* a, const float* b, int n, float* result) const
|
||||
{
|
||||
if( n < 2*v_float32::nlanes )
|
||||
if( n < 2*VTraits<v_float32>::vlanes() )
|
||||
return 0;
|
||||
int k = 0;
|
||||
v_float32 s0 = vx_setzero_f32();
|
||||
for( ; k <= n - v_float32::nlanes; k += v_float32::nlanes )
|
||||
for( ; k <= n - VTraits<v_float32>::vlanes(); k += VTraits<v_float32>::vlanes() )
|
||||
{
|
||||
v_float32 a0 = vx_load(a + k);
|
||||
v_float32 b0 = vx_load(b + k);
|
||||
|
||||
s0 += a0 * b0;
|
||||
s0 = v_add(s0, v_mul(a0, b0));
|
||||
}
|
||||
*result = v_reduce_sum(s0);
|
||||
vx_cleanup();
|
||||
@@ -299,16 +298,16 @@ template<> inline int VBLAS<float>::dot(const float* a, const float* b, int n, f
|
||||
|
||||
template<> inline int VBLAS<float>::givens(float* a, float* b, int n, float c, float s) const
|
||||
{
|
||||
if( n < v_float32::nlanes)
|
||||
if( n < VTraits<v_float32>::vlanes())
|
||||
return 0;
|
||||
int k = 0;
|
||||
v_float32 c4 = vx_setall_f32(c), s4 = vx_setall_f32(s);
|
||||
for( ; k <= n - v_float32::nlanes; k += v_float32::nlanes )
|
||||
for( ; k <= n - VTraits<v_float32>::vlanes(); k += VTraits<v_float32>::vlanes() )
|
||||
{
|
||||
v_float32 a0 = vx_load(a + k);
|
||||
v_float32 b0 = vx_load(b + k);
|
||||
v_float32 t0 = (a0 * c4) + (b0 * s4);
|
||||
v_float32 t1 = (b0 * c4) - (a0 * s4);
|
||||
v_float32 t0 = v_add(v_mul(a0, c4), v_mul(b0, s4));
|
||||
v_float32 t1 = v_sub(v_mul(b0, c4), v_mul(a0, s4));
|
||||
v_store(a + k, t0);
|
||||
v_store(b + k, t1);
|
||||
}
|
||||
@@ -317,44 +316,19 @@ template<> inline int VBLAS<float>::givens(float* a, float* b, int n, float c, f
|
||||
}
|
||||
|
||||
|
||||
template<> inline int VBLAS<float>::givensx(float* a, float* b, int n, float c, float s,
|
||||
float* anorm, float* bnorm) const
|
||||
{
|
||||
if( n < v_float32::nlanes)
|
||||
return 0;
|
||||
int k = 0;
|
||||
v_float32 c4 = vx_setall_f32(c), s4 = vx_setall_f32(s);
|
||||
v_float32 sa = vx_setzero_f32(), sb = vx_setzero_f32();
|
||||
for( ; k <= n - v_float32::nlanes; k += v_float32::nlanes )
|
||||
{
|
||||
v_float32 a0 = vx_load(a + k);
|
||||
v_float32 b0 = vx_load(b + k);
|
||||
v_float32 t0 = (a0 * c4) + (b0 * s4);
|
||||
v_float32 t1 = (b0 * c4) - (a0 * s4);
|
||||
v_store(a + k, t0);
|
||||
v_store(b + k, t1);
|
||||
sa += t0 + t0;
|
||||
sb += t1 + t1;
|
||||
}
|
||||
*anorm = v_reduce_sum(sa);
|
||||
*bnorm = v_reduce_sum(sb);
|
||||
vx_cleanup();
|
||||
return k;
|
||||
}
|
||||
|
||||
#if CV_SIMD_64F
|
||||
#if (CV_SIMD_64F || CV_SIMD_SCALABLE_64F)
|
||||
template<> inline int VBLAS<double>::dot(const double* a, const double* b, int n, double* result) const
|
||||
{
|
||||
if( n < 2*v_float64::nlanes )
|
||||
if( n < 2*VTraits<v_float64>::vlanes() )
|
||||
return 0;
|
||||
int k = 0;
|
||||
v_float64 s0 = vx_setzero_f64();
|
||||
for( ; k <= n - v_float64::nlanes; k += v_float64::nlanes )
|
||||
for( ; k <= n - VTraits<v_float64>::vlanes(); k += VTraits<v_float64>::vlanes() )
|
||||
{
|
||||
v_float64 a0 = vx_load(a + k);
|
||||
v_float64 b0 = vx_load(b + k);
|
||||
|
||||
s0 += a0 * b0;
|
||||
s0 = v_add(s0, v_mul(a0, b0));
|
||||
}
|
||||
double sbuf[2];
|
||||
v_store(sbuf, s0);
|
||||
@@ -368,12 +342,12 @@ template<> inline int VBLAS<double>::givens(double* a, double* b, int n, double
|
||||
{
|
||||
int k = 0;
|
||||
v_float64 c2 = vx_setall_f64(c), s2 = vx_setall_f64(s);
|
||||
for( ; k <= n - v_float64::nlanes; k += v_float64::nlanes )
|
||||
for( ; k <= n - VTraits<v_float64>::vlanes(); k += VTraits<v_float64>::vlanes() )
|
||||
{
|
||||
v_float64 a0 = vx_load(a + k);
|
||||
v_float64 b0 = vx_load(b + k);
|
||||
v_float64 t0 = (a0 * c2) + (b0 * s2);
|
||||
v_float64 t1 = (b0 * c2) - (a0 * s2);
|
||||
v_float64 t0 = v_add(v_mul(a0, c2), v_mul(b0, s2));
|
||||
v_float64 t1 = v_sub(v_mul(b0, c2), v_mul(a0, s2));
|
||||
v_store(a + k, t0);
|
||||
v_store(b + k, t1);
|
||||
}
|
||||
@@ -382,30 +356,6 @@ template<> inline int VBLAS<double>::givens(double* a, double* b, int n, double
|
||||
}
|
||||
|
||||
|
||||
template<> inline int VBLAS<double>::givensx(double* a, double* b, int n, double c, double s,
|
||||
double* anorm, double* bnorm) const
|
||||
{
|
||||
int k = 0;
|
||||
v_float64 c2 = vx_setall_f64(c), s2 = vx_setall_f64(s);
|
||||
v_float64 sa = vx_setzero_f64(), sb = vx_setzero_f64();
|
||||
for( ; k <= n - v_float64::nlanes; k += v_float64::nlanes )
|
||||
{
|
||||
v_float64 a0 = vx_load(a + k);
|
||||
v_float64 b0 = vx_load(b + k);
|
||||
v_float64 t0 = (a0 * c2) + (b0 * s2);
|
||||
v_float64 t1 = (b0 * c2) - (a0 * s2);
|
||||
v_store(a + k, t0);
|
||||
v_store(b + k, t1);
|
||||
sa += t0 * t0;
|
||||
sb += t1 * t1;
|
||||
}
|
||||
double abuf[2], bbuf[2];
|
||||
v_store(abuf, sa);
|
||||
v_store(bbuf, sb);
|
||||
*anorm = abuf[0] + abuf[1];
|
||||
*bnorm = bbuf[0] + bbuf[1];
|
||||
return k;
|
||||
}
|
||||
#endif //CV_SIMD_64F
|
||||
#endif //CV_SIMD
|
||||
|
||||
@@ -916,7 +866,7 @@ double invert( InputArray _src, OutputArray _dst, int method )
|
||||
#if CV_SIMD128
|
||||
const float d_32f = (float)d;
|
||||
const v_float32x4 d_vec(d_32f, -d_32f, -d_32f, d_32f);
|
||||
v_float32x4 s0 = v_load_halves((const float*)srcdata, (const float*)(srcdata + srcstep)) * d_vec;//0123//3120
|
||||
v_float32x4 s0 = v_mul(v_load_halves((const float *)srcdata, (const float *)(srcdata + srcstep)), d_vec);//0123//3120
|
||||
s0 = v_extract<3>(s0, v_combine_low(v_rotate_right<1>(s0), s0));
|
||||
v_store_low((float*)dstdata, s0);
|
||||
v_store_high((float*)(dstdata + dststep), s0);
|
||||
@@ -942,10 +892,10 @@ double invert( InputArray _src, OutputArray _dst, int method )
|
||||
d = 1./d;
|
||||
#if CV_SIMD128_64F
|
||||
v_float64x2 det = v_setall_f64(d);
|
||||
v_float64x2 s0 = v_load((const double*)srcdata) * det;
|
||||
v_float64x2 s1 = v_load((const double*)(srcdata+srcstep)) * det;
|
||||
v_float64x2 s0 = v_mul(v_load((const double *)srcdata), det);
|
||||
v_float64x2 s1 = v_mul(v_load((const double *)(srcdata + srcstep)), det);
|
||||
v_float64x2 sm = v_extract<1>(s1, s0);//30
|
||||
v_float64x2 ss = v_setall<double>(0) - v_extract<1>(s0, s1);//12
|
||||
v_float64x2 ss = v_sub(v_setall<double>(0), v_extract<1>(s0, s1));//12
|
||||
v_store((double*)dstdata, v_combine_low(sm, ss));//31
|
||||
v_store((double*)(dstdata + dststep), v_combine_high(ss, sm));//20
|
||||
#else
|
||||
|
||||
Reference in New Issue
Block a user