mirror of https://github.com/opencv/opencv.git
Merge pull request #12411 from vpisarev:wide_convert
* rewrote Mat::convertTo() and convertScaleAbs() to wide universal intrinsics; added always-available and SIMD-optimized FP16<=>FP32 conversion * fixed compile warnings * fix some more compile errors * slightly relaxed accuracy threshold for int->float conversion (since we now do it using single-precision arithmetics, not double-precision) * fixed compile errors on iOS, Android and in the baseline C++ version (intrin_cpp.hpp) * trying to fix ARM-neon builds * trying to fix ARM-neon builds * trying to fix ARM-neon builds * trying to fix ARM-neon buildspull/12459/head
parent
54279523a3
commit
80b62a41c6
18 changed files with 1131 additions and 3494 deletions
@ -1,40 +0,0 @@ |
|||||||
// This file is part of OpenCV project.
|
|
||||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
||||||
// of this distribution and at http://opencv.org/license.html
|
|
||||||
|
|
||||||
|
|
||||||
#include "precomp.hpp" |
|
||||||
#include "convert.hpp" |
|
||||||
|
|
||||||
namespace cv |
|
||||||
{ |
|
||||||
namespace opt_AVX2 |
|
||||||
{ |
|
||||||
|
|
||||||
void cvtScale_s16s32f32Line_AVX2(const short* src, int* dst, float scale, float shift, int width) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
__m256 scale256 = _mm256_set1_ps(scale); |
|
||||||
__m256 shift256 = _mm256_set1_ps(shift); |
|
||||||
const int shuffle = 0xD8; |
|
||||||
|
|
||||||
for (; x <= width - 16; x += 16) |
|
||||||
{ |
|
||||||
__m256i v_src = _mm256_loadu_si256((const __m256i *)(src + x)); |
|
||||||
v_src = _mm256_permute4x64_epi64(v_src, shuffle); |
|
||||||
__m256i v_src_lo = _mm256_srai_epi32(_mm256_unpacklo_epi16(v_src, v_src), 16); |
|
||||||
__m256i v_src_hi = _mm256_srai_epi32(_mm256_unpackhi_epi16(v_src, v_src), 16); |
|
||||||
__m256 v_dst0 = _mm256_add_ps(_mm256_mul_ps(_mm256_cvtepi32_ps(v_src_lo), scale256), shift256); |
|
||||||
__m256 v_dst1 = _mm256_add_ps(_mm256_mul_ps(_mm256_cvtepi32_ps(v_src_hi), scale256), shift256); |
|
||||||
_mm256_storeu_si256((__m256i *)(dst + x), _mm256_cvtps_epi32(v_dst0)); |
|
||||||
_mm256_storeu_si256((__m256i *)(dst + x + 8), _mm256_cvtps_epi32(v_dst1)); |
|
||||||
} |
|
||||||
|
|
||||||
for (; x < width; x++) |
|
||||||
dst[x] = saturate_cast<int>(src[x] * scale + shift); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} // cv::
|
|
||||||
/* End of file. */ |
|
File diff suppressed because it is too large
Load Diff
@ -1,126 +0,0 @@ |
|||||||
// This file is part of OpenCV project.
|
|
||||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
||||||
// of this distribution and at http://opencv.org/license.html
|
|
||||||
|
|
||||||
|
|
||||||
#include "precomp.hpp" |
|
||||||
#include "convert.hpp" |
|
||||||
|
|
||||||
namespace cv |
|
||||||
{ |
|
||||||
namespace opt_FP16 |
|
||||||
{ |
|
||||||
#if !defined(CV_NEON) || !CV_NEON |
|
||||||
const static int cVectorWidth = 8; |
|
||||||
|
|
||||||
void cvtScaleHalf_SIMD32f16f( const float* src, size_t sstep, short* dst, size_t dstep, cv::Size size ) |
|
||||||
{ |
|
||||||
CV_INSTRUMENT_REGION() |
|
||||||
|
|
||||||
sstep /= sizeof(src[0]); |
|
||||||
dstep /= sizeof(dst[0]); |
|
||||||
|
|
||||||
for( ; size.height--; src += sstep, dst += dstep ) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
for ( ; x <= size.width - cVectorWidth ; x += cVectorWidth ) |
|
||||||
{ |
|
||||||
__m256 v_src = _mm256_loadu_ps(src + x); |
|
||||||
|
|
||||||
// round to nearest even
|
|
||||||
__m128i v_dst = _mm256_cvtps_ph(v_src, 0); |
|
||||||
|
|
||||||
_mm_storeu_si128((__m128i*)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
for ( ; x < size.width; x++ ) |
|
||||||
{ |
|
||||||
dst[x] = convertFp16SW(src[x]); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void cvtScaleHalf_SIMD16f32f( const short* src, size_t sstep, float* dst, size_t dstep, cv::Size size ) |
|
||||||
{ |
|
||||||
CV_INSTRUMENT_REGION() |
|
||||||
|
|
||||||
sstep /= sizeof(src[0]); |
|
||||||
dstep /= sizeof(dst[0]); |
|
||||||
|
|
||||||
for( ; size.height--; src += sstep, dst += dstep ) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
for ( ; x <= size.width - cVectorWidth ; x += cVectorWidth ) |
|
||||||
{ |
|
||||||
__m128i v_src = _mm_loadu_si128((__m128i*)(src + x)); |
|
||||||
|
|
||||||
__m256 v_dst = _mm256_cvtph_ps(v_src); |
|
||||||
|
|
||||||
_mm256_storeu_ps(dst + x, v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
for ( ; x < size.width; x++ ) |
|
||||||
{ |
|
||||||
dst[x] = convertFp16SW(src[x]); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
#elif CV_NEON |
|
||||||
const static int cVectorWidth = 4; |
|
||||||
|
|
||||||
void cvtScaleHalf_SIMD32f16f( const float* src, size_t sstep, short* dst, size_t dstep, cv::Size size ) |
|
||||||
{ |
|
||||||
CV_INSTRUMENT_REGION() |
|
||||||
|
|
||||||
sstep /= sizeof(src[0]); |
|
||||||
dstep /= sizeof(dst[0]); |
|
||||||
|
|
||||||
for( ; size.height--; src += sstep, dst += dstep ) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
for ( ; x <= size.width - cVectorWidth ; x += cVectorWidth) |
|
||||||
{ |
|
||||||
float32x4_t v_src = vld1q_f32(src + x); |
|
||||||
float16x4_t v_dst = vcvt_f16_f32(v_src); |
|
||||||
|
|
||||||
cv_vst1_f16(dst + x, v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
for ( ; x < size.width; x++ ) |
|
||||||
{ |
|
||||||
dst[x] = convertFp16SW(src[x]); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void cvtScaleHalf_SIMD16f32f( const short* src, size_t sstep, float* dst, size_t dstep, cv::Size size ) |
|
||||||
{ |
|
||||||
CV_INSTRUMENT_REGION() |
|
||||||
|
|
||||||
sstep /= sizeof(src[0]); |
|
||||||
dstep /= sizeof(dst[0]); |
|
||||||
|
|
||||||
for( ; size.height--; src += sstep, dst += dstep ) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
for ( ; x <= size.width - cVectorWidth ; x += cVectorWidth ) |
|
||||||
{ |
|
||||||
float16x4_t v_src = cv_vld1_f16((__fp16*)src + x); |
|
||||||
|
|
||||||
float32x4_t v_dst = vcvt_f32_f16(v_src); |
|
||||||
|
|
||||||
vst1q_f32(dst + x, v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
for ( ; x < size.width; x++ ) |
|
||||||
{ |
|
||||||
dst[x] = convertFp16SW(src[x]); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
#else |
|
||||||
#error "Unsupported build configuration" |
|
||||||
#endif |
|
||||||
} |
|
||||||
|
|
||||||
} // cv::
|
|
@ -1,203 +0,0 @@ |
|||||||
// This file is part of OpenCV project.
|
|
||||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
||||||
// of this distribution and at http://opencv.org/license.html
|
|
||||||
|
|
||||||
|
|
||||||
#include "precomp.hpp" |
|
||||||
#include "convert.hpp" |
|
||||||
|
|
||||||
namespace cv |
|
||||||
{ |
|
||||||
namespace opt_SSE4_1 |
|
||||||
{ |
|
||||||
|
|
||||||
int cvtScale_SIMD_u8u16f32_SSE41(const uchar * src, ushort * dst, int width, float scale, float shift) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
__m128i v_zero = _mm_setzero_si128(); |
|
||||||
__m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift); |
|
||||||
|
|
||||||
for ( ; x <= width - 8; x += 8) |
|
||||||
{ |
|
||||||
__m128i v_src = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i const *)(src + x)), v_zero); |
|
||||||
__m128 v_src_f = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v_src, v_zero)); |
|
||||||
__m128 v_dst_0 = _mm_add_ps(_mm_mul_ps(v_src_f, v_scale), v_shift); |
|
||||||
|
|
||||||
v_src_f = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v_src, v_zero)); |
|
||||||
__m128 v_dst_1 = _mm_add_ps(_mm_mul_ps(v_src_f, v_scale), v_shift); |
|
||||||
|
|
||||||
__m128i v_dst = _mm_packus_epi32(_mm_cvtps_epi32(v_dst_0), |
|
||||||
_mm_cvtps_epi32(v_dst_1)); |
|
||||||
_mm_storeu_si128((__m128i *)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
return x; |
|
||||||
} |
|
||||||
|
|
||||||
int cvtScale_SIMD_s8u16f32_SSE41(const schar * src, ushort * dst, int width, float scale, float shift) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
__m128i v_zero = _mm_setzero_si128(); |
|
||||||
__m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift); |
|
||||||
|
|
||||||
for ( ; x <= width - 8; x += 8) |
|
||||||
{ |
|
||||||
__m128i v_src = _mm_srai_epi16(_mm_unpacklo_epi8(v_zero, _mm_loadl_epi64((__m128i const *)(src + x))), 8); |
|
||||||
__m128 v_src_f = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(v_zero, v_src), 16)); |
|
||||||
__m128 v_dst_0 = _mm_add_ps(_mm_mul_ps(v_src_f, v_scale), v_shift); |
|
||||||
|
|
||||||
v_src_f = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpackhi_epi16(v_zero, v_src), 16)); |
|
||||||
__m128 v_dst_1 = _mm_add_ps(_mm_mul_ps(v_src_f, v_scale), v_shift); |
|
||||||
|
|
||||||
__m128i v_dst = _mm_packus_epi32(_mm_cvtps_epi32(v_dst_0), |
|
||||||
_mm_cvtps_epi32(v_dst_1)); |
|
||||||
_mm_storeu_si128((__m128i *)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
return x; |
|
||||||
} |
|
||||||
|
|
||||||
int cvtScale_SIMD_u16u16f32_SSE41(const ushort * src, ushort * dst, int width, float scale, float shift) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
__m128i v_zero = _mm_setzero_si128(); |
|
||||||
__m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift); |
|
||||||
|
|
||||||
for ( ; x <= width - 8; x += 8) |
|
||||||
{ |
|
||||||
__m128i v_src = _mm_loadu_si128((__m128i const *)(src + x)); |
|
||||||
__m128 v_src_f = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v_src, v_zero)); |
|
||||||
__m128 v_dst_0 = _mm_add_ps(_mm_mul_ps(v_src_f, v_scale), v_shift); |
|
||||||
|
|
||||||
v_src_f = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v_src, v_zero)); |
|
||||||
__m128 v_dst_1 = _mm_add_ps(_mm_mul_ps(v_src_f, v_scale), v_shift); |
|
||||||
|
|
||||||
__m128i v_dst = _mm_packus_epi32(_mm_cvtps_epi32(v_dst_0), |
|
||||||
_mm_cvtps_epi32(v_dst_1)); |
|
||||||
_mm_storeu_si128((__m128i *)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
return x; |
|
||||||
} |
|
||||||
|
|
||||||
int cvtScale_SIMD_s16u16f32_SSE41(const short * src, ushort * dst, int width, float scale, float shift) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
__m128i v_zero = _mm_setzero_si128(); |
|
||||||
__m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift); |
|
||||||
|
|
||||||
for ( ; x <= width - 8; x += 8) |
|
||||||
{ |
|
||||||
__m128i v_src = _mm_loadu_si128((__m128i const *)(src + x)); |
|
||||||
__m128 v_src_f = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(v_zero, v_src), 16)); |
|
||||||
__m128 v_dst_0 = _mm_add_ps(_mm_mul_ps(v_src_f, v_scale), v_shift); |
|
||||||
|
|
||||||
v_src_f = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpackhi_epi16(v_zero, v_src), 16)); |
|
||||||
__m128 v_dst_1 = _mm_add_ps(_mm_mul_ps(v_src_f, v_scale), v_shift); |
|
||||||
|
|
||||||
__m128i v_dst = _mm_packus_epi32(_mm_cvtps_epi32(v_dst_0), |
|
||||||
_mm_cvtps_epi32(v_dst_1)); |
|
||||||
_mm_storeu_si128((__m128i *)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
return x; |
|
||||||
} |
|
||||||
|
|
||||||
int cvtScale_SIMD_s32u16f32_SSE41(const int * src, ushort * dst, int width, float scale, float shift) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
__m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift); |
|
||||||
|
|
||||||
for ( ; x <= width - 8; x += 8) |
|
||||||
{ |
|
||||||
__m128i v_src = _mm_loadu_si128((__m128i const *)(src + x)); |
|
||||||
__m128 v_dst_0 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(v_src), v_scale), v_shift); |
|
||||||
|
|
||||||
v_src = _mm_loadu_si128((__m128i const *)(src + x + 4)); |
|
||||||
__m128 v_dst_1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(v_src), v_scale), v_shift); |
|
||||||
|
|
||||||
__m128i v_dst = _mm_packus_epi32(_mm_cvtps_epi32(v_dst_0), |
|
||||||
_mm_cvtps_epi32(v_dst_1)); |
|
||||||
_mm_storeu_si128((__m128i *)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
return x; |
|
||||||
} |
|
||||||
|
|
||||||
int cvtScale_SIMD_f32u16f32_SSE41(const float * src, ushort * dst, int width, float scale, float shift) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
__m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift); |
|
||||||
|
|
||||||
for ( ; x <= width - 8; x += 8) |
|
||||||
{ |
|
||||||
__m128 v_src = _mm_loadu_ps(src + x); |
|
||||||
__m128 v_dst_0 = _mm_add_ps(_mm_mul_ps(v_src, v_scale), v_shift); |
|
||||||
|
|
||||||
v_src = _mm_loadu_ps(src + x + 4); |
|
||||||
__m128 v_dst_1 = _mm_add_ps(_mm_mul_ps(v_src, v_scale), v_shift); |
|
||||||
|
|
||||||
__m128i v_dst = _mm_packus_epi32(_mm_cvtps_epi32(v_dst_0), |
|
||||||
_mm_cvtps_epi32(v_dst_1)); |
|
||||||
_mm_storeu_si128((__m128i *)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
return x; |
|
||||||
} |
|
||||||
|
|
||||||
int cvtScale_SIMD_f64u16f32_SSE41(const double * src, ushort * dst, int width, float scale, float shift) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
__m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift); |
|
||||||
|
|
||||||
for ( ; x <= width - 8; x += 8) |
|
||||||
{ |
|
||||||
__m128 v_src = _mm_movelh_ps(_mm_cvtpd_ps(_mm_loadu_pd(src + x)), |
|
||||||
_mm_cvtpd_ps(_mm_loadu_pd(src + x + 2))); |
|
||||||
__m128 v_dst_0 = _mm_add_ps(_mm_mul_ps(v_src, v_scale), v_shift); |
|
||||||
|
|
||||||
v_src = _mm_movelh_ps(_mm_cvtpd_ps(_mm_loadu_pd(src + x + 4)), |
|
||||||
_mm_cvtpd_ps(_mm_loadu_pd(src + x + 6))); |
|
||||||
__m128 v_dst_1 = _mm_add_ps(_mm_mul_ps(v_src, v_scale), v_shift); |
|
||||||
|
|
||||||
__m128i v_dst = _mm_packus_epi32(_mm_cvtps_epi32(v_dst_0), |
|
||||||
_mm_cvtps_epi32(v_dst_1)); |
|
||||||
_mm_storeu_si128((__m128i *)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
return x; |
|
||||||
} |
|
||||||
|
|
||||||
int Cvt_SIMD_f64u16_SSE41(const double * src, ushort * dst, int width) |
|
||||||
{ |
|
||||||
int x = 0; |
|
||||||
|
|
||||||
for ( ; x <= width - 8; x += 8) |
|
||||||
{ |
|
||||||
__m128 v_src0 = _mm_cvtpd_ps(_mm_loadu_pd(src + x)); |
|
||||||
__m128 v_src1 = _mm_cvtpd_ps(_mm_loadu_pd(src + x + 2)); |
|
||||||
__m128 v_src2 = _mm_cvtpd_ps(_mm_loadu_pd(src + x + 4)); |
|
||||||
__m128 v_src3 = _mm_cvtpd_ps(_mm_loadu_pd(src + x + 6)); |
|
||||||
|
|
||||||
v_src0 = _mm_movelh_ps(v_src0, v_src1); |
|
||||||
v_src1 = _mm_movelh_ps(v_src2, v_src3); |
|
||||||
|
|
||||||
__m128i v_dst = _mm_packus_epi32(_mm_cvtps_epi32(v_src0), |
|
||||||
_mm_cvtps_epi32(v_src1)); |
|
||||||
_mm_storeu_si128((__m128i *)(dst + x), v_dst); |
|
||||||
} |
|
||||||
|
|
||||||
return x; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} // cv::
|
|
||||||
|
|
||||||
/* End of file. */ |
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue