saturate_cast.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #pragma once
  44. #ifndef OPENCV_CUDEV_UTIL_SATURATE_CAST_HPP
  45. #define OPENCV_CUDEV_UTIL_SATURATE_CAST_HPP
  46. #include "../common.hpp"
  47. #if __CUDACC_VER_MAJOR__ >= 9
  48. #include <cuda_fp16.h>
  49. #endif
  50. namespace cv { namespace cudev {
  51. //! @addtogroup cudev
  52. //! @{
  53. template <typename T> __device__ __forceinline__ T saturate_cast(uchar v) { return T(v); }
  54. template <typename T> __device__ __forceinline__ T saturate_cast(schar v) { return T(v); }
  55. template <typename T> __device__ __forceinline__ T saturate_cast(ushort v) { return T(v); }
  56. template <typename T> __device__ __forceinline__ T saturate_cast(short v) { return T(v); }
  57. template <typename T> __device__ __forceinline__ T saturate_cast(uint v) { return T(v); }
  58. template <typename T> __device__ __forceinline__ T saturate_cast(int v) { return T(v); }
  59. template <typename T> __device__ __forceinline__ T saturate_cast(float v) { return T(v); }
  60. template <typename T> __device__ __forceinline__ T saturate_cast(double v) { return T(v); }
  61. template <> __device__ __forceinline__ uchar saturate_cast<uchar>(schar v)
  62. {
  63. uint res = 0;
  64. int vi = v;
  65. asm("cvt.sat.u8.s8 %0, %1;" : "=r"(res) : "r"(vi));
  66. return res;
  67. }
  68. template <> __device__ __forceinline__ uchar saturate_cast<uchar>(short v)
  69. {
  70. uint res = 0;
  71. asm("cvt.sat.u8.s16 %0, %1;" : "=r"(res) : "h"(v));
  72. return res;
  73. }
  74. template <> __device__ __forceinline__ uchar saturate_cast<uchar>(ushort v)
  75. {
  76. uint res = 0;
  77. asm("cvt.sat.u8.u16 %0, %1;" : "=r"(res) : "h"(v));
  78. return res;
  79. }
  80. template <> __device__ __forceinline__ uchar saturate_cast<uchar>(int v)
  81. {
  82. uint res = 0;
  83. asm("cvt.sat.u8.s32 %0, %1;" : "=r"(res) : "r"(v));
  84. return res;
  85. }
  86. template <> __device__ __forceinline__ uchar saturate_cast<uchar>(uint v)
  87. {
  88. uint res = 0;
  89. asm("cvt.sat.u8.u32 %0, %1;" : "=r"(res) : "r"(v));
  90. return res;
  91. }
  92. template <> __device__ __forceinline__ uchar saturate_cast<uchar>(float v)
  93. {
  94. uint res = 0;
  95. asm("cvt.rni.sat.u8.f32 %0, %1;" : "=r"(res) : "f"(v));
  96. return res;
  97. }
  98. template <> __device__ __forceinline__ uchar saturate_cast<uchar>(double v)
  99. {
  100. uint res = 0;
  101. asm("cvt.rni.sat.u8.f64 %0, %1;" : "=r"(res) : "d"(v));
  102. return res;
  103. }
  104. template <> __device__ __forceinline__ schar saturate_cast<schar>(uchar v)
  105. {
  106. uint res = 0;
  107. uint vi = v;
  108. asm("cvt.sat.s8.u8 %0, %1;" : "=r"(res) : "r"(vi));
  109. return res;
  110. }
  111. template <> __device__ __forceinline__ schar saturate_cast<schar>(short v)
  112. {
  113. uint res = 0;
  114. asm("cvt.sat.s8.s16 %0, %1;" : "=r"(res) : "h"(v));
  115. return res;
  116. }
  117. template <> __device__ __forceinline__ schar saturate_cast<schar>(ushort v)
  118. {
  119. uint res = 0;
  120. asm("cvt.sat.s8.u16 %0, %1;" : "=r"(res) : "h"(v));
  121. return res;
  122. }
  123. template <> __device__ __forceinline__ schar saturate_cast<schar>(int v)
  124. {
  125. uint res = 0;
  126. asm("cvt.sat.s8.s32 %0, %1;" : "=r"(res) : "r"(v));
  127. return res;
  128. }
  129. template <> __device__ __forceinline__ schar saturate_cast<schar>(uint v)
  130. {
  131. uint res = 0;
  132. asm("cvt.sat.s8.u32 %0, %1;" : "=r"(res) : "r"(v));
  133. return res;
  134. }
  135. template <> __device__ __forceinline__ schar saturate_cast<schar>(float v)
  136. {
  137. uint res = 0;
  138. asm("cvt.rni.sat.s8.f32 %0, %1;" : "=r"(res) : "f"(v));
  139. return res;
  140. }
  141. template <> __device__ __forceinline__ schar saturate_cast<schar>(double v)
  142. {
  143. uint res = 0;
  144. asm("cvt.rni.sat.s8.f64 %0, %1;" : "=r"(res) : "d"(v));
  145. return res;
  146. }
  147. template <> __device__ __forceinline__ ushort saturate_cast<ushort>(schar v)
  148. {
  149. ushort res = 0;
  150. int vi = v;
  151. asm("cvt.sat.u16.s8 %0, %1;" : "=h"(res) : "r"(vi));
  152. return res;
  153. }
  154. template <> __device__ __forceinline__ ushort saturate_cast<ushort>(short v)
  155. {
  156. ushort res = 0;
  157. asm("cvt.sat.u16.s16 %0, %1;" : "=h"(res) : "h"(v));
  158. return res;
  159. }
  160. template <> __device__ __forceinline__ ushort saturate_cast<ushort>(int v)
  161. {
  162. ushort res = 0;
  163. asm("cvt.sat.u16.s32 %0, %1;" : "=h"(res) : "r"(v));
  164. return res;
  165. }
  166. template <> __device__ __forceinline__ ushort saturate_cast<ushort>(uint v)
  167. {
  168. ushort res = 0;
  169. asm("cvt.sat.u16.u32 %0, %1;" : "=h"(res) : "r"(v));
  170. return res;
  171. }
  172. template <> __device__ __forceinline__ ushort saturate_cast<ushort>(float v)
  173. {
  174. ushort res = 0;
  175. asm("cvt.rni.sat.u16.f32 %0, %1;" : "=h"(res) : "f"(v));
  176. return res;
  177. }
  178. template <> __device__ __forceinline__ ushort saturate_cast<ushort>(double v)
  179. {
  180. ushort res = 0;
  181. asm("cvt.rni.sat.u16.f64 %0, %1;" : "=h"(res) : "d"(v));
  182. return res;
  183. }
  184. template <> __device__ __forceinline__ short saturate_cast<short>(ushort v)
  185. {
  186. short res = 0;
  187. asm("cvt.sat.s16.u16 %0, %1;" : "=h"(res) : "h"(v));
  188. return res;
  189. }
  190. template <> __device__ __forceinline__ short saturate_cast<short>(int v)
  191. {
  192. short res = 0;
  193. asm("cvt.sat.s16.s32 %0, %1;" : "=h"(res) : "r"(v));
  194. return res;
  195. }
  196. template <> __device__ __forceinline__ short saturate_cast<short>(uint v)
  197. {
  198. short res = 0;
  199. asm("cvt.sat.s16.u32 %0, %1;" : "=h"(res) : "r"(v));
  200. return res;
  201. }
  202. template <> __device__ __forceinline__ short saturate_cast<short>(float v)
  203. {
  204. short res = 0;
  205. asm("cvt.rni.sat.s16.f32 %0, %1;" : "=h"(res) : "f"(v));
  206. return res;
  207. }
  208. template <> __device__ __forceinline__ short saturate_cast<short>(double v)
  209. {
  210. short res = 0;
  211. asm("cvt.rni.sat.s16.f64 %0, %1;" : "=h"(res) : "d"(v));
  212. return res;
  213. }
  214. template <> __device__ __forceinline__ int saturate_cast<int>(uint v)
  215. {
  216. int res = 0;
  217. asm("cvt.sat.s32.u32 %0, %1;" : "=r"(res) : "r"(v));
  218. return res;
  219. }
  220. template <> __device__ __forceinline__ int saturate_cast<int>(float v)
  221. {
  222. return __float2int_rn(v);
  223. }
  224. template <> __device__ __forceinline__ int saturate_cast<int>(double v)
  225. {
  226. #if CV_CUDEV_ARCH >= 130
  227. return __double2int_rn(v);
  228. #else
  229. return saturate_cast<int>((float) v);
  230. #endif
  231. }
  232. template <> __device__ __forceinline__ uint saturate_cast<uint>(schar v)
  233. {
  234. uint res = 0;
  235. int vi = v;
  236. asm("cvt.sat.u32.s8 %0, %1;" : "=r"(res) : "r"(vi));
  237. return res;
  238. }
  239. template <> __device__ __forceinline__ uint saturate_cast<uint>(short v)
  240. {
  241. uint res = 0;
  242. asm("cvt.sat.u32.s16 %0, %1;" : "=r"(res) : "h"(v));
  243. return res;
  244. }
  245. template <> __device__ __forceinline__ uint saturate_cast<uint>(int v)
  246. {
  247. uint res = 0;
  248. asm("cvt.sat.u32.s32 %0, %1;" : "=r"(res) : "r"(v));
  249. return res;
  250. }
  251. template <> __device__ __forceinline__ uint saturate_cast<uint>(float v)
  252. {
  253. return __float2uint_rn(v);
  254. }
  255. template <> __device__ __forceinline__ uint saturate_cast<uint>(double v)
  256. {
  257. #if CV_CUDEV_ARCH >= 130
  258. return __double2uint_rn(v);
  259. #else
  260. return saturate_cast<uint>((float) v);
  261. #endif
  262. }
  263. template <typename T, typename D> __device__ __forceinline__ D cast_fp16(T v);
  264. template <> __device__ __forceinline__ float cast_fp16<short, float>(short v)
  265. {
  266. #if __CUDACC_VER_MAJOR__ >= 9
  267. return float(*(__half*)&v);
  268. #else
  269. return __half2float(v);
  270. #endif
  271. }
  272. template <> __device__ __forceinline__ short cast_fp16<float, short>(float v)
  273. {
  274. #if __CUDACC_VER_MAJOR__ >= 9
  275. __half h(v);
  276. return *(short*)&h;
  277. #else
  278. return (short)__float2half_rn(v);
  279. #endif
  280. }
  281. //! @}
  282. }}
  283. #endif