Path: blob/master/3rdparty/carotene/src/saturate_cast.hpp
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#ifndef CAROTENE_SATURATE_CAST_HPP40#define CAROTENE_SATURATE_CAST_HPP4142#include <algorithm>43#include <climits>44#include <cmath>4546#if defined _MSC_VER && defined _M_ARM47# include <intrin.h>48#endif4950#include <carotene/definitions.hpp>51#include <carotene/types.hpp>5253namespace CAROTENE_NS { namespace internal {5455#if defined _MSC_VER && defined _M_ARM5657__declspec(naked) static void vcvtr_s32_f64_imp(f64 d)58{59(void)d;60__emit(0xEEBD); // vcvtr.s32.f64 s0, d061__emit(0x0B40);62__emit(0xEE10); // vmov r0, s063__emit(0x0A10);64__emit(0x4770); // bx lr65}6667# define CAROTENE_ROUND_FLT(x) return ((s32 (*)(f64))vcvtr_s32_f64_imp)((f64)x);68# define CAROTENE_ROUND_DBL(x) return ((s32 (*)(f64))vcvtr_s32_f64_imp)(x);6970#elif defined CV_ICC || defined __GNUC__7172# if defined(__VFP_FP__) && !defined(__SOFTFP__) && !(defined _DEBUG || defined DEBUG) && !defined(__CUDACC__)73# define CAROTENE_ROUND_FLT(value) { \74union { f32 f; s32 i; } result; \75asm ("ftosis %0, %1 \n" : "=w" (result.f) : "w" (value) ); \76return result.i; }77# define CAROTENE_ROUND_DBL(value) { \78union {f32 f; s32 i;} __tegra_result; \79asm ( \80"ftosid %0, %P1\n" \81: "=w" (__tegra_result.f) \82: "w" (value) \83); \84return __tegra_result.i; \85}86# else87# define CAROTENE_ROUND_FLT(x) return (s32)lrintf(value);88# define CAROTENE_ROUND_DBL(value) return (s32)lrint(value);89# endif9091#endif9293inline s32 round(f32 value)94{95#ifdef CAROTENE_ROUND_FLT96CAROTENE_ROUND_FLT(value)97#else98s32 intpart = (s32)(value);99f32 fractpart = value - intpart;100if ((fractpart != 0.5 && fractpart != -0.5) || ((intpart % 2) != 0))101return (s32)(value + (value >= 0 ? 0.5 : -0.5));102else103return intpart;104#endif105}106107inline s32 round(f64 value)108{109#ifdef CAROTENE_ROUND_DBL110CAROTENE_ROUND_DBL(value)111#else112s32 intpart = (s32)(value);113f64 fractpart = value - intpart;114if ((fractpart != 0.5 && fractpart != -0.5) || ((intpart % 2) != 0))115return (s32)(value + (value >= 0 ? 0.5 : -0.5));116else117return intpart;118#endif119}120/////////////// saturate_cast (used in image & signal processing) ///////////////////121122template<typename _Tp> inline _Tp saturate_cast(u8 v) { return _Tp(v); }123template<typename _Tp> inline _Tp saturate_cast(s8 v) { return _Tp(v); }124template<typename _Tp> inline _Tp saturate_cast(u16 v) { return _Tp(v); }125template<typename _Tp> inline _Tp saturate_cast(s16 v) { return _Tp(v); }126template<typename _Tp> inline _Tp saturate_cast(u32 v) { return _Tp(v); }127template<typename _Tp> inline _Tp saturate_cast(s32 v) { return _Tp(v); }128template<typename _Tp> inline _Tp saturate_cast(s64 v) { return _Tp(v); }129template<typename _Tp> inline _Tp saturate_cast(u64 v) { return _Tp(v); }130template<typename _Tp> inline _Tp saturate_cast(f32 v) { return _Tp(v); }131template<typename _Tp> inline _Tp saturate_cast(f64 v) { return _Tp(v); }132133template<> inline u8 saturate_cast<u8>(s8 v) { return (u8)std::max((s32)v, 0); }134template<> inline u8 saturate_cast<u8>(u16 v) { return (u8)std::min((u32)v, (u32)UCHAR_MAX); }135template<> inline u8 saturate_cast<u8>(s32 v) { return (u8)((u32)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }136template<> inline u8 saturate_cast<u8>(s16 v) { return saturate_cast<u8>((s32)v); }137template<> inline u8 saturate_cast<u8>(u32 v) { return (u8)std::min(v, (u32)UCHAR_MAX); }138template<> inline u8 saturate_cast<u8>(s64 v) { return (u8)((u64)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }139template<> inline u8 saturate_cast<u8>(u64 v) { return (u8)std::min(v, (u64)UCHAR_MAX); }140template<> inline u8 saturate_cast<u8>(f32 v) { return saturate_cast<u8>(round(v)); }141template<> inline u8 saturate_cast<u8>(f64 v) { return saturate_cast<u8>(round(v)); }142143template<> inline s8 saturate_cast<s8>(u8 v) { return (s8)std::min((s32)v, SCHAR_MAX); }144template<> inline s8 saturate_cast<s8>(u16 v) { return (s8)std::min((u32)v, (u32)SCHAR_MAX); }145template<> inline s8 saturate_cast<s8>(s32 v) { return (s8)((u32)(v-SCHAR_MIN) <= (u32)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); }146template<> inline s8 saturate_cast<s8>(s16 v) { return saturate_cast<s8>((s32)v); }147template<> inline s8 saturate_cast<s8>(u32 v) { return (s8)std::min(v, (u32)SCHAR_MAX); }148template<> inline s8 saturate_cast<s8>(s64 v) { return (s8)((u64)(v-SCHAR_MIN) <= (u64)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); }149template<> inline s8 saturate_cast<s8>(u64 v) { return (s8)std::min(v, (u64)SCHAR_MAX); }150template<> inline s8 saturate_cast<s8>(f32 v) { return saturate_cast<s8>(round(v)); }151template<> inline s8 saturate_cast<s8>(f64 v) { return saturate_cast<s8>(round(v)); }152153template<> inline u16 saturate_cast<u16>(s8 v) { return (u16)std::max((s32)v, 0); }154template<> inline u16 saturate_cast<u16>(s16 v) { return (u16)std::max((s32)v, 0); }155template<> inline u16 saturate_cast<u16>(s32 v) { return (u16)((u32)v <= (u32)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); }156template<> inline u16 saturate_cast<u16>(u32 v) { return (u16)std::min(v, (u32)USHRT_MAX); }157template<> inline u16 saturate_cast<u16>(s64 v) { return (u16)((u64)v <= (u64)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); }158template<> inline u16 saturate_cast<u16>(u64 v) { return (u16)std::min(v, (u64)USHRT_MAX); }159template<> inline u16 saturate_cast<u16>(f32 v) { return saturate_cast<u16>(round(v)); }160template<> inline u16 saturate_cast<u16>(f64 v) { return saturate_cast<u16>(round(v)); }161162template<> inline s16 saturate_cast<s16>(u16 v) { return (s16)std::min((s32)v, SHRT_MAX); }163template<> inline s16 saturate_cast<s16>(s32 v) { return (s16)((u32)(v - SHRT_MIN) <= (u32)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); }164template<> inline s16 saturate_cast<s16>(u32 v) { return (s16)std::min(v, (u32)SHRT_MAX); }165template<> inline s16 saturate_cast<s16>(s64 v) { return (s16)((u64)(v - SHRT_MIN) <= (u64)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); }166template<> inline s16 saturate_cast<s16>(u64 v) { return (s16)std::min(v, (u64)SHRT_MAX); }167template<> inline s16 saturate_cast<s16>(f32 v) { return saturate_cast<s16>(round(v)); }168template<> inline s16 saturate_cast<s16>(f64 v) { return saturate_cast<s16>(round(v)); }169170template<> inline u32 saturate_cast<u32>(s8 v) { return (u32)std::max(v, (s8)0); }171template<> inline u32 saturate_cast<u32>(s16 v) { return (u32)std::max(v, (s16)0); }172template<> inline u32 saturate_cast<u32>(s32 v) { return (u32)std::max(v, (s32)0); }173template<> inline u32 saturate_cast<u32>(s64 v) { return (u32)((u64)v <= (u64)UINT_MAX ? v : v > 0 ? UINT_MAX : 0); }174template<> inline u32 saturate_cast<u32>(u64 v) { return (u32)std::min(v, (u64)UINT_MAX); }175//OpenCV like f32/f64 -> u32 conversion176//we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc.177template<> inline u32 saturate_cast<u32>(f32 v) { return round(v); }178template<> inline u32 saturate_cast<u32>(f64 v) { return round(v); }179//Negative clipping implementation180//template<> inline u32 saturate_cast<u32>(f32 v) { return saturate_cast<u32>(round(v)); }181//template<> inline u32 saturate_cast<u32>(f64 v) { return saturate_cast<u32>(round(v)); }182183template<> inline s32 saturate_cast<s32>(u32 v) { return (s32)std::min(v, (u32)INT_MAX); }184template<> inline s32 saturate_cast<s32>(s64 v) { return (s32)((u64)(v - INT_MIN) <= (u64)UINT_MAX ? v : v > 0 ? INT_MAX : INT_MIN); }185template<> inline s32 saturate_cast<s32>(u64 v) { return (s32)std::min(v, (u64)INT_MAX); }186template<> inline s32 saturate_cast<s32>(f32 v) { return round(v); }187template<> inline s32 saturate_cast<s32>(f64 v) { return round(v); }188189template<> inline u64 saturate_cast<u64>(s8 v) { return (u64)std::max(v, (s8)0); }190template<> inline u64 saturate_cast<u64>(s16 v) { return (u64)std::max(v, (s16)0); }191template<> inline u64 saturate_cast<u64>(s32 v) { return (u64)std::max(v, (s32)0); }192template<> inline u64 saturate_cast<u64>(s64 v) { return (u64)std::max(v, (s64)0); }193194template<> inline s64 saturate_cast<s64>(u64 v) { return (s64)std::min(v, (u64)LLONG_MAX); }195196} }197198#endif199200201