Path: blob/master/3rdparty/carotene/src/in_range.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-2015, 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"4041#include "vtransform.hpp"4243namespace CAROTENE_NS {4445#ifdef CAROTENE_NEON4647namespace {4849inline void vnst(u8* dst, uint8x16_t v1, uint8x16_t v2) { vst1q_u8(dst, v1); vst1q_u8(dst+16, v2); }50inline void vnst(u8* dst, uint16x8_t v1, uint16x8_t v2) { vst1q_u8(dst, vcombine_u8(vmovn_u16(v1), vmovn_u16(v2))); }51inline void vnst(u8* dst, uint32x4_t v1, uint32x4_t v2) { vst1_u8(dst, vmovn_u16(vcombine_u16(vmovn_u32(v1), vmovn_u32(v2)))); }5253template <typename T, int elsize> struct vtail54{55static inline void inRange(const T *, const T *, const T *,56u8 *, size_t &, size_t)57{58//do nothing since there couldn't be enough data59}60};61template <typename T> struct vtail<T, 2>62{63static inline void inRange(const T * src, const T * rng1, const T * rng2,64u8 * dst, size_t &x, size_t width)65{66typedef typename internal::VecTraits<T>::vec128 vec128;67typedef typename internal::VecTraits<T>::unsign::vec128 uvec128;68//There no more than 15 elements in the tail, so we could handle 8 element vector only once69if( x + 8 < width)70{71vec128 vs = internal::vld1q( src + x);72vec128 vr1 = internal::vld1q(rng1 + x);73vec128 vr2 = internal::vld1q(rng2 + x);74uvec128 vd = internal::vandq(internal::vcgeq(vs, vr1), internal::vcgeq(vr2, vs));75internal::vst1(dst + x, internal::vmovn(vd));76x+=8;77}78}79};80template <typename T> struct vtail<T, 1>81{82static inline void inRange(const T * src, const T * rng1, const T * rng2,83u8 * dst, size_t &x, size_t width)84{85typedef typename internal::VecTraits<T>::vec128 vec128;86typedef typename internal::VecTraits<T>::unsign::vec128 uvec128;87typedef typename internal::VecTraits<T>::vec64 vec64;88typedef typename internal::VecTraits<T>::unsign::vec64 uvec64;89//There no more than 31 elements in the tail, so we could handle once 16+8 or 16 or 8 elements90if( x + 16 < width)91{92vec128 vs = internal::vld1q( src + x);93vec128 vr1 = internal::vld1q(rng1 + x);94vec128 vr2 = internal::vld1q(rng2 + x);95uvec128 vd = internal::vandq(internal::vcgeq(vs, vr1), internal::vcgeq(vr2, vs));96internal::vst1q(dst + x, vd);97x+=16;98}99if( x + 8 < width)100{101vec64 vs = internal::vld1( src + x);102vec64 vr1 = internal::vld1(rng1 + x);103vec64 vr2 = internal::vld1(rng2 + x);104uvec64 vd = internal::vand(internal::vcge(vs, vr1), internal::vcge(vr2, vs));105internal::vst1(dst + x, vd);106x+=8;107}108}109};110111template <typename T>112inline void inRangeCheck(const Size2D &_size,113const T * srcBase, ptrdiff_t srcStride,114const T * rng1Base, ptrdiff_t rng1Stride,115const T * rng2Base, ptrdiff_t rng2Stride,116u8 * dstBase, ptrdiff_t dstStride)117{118typedef typename internal::VecTraits<T>::vec128 vec128;119typedef typename internal::VecTraits<T>::unsign::vec128 uvec128;120121Size2D size(_size);122if (srcStride == dstStride &&123srcStride == rng1Stride &&124srcStride == rng2Stride &&125srcStride == (ptrdiff_t)(size.width))126{127size.width *= size.height;128size.height = 1;129}130const size_t width = size.width & ~( 32/sizeof(T) - 1 );131132for(size_t j = 0; j < size.height; ++j)133{134const T * src = internal::getRowPtr( srcBase, srcStride, j);135const T * rng1 = internal::getRowPtr(rng1Base, rng1Stride, j);136const T * rng2 = internal::getRowPtr(rng2Base, rng2Stride, j);137u8 * dst = internal::getRowPtr( dstBase, dstStride, j);138size_t i = 0;139for( ; i < width; i += 32/sizeof(T) )140{141internal::prefetch(src + i);142internal::prefetch(rng1 + i);143internal::prefetch(rng2 + i);144145vec128 vs = internal::vld1q( src + i);146vec128 vr1 = internal::vld1q(rng1 + i);147vec128 vr2 = internal::vld1q(rng2 + i);148uvec128 vd1 = internal::vandq(internal::vcgeq(vs, vr1), internal::vcgeq(vr2, vs));149vs = internal::vld1q( src + i + 16/sizeof(T));150vr1 = internal::vld1q(rng1 + i + 16/sizeof(T));151vr2 = internal::vld1q(rng2 + i + 16/sizeof(T));152uvec128 vd2 = internal::vandq(internal::vcgeq(vs, vr1), internal::vcgeq(vr2, vs));153vnst(dst + i, vd1, vd2);154}155vtail<T, sizeof(T)>::inRange(src, rng1, rng2, dst, i, size.width);156for( ; i < size.width; i++ )157dst[i] = (u8)(-(rng1[i] <= src[i] && src[i] <= rng2[i]));158}159}160161}162163#define INRANGEFUNC(T) \164void inRange(const Size2D &_size, \165const T * srcBase, ptrdiff_t srcStride, \166const T * rng1Base, ptrdiff_t rng1Stride, \167const T * rng2Base, ptrdiff_t rng2Stride, \168u8 * dstBase, ptrdiff_t dstStride) \169{ \170internal::assertSupportedConfiguration(); \171inRangeCheck(_size, srcBase, srcStride, \172rng1Base, rng1Stride, rng2Base, rng2Stride, \173dstBase, dstStride); \174}175#else176#define INRANGEFUNC(T) \177void inRange(const Size2D &, \178const T *, ptrdiff_t, \179const T *, ptrdiff_t, \180const T *, ptrdiff_t, \181u8 *, ptrdiff_t) \182{ \183internal::assertSupportedConfiguration(); \184}185#endif186187INRANGEFUNC(u8)188INRANGEFUNC(s8)189INRANGEFUNC(u16)190INRANGEFUNC(s16)191INRANGEFUNC(s32)192INRANGEFUNC(f32)193194} // namespace CAROTENE_NS195196197