functional.hpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. // 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 documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // 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 damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef OPENCV_CUDA_FUNCTIONAL_HPP
  43. #define OPENCV_CUDA_FUNCTIONAL_HPP
  44. #include <functional>
  45. #include "saturate_cast.hpp"
  46. #include "vec_traits.hpp"
  47. #include "type_traits.hpp"
  48. /** @file
  49. * @deprecated Use @ref cudev instead.
  50. */
  51. //! @cond IGNORED
  52. namespace cv { namespace cuda { namespace device
  53. {
  54. // Function Objects
  55. template<typename Argument, typename Result> struct unary_function
  56. {
  57. typedef Argument argument_type;
  58. typedef Result result_type;
  59. };
  60. template<typename Argument1, typename Argument2, typename Result> struct binary_function
  61. {
  62. typedef Argument1 first_argument_type;
  63. typedef Argument2 second_argument_type;
  64. typedef Result result_type;
  65. };
  66. // Arithmetic Operations
  67. template <typename T> struct plus : binary_function<T, T, T>
  68. {
  69. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a,
  70. typename TypeTraits<T>::ParameterType b) const
  71. {
  72. return a + b;
  73. }
  74. __host__ __device__ __forceinline__ plus() {}
  75. __host__ __device__ __forceinline__ plus(const plus&) {}
  76. };
  77. template <typename T> struct minus : binary_function<T, T, T>
  78. {
  79. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a,
  80. typename TypeTraits<T>::ParameterType b) const
  81. {
  82. return a - b;
  83. }
  84. __host__ __device__ __forceinline__ minus() {}
  85. __host__ __device__ __forceinline__ minus(const minus&) {}
  86. };
  87. template <typename T> struct multiplies : binary_function<T, T, T>
  88. {
  89. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a,
  90. typename TypeTraits<T>::ParameterType b) const
  91. {
  92. return a * b;
  93. }
  94. __host__ __device__ __forceinline__ multiplies() {}
  95. __host__ __device__ __forceinline__ multiplies(const multiplies&) {}
  96. };
  97. template <typename T> struct divides : binary_function<T, T, T>
  98. {
  99. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a,
  100. typename TypeTraits<T>::ParameterType b) const
  101. {
  102. return a / b;
  103. }
  104. __host__ __device__ __forceinline__ divides() {}
  105. __host__ __device__ __forceinline__ divides(const divides&) {}
  106. };
  107. template <typename T> struct modulus : binary_function<T, T, T>
  108. {
  109. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a,
  110. typename TypeTraits<T>::ParameterType b) const
  111. {
  112. return a % b;
  113. }
  114. __host__ __device__ __forceinline__ modulus() {}
  115. __host__ __device__ __forceinline__ modulus(const modulus&) {}
  116. };
  117. template <typename T> struct negate : unary_function<T, T>
  118. {
  119. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a) const
  120. {
  121. return -a;
  122. }
  123. __host__ __device__ __forceinline__ negate() {}
  124. __host__ __device__ __forceinline__ negate(const negate&) {}
  125. };
  126. // Comparison Operations
  127. template <typename T> struct equal_to : binary_function<T, T, bool>
  128. {
  129. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a,
  130. typename TypeTraits<T>::ParameterType b) const
  131. {
  132. return a == b;
  133. }
  134. __host__ __device__ __forceinline__ equal_to() {}
  135. __host__ __device__ __forceinline__ equal_to(const equal_to&) {}
  136. };
  137. template <typename T> struct not_equal_to : binary_function<T, T, bool>
  138. {
  139. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a,
  140. typename TypeTraits<T>::ParameterType b) const
  141. {
  142. return a != b;
  143. }
  144. __host__ __device__ __forceinline__ not_equal_to() {}
  145. __host__ __device__ __forceinline__ not_equal_to(const not_equal_to&) {}
  146. };
  147. template <typename T> struct greater : binary_function<T, T, bool>
  148. {
  149. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a,
  150. typename TypeTraits<T>::ParameterType b) const
  151. {
  152. return a > b;
  153. }
  154. __host__ __device__ __forceinline__ greater() {}
  155. __host__ __device__ __forceinline__ greater(const greater&) {}
  156. };
  157. template <typename T> struct less : binary_function<T, T, bool>
  158. {
  159. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a,
  160. typename TypeTraits<T>::ParameterType b) const
  161. {
  162. return a < b;
  163. }
  164. __host__ __device__ __forceinline__ less() {}
  165. __host__ __device__ __forceinline__ less(const less&) {}
  166. };
  167. template <typename T> struct greater_equal : binary_function<T, T, bool>
  168. {
  169. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a,
  170. typename TypeTraits<T>::ParameterType b) const
  171. {
  172. return a >= b;
  173. }
  174. __host__ __device__ __forceinline__ greater_equal() {}
  175. __host__ __device__ __forceinline__ greater_equal(const greater_equal&) {}
  176. };
  177. template <typename T> struct less_equal : binary_function<T, T, bool>
  178. {
  179. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a,
  180. typename TypeTraits<T>::ParameterType b) const
  181. {
  182. return a <= b;
  183. }
  184. __host__ __device__ __forceinline__ less_equal() {}
  185. __host__ __device__ __forceinline__ less_equal(const less_equal&) {}
  186. };
  187. // Logical Operations
  188. template <typename T> struct logical_and : binary_function<T, T, bool>
  189. {
  190. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a,
  191. typename TypeTraits<T>::ParameterType b) const
  192. {
  193. return a && b;
  194. }
  195. __host__ __device__ __forceinline__ logical_and() {}
  196. __host__ __device__ __forceinline__ logical_and(const logical_and&) {}
  197. };
  198. template <typename T> struct logical_or : binary_function<T, T, bool>
  199. {
  200. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a,
  201. typename TypeTraits<T>::ParameterType b) const
  202. {
  203. return a || b;
  204. }
  205. __host__ __device__ __forceinline__ logical_or() {}
  206. __host__ __device__ __forceinline__ logical_or(const logical_or&) {}
  207. };
  208. template <typename T> struct logical_not : unary_function<T, bool>
  209. {
  210. __device__ __forceinline__ bool operator ()(typename TypeTraits<T>::ParameterType a) const
  211. {
  212. return !a;
  213. }
  214. __host__ __device__ __forceinline__ logical_not() {}
  215. __host__ __device__ __forceinline__ logical_not(const logical_not&) {}
  216. };
  217. // Bitwise Operations
  218. template <typename T> struct bit_and : binary_function<T, T, T>
  219. {
  220. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a,
  221. typename TypeTraits<T>::ParameterType b) const
  222. {
  223. return a & b;
  224. }
  225. __host__ __device__ __forceinline__ bit_and() {}
  226. __host__ __device__ __forceinline__ bit_and(const bit_and&) {}
  227. };
  228. template <typename T> struct bit_or : binary_function<T, T, T>
  229. {
  230. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a,
  231. typename TypeTraits<T>::ParameterType b) const
  232. {
  233. return a | b;
  234. }
  235. __host__ __device__ __forceinline__ bit_or() {}
  236. __host__ __device__ __forceinline__ bit_or(const bit_or&) {}
  237. };
  238. template <typename T> struct bit_xor : binary_function<T, T, T>
  239. {
  240. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType a,
  241. typename TypeTraits<T>::ParameterType b) const
  242. {
  243. return a ^ b;
  244. }
  245. __host__ __device__ __forceinline__ bit_xor() {}
  246. __host__ __device__ __forceinline__ bit_xor(const bit_xor&) {}
  247. };
  248. template <typename T> struct bit_not : unary_function<T, T>
  249. {
  250. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType v) const
  251. {
  252. return ~v;
  253. }
  254. __host__ __device__ __forceinline__ bit_not() {}
  255. __host__ __device__ __forceinline__ bit_not(const bit_not&) {}
  256. };
  257. // Generalized Identity Operations
  258. template <typename T> struct identity : unary_function<T, T>
  259. {
  260. __device__ __forceinline__ typename TypeTraits<T>::ParameterType operator()(typename TypeTraits<T>::ParameterType x) const
  261. {
  262. return x;
  263. }
  264. __host__ __device__ __forceinline__ identity() {}
  265. __host__ __device__ __forceinline__ identity(const identity&) {}
  266. };
  267. template <typename T1, typename T2> struct project1st : binary_function<T1, T2, T1>
  268. {
  269. __device__ __forceinline__ typename TypeTraits<T1>::ParameterType operator()(typename TypeTraits<T1>::ParameterType lhs, typename TypeTraits<T2>::ParameterType rhs) const
  270. {
  271. return lhs;
  272. }
  273. __host__ __device__ __forceinline__ project1st() {}
  274. __host__ __device__ __forceinline__ project1st(const project1st&) {}
  275. };
  276. template <typename T1, typename T2> struct project2nd : binary_function<T1, T2, T2>
  277. {
  278. __device__ __forceinline__ typename TypeTraits<T2>::ParameterType operator()(typename TypeTraits<T1>::ParameterType lhs, typename TypeTraits<T2>::ParameterType rhs) const
  279. {
  280. return rhs;
  281. }
  282. __host__ __device__ __forceinline__ project2nd() {}
  283. __host__ __device__ __forceinline__ project2nd(const project2nd&) {}
  284. };
  285. // Min/Max Operations
  286. #define OPENCV_CUDA_IMPLEMENT_MINMAX(name, type, op) \
  287. template <> struct name<type> : binary_function<type, type, type> \
  288. { \
  289. __device__ __forceinline__ type operator()(type lhs, type rhs) const {return op(lhs, rhs);} \
  290. __host__ __device__ __forceinline__ name() {}\
  291. __host__ __device__ __forceinline__ name(const name&) {}\
  292. };
  293. template <typename T> struct maximum : binary_function<T, T, T>
  294. {
  295. __device__ __forceinline__ T operator()(typename TypeTraits<T>::ParameterType lhs, typename TypeTraits<T>::ParameterType rhs) const
  296. {
  297. return max(lhs, rhs);
  298. }
  299. __host__ __device__ __forceinline__ maximum() {}
  300. __host__ __device__ __forceinline__ maximum(const maximum&) {}
  301. };
  302. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, uchar, ::max)
  303. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, schar, ::max)
  304. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, char, ::max)
  305. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, ushort, ::max)
  306. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, short, ::max)
  307. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, int, ::max)
  308. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, uint, ::max)
  309. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, float, ::fmax)
  310. OPENCV_CUDA_IMPLEMENT_MINMAX(maximum, double, ::fmax)
  311. template <typename T> struct minimum : binary_function<T, T, T>
  312. {
  313. __device__ __forceinline__ T operator()(typename TypeTraits<T>::ParameterType lhs, typename TypeTraits<T>::ParameterType rhs) const
  314. {
  315. return min(lhs, rhs);
  316. }
  317. __host__ __device__ __forceinline__ minimum() {}
  318. __host__ __device__ __forceinline__ minimum(const minimum&) {}
  319. };
  320. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, uchar, ::min)
  321. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, schar, ::min)
  322. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, char, ::min)
  323. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, ushort, ::min)
  324. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, short, ::min)
  325. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, int, ::min)
  326. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, uint, ::min)
  327. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, float, ::fmin)
  328. OPENCV_CUDA_IMPLEMENT_MINMAX(minimum, double, ::fmin)
  329. #undef OPENCV_CUDA_IMPLEMENT_MINMAX
  330. // Math functions
  331. template <typename T> struct abs_func : unary_function<T, T>
  332. {
  333. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType x) const
  334. {
  335. return abs(x);
  336. }
  337. __host__ __device__ __forceinline__ abs_func() {}
  338. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  339. };
  340. template <> struct abs_func<unsigned char> : unary_function<unsigned char, unsigned char>
  341. {
  342. __device__ __forceinline__ unsigned char operator ()(unsigned char x) const
  343. {
  344. return x;
  345. }
  346. __host__ __device__ __forceinline__ abs_func() {}
  347. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  348. };
  349. template <> struct abs_func<signed char> : unary_function<signed char, signed char>
  350. {
  351. __device__ __forceinline__ signed char operator ()(signed char x) const
  352. {
  353. return ::abs((int)x);
  354. }
  355. __host__ __device__ __forceinline__ abs_func() {}
  356. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  357. };
  358. template <> struct abs_func<char> : unary_function<char, char>
  359. {
  360. __device__ __forceinline__ char operator ()(char x) const
  361. {
  362. return ::abs((int)x);
  363. }
  364. __host__ __device__ __forceinline__ abs_func() {}
  365. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  366. };
  367. template <> struct abs_func<unsigned short> : unary_function<unsigned short, unsigned short>
  368. {
  369. __device__ __forceinline__ unsigned short operator ()(unsigned short x) const
  370. {
  371. return x;
  372. }
  373. __host__ __device__ __forceinline__ abs_func() {}
  374. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  375. };
  376. template <> struct abs_func<short> : unary_function<short, short>
  377. {
  378. __device__ __forceinline__ short operator ()(short x) const
  379. {
  380. return ::abs((int)x);
  381. }
  382. __host__ __device__ __forceinline__ abs_func() {}
  383. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  384. };
  385. template <> struct abs_func<unsigned int> : unary_function<unsigned int, unsigned int>
  386. {
  387. __device__ __forceinline__ unsigned int operator ()(unsigned int x) const
  388. {
  389. return x;
  390. }
  391. __host__ __device__ __forceinline__ abs_func() {}
  392. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  393. };
  394. template <> struct abs_func<int> : unary_function<int, int>
  395. {
  396. __device__ __forceinline__ int operator ()(int x) const
  397. {
  398. return ::abs(x);
  399. }
  400. __host__ __device__ __forceinline__ abs_func() {}
  401. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  402. };
  403. template <> struct abs_func<float> : unary_function<float, float>
  404. {
  405. __device__ __forceinline__ float operator ()(float x) const
  406. {
  407. return ::fabsf(x);
  408. }
  409. __host__ __device__ __forceinline__ abs_func() {}
  410. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  411. };
  412. template <> struct abs_func<double> : unary_function<double, double>
  413. {
  414. __device__ __forceinline__ double operator ()(double x) const
  415. {
  416. return ::fabs(x);
  417. }
  418. __host__ __device__ __forceinline__ abs_func() {}
  419. __host__ __device__ __forceinline__ abs_func(const abs_func&) {}
  420. };
  421. #define OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(name, func) \
  422. template <typename T> struct name ## _func : unary_function<T, float> \
  423. { \
  424. __device__ __forceinline__ float operator ()(typename TypeTraits<T>::ParameterType v) const \
  425. { \
  426. return func ## f(v); \
  427. } \
  428. __host__ __device__ __forceinline__ name ## _func() {} \
  429. __host__ __device__ __forceinline__ name ## _func(const name ## _func&) {} \
  430. }; \
  431. template <> struct name ## _func<double> : unary_function<double, double> \
  432. { \
  433. __device__ __forceinline__ double operator ()(double v) const \
  434. { \
  435. return func(v); \
  436. } \
  437. __host__ __device__ __forceinline__ name ## _func() {} \
  438. __host__ __device__ __forceinline__ name ## _func(const name ## _func&) {} \
  439. };
  440. #define OPENCV_CUDA_IMPLEMENT_BIN_FUNCTOR(name, func) \
  441. template <typename T> struct name ## _func : binary_function<T, T, float> \
  442. { \
  443. __device__ __forceinline__ float operator ()(typename TypeTraits<T>::ParameterType v1, typename TypeTraits<T>::ParameterType v2) const \
  444. { \
  445. return func ## f(v1, v2); \
  446. } \
  447. __host__ __device__ __forceinline__ name ## _func() {} \
  448. __host__ __device__ __forceinline__ name ## _func(const name ## _func&) {} \
  449. }; \
  450. template <> struct name ## _func<double> : binary_function<double, double, double> \
  451. { \
  452. __device__ __forceinline__ double operator ()(double v1, double v2) const \
  453. { \
  454. return func(v1, v2); \
  455. } \
  456. __host__ __device__ __forceinline__ name ## _func() {} \
  457. __host__ __device__ __forceinline__ name ## _func(const name ## _func&) {} \
  458. };
  459. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(sqrt, ::sqrt)
  460. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(exp, ::exp)
  461. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(exp2, ::exp2)
  462. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(exp10, ::exp10)
  463. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(log, ::log)
  464. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(log2, ::log2)
  465. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(log10, ::log10)
  466. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(sin, ::sin)
  467. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(cos, ::cos)
  468. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(tan, ::tan)
  469. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(asin, ::asin)
  470. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(acos, ::acos)
  471. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(atan, ::atan)
  472. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(sinh, ::sinh)
  473. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(cosh, ::cosh)
  474. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(tanh, ::tanh)
  475. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(asinh, ::asinh)
  476. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(acosh, ::acosh)
  477. OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR(atanh, ::atanh)
  478. OPENCV_CUDA_IMPLEMENT_BIN_FUNCTOR(hypot, ::hypot)
  479. OPENCV_CUDA_IMPLEMENT_BIN_FUNCTOR(atan2, ::atan2)
  480. OPENCV_CUDA_IMPLEMENT_BIN_FUNCTOR(pow, ::pow)
  481. #undef OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR
  482. #undef OPENCV_CUDA_IMPLEMENT_UN_FUNCTOR_NO_DOUBLE
  483. #undef OPENCV_CUDA_IMPLEMENT_BIN_FUNCTOR
  484. template<typename T> struct hypot_sqr_func : binary_function<T, T, float>
  485. {
  486. __device__ __forceinline__ T operator ()(typename TypeTraits<T>::ParameterType src1, typename TypeTraits<T>::ParameterType src2) const
  487. {
  488. return src1 * src1 + src2 * src2;
  489. }
  490. __host__ __device__ __forceinline__ hypot_sqr_func() {}
  491. __host__ __device__ __forceinline__ hypot_sqr_func(const hypot_sqr_func&) {}
  492. };
  493. // Saturate Cast Functor
  494. template <typename T, typename D> struct saturate_cast_func : unary_function<T, D>
  495. {
  496. __device__ __forceinline__ D operator ()(typename TypeTraits<T>::ParameterType v) const
  497. {
  498. return saturate_cast<D>(v);
  499. }
  500. __host__ __device__ __forceinline__ saturate_cast_func() {}
  501. __host__ __device__ __forceinline__ saturate_cast_func(const saturate_cast_func&) {}
  502. };
  503. // Threshold Functors
  504. template <typename T> struct thresh_binary_func : unary_function<T, T>
  505. {
  506. __host__ __device__ __forceinline__ thresh_binary_func(T thresh_, T maxVal_) : thresh(thresh_), maxVal(maxVal_) {}
  507. __device__ __forceinline__ T operator()(typename TypeTraits<T>::ParameterType src) const
  508. {
  509. return (src > thresh) * maxVal;
  510. }
  511. __host__ __device__ __forceinline__ thresh_binary_func() {}
  512. __host__ __device__ __forceinline__ thresh_binary_func(const thresh_binary_func& other)
  513. : thresh(other.thresh), maxVal(other.maxVal) {}
  514. T thresh;
  515. T maxVal;
  516. };
  517. template <typename T> struct thresh_binary_inv_func : unary_function<T, T>
  518. {
  519. __host__ __device__ __forceinline__ thresh_binary_inv_func(T thresh_, T maxVal_) : thresh(thresh_), maxVal(maxVal_) {}
  520. __device__ __forceinline__ T operator()(typename TypeTraits<T>::ParameterType src) const
  521. {
  522. return (src <= thresh) * maxVal;
  523. }
  524. __host__ __device__ __forceinline__ thresh_binary_inv_func() {}
  525. __host__ __device__ __forceinline__ thresh_binary_inv_func(const thresh_binary_inv_func& other)
  526. : thresh(other.thresh), maxVal(other.maxVal) {}
  527. T thresh;
  528. T maxVal;
  529. };
  530. template <typename T> struct thresh_trunc_func : unary_function<T, T>
  531. {
  532. explicit __host__ __device__ __forceinline__ thresh_trunc_func(T thresh_, T maxVal_ = 0) : thresh(thresh_) {CV_UNUSED(maxVal_);}
  533. __device__ __forceinline__ T operator()(typename TypeTraits<T>::ParameterType src) const
  534. {
  535. return minimum<T>()(src, thresh);
  536. }
  537. __host__ __device__ __forceinline__ thresh_trunc_func() {}
  538. __host__ __device__ __forceinline__ thresh_trunc_func(const thresh_trunc_func& other)
  539. : thresh(other.thresh) {}
  540. T thresh;
  541. };
  542. template <typename T> struct thresh_to_zero_func : unary_function<T, T>
  543. {
  544. explicit __host__ __device__ __forceinline__ thresh_to_zero_func(T thresh_, T maxVal_ = 0) : thresh(thresh_) {CV_UNUSED(maxVal_);}
  545. __device__ __forceinline__ T operator()(typename TypeTraits<T>::ParameterType src) const
  546. {
  547. return (src > thresh) * src;
  548. }
  549. __host__ __device__ __forceinline__ thresh_to_zero_func() {}
  550. __host__ __device__ __forceinline__ thresh_to_zero_func(const thresh_to_zero_func& other)
  551. : thresh(other.thresh) {}
  552. T thresh;
  553. };
  554. template <typename T> struct thresh_to_zero_inv_func : unary_function<T, T>
  555. {
  556. explicit __host__ __device__ __forceinline__ thresh_to_zero_inv_func(T thresh_, T maxVal_ = 0) : thresh(thresh_) {CV_UNUSED(maxVal_);}
  557. __device__ __forceinline__ T operator()(typename TypeTraits<T>::ParameterType src) const
  558. {
  559. return (src <= thresh) * src;
  560. }
  561. __host__ __device__ __forceinline__ thresh_to_zero_inv_func() {}
  562. __host__ __device__ __forceinline__ thresh_to_zero_inv_func(const thresh_to_zero_inv_func& other)
  563. : thresh(other.thresh) {}
  564. T thresh;
  565. };
  566. // Function Object Adaptors
  567. template <typename Predicate> struct unary_negate : unary_function<typename Predicate::argument_type, bool>
  568. {
  569. explicit __host__ __device__ __forceinline__ unary_negate(const Predicate& p) : pred(p) {}
  570. __device__ __forceinline__ bool operator()(typename TypeTraits<typename Predicate::argument_type>::ParameterType x) const
  571. {
  572. return !pred(x);
  573. }
  574. __host__ __device__ __forceinline__ unary_negate() {}
  575. __host__ __device__ __forceinline__ unary_negate(const unary_negate& other) : pred(other.pred) {}
  576. Predicate pred;
  577. };
  578. template <typename Predicate> __host__ __device__ __forceinline__ unary_negate<Predicate> not1(const Predicate& pred)
  579. {
  580. return unary_negate<Predicate>(pred);
  581. }
  582. template <typename Predicate> struct binary_negate : binary_function<typename Predicate::first_argument_type, typename Predicate::second_argument_type, bool>
  583. {
  584. explicit __host__ __device__ __forceinline__ binary_negate(const Predicate& p) : pred(p) {}
  585. __device__ __forceinline__ bool operator()(typename TypeTraits<typename Predicate::first_argument_type>::ParameterType x,
  586. typename TypeTraits<typename Predicate::second_argument_type>::ParameterType y) const
  587. {
  588. return !pred(x,y);
  589. }
  590. __host__ __device__ __forceinline__ binary_negate() {}
  591. __host__ __device__ __forceinline__ binary_negate(const binary_negate& other) : pred(other.pred) {}
  592. Predicate pred;
  593. };
  594. template <typename BinaryPredicate> __host__ __device__ __forceinline__ binary_negate<BinaryPredicate> not2(const BinaryPredicate& pred)
  595. {
  596. return binary_negate<BinaryPredicate>(pred);
  597. }
  598. template <typename Op> struct binder1st : unary_function<typename Op::second_argument_type, typename Op::result_type>
  599. {
  600. __host__ __device__ __forceinline__ binder1st(const Op& op_, const typename Op::first_argument_type& arg1_) : op(op_), arg1(arg1_) {}
  601. __device__ __forceinline__ typename Op::result_type operator ()(typename TypeTraits<typename Op::second_argument_type>::ParameterType a) const
  602. {
  603. return op(arg1, a);
  604. }
  605. __host__ __device__ __forceinline__ binder1st() {}
  606. __host__ __device__ __forceinline__ binder1st(const binder1st& other) : op(other.op), arg1(other.arg1) {}
  607. Op op;
  608. typename Op::first_argument_type arg1;
  609. };
  610. template <typename Op, typename T> __host__ __device__ __forceinline__ binder1st<Op> bind1st(const Op& op, const T& x)
  611. {
  612. return binder1st<Op>(op, typename Op::first_argument_type(x));
  613. }
  614. template <typename Op> struct binder2nd : unary_function<typename Op::first_argument_type, typename Op::result_type>
  615. {
  616. __host__ __device__ __forceinline__ binder2nd(const Op& op_, const typename Op::second_argument_type& arg2_) : op(op_), arg2(arg2_) {}
  617. __forceinline__ __device__ typename Op::result_type operator ()(typename TypeTraits<typename Op::first_argument_type>::ParameterType a) const
  618. {
  619. return op(a, arg2);
  620. }
  621. __host__ __device__ __forceinline__ binder2nd() {}
  622. __host__ __device__ __forceinline__ binder2nd(const binder2nd& other) : op(other.op), arg2(other.arg2) {}
  623. Op op;
  624. typename Op::second_argument_type arg2;
  625. };
  626. template <typename Op, typename T> __host__ __device__ __forceinline__ binder2nd<Op> bind2nd(const Op& op, const T& x)
  627. {
  628. return binder2nd<Op>(op, typename Op::second_argument_type(x));
  629. }
  630. // Functor Traits
  631. template <typename F> struct IsUnaryFunction
  632. {
  633. typedef char Yes;
  634. struct No {Yes a[2];};
  635. template <typename T, typename D> static Yes check(unary_function<T, D>);
  636. static No check(...);
  637. static F makeF();
  638. enum { value = (sizeof(check(makeF())) == sizeof(Yes)) };
  639. };
  640. template <typename F> struct IsBinaryFunction
  641. {
  642. typedef char Yes;
  643. struct No {Yes a[2];};
  644. template <typename T1, typename T2, typename D> static Yes check(binary_function<T1, T2, D>);
  645. static No check(...);
  646. static F makeF();
  647. enum { value = (sizeof(check(makeF())) == sizeof(Yes)) };
  648. };
  649. namespace functional_detail
  650. {
  651. template <size_t src_elem_size, size_t dst_elem_size> struct UnOpShift { enum { shift = 1 }; };
  652. template <size_t src_elem_size> struct UnOpShift<src_elem_size, 1> { enum { shift = 4 }; };
  653. template <size_t src_elem_size> struct UnOpShift<src_elem_size, 2> { enum { shift = 2 }; };
  654. template <typename T, typename D> struct DefaultUnaryShift
  655. {
  656. enum { shift = UnOpShift<sizeof(T), sizeof(D)>::shift };
  657. };
  658. template <size_t src_elem_size1, size_t src_elem_size2, size_t dst_elem_size> struct BinOpShift { enum { shift = 1 }; };
  659. template <size_t src_elem_size1, size_t src_elem_size2> struct BinOpShift<src_elem_size1, src_elem_size2, 1> { enum { shift = 4 }; };
  660. template <size_t src_elem_size1, size_t src_elem_size2> struct BinOpShift<src_elem_size1, src_elem_size2, 2> { enum { shift = 2 }; };
  661. template <typename T1, typename T2, typename D> struct DefaultBinaryShift
  662. {
  663. enum { shift = BinOpShift<sizeof(T1), sizeof(T2), sizeof(D)>::shift };
  664. };
  665. template <typename Func, bool unary = IsUnaryFunction<Func>::value> struct ShiftDispatcher;
  666. template <typename Func> struct ShiftDispatcher<Func, true>
  667. {
  668. enum { shift = DefaultUnaryShift<typename Func::argument_type, typename Func::result_type>::shift };
  669. };
  670. template <typename Func> struct ShiftDispatcher<Func, false>
  671. {
  672. enum { shift = DefaultBinaryShift<typename Func::first_argument_type, typename Func::second_argument_type, typename Func::result_type>::shift };
  673. };
  674. }
  675. template <typename Func> struct DefaultTransformShift
  676. {
  677. enum { shift = functional_detail::ShiftDispatcher<Func>::shift };
  678. };
  679. template <typename Func> struct DefaultTransformFunctorTraits
  680. {
  681. enum { simple_block_dim_x = 16 };
  682. enum { simple_block_dim_y = 16 };
  683. enum { smart_block_dim_x = 16 };
  684. enum { smart_block_dim_y = 16 };
  685. enum { smart_shift = DefaultTransformShift<Func>::shift };
  686. };
  687. template <typename Func> struct TransformFunctorTraits : DefaultTransformFunctorTraits<Func> {};
  688. #define OPENCV_CUDA_TRANSFORM_FUNCTOR_TRAITS(type) \
  689. template <> struct TransformFunctorTraits< type > : DefaultTransformFunctorTraits< type >
  690. }}} // namespace cv { namespace cuda { namespace cudev
  691. //! @endcond
  692. #endif // OPENCV_CUDA_FUNCTIONAL_HPP