Path: blob/master/modules/imgproc/src/corner.avx.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Copyright (C) 2014-2015, Itseez Inc., all rights reserved.15// Third party copyrights are property of their respective owners.16//17// Redistribution and use in source and binary forms, with or without modification,18// are permitted provided that the following conditions are met:19//20// * Redistribution's of source code must retain the above copyright notice,21// this list of conditions and the following disclaimer.22//23// * Redistribution's in binary form must reproduce the above copyright notice,24// this list of conditions and the following disclaimer in the documentation25// and/or other materials provided with the distribution.26//27// * The name of the copyright holders may not be used to endorse or promote products28// derived from this software without specific prior written permission.29//30// This software is provided by the copyright holders and contributors "as is" and31// any express or implied warranties, including, but not limited to, the implied32// warranties of merchantability and fitness for a particular purpose are disclaimed.33// In no event shall the Intel Corporation or contributors be liable for any direct,34// indirect, incidental, special, exemplary, or consequential damages35// (including, but not limited to, procurement of substitute goods or services;36// loss of use, data, or profits; or business interruption) however caused37// and on any theory of liability, whether in contract, strict liability,38// or tort (including negligence or otherwise) arising in any way out of39// the use of this software, even if advised of the possibility of such damage.40//41//M*/4243#include "precomp.hpp"44#include "opencv2/core/hal/intrin.hpp"45#include "corner.hpp"4647namespace cv48{4950// load three 8-packed float vector and deinterleave51// probably it's better to write down somewhere else52static void load_deinterleave(const float* ptr, __m256& a, __m256& b, __m256& c)53{54__m256 s0 = _mm256_loadu_ps(ptr); // a0, b0, c0, a1, b1, c1, a2, b2,55__m256 s1 = _mm256_loadu_ps(ptr + 8); // c2, a3, b3, c3, a4, b4, c4, a5,56__m256 s2 = _mm256_loadu_ps(ptr + 16); // b5, c5, a6, b6, c6, a7, b7, c7,57__m256 s3 = _mm256_permute2f128_ps(s1, s2, 0x21); // a4, b4, c4, a5, b5, c5, a6, b6,58__m256 s4 = _mm256_permute2f128_ps(s2, s2, 0x33); // c6, a7, b7, c7, c6, a7, b7, c7,5960__m256 v00 = _mm256_unpacklo_ps(s0, s3); // a0, a4, b0, b4, b1, b5, c1, c5,61__m256 v01 = _mm256_unpackhi_ps(s0, s3); // c0, c4, a1, a5, a2, a6, b2, b6,62__m256 v02 = _mm256_unpacklo_ps(s1, s4); // c2, c6, a3, a7, x, x, x, x,63__m256 v03 = _mm256_unpackhi_ps(s1, s4); // b3, b7, c3, c7, x, x, x, x,64__m256 v04 = _mm256_permute2f128_ps(v02, v03, 0x20); // c2, c6, a3, a7, b3, b7, c3, c7,65__m256 v05 = _mm256_permute2f128_ps(v01, v03, 0x21); // a2, a6, b2, b6, b3, b7, c3, c7,6667__m256 v10 = _mm256_unpacklo_ps(v00, v05); // a0, a2, a4, a6, b1, b3, b5, b7,68__m256 v11 = _mm256_unpackhi_ps(v00, v05); // b0, b2, b4, b6, c1, c3, c5, c7,69__m256 v12 = _mm256_unpacklo_ps(v01, v04); // c0, c2, c4, c6, x, x, x, x,70__m256 v13 = _mm256_unpackhi_ps(v01, v04); // a1, a3, a5, a7, x, x, x, x,71__m256 v14 = _mm256_permute2f128_ps(v11, v12, 0x20); // b0, b2, b4, b6, c0, c2, c4, c6,72__m256 v15 = _mm256_permute2f128_ps(v10, v11, 0x31); // b1, b3, b5, b7, c1, c3, c5, c7,7374__m256 v20 = _mm256_unpacklo_ps(v14, v15); // b0, b1, b2, b3, c0, c1, c2, c3,75__m256 v21 = _mm256_unpackhi_ps(v14, v15); // b4, b5, b6, b7, c4, c5, c6, c7,76__m256 v22 = _mm256_unpacklo_ps(v10, v13); // a0, a1, a2, a3, x, x, x, x,77__m256 v23 = _mm256_unpackhi_ps(v10, v13); // a4, a5, a6, a7, x, x, x, x,7879a = _mm256_permute2f128_ps(v22, v23, 0x20); // a0, a1, a2, a3, a4, a5, a6, a7,80b = _mm256_permute2f128_ps(v20, v21, 0x20); // b0, b1, b2, b3, b4, b5, b6, b7,81c = _mm256_permute2f128_ps(v20, v21, 0x31); // c0, c1, c2, c3, c4, c5, c6, c7,82}8384// realign four 3-packed vector to three 4-packed vector85static void v_pack4x3to3x4(const __m128i& s0, const __m128i& s1, const __m128i& s2, const __m128i& s3, __m128i& d0, __m128i& d1, __m128i& d2)86{87d0 = _mm_or_si128(s0, _mm_slli_si128(s1, 12));88d1 = _mm_or_si128(_mm_srli_si128(s1, 4), _mm_slli_si128(s2, 8));89d2 = _mm_or_si128(_mm_srli_si128(s2, 8), _mm_slli_si128(s3, 4));90}9192// separate high and low 128 bit and cast to __m128i93static void v_separate_lo_hi(const __m256& src, __m128i& lo, __m128i& hi)94{95lo = _mm_castps_si128(_mm256_castps256_ps128(src));96hi = _mm_castps_si128(_mm256_extractf128_ps(src, 1));97}9899// interleave three 8-float vector and store100static void store_interleave(float* ptr, const __m256& a, const __m256& b, const __m256& c)101{102__m128i a0, a1, b0, b1, c0, c1;103v_separate_lo_hi(a, a0, a1);104v_separate_lo_hi(b, b0, b1);105v_separate_lo_hi(c, c0, c1);106107v_uint32x4 z = v_setzero_u32();108v_uint32x4 u0, u1, u2, u3;109v_transpose4x4(v_uint32x4(a0), v_uint32x4(b0), v_uint32x4(c0), z, u0, u1, u2, u3);110v_pack4x3to3x4(u0.val, u1.val, u2.val, u3.val, a0, b0, c0);111v_transpose4x4(v_uint32x4(a1), v_uint32x4(b1), v_uint32x4(c1), z, u0, u1, u2, u3);112v_pack4x3to3x4(u0.val, u1.val, u2.val, u3.val, a1, b1, c1);113114#if !defined(__GNUC__) || defined(__INTEL_COMPILER)115_mm256_storeu_ps(ptr, _mm256_setr_m128(_mm_castsi128_ps(a0), _mm_castsi128_ps(b0)));116_mm256_storeu_ps(ptr + 8, _mm256_setr_m128(_mm_castsi128_ps(c0), _mm_castsi128_ps(a1)));117_mm256_storeu_ps(ptr + 16, _mm256_setr_m128(_mm_castsi128_ps(b1), _mm_castsi128_ps(c1)));118#else119// GCC: workaround for missing AVX intrinsic: "_mm256_setr_m128()"120_mm256_storeu_ps(ptr, _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_castsi128_ps(a0)), _mm_castsi128_ps(b0), 1));121_mm256_storeu_ps(ptr + 8, _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_castsi128_ps(c0)), _mm_castsi128_ps(a1), 1));122_mm256_storeu_ps(ptr + 16, _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_castsi128_ps(b1)), _mm_castsi128_ps(c1), 1));123#endif124}125126int calcMinEigenValLine_AVX(const float* cov, float* dst, int width)127{128int j = 0;129__m256 half = _mm256_set1_ps(0.5f);130for (; j <= width - 8; j += 8)131{132__m256 v_a, v_b, v_c, v_t;133load_deinterleave(cov + j * 3, v_a, v_b, v_c);134v_a = _mm256_mul_ps(v_a, half);135v_c = _mm256_mul_ps(v_c, half);136v_t = _mm256_sub_ps(v_a, v_c);137v_t = _mm256_add_ps(_mm256_mul_ps(v_b, v_b), _mm256_mul_ps(v_t, v_t));138_mm256_storeu_ps(dst + j, _mm256_sub_ps(_mm256_add_ps(v_a, v_c), _mm256_sqrt_ps(v_t)));139}140return j;141}142143int calcHarrisLine_AVX(const float* cov, float* dst, double k, int width)144{145int j = 0;146__m256 v_k = _mm256_set1_ps((float)k);147148for (; j <= width - 8; j += 8)149{150__m256 v_a, v_b, v_c;151load_deinterleave(cov + j * 3, v_a, v_b, v_c);152153__m256 v_ac_bb = _mm256_sub_ps(_mm256_mul_ps(v_a, v_c), _mm256_mul_ps(v_b, v_b));154__m256 v_ac = _mm256_add_ps(v_a, v_c);155__m256 v_dst = _mm256_sub_ps(v_ac_bb, _mm256_mul_ps(v_k, _mm256_mul_ps(v_ac, v_ac)));156_mm256_storeu_ps(dst + j, v_dst);157}158return j;159}160161int cornerEigenValsVecsLine_AVX(const float* dxdata, const float* dydata, float* cov_data, int width)162{163int j = 0;164for (; j <= width - 8; j += 8)165{166__m256 v_dx = _mm256_loadu_ps(dxdata + j);167__m256 v_dy = _mm256_loadu_ps(dydata + j);168169__m256 v_dst0, v_dst1, v_dst2;170v_dst0 = _mm256_mul_ps(v_dx, v_dx);171v_dst1 = _mm256_mul_ps(v_dx, v_dy);172v_dst2 = _mm256_mul_ps(v_dy, v_dy);173174store_interleave(cov_data + j * 3, v_dst0, v_dst1, v_dst2);175}176return j;177}178179}180/* End of file */181182183