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:
@@ -614,13 +614,13 @@ void polarToCart( InputArray src1, InputArray src2,
|
||||
{
|
||||
k = 0;
|
||||
|
||||
#if CV_SIMD
|
||||
int cWidth = v_float32::nlanes;
|
||||
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
||||
int cWidth = VTraits<v_float32>::vlanes();
|
||||
for( ; k <= len - cWidth; k += cWidth )
|
||||
{
|
||||
v_float32 v_m = vx_load(mag + k);
|
||||
v_store(x + k, vx_load(x + k) * v_m);
|
||||
v_store(y + k, vx_load(y + k) * v_m);
|
||||
v_store(x + k, v_mul(vx_load(x + k), v_m));
|
||||
v_store(y + k, v_mul(vx_load(y + k), v_m));
|
||||
}
|
||||
vx_cleanup();
|
||||
#endif
|
||||
@@ -741,7 +741,7 @@ struct iPow_SIMD
|
||||
}
|
||||
};
|
||||
|
||||
#if CV_SIMD
|
||||
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
||||
|
||||
template <>
|
||||
struct iPow_SIMD<uchar, int>
|
||||
@@ -751,7 +751,7 @@ struct iPow_SIMD<uchar, int>
|
||||
int i = 0;
|
||||
v_uint32 v_1 = vx_setall_u32(1u);
|
||||
|
||||
for ( ; i <= len - v_uint16::nlanes; i += v_uint16::nlanes)
|
||||
for ( ; i <= len - VTraits<v_uint16>::vlanes(); i += VTraits<v_uint16>::vlanes())
|
||||
{
|
||||
v_uint32 v_a1 = v_1, v_a2 = v_1;
|
||||
v_uint16 v = vx_load_expand(src + i);
|
||||
@@ -763,16 +763,16 @@ struct iPow_SIMD<uchar, int>
|
||||
{
|
||||
if (p & 1)
|
||||
{
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
}
|
||||
v_b1 *= v_b1;
|
||||
v_b2 *= v_b2;
|
||||
v_b1 = v_mul(v_b1, v_b1);
|
||||
v_b2 = v_mul(v_b2, v_b2);
|
||||
p >>= 1;
|
||||
}
|
||||
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
|
||||
v = v_pack(v_a1, v_a2);
|
||||
v_pack_store(dst + i, v);
|
||||
@@ -791,7 +791,7 @@ struct iPow_SIMD<schar, int>
|
||||
int i = 0;
|
||||
v_int32 v_1 = vx_setall_s32(1);
|
||||
|
||||
for ( ; i <= len - v_int16::nlanes; i += v_int16::nlanes)
|
||||
for ( ; i <= len - VTraits<v_int16>::vlanes(); i += VTraits<v_int16>::vlanes())
|
||||
{
|
||||
v_int32 v_a1 = v_1, v_a2 = v_1;
|
||||
v_int16 v = vx_load_expand(src + i);
|
||||
@@ -803,16 +803,16 @@ struct iPow_SIMD<schar, int>
|
||||
{
|
||||
if (p & 1)
|
||||
{
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
}
|
||||
v_b1 *= v_b1;
|
||||
v_b2 *= v_b2;
|
||||
v_b1 = v_mul(v_b1, v_b1);
|
||||
v_b2 = v_mul(v_b2, v_b2);
|
||||
p >>= 1;
|
||||
}
|
||||
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
|
||||
v = v_pack(v_a1, v_a2);
|
||||
v_pack_store(dst + i, v);
|
||||
@@ -831,7 +831,7 @@ struct iPow_SIMD<ushort, int>
|
||||
int i = 0;
|
||||
v_uint32 v_1 = vx_setall_u32(1u);
|
||||
|
||||
for ( ; i <= len - v_uint16::nlanes; i += v_uint16::nlanes)
|
||||
for ( ; i <= len - VTraits<v_uint16>::vlanes(); i += VTraits<v_uint16>::vlanes())
|
||||
{
|
||||
v_uint32 v_a1 = v_1, v_a2 = v_1;
|
||||
v_uint16 v = vx_load(src + i);
|
||||
@@ -843,16 +843,16 @@ struct iPow_SIMD<ushort, int>
|
||||
{
|
||||
if (p & 1)
|
||||
{
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
}
|
||||
v_b1 *= v_b1;
|
||||
v_b2 *= v_b2;
|
||||
v_b1 = v_mul(v_b1, v_b1);
|
||||
v_b2 = v_mul(v_b2, v_b2);
|
||||
p >>= 1;
|
||||
}
|
||||
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
|
||||
v = v_pack(v_a1, v_a2);
|
||||
v_store(dst + i, v);
|
||||
@@ -871,7 +871,7 @@ struct iPow_SIMD<short, int>
|
||||
int i = 0;
|
||||
v_int32 v_1 = vx_setall_s32(1);
|
||||
|
||||
for ( ; i <= len - v_int16::nlanes; i += v_int16::nlanes)
|
||||
for ( ; i <= len - VTraits<v_int16>::vlanes(); i += VTraits<v_int16>::vlanes())
|
||||
{
|
||||
v_int32 v_a1 = v_1, v_a2 = v_1;
|
||||
v_int16 v = vx_load(src + i);
|
||||
@@ -883,16 +883,16 @@ struct iPow_SIMD<short, int>
|
||||
{
|
||||
if (p & 1)
|
||||
{
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
}
|
||||
v_b1 *= v_b1;
|
||||
v_b2 *= v_b2;
|
||||
v_b1 = v_mul(v_b1, v_b1);
|
||||
v_b2 = v_mul(v_b2, v_b2);
|
||||
p >>= 1;
|
||||
}
|
||||
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
|
||||
v = v_pack(v_a1, v_a2);
|
||||
v_store(dst + i, v);
|
||||
@@ -911,29 +911,29 @@ struct iPow_SIMD<int, int>
|
||||
int i = 0;
|
||||
v_int32 v_1 = vx_setall_s32(1);
|
||||
|
||||
for ( ; i <= len - v_int32::nlanes*2; i += v_int32::nlanes*2)
|
||||
for ( ; i <= len - VTraits<v_int32>::vlanes()*2; i += VTraits<v_int32>::vlanes()*2)
|
||||
{
|
||||
v_int32 v_a1 = v_1, v_a2 = v_1;
|
||||
v_int32 v_b1 = vx_load(src + i), v_b2 = vx_load(src + i + v_int32::nlanes);
|
||||
v_int32 v_b1 = vx_load(src + i), v_b2 = vx_load(src + i + VTraits<v_int32>::vlanes());
|
||||
int p = power;
|
||||
|
||||
while( p > 1 )
|
||||
{
|
||||
if (p & 1)
|
||||
{
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
}
|
||||
v_b1 *= v_b1;
|
||||
v_b2 *= v_b2;
|
||||
v_b1 = v_mul(v_b1, v_b1);
|
||||
v_b2 = v_mul(v_b2, v_b2);
|
||||
p >>= 1;
|
||||
}
|
||||
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
|
||||
v_store(dst + i, v_a1);
|
||||
v_store(dst + i + v_int32::nlanes, v_a2);
|
||||
v_store(dst + i + VTraits<v_int32>::vlanes(), v_a2);
|
||||
}
|
||||
vx_cleanup();
|
||||
|
||||
@@ -949,34 +949,34 @@ struct iPow_SIMD<float, float>
|
||||
int i = 0;
|
||||
v_float32 v_1 = vx_setall_f32(1.f);
|
||||
|
||||
for ( ; i <= len - v_float32::nlanes*2; i += v_float32::nlanes*2)
|
||||
for ( ; i <= len - VTraits<v_float32>::vlanes()*2; i += VTraits<v_float32>::vlanes()*2)
|
||||
{
|
||||
v_float32 v_a1 = v_1, v_a2 = v_1;
|
||||
v_float32 v_b1 = vx_load(src + i), v_b2 = vx_load(src + i + v_float32::nlanes);
|
||||
v_float32 v_b1 = vx_load(src + i), v_b2 = vx_load(src + i + VTraits<v_float32>::vlanes());
|
||||
int p = std::abs(power);
|
||||
if( power < 0 )
|
||||
{
|
||||
v_b1 = v_1 / v_b1;
|
||||
v_b2 = v_1 / v_b2;
|
||||
v_b1 = v_div(v_1, v_b1);
|
||||
v_b2 = v_div(v_1, v_b2);
|
||||
}
|
||||
|
||||
while( p > 1 )
|
||||
{
|
||||
if (p & 1)
|
||||
{
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
}
|
||||
v_b1 *= v_b1;
|
||||
v_b2 *= v_b2;
|
||||
v_b1 = v_mul(v_b1, v_b1);
|
||||
v_b2 = v_mul(v_b2, v_b2);
|
||||
p >>= 1;
|
||||
}
|
||||
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
|
||||
v_store(dst + i, v_a1);
|
||||
v_store(dst + i + v_float32::nlanes, v_a2);
|
||||
v_store(dst + i + VTraits<v_float32>::vlanes(), v_a2);
|
||||
}
|
||||
vx_cleanup();
|
||||
|
||||
@@ -984,7 +984,7 @@ struct iPow_SIMD<float, float>
|
||||
}
|
||||
};
|
||||
|
||||
#if CV_SIMD_64F
|
||||
#if (CV_SIMD_64F || CV_SIMD_SCALABLE_64F)
|
||||
template <>
|
||||
struct iPow_SIMD<double, double>
|
||||
{
|
||||
@@ -993,34 +993,34 @@ struct iPow_SIMD<double, double>
|
||||
int i = 0;
|
||||
v_float64 v_1 = vx_setall_f64(1.);
|
||||
|
||||
for ( ; i <= len - v_float64::nlanes*2; i += v_float64::nlanes*2)
|
||||
for ( ; i <= len - VTraits<v_float64>::vlanes()*2; i += VTraits<v_float64>::vlanes()*2)
|
||||
{
|
||||
v_float64 v_a1 = v_1, v_a2 = v_1;
|
||||
v_float64 v_b1 = vx_load(src + i), v_b2 = vx_load(src + i + v_float64::nlanes);
|
||||
v_float64 v_b1 = vx_load(src + i), v_b2 = vx_load(src + i + VTraits<v_float64>::vlanes());
|
||||
int p = std::abs(power);
|
||||
if( power < 0 )
|
||||
{
|
||||
v_b1 = v_1 / v_b1;
|
||||
v_b2 = v_1 / v_b2;
|
||||
v_b1 = v_div(v_1, v_b1);
|
||||
v_b2 = v_div(v_1, v_b2);
|
||||
}
|
||||
|
||||
while( p > 1 )
|
||||
{
|
||||
if (p & 1)
|
||||
{
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
}
|
||||
v_b1 *= v_b1;
|
||||
v_b2 *= v_b2;
|
||||
v_b1 = v_mul(v_b1, v_b1);
|
||||
v_b2 = v_mul(v_b2, v_b2);
|
||||
p >>= 1;
|
||||
}
|
||||
|
||||
v_a1 *= v_b1;
|
||||
v_a2 *= v_b2;
|
||||
v_a1 = v_mul(v_a1, v_b1);
|
||||
v_a2 = v_mul(v_a2, v_b2);
|
||||
|
||||
v_store(dst + i, v_a1);
|
||||
v_store(dst + i + v_float64::nlanes, v_a2);
|
||||
v_store(dst + i + VTraits<v_float64>::vlanes(), v_a2);
|
||||
}
|
||||
vx_cleanup();
|
||||
|
||||
@@ -1614,7 +1614,7 @@ void patchNaNs( InputOutputArray _a, double _val )
|
||||
Cv32suf val;
|
||||
val.f = (float)_val;
|
||||
|
||||
#if CV_SIMD
|
||||
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
||||
v_int32 v_mask1 = vx_setall_s32(0x7fffffff), v_mask2 = vx_setall_s32(0x7f800000);
|
||||
v_int32 v_val = vx_setall_s32(val.i);
|
||||
#endif
|
||||
@@ -1624,12 +1624,12 @@ void patchNaNs( InputOutputArray _a, double _val )
|
||||
int* tptr = ptrs[0];
|
||||
size_t j = 0;
|
||||
|
||||
#if CV_SIMD
|
||||
size_t cWidth = (size_t)v_int32::nlanes;
|
||||
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
||||
size_t cWidth = (size_t)VTraits<v_int32>::vlanes();
|
||||
for ( ; j + cWidth <= len; j += cWidth)
|
||||
{
|
||||
v_int32 v_src = vx_load(tptr + j);
|
||||
v_int32 v_cmp_mask = v_mask2 < (v_src & v_mask1);
|
||||
v_int32 v_cmp_mask = v_lt(v_mask2, v_and(v_src, v_mask1));
|
||||
v_int32 v_dst = v_select(v_cmp_mask, v_val, v_src);
|
||||
v_store(tptr + j, v_dst);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user