From 14722baee34f653ad346449fddcbf8b4d8cb09cb Mon Sep 17 00:00:00 2001 From: shimat Date: Fri, 3 Apr 2020 10:06:18 +0900 Subject: [PATCH] fix popcnt error for ARM/ARM64 --- modules/bgsegm/src/bgfg_gsoc.cpp | 4 ++-- modules/line_descriptor/src/bitops.hpp | 10 +++++++++- modules/saliency/src/BING/kyheader.hpp | 27 +++++++++++++++----------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/modules/bgsegm/src/bgfg_gsoc.cpp b/modules/bgsegm/src/bgfg_gsoc.cpp index abf18d7b2..0ed676482 100644 --- a/modules/bgsegm/src/bgfg_gsoc.cpp +++ b/modules/bgsegm/src/bgfg_gsoc.cpp @@ -63,14 +63,14 @@ namespace const float LSBPtau = 0.05f; -#ifdef _MSC_VER +#if defined(_MSC_VER) && !(defined(_M_ARM) || defined(_M_ARM64)) #include #pragma intrinsic(__popcnt) #endif inline int LSBPDist32(unsigned n) { #if defined(__GNUC__) || defined(__clang__) return __builtin_popcount(n); -#elif defined(_MSC_VER) +#elif defined(_MSC_VER) && !(defined(_M_ARM) || defined(_M_ARM64)) return __popcnt(n); #else // Taken from http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel diff --git a/modules/line_descriptor/src/bitops.hpp b/modules/line_descriptor/src/bitops.hpp index f2e2a1a52..29182d20b 100644 --- a/modules/line_descriptor/src/bitops.hpp +++ b/modules/line_descriptor/src/bitops.hpp @@ -46,12 +46,20 @@ #include "precomp.hpp" #ifdef _MSC_VER +#if defined(_M_ARM) || defined(_M_ARM64) +static inline UINT32 popcnt(UINT32 v) +{ + v = v - ((v >> 1) & 0x55555555); + v = (v & 0x33333333) + ((v >> 2) & 0x33333333); + return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; +} +#else # include # define popcnt __popcnt # pragma warning( disable : 4267 ) +#endif #else # define popcnt __builtin_popcount - #endif /* LUT */ diff --git a/modules/saliency/src/BING/kyheader.hpp b/modules/saliency/src/BING/kyheader.hpp index 0a8eeaa7e..42187cdb6 100644 --- a/modules/saliency/src/BING/kyheader.hpp +++ b/modules/saliency/src/BING/kyheader.hpp @@ -152,17 +152,6 @@ inline cv::Rect Vec4i2Rect( cv::Vec4i &v ) } -#if defined(_MSC_VER) -# include -# define POPCNT(x) __popcnt(x) -# define POPCNT64(x) (__popcnt((unsigned)(x)) + __popcnt((unsigned)((uint64_t)(x) >> 32))) -#endif - -#if defined(__GNUC__) -# define POPCNT(x) __builtin_popcount(x) -# define POPCNT64(x) __builtin_popcountll(x) -#endif - inline int popcnt64( uint64_t u ) { u = ( u & 0x5555555555555555 ) + ( ( u >> 1 ) & 0x5555555555555555 ); @@ -252,6 +241,22 @@ inline int popcnt_byte( uint32_t u ) return (int)c; } +#if defined(_MSC_VER) +#if defined(_M_ARM) || defined(_M_ARM64) +# define POPCNT(x) popcnt((x)) +# define POPCNT64(x) popcnt64((x)) +#else +# include +# define POPCNT(x) __popcnt(x) +# define POPCNT64(x) (__popcnt((unsigned)(x)) + __popcnt((unsigned)((uint64_t)(x) >> 32))) +#endif +#endif + +#if defined(__GNUC__) +# define POPCNT(x) __builtin_popcount(x) +# define POPCNT64(x) __builtin_popcountll(x) +#endif + } }