Skip to content

Commit c8080aa

Browse files
authored
Merge pull request opencv#26109 from WanliZhong:univ_intrin_operator2warpper
Replace operators with wrapper functions on universal intrinsics backends opencv#26109 This PR aims to replace the operators(logic, arithmetic, bit) with wrapper functions(v_add, v_eq, v_and...) ### 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 - [ ] 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
1 parent 4c81e17 commit c8080aa

File tree

14 files changed

+962
-1379
lines changed

14 files changed

+962
-1379
lines changed

modules/core/include/opencv2/core/hal/intrin.hpp

Lines changed: 55 additions & 332 deletions
Large diffs are not rendered by default.

modules/core/include/opencv2/core/hal/intrin_avx.hpp

Lines changed: 130 additions & 140 deletions
Large diffs are not rendered by default.

modules/core/include/opencv2/core/hal/intrin_avx512.hpp

Lines changed: 119 additions & 130 deletions
Large diffs are not rendered by default.

modules/core/include/opencv2/core/hal/intrin_cpp.hpp

Lines changed: 57 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -225,32 +225,32 @@ These operations allow to reorder or recombine elements in one or multiple vecto
225225
Element-wise binary and unary operations.
226226
227227
- Arithmetics:
228-
@ref operator +(const v_reg &a, const v_reg &b) "+",
229-
@ref operator -(const v_reg &a, const v_reg &b) "-",
230-
@ref operator *(const v_reg &a, const v_reg &b) "*",
231-
@ref operator /(const v_reg &a, const v_reg &b) "/",
228+
@ref v_add(const v_reg &a, const v_reg &b) "+",
229+
@ref v_sub(const v_reg &a, const v_reg &b) "-",
230+
@ref v_mul(const v_reg &a, const v_reg &b) "*",
231+
@ref v_div(const v_reg &a, const v_reg &b) "/",
232232
@ref v_mul_expand
233233
234234
- Non-saturating arithmetics: @ref v_add_wrap, @ref v_sub_wrap
235235
236236
- Bitwise shifts:
237-
@ref operator <<(const v_reg &a, int s) "<<",
238-
@ref operator >>(const v_reg &a, int s) ">>",
237+
@ref v_shl(const v_reg &a, int s) "<<",
238+
@ref v_shr(const v_reg &a, int s) ">>",
239239
@ref v_shl, @ref v_shr
240240
241241
- Bitwise logic:
242-
@ref operator &(const v_reg &a, const v_reg &b) "&",
243-
@ref operator |(const v_reg &a, const v_reg &b) "|",
244-
@ref operator ^(const v_reg &a, const v_reg &b) "^",
245-
@ref operator ~(const v_reg &a) "~"
242+
@ref v_and(const v_reg &a, const v_reg &b) "&",
243+
@ref v_or(const v_reg &a, const v_reg &b) "|",
244+
@ref v_xor(const v_reg &a, const v_reg &b) "^",
245+
@ref v_not(const v_reg &a) "~"
246246
247247
- Comparison:
248-
@ref operator >(const v_reg &a, const v_reg &b) ">",
249-
@ref operator >=(const v_reg &a, const v_reg &b) ">=",
250-
@ref operator <(const v_reg &a, const v_reg &b) "<",
251-
@ref operator <=(const v_reg &a, const v_reg &b) "<=",
252-
@ref operator ==(const v_reg &a, const v_reg &b) "==",
253-
@ref operator !=(const v_reg &a, const v_reg &b) "!="
248+
@ref v_gt(const v_reg &a, const v_reg &b) ">",
249+
@ref v_ge(const v_reg &a, const v_reg &b) ">=",
250+
@ref v_lt(const v_reg &a, const v_reg &b) "<",
251+
@ref v_le(const v_reg &a, const v_reg &b) "<=",
252+
@ref v_eq(const v_reg &a, const v_reg &b) "==",
253+
@ref v_ne(const v_reg &a, const v_reg &b) "!="
254254
255255
- min/max: @ref v_min, @ref v_max
256256
@@ -573,50 +573,43 @@ enum {
573573
/** @brief Add values
574574
575575
For all types. */
576-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator+(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
577-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n>& operator+=(v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
576+
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> v_add(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
578577

579578
/** @brief Subtract values
580579
581580
For all types. */
582-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator-(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
583-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n>& operator-=(v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
581+
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> v_sub(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
584582

585583
/** @brief Multiply values
586584
587585
For 16- and 32-bit integer types and floating types. */
588-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator*(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
589-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n>& operator*=(v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
586+
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> v_mul(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
590587

591588
/** @brief Divide values
592589
593590
For floating types only. */
594-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator/(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
595-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n>& operator/=(v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
591+
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> v_div(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
596592

597593

598594
/** @brief Bitwise AND
599595
600596
Only for integer types. */
601-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator&(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
602-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n>& operator&=(v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
597+
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> v_and(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
603598

604599
/** @brief Bitwise OR
605600
606601
Only for integer types. */
607-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator|(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
608-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n>& operator|=(v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
602+
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> v_or(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
609603

610604
/** @brief Bitwise XOR
611605
612606
Only for integer types.*/
613-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator^(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
614-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n>& operator^=(v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
607+
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> v_xor(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b);
615608

616609
/** @brief Bitwise NOT
617610
618611
Only for integer types.*/
619-
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> operator~(const v_reg<_Tp, n>& a);
612+
template<typename _Tp, int n> CV_INLINE v_reg<_Tp, n> v_not(const v_reg<_Tp, n>& a);
620613

621614

622615
#ifndef CV_DOXYGEN
@@ -639,71 +632,55 @@ __CV_EXPAND(macro_name(double, __VA_ARGS__)) \
639632
CV__HAL_INTRIN_EXPAND_WITH_INTEGER_TYPES(macro_name, __VA_ARGS__) \
640633
CV__HAL_INTRIN_EXPAND_WITH_FP_TYPES(macro_name, __VA_ARGS__) \
641634

642-
#define CV__HAL_INTRIN_IMPL_BIN_OP_(_Tp, bin_op) \
635+
#define CV__HAL_INTRIN_IMPL_BIN_OP_(_Tp, bin_op, func) \
643636
template<int n> inline \
644-
v_reg<_Tp, n> operator bin_op (const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \
637+
v_reg<_Tp, n> func(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \
645638
{ \
646639
v_reg<_Tp, n> c; \
647640
for( int i = 0; i < n; i++ ) \
648641
c.s[i] = saturate_cast<_Tp>(a.s[i] bin_op b.s[i]); \
649642
return c; \
650-
} \
651-
template<int n> inline \
652-
v_reg<_Tp, n>& operator bin_op##= (v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \
653-
{ \
654-
for( int i = 0; i < n; i++ ) \
655-
a.s[i] = saturate_cast<_Tp>(a.s[i] bin_op b.s[i]); \
656-
return a; \
657643
}
658644

659-
#define CV__HAL_INTRIN_IMPL_BIN_OP(bin_op) CV__HAL_INTRIN_EXPAND_WITH_ALL_TYPES(CV__HAL_INTRIN_IMPL_BIN_OP_, bin_op)
645+
#define CV__HAL_INTRIN_IMPL_BIN_OP(bin_op, func) CV__HAL_INTRIN_EXPAND_WITH_ALL_TYPES(CV__HAL_INTRIN_IMPL_BIN_OP_, bin_op, func)
660646

661-
CV__HAL_INTRIN_IMPL_BIN_OP(+)
662-
CV__HAL_INTRIN_IMPL_BIN_OP(-)
663-
CV__HAL_INTRIN_IMPL_BIN_OP(*)
664-
CV__HAL_INTRIN_EXPAND_WITH_FP_TYPES(CV__HAL_INTRIN_IMPL_BIN_OP_, /)
647+
CV__HAL_INTRIN_IMPL_BIN_OP(+, v_add)
648+
CV__HAL_INTRIN_IMPL_BIN_OP(-, v_sub)
649+
CV__HAL_INTRIN_IMPL_BIN_OP(*, v_mul)
650+
CV__HAL_INTRIN_EXPAND_WITH_FP_TYPES(CV__HAL_INTRIN_IMPL_BIN_OP_, /, v_div)
665651

666-
#define CV__HAL_INTRIN_IMPL_BIT_OP_(_Tp, bit_op) \
652+
#define CV__HAL_INTRIN_IMPL_BIT_OP_(_Tp, bit_op, func) \
667653
template<int n> CV_INLINE \
668-
v_reg<_Tp, n> operator bit_op (const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \
654+
v_reg<_Tp, n> func(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \
669655
{ \
670656
v_reg<_Tp, n> c; \
671657
typedef typename V_TypeTraits<_Tp>::int_type itype; \
672658
for( int i = 0; i < n; i++ ) \
673659
c.s[i] = V_TypeTraits<_Tp>::reinterpret_from_int((itype)(V_TypeTraits<_Tp>::reinterpret_int(a.s[i]) bit_op \
674660
V_TypeTraits<_Tp>::reinterpret_int(b.s[i]))); \
675661
return c; \
676-
} \
677-
template<int n> CV_INLINE \
678-
v_reg<_Tp, n>& operator bit_op##= (v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \
679-
{ \
680-
typedef typename V_TypeTraits<_Tp>::int_type itype; \
681-
for( int i = 0; i < n; i++ ) \
682-
a.s[i] = V_TypeTraits<_Tp>::reinterpret_from_int((itype)(V_TypeTraits<_Tp>::reinterpret_int(a.s[i]) bit_op \
683-
V_TypeTraits<_Tp>::reinterpret_int(b.s[i]))); \
684-
return a; \
685662
}
686663

687-
#define CV__HAL_INTRIN_IMPL_BIT_OP(bit_op) \
688-
CV__HAL_INTRIN_EXPAND_WITH_INTEGER_TYPES(CV__HAL_INTRIN_IMPL_BIT_OP_, bit_op) \
689-
CV__HAL_INTRIN_EXPAND_WITH_FP_TYPES(CV__HAL_INTRIN_IMPL_BIT_OP_, bit_op) /* TODO: FIXIT remove this after masks refactoring */
664+
#define CV__HAL_INTRIN_IMPL_BIT_OP(bit_op, func) \
665+
CV__HAL_INTRIN_EXPAND_WITH_INTEGER_TYPES(CV__HAL_INTRIN_IMPL_BIT_OP_, bit_op, func) \
666+
CV__HAL_INTRIN_EXPAND_WITH_FP_TYPES(CV__HAL_INTRIN_IMPL_BIT_OP_, bit_op, func) /* TODO: FIXIT remove this after masks refactoring */
690667

691668

692-
CV__HAL_INTRIN_IMPL_BIT_OP(&)
693-
CV__HAL_INTRIN_IMPL_BIT_OP(|)
694-
CV__HAL_INTRIN_IMPL_BIT_OP(^)
669+
CV__HAL_INTRIN_IMPL_BIT_OP(&, v_and)
670+
CV__HAL_INTRIN_IMPL_BIT_OP(|, v_or)
671+
CV__HAL_INTRIN_IMPL_BIT_OP(^, v_xor)
695672

696-
#define CV__HAL_INTRIN_IMPL_BITWISE_NOT_(_Tp, dummy) \
673+
#define CV__HAL_INTRIN_IMPL_BITWISE_NOT_(_Tp, dummy, dummy2) \
697674
template<int n> CV_INLINE \
698-
v_reg<_Tp, n> operator ~ (const v_reg<_Tp, n>& a) \
675+
v_reg<_Tp, n> v_not(const v_reg<_Tp, n>& a) \
699676
{ \
700677
v_reg<_Tp, n> c; \
701678
for( int i = 0; i < n; i++ ) \
702679
c.s[i] = V_TypeTraits<_Tp>::reinterpret_from_int(~V_TypeTraits<_Tp>::reinterpret_int(a.s[i])); \
703680
return c; \
704681
} \
705682

706-
CV__HAL_INTRIN_EXPAND_WITH_INTEGER_TYPES(CV__HAL_INTRIN_IMPL_BITWISE_NOT_, ~)
683+
CV__HAL_INTRIN_EXPAND_WITH_INTEGER_TYPES(CV__HAL_INTRIN_IMPL_BITWISE_NOT_, ~, v_not)
707684

708685
#endif // !CV_DOXYGEN
709686

@@ -760,7 +737,6 @@ OPENCV_HAL_IMPL_MATH_FUNC(v_exp, std::exp, _Tp)
760737
* @note Similar to the behavior of std::log(), \f$ \ln(0) = -\infty \f$.
761738
*/
762739
OPENCV_HAL_IMPL_MATH_FUNC(v_log, std::log, _Tp)
763-
#define OPENCV_HAL_MATH_HAVE_LOG 1
764740

765741
/**
766742
* @brief Error function.
@@ -771,9 +747,7 @@ OPENCV_HAL_IMPL_MATH_FUNC(v_erf, std::erf, _Tp)
771747

772748
//! @cond IGNORED
773749
OPENCV_HAL_IMPL_MATH_FUNC(v_sin, std::sin, _Tp)
774-
#define OPENCV_HAL_MATH_HAVE_SIN 1
775750
OPENCV_HAL_IMPL_MATH_FUNC(v_cos, std::cos, _Tp)
776-
#define OPENCV_HAL_MATH_HAVE_COS 1
777751
//! @endcond
778752

779753
/** @brief Absolute value of elements
@@ -897,9 +871,9 @@ inline void v_minmax( const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b,
897871

898872
//! @brief Helper macro
899873
//! @ingroup core_hal_intrin_impl
900-
#define OPENCV_HAL_IMPL_CMP_OP(cmp_op) \
874+
#define OPENCV_HAL_IMPL_CMP_OP(cmp_op, func) \
901875
template<typename _Tp, int n> \
902-
inline v_reg<_Tp, n> operator cmp_op(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \
876+
inline v_reg<_Tp, n> func(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \
903877
{ \
904878
typedef typename V_TypeTraits<_Tp>::int_type itype; \
905879
v_reg<_Tp, n> c; \
@@ -911,28 +885,28 @@ inline v_reg<_Tp, n> operator cmp_op(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>
911885
/** @brief Less-than comparison
912886
913887
For all types except 64-bit integer values. */
914-
OPENCV_HAL_IMPL_CMP_OP(<)
888+
OPENCV_HAL_IMPL_CMP_OP(<, v_lt)
915889

916890
/** @brief Greater-than comparison
917891
918892
For all types except 64-bit integer values. */
919-
OPENCV_HAL_IMPL_CMP_OP(>)
893+
OPENCV_HAL_IMPL_CMP_OP(>, v_gt)
920894

921895
/** @brief Less-than or equal comparison
922896
923897
For all types except 64-bit integer values. */
924-
OPENCV_HAL_IMPL_CMP_OP(<=)
898+
OPENCV_HAL_IMPL_CMP_OP(<=, v_le)
925899

926900
/** @brief Greater-than or equal comparison
927901
928902
For all types except 64-bit integer values. */
929-
OPENCV_HAL_IMPL_CMP_OP(>=)
903+
OPENCV_HAL_IMPL_CMP_OP(>=, v_ge)
930904

931905
/** @brief Equal comparison */
932-
OPENCV_HAL_IMPL_CMP_OP(==)
906+
OPENCV_HAL_IMPL_CMP_OP(==, v_eq)
933907

934908
/** @brief Not equal comparison */
935-
OPENCV_HAL_IMPL_CMP_OP(!=)
909+
OPENCV_HAL_IMPL_CMP_OP(!=, v_ne)
936910

937911
template<int n>
938912
inline v_reg<float, n> v_not_nan(const v_reg<float, n>& a)
@@ -1301,8 +1275,8 @@ template<typename _Tp, int n> inline void v_hsum(const v_reg<_Tp, n>& a,
13011275

13021276
//! @brief Helper macro
13031277
//! @ingroup core_hal_intrin_impl
1304-
#define OPENCV_HAL_IMPL_SHIFT_OP(shift_op) \
1305-
template<typename _Tp, int n> inline v_reg<_Tp, n> operator shift_op(const v_reg<_Tp, n>& a, int imm) \
1278+
#define OPENCV_HAL_IMPL_SHIFT_OP(shift_op, func) \
1279+
template<typename _Tp, int n> inline v_reg<_Tp, n> func(const v_reg<_Tp, n>& a, int imm) \
13061280
{ \
13071281
v_reg<_Tp, n> c; \
13081282
for( int i = 0; i < n; i++ ) \
@@ -1313,12 +1287,12 @@ template<typename _Tp, int n> inline v_reg<_Tp, n> operator shift_op(const v_reg
13131287
/** @brief Bitwise shift left
13141288
13151289
For 16-, 32- and 64-bit integer values. */
1316-
OPENCV_HAL_IMPL_SHIFT_OP(<< )
1290+
OPENCV_HAL_IMPL_SHIFT_OP(<<, v_shl)
13171291

13181292
/** @brief Bitwise shift right
13191293
13201294
For 16-, 32- and 64-bit integer values. */
1321-
OPENCV_HAL_IMPL_SHIFT_OP(>> )
1295+
OPENCV_HAL_IMPL_SHIFT_OP(>>, v_shr)
13221296

13231297
//! @brief Helper macro
13241298
//! @ingroup core_hal_intrin_impl
@@ -2942,7 +2916,7 @@ OPENCV_HAL_IMPL_C_REINTERPRET(int64, s64)
29422916
//! @ingroup core_hal_intrin_impl
29432917
#define OPENCV_HAL_IMPL_C_SHIFTL(_Tp) \
29442918
template<int shift, int n> inline v_reg<_Tp, n> v_shl(const v_reg<_Tp, n>& a) \
2945-
{ return a << shift; }
2919+
{ return v_shl(a, shift); }
29462920

29472921
//! @name Left shift
29482922
//! @{
@@ -2959,7 +2933,7 @@ OPENCV_HAL_IMPL_C_SHIFTL(int64)
29592933
//! @ingroup core_hal_intrin_impl
29602934
#define OPENCV_HAL_IMPL_C_SHIFTR(_Tp) \
29612935
template<int shift, int n> inline v_reg<_Tp, n> v_shr(const v_reg<_Tp, n>& a) \
2962-
{ return a >> shift; }
2936+
{ return v_shr(a, shift); }
29632937

29642938
//! @name Right shift
29652939
//! @{
@@ -3285,7 +3259,7 @@ inline v_reg<float, n> v_matmuladd(const v_reg<float, n>& v,
32853259

32863260

32873261
template<int n> inline v_reg<double, n/2> v_dotprod_expand(const v_reg<int, n>& a, const v_reg<int, n>& b)
3288-
{ return v_fma(v_cvt_f64(a), v_cvt_f64(b), v_cvt_f64_high(a) * v_cvt_f64_high(b)); }
3262+
{ return v_fma(v_cvt_f64(a), v_cvt_f64(b), v_mul(v_cvt_f64_high(a), v_cvt_f64_high(b))); }
32893263
template<int n> inline v_reg<double, n/2> v_dotprod_expand(const v_reg<int, n>& a, const v_reg<int, n>& b,
32903264
const v_reg<double, n/2>& c)
32913265
{ return v_fma(v_cvt_f64(a), v_cvt_f64(b), v_fma(v_cvt_f64_high(a), v_cvt_f64_high(b), c)); }

0 commit comments

Comments
 (0)