Path: blob/master/3rdparty/carotene/src/integral.cpp
16337 views
/*1* By downloading, copying, installing or using the software you agree to this license.2* If you do not agree to this license, do not download, install,3* copy or use the software.4*5*6* License Agreement7* For Open Source Computer Vision Library8* (3-clause BSD License)9*10* Copyright (C) 2012-2014, NVIDIA Corporation, all rights reserved.11* Third party copyrights are property of their respective owners.12*13* Redistribution and use in source and binary forms, with or without modification,14* are permitted provided that the following conditions are met:15*16* * Redistributions of source code must retain the above copyright notice,17* this list of conditions and the following disclaimer.18*19* * Redistributions in binary form must reproduce the above copyright notice,20* this list of conditions and the following disclaimer in the documentation21* and/or other materials provided with the distribution.22*23* * Neither the names of the copyright holders nor the names of the contributors24* may be used to endorse or promote products derived from this software25* without specific prior written permission.26*27* This software is provided by the copyright holders and contributors "as is" and28* any express or implied warranties, including, but not limited to, the implied29* warranties of merchantability and fitness for a particular purpose are disclaimed.30* In no event shall copyright holders or contributors be liable for any direct,31* indirect, incidental, special, exemplary, or consequential damages32* (including, but not limited to, procurement of substitute goods or services;33* loss of use, data, or profits; or business interruption) however caused34* and on any theory of liability, whether in contract, strict liability,35* or tort (including negligence or otherwise) arising in any way out of36* the use of this software, even if advised of the possibility of such damage.37*/3839#include "common.hpp"4041namespace CAROTENE_NS {4243void integral(const Size2D &size,44const u8 * srcBase, ptrdiff_t srcStride,45u32 * sumBase, ptrdiff_t sumStride)46{47internal::assertSupportedConfiguration();48#ifdef CAROTENE_NEON49uint32x4_t v_zero = vmovq_n_u32(0u);5051// the first iteration52const u8 * src = internal::getRowPtr(srcBase, srcStride, 0);53u32 * sum = internal::getRowPtr(sumBase, sumStride, 0);5455uint32x4_t prev = v_zero;56size_t j = 0u;5758for ( ; j + 7 < size.width; j += 8)59{60internal::prefetch(sum + j);61internal::prefetch(src + j);6263uint8x8_t el8shr0 = vld1_u8(src + j);64uint8x8_t el8shr1 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 8));65uint8x8_t el8shr2 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 16));66uint8x8_t el8shr3 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 24));6768uint16x8_t el8shr12 = vaddl_u8(el8shr1, el8shr2);69uint16x8_t el8shr03 = vaddl_u8(el8shr0, el8shr3);7071uint16x8_t el8 = vaddq_u16(el8shr12, el8shr03);72uint16x4_t el4h = vadd_u16(vget_low_u16(el8), vget_high_u16(el8));7374uint32x4_t vsuml = vaddw_u16(prev, vget_low_u16(el8));75uint32x4_t vsumh = vaddw_u16(prev, el4h);7677vst1q_u32(sum + j, vsuml);78vst1q_u32(sum + j + 4, vsumh);7980prev = vaddw_u16(prev, vdup_lane_u16(el4h, 3));81}8283for (u32 v = vgetq_lane_u32(prev, 3); j < size.width; ++j)84sum[j] = (v += src[j]);8586// the others87for (size_t i = 1; i < size.height ; ++i)88{89src = internal::getRowPtr(srcBase, srcStride, i);90u32 * prevSum = internal::getRowPtr(sumBase, sumStride, i - 1);91sum = internal::getRowPtr(sumBase, sumStride, i);9293prev = v_zero;94j = 0u;9596for ( ; j + 7 < size.width; j += 8)97{98internal::prefetch(sum + j);99internal::prefetch(src + j);100101uint32x4_t vsuml = vld1q_u32(prevSum + j);102uint32x4_t vsumh = vld1q_u32(prevSum + j + 4);103104uint8x8_t el8shr0 = vld1_u8(src + j);105uint8x8_t el8shr1 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 8));106uint8x8_t el8shr2 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 16));107uint8x8_t el8shr3 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 24));108109vsuml = vaddq_u32(vsuml, prev);110vsumh = vaddq_u32(vsumh, prev);111112uint16x8_t el8shr12 = vaddl_u8(el8shr1, el8shr2);113uint16x8_t el8shr03 = vaddl_u8(el8shr0, el8shr3);114115uint16x8_t el8 = vaddq_u16(el8shr12, el8shr03);116uint16x4_t el4h = vadd_u16(vget_low_u16(el8), vget_high_u16(el8));117118vsuml = vaddw_u16(vsuml, vget_low_u16(el8));119vsumh = vaddw_u16(vsumh, el4h);120121vst1q_u32(sum + j, vsuml);122vst1q_u32(sum + j + 4, vsumh);123124prev = vaddw_u16(prev, vdup_lane_u16(el4h, 3));125}126127for (u32 v = vgetq_lane_u32(prev, 3); j < size.width; ++j)128sum[j] = (v += src[j]) + prevSum[j];129}130#else131(void)size;132(void)srcBase;133(void)srcStride;134(void)sumBase;135(void)sumStride;136#endif137}138139void sqrIntegral(const Size2D &size,140const u8 * srcBase, ptrdiff_t srcStride,141f64 * sqsumBase, ptrdiff_t sqsumStride)142{143internal::assertSupportedConfiguration();144#ifdef CAROTENE_NEON145uint16x8_t v_zero8 = vmovq_n_u16(0u);146147// the first iteration148const u8 * src = internal::getRowPtr(srcBase, srcStride, 0);149f64 * sqsum = internal::getRowPtr(sqsumBase, sqsumStride, 0);150151double prev = 0.;152size_t j = 0u;153154for ( ; j + 7 < size.width; j += 8)155{156internal::prefetch(sqsum + j);157internal::prefetch(src + j);158159uint8x8_t vsrc = vld1_u8(src + j);160161uint16x8_t el8shr0 = vmull_u8(vsrc, vsrc);162uint16x8_t el8shr1 = vextq_u16(v_zero8, el8shr0, 7);163164uint32x4_t el8shr01l = vaddl_u16(vget_low_u16(el8shr0), vget_low_u16(el8shr1));165uint32x4_t el8shr01h = vaddl_u16(vget_high_u16(el8shr0), vget_high_u16(el8shr1));166167uint32x4_t el4h = vaddq_u32(el8shr01l, el8shr01h);168169uint32x2_t el2l = vadd_u32(vget_low_u32(el8shr01l), vget_high_u32(el8shr01l));170uint32x2_t el2hl = vadd_u32(vget_low_u32(el4h), vget_high_u32(el8shr01l));171uint32x2_t el2hh = vadd_u32(vget_low_u32(el4h), vget_high_u32(el4h));172173u32 buf[8];174vst1_u32(buf, vget_low_u32(el8shr01l));175vst1_u32(buf+2, el2l);176vst1_u32(buf+4, el2hl);177vst1_u32(buf+6, el2hh);178for(u32 k=0; k < 8; k++)179sqsum[j+k] = prev + buf[k];180prev += buf[7];181}182183for (; j < size.width; ++j)184sqsum[j] = (prev += src[j]*src[j]);185186// the others187for (size_t i = 1; i < size.height ; ++i)188{189src = internal::getRowPtr(srcBase, srcStride, i);190f64 * prevSqSum = internal::getRowPtr(sqsumBase, sqsumStride, i - 1);191sqsum = internal::getRowPtr(sqsumBase, sqsumStride, i);192193prev = 0.;194j = 0u;195196for ( ; j + 7 < size.width; j += 8)197{198internal::prefetch(sqsum + j);199internal::prefetch(src + j);200201uint8x8_t vsrc = vld1_u8(src + j);202203uint16x8_t el8shr0 = vmull_u8(vsrc, vsrc);204uint16x8_t el8shr1 = vextq_u16(v_zero8, el8shr0, 7);205206uint32x4_t el8shr01l = vaddl_u16(vget_low_u16(el8shr0), vget_low_u16(el8shr1));207uint32x4_t el8shr01h = vaddl_u16(vget_high_u16(el8shr0), vget_high_u16(el8shr1));208209uint32x4_t el4h = vaddq_u32(el8shr01l, el8shr01h);210211uint32x2_t el2l = vadd_u32(vget_low_u32(el8shr01l), vget_high_u32(el8shr01l));212uint32x2_t el2hl = vadd_u32(vget_low_u32(el4h), vget_high_u32(el8shr01l));213uint32x2_t el2hh = vadd_u32(vget_low_u32(el4h), vget_high_u32(el4h));214215u32 buf[8];216vst1_u32(buf, vget_low_u32(el8shr01l));217vst1_u32(buf+2, el2l);218vst1_u32(buf+4, el2hl);219vst1_u32(buf+6, el2hh);220for(u32 k=0; k < 8; k++)221sqsum[j+k] = prev + prevSqSum[j+k] + buf[k];222prev += buf[7];223}224225for (; j < size.width; ++j)226sqsum[j] = (prev += src[j]*src[j]) + prevSqSum[j];227}228#else229(void)size;230(void)srcBase;231(void)srcStride;232(void)sqsumBase;233(void)sqsumStride;234#endif235}236237} // namespace CAROTENE_NS238239240