Skip to content

Wide univ intrinsics - #11953

Merged
vpisarev merged 17 commits into
opencv:3.4from
vpisarev:wide_univ_intrin
Jul 16, 2018
Merged

vpisarev merged 17 commits into
opencv:3.4from
vpisarev:wide_univ_intrin

Conversation

@vpisarev

@vpisarev vpisarev commented Jul 12, 2018 •

Copy link
Copy Markdown
Contributor

this is reworked and updated #10708 by @seiko2plus:

Tasks

  • Prepare universal intrinsics to expand (OE-27 "Wide Universal Intrinsics" discussion #11022)
  • Add universal intrinsics for AVX2 (not very complete, but mostly usable already)
  • Expand hal_intrin tests to support SIMD 256
  • Convert math functions (sqrt, invsqrt, magnitude, fastatan, exp, log) to univ. intrin.
  • Replace raw intrinsics with universal SIMD for arithmetic operations
  • Update docs
force_builders=Custom
docker_image:Custom=powerpc64le

@seiko2plus

Copy link
Copy Markdown
Contributor

@vpisarev, Please could you combine the last changes that I made on "intrin_avx.hpp"?

#ifdef CV_CPU_COMPILE_AVX2
# include <immintrin.h>
# define CV_AVX2 1
# define CV_FMA3 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes in this file are not necessary.
Please avoid logic duplication - it is already moved out to configuration stage (CMake).

#define CV_SIMD 1
#endif
#else
#define CV_SIMD128 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code doesn't look consistent. There mess of CV_SIMD / CV_SIMD128.

Update: It is better (clear) to write in the end:

#if CV_SIMD128 || CV_SIMD256
#define CV_SIMD 1
#else
#define CV_SIMD 0
#endif

typedef v_float32x8 v_float32;
#if CV_SIMD256_64F
typedef v_float64x4 v_float64;
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallback ?

    #elif CV_SIMD128_64F
    typedef v_float64x2 v_float64;

inline vtyp vx_setall_##short_typ(typ v) { return prefix##_setall_##short_typ(v); } \
inline vtyp vx_setzero_##short_typ() { return prefix##_setzero_##short_typ(); } \
inline vtyp vx_##loadsfx(const typ* ptr) { return prefix##_##loadsfx(ptr); } \
inline vtyp vx_##loadsfx##_aligned(const typ* ptr) { return prefix##_##loadsfx##_aligned(ptr); } \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like loadsfx is always "load".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, sometimes it's load_f16

template<> struct V_TypeTraits<type> \
{ \
typedef int_type_ int_type; \
typedef abs_type_ abs_type; \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backward compatibility would be nice to keep:

  • value_type
  • uint_type (and perhaps reinterpret_uint())

// 2
inline void v_load_deinterleave(const uchar* ptr, v_uint8x32& a, v_uint8x32& b)
{
// todo fix it ,, max 16

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo fix it?

//! @brief Check CPU capability of SIMD operation
static inline bool hasSIMD256()
{
return (CV_CPU_HAS_SUPPORT_AVX2) ? true : false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-CV_CPU_HAS_SUPPORT_AVX2
+CV_CPU_HAS_SUPPORT_AVX

// scalar code
if( i == 0 || angle == X || angle == Y )
break;
i = len - VECSZ*2;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's inplace operation and something is wrong then X/Y are already corrupted.

IMHO, vectorized loops in this file are malformed.
Proper design pattern is:

for( ; i <= len - BLOCK_SIZE; i += BLOCK_SIZE)
{
    // no more 'if's
    ...
}
for( ; i < len; i++ )
{
    ...
}

ybuf[j] = (float)Y[i + j];
xbuf[j] = (float)X[i + j];
}
fastAtan32f(ybuf, xbuf, abuf, blksz, angleInDegrees);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fastAtan32f() is function with CV_INSTRUMENT_REGION() (required for OpenCV entry-points, but using this inside of "per-element" loops should be avoided).

Performance may be terrible, than computation in doubles.
Do we have performance tests for this?


#endif

#if CV_AVX2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this should support CV_AVX too.

typedef v_int64x2 reg;
static v_int64x2 zero() { return v_setzero_s64(); }
static v_int64x2 all(int64 val) { return v_setall_s64(val); }
#define CV_INTRIN_DEFINE_WIDE_INTRIN(typ, vtyp, short_typ, prefix, loadsfx) \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice, for "internal" macros using CV__ prefix (with double underscores).

  • ASCII code of '_' is greater than A-Z, so IDE with auto-completion shows normal macros at first.
  • These macros are filtered out from Doxygen automatically

__m256i swapz = _mm256_permute2x128_si256(a.val, a.val, _MM_SHUFFLE(2, 0, 0, 1));

if (imm == 0)
return a;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is about definition of template specialization instead of if? At least we don't need "swapz" in this case.

CV_CPU_OPTIMIZATION_HAL_NAMESPACE_BEGIN
CV_CPU_OPTIMIZATION_HAL_NAMESPACE_END
using namespace CV_CPU_OPTIMIZATION_HAL_NAMESPACE;
CV_CPU_OPTIMIZATION_HAL_NAMESPACE_BEGIN

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore this code block on this place (exact after "namespace cv {").

All HAL intrinsic types must be guarded, including "V_TypeTraits".

@vpisarev

Copy link
Copy Markdown
Contributor Author

@seiko2plus, thank you! I've merged your changes

@vpisarev vpisarev self-assigned this Jul 16, 2018
@vpisarev
vpisarev merged commit f058b5f into opencv:3.4 Jul 16, 2018
@alalek alalek mentioned this pull request Jul 17, 2018
@vpisarev
vpisarev deleted the wide_univ_intrin branch September 12, 2018 12:25
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
* core:OE-27 prepare universal intrinsics to expand (opencv#11022)

* core:OE-27 prepare universal intrinsics to expand (opencv#11022)

* core: Add universal intrinsics for AVX2

* updated implementation of wide univ. intrinsics; converted several OpenCV HAL functions: sqrt, invsqrt, magnitude, phase, exp to the wide universal intrinsics.

* converted log to universal intrinsics; cleaned up the code a bit; added v_lut_deinterleave intrinsics.

* core: Add universal intrinsics for AVX2

* fixed multiple compile errors

* fixed many more compile errors and hopefully some test failures

* fixed some more compile errors

* temporarily disabled IPP to debug exp & log; hopefully fixed Doxygen complains

* fixed some more compile errors

* fixed v_store(short*, v_float16&) signatures

* trying to fix the test failures on Linux

* fixed some issues found by alalek

* restored IPP optimization after the patch with AVX wide intrinsics has been properly tested

* restored IPP optimization after the patch with AVX wide intrinsics has been properly tested
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants