cvdef.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. // Copyright (C) 2015, Itseez Inc., all rights reserved.
  17. // Third party copyrights are property of their respective owners.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification,
  20. // are permitted provided that the following conditions are met:
  21. //
  22. // * Redistribution's of source code must retain the above copyright notice,
  23. // this list of conditions and the following disclaimer.
  24. //
  25. // * Redistribution's in binary form must reproduce the above copyright notice,
  26. // this list of conditions and the following disclaimer in the documentation
  27. // and/or other materials provided with the distribution.
  28. //
  29. // * The name of the copyright holders may not be used to endorse or promote products
  30. // derived from this software without specific prior written permission.
  31. //
  32. // This software is provided by the copyright holders and contributors "as is" and
  33. // any express or implied warranties, including, but not limited to, the implied
  34. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  35. // In no event shall the Intel Corporation or contributors be liable for any direct,
  36. // indirect, incidental, special, exemplary, or consequential damages
  37. // (including, but not limited to, procurement of substitute goods or services;
  38. // loss of use, data, or profits; or business interruption) however caused
  39. // and on any theory of liability, whether in contract, strict liability,
  40. // or tort (including negligence or otherwise) arising in any way out of
  41. // the use of this software, even if advised of the possibility of such damage.
  42. //
  43. //M*/
  44. #ifndef OPENCV_CORE_CVDEF_H
  45. #define OPENCV_CORE_CVDEF_H
  46. //! @addtogroup core_utils
  47. //! @{
  48. #if !defined CV_DOXYGEN && !defined CV_IGNORE_DEBUG_BUILD_GUARD
  49. #if (defined(_MSC_VER) && (defined(DEBUG) || defined(_DEBUG))) || \
  50. (defined(_GLIBCXX_DEBUG) || defined(_GLIBCXX_DEBUG_PEDANTIC))
  51. // Guard to prevent using of binary incompatible binaries / runtimes
  52. // https://github.com/opencv/opencv/pull/9161
  53. #define CV__DEBUG_NS_BEGIN namespace debug_build_guard {
  54. #define CV__DEBUG_NS_END }
  55. namespace cv { namespace debug_build_guard { } using namespace debug_build_guard; }
  56. #endif
  57. #endif
  58. #ifndef CV__DEBUG_NS_BEGIN
  59. #define CV__DEBUG_NS_BEGIN
  60. #define CV__DEBUG_NS_END
  61. #endif
  62. #ifdef __OPENCV_BUILD
  63. #include "cvconfig.h"
  64. #endif
  65. #ifndef __CV_EXPAND
  66. #define __CV_EXPAND(x) x
  67. #endif
  68. #ifndef __CV_CAT
  69. #define __CV_CAT__(x, y) x ## y
  70. #define __CV_CAT_(x, y) __CV_CAT__(x, y)
  71. #define __CV_CAT(x, y) __CV_CAT_(x, y)
  72. #endif
  73. // undef problematic defines sometimes defined by system headers (windows.h in particular)
  74. #undef small
  75. #undef min
  76. #undef max
  77. #undef abs
  78. #undef Complex
  79. #include <limits.h>
  80. #include "opencv2/core/hal/interface.h"
  81. #if defined __ICL
  82. # define CV_ICC __ICL
  83. #elif defined __ICC
  84. # define CV_ICC __ICC
  85. #elif defined __ECL
  86. # define CV_ICC __ECL
  87. #elif defined __ECC
  88. # define CV_ICC __ECC
  89. #elif defined __INTEL_COMPILER
  90. # define CV_ICC __INTEL_COMPILER
  91. #endif
  92. #ifndef CV_INLINE
  93. # if defined __cplusplus
  94. # define CV_INLINE static inline
  95. # elif defined _MSC_VER
  96. # define CV_INLINE __inline
  97. # else
  98. # define CV_INLINE static
  99. # endif
  100. #endif
  101. #if defined CV_DISABLE_OPTIMIZATION || (defined CV_ICC && !defined CV_ENABLE_UNROLLED)
  102. # define CV_ENABLE_UNROLLED 0
  103. #else
  104. # define CV_ENABLE_UNROLLED 1
  105. #endif
  106. #ifdef __GNUC__
  107. # define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
  108. #elif defined _MSC_VER
  109. # define CV_DECL_ALIGNED(x) __declspec(align(x))
  110. #else
  111. # define CV_DECL_ALIGNED(x)
  112. #endif
  113. /* CPU features and intrinsics support */
  114. #define CV_CPU_NONE 0
  115. #define CV_CPU_MMX 1
  116. #define CV_CPU_SSE 2
  117. #define CV_CPU_SSE2 3
  118. #define CV_CPU_SSE3 4
  119. #define CV_CPU_SSSE3 5
  120. #define CV_CPU_SSE4_1 6
  121. #define CV_CPU_SSE4_2 7
  122. #define CV_CPU_POPCNT 8
  123. #define CV_CPU_FP16 9
  124. #define CV_CPU_AVX 10
  125. #define CV_CPU_AVX2 11
  126. #define CV_CPU_FMA3 12
  127. #define CV_CPU_AVX_512F 13
  128. #define CV_CPU_AVX_512BW 14
  129. #define CV_CPU_AVX_512CD 15
  130. #define CV_CPU_AVX_512DQ 16
  131. #define CV_CPU_AVX_512ER 17
  132. #define CV_CPU_AVX_512IFMA512 18
  133. #define CV_CPU_AVX_512PF 19
  134. #define CV_CPU_AVX_512VBMI 20
  135. #define CV_CPU_AVX_512VL 21
  136. #define CV_CPU_NEON 100
  137. #define CV_CPU_VSX 200
  138. // when adding to this list remember to update the following enum
  139. #define CV_HARDWARE_MAX_FEATURE 255
  140. /** @brief Available CPU features.
  141. */
  142. enum CpuFeatures {
  143. CPU_MMX = 1,
  144. CPU_SSE = 2,
  145. CPU_SSE2 = 3,
  146. CPU_SSE3 = 4,
  147. CPU_SSSE3 = 5,
  148. CPU_SSE4_1 = 6,
  149. CPU_SSE4_2 = 7,
  150. CPU_POPCNT = 8,
  151. CPU_FP16 = 9,
  152. CPU_AVX = 10,
  153. CPU_AVX2 = 11,
  154. CPU_FMA3 = 12,
  155. CPU_AVX_512F = 13,
  156. CPU_AVX_512BW = 14,
  157. CPU_AVX_512CD = 15,
  158. CPU_AVX_512DQ = 16,
  159. CPU_AVX_512ER = 17,
  160. CPU_AVX_512IFMA512 = 18,
  161. CPU_AVX_512PF = 19,
  162. CPU_AVX_512VBMI = 20,
  163. CPU_AVX_512VL = 21,
  164. CPU_NEON = 100,
  165. CPU_VSX = 200
  166. };
  167. #include "cv_cpu_dispatch.h"
  168. /* fundamental constants */
  169. #define CV_PI 3.1415926535897932384626433832795
  170. #define CV_2PI 6.283185307179586476925286766559
  171. #define CV_LOG2 0.69314718055994530941723212145818
  172. #if defined __ARM_FP16_FORMAT_IEEE \
  173. && !defined __CUDACC__
  174. # define CV_FP16_TYPE 1
  175. #else
  176. # define CV_FP16_TYPE 0
  177. #endif
  178. typedef union Cv16suf
  179. {
  180. short i;
  181. #if CV_FP16_TYPE
  182. __fp16 h;
  183. #endif
  184. struct _fp16Format
  185. {
  186. unsigned int significand : 10;
  187. unsigned int exponent : 5;
  188. unsigned int sign : 1;
  189. } fmt;
  190. }
  191. Cv16suf;
  192. typedef union Cv32suf
  193. {
  194. int i;
  195. unsigned u;
  196. float f;
  197. struct _fp32Format
  198. {
  199. unsigned int significand : 23;
  200. unsigned int exponent : 8;
  201. unsigned int sign : 1;
  202. } fmt;
  203. }
  204. Cv32suf;
  205. typedef union Cv64suf
  206. {
  207. int64 i;
  208. uint64 u;
  209. double f;
  210. }
  211. Cv64suf;
  212. #define OPENCV_ABI_COMPATIBILITY 300
  213. #ifdef __OPENCV_BUILD
  214. # define DISABLE_OPENCV_24_COMPATIBILITY
  215. #endif
  216. #if (defined _WIN32 || defined WINCE || defined __CYGWIN__) && defined CVAPI_EXPORTS
  217. # define CV_EXPORTS __declspec(dllexport)
  218. #elif defined __GNUC__ && __GNUC__ >= 4
  219. # define CV_EXPORTS __attribute__ ((visibility ("default")))
  220. #else
  221. # define CV_EXPORTS
  222. #endif
  223. #ifndef CV_DEPRECATED
  224. # if defined(__GNUC__)
  225. # define CV_DEPRECATED __attribute__ ((deprecated))
  226. # elif defined(_MSC_VER)
  227. # define CV_DEPRECATED __declspec(deprecated)
  228. # else
  229. # define CV_DEPRECATED
  230. # endif
  231. #endif
  232. #ifndef CV_EXTERN_C
  233. # ifdef __cplusplus
  234. # define CV_EXTERN_C extern "C"
  235. # else
  236. # define CV_EXTERN_C
  237. # endif
  238. #endif
  239. /* special informative macros for wrapper generators */
  240. #define CV_EXPORTS_W CV_EXPORTS
  241. #define CV_EXPORTS_W_SIMPLE CV_EXPORTS
  242. #define CV_EXPORTS_AS(synonym) CV_EXPORTS
  243. #define CV_EXPORTS_W_MAP CV_EXPORTS
  244. #define CV_IN_OUT
  245. #define CV_OUT
  246. #define CV_PROP
  247. #define CV_PROP_RW
  248. #define CV_WRAP
  249. #define CV_WRAP_AS(synonym)
  250. /****************************************************************************************\
  251. * Matrix type (Mat) *
  252. \****************************************************************************************/
  253. #define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
  254. #define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
  255. #define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
  256. #define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
  257. #define CV_MAT_CONT_FLAG_SHIFT 14
  258. #define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
  259. #define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
  260. #define CV_IS_CONT_MAT CV_IS_MAT_CONT
  261. #define CV_SUBMAT_FLAG_SHIFT 15
  262. #define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
  263. #define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
  264. /** Size of each channel item,
  265. 0x8442211 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
  266. #define CV_ELEM_SIZE1(type) \
  267. ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15)
  268. /** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
  269. #define CV_ELEM_SIZE(type) \
  270. (CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3))
  271. #ifndef MIN
  272. # define MIN(a,b) ((a) > (b) ? (b) : (a))
  273. #endif
  274. #ifndef MAX
  275. # define MAX(a,b) ((a) < (b) ? (b) : (a))
  276. #endif
  277. /****************************************************************************************\
  278. * static analysys *
  279. \****************************************************************************************/
  280. // In practice, some macro are not processed correctly (noreturn is not detected).
  281. // We need to use simplified definition for them.
  282. #ifndef CV_STATIC_ANALYSIS
  283. # if defined(__KLOCWORK__) || defined(__clang_analyzer__) || defined(__COVERITY__)
  284. # define CV_STATIC_ANALYSIS
  285. # endif
  286. #endif
  287. /****************************************************************************************\
  288. * Thread sanitizer *
  289. \****************************************************************************************/
  290. #ifndef CV_THREAD_SANITIZER
  291. # if defined(__has_feature)
  292. # if __has_feature(thread_sanitizer)
  293. # define CV_THREAD_SANITIZER
  294. # endif
  295. # endif
  296. #endif
  297. /****************************************************************************************\
  298. * exchange-add operation for atomic operations on reference counters *
  299. \****************************************************************************************/
  300. #ifdef CV_XADD
  301. // allow to use user-defined macro
  302. #elif defined __GNUC__ || defined __clang__
  303. # if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__)
  304. # ifdef __ATOMIC_ACQ_REL
  305. # define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
  306. # else
  307. # define CV_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
  308. # endif
  309. # else
  310. # if defined __ATOMIC_ACQ_REL && !defined __clang__
  311. // version for gcc >= 4.7
  312. # define CV_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
  313. # else
  314. # define CV_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
  315. # endif
  316. # endif
  317. #elif defined _MSC_VER && !defined RC_INVOKED
  318. # include <intrin.h>
  319. # define CV_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
  320. #else
  321. CV_INLINE CV_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; }
  322. #endif
  323. /****************************************************************************************\
  324. * CV_NORETURN attribute *
  325. \****************************************************************************************/
  326. #ifndef CV_NORETURN
  327. # if defined(__GNUC__)
  328. # define CV_NORETURN __attribute__((__noreturn__))
  329. # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
  330. # define CV_NORETURN __declspec(noreturn)
  331. # else
  332. # define CV_NORETURN /* nothing by default */
  333. # endif
  334. #endif
  335. /****************************************************************************************\
  336. * C++ 11 *
  337. \****************************************************************************************/
  338. #ifndef CV_CXX11
  339. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
  340. # define CV_CXX11 1
  341. # endif
  342. #else
  343. # if CV_CXX11 == 0
  344. # undef CV_CXX11
  345. # endif
  346. #endif
  347. /****************************************************************************************\
  348. * C++ Move semantics *
  349. \****************************************************************************************/
  350. #ifndef CV_CXX_MOVE_SEMANTICS
  351. # if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(_MSC_VER) && _MSC_VER >= 1600)
  352. # define CV_CXX_MOVE_SEMANTICS 1
  353. # elif defined(__clang)
  354. # if __has_feature(cxx_rvalue_references)
  355. # define CV_CXX_MOVE_SEMANTICS 1
  356. # endif
  357. # endif
  358. #else
  359. # if CV_CXX_MOVE_SEMANTICS == 0
  360. # undef CV_CXX_MOVE_SEMANTICS
  361. # endif
  362. #endif
  363. /****************************************************************************************\
  364. * C++11 std::array *
  365. \****************************************************************************************/
  366. #ifndef CV_CXX_STD_ARRAY
  367. # if __cplusplus >= 201103L
  368. # define CV_CXX_STD_ARRAY 1
  369. # include <array>
  370. # endif
  371. #else
  372. # if CV_CXX_STD_ARRAY == 0
  373. # undef CV_CXX_STD_ARRAY
  374. # endif
  375. #endif
  376. //! @}
  377. #endif // OPENCV_CORE_CVDEF_H