cvdef.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 // deprecated
  133. #define CV_CPU_AVX_512IFMA 18
  134. #define CV_CPU_AVX_512PF 19
  135. #define CV_CPU_AVX_512VBMI 20
  136. #define CV_CPU_AVX_512VL 21
  137. #define CV_CPU_NEON 100
  138. #define CV_CPU_VSX 200
  139. // CPU features groups
  140. #define CV_CPU_AVX512_SKX 256
  141. // when adding to this list remember to update the following enum
  142. #define CV_HARDWARE_MAX_FEATURE 512
  143. /** @brief Available CPU features.
  144. */
  145. enum CpuFeatures {
  146. CPU_MMX = 1,
  147. CPU_SSE = 2,
  148. CPU_SSE2 = 3,
  149. CPU_SSE3 = 4,
  150. CPU_SSSE3 = 5,
  151. CPU_SSE4_1 = 6,
  152. CPU_SSE4_2 = 7,
  153. CPU_POPCNT = 8,
  154. CPU_FP16 = 9,
  155. CPU_AVX = 10,
  156. CPU_AVX2 = 11,
  157. CPU_FMA3 = 12,
  158. CPU_AVX_512F = 13,
  159. CPU_AVX_512BW = 14,
  160. CPU_AVX_512CD = 15,
  161. CPU_AVX_512DQ = 16,
  162. CPU_AVX_512ER = 17,
  163. CPU_AVX_512IFMA512 = 18, // deprecated
  164. CPU_AVX_512IFMA = 18,
  165. CPU_AVX_512PF = 19,
  166. CPU_AVX_512VBMI = 20,
  167. CPU_AVX_512VL = 21,
  168. CPU_NEON = 100,
  169. CPU_VSX = 200,
  170. CPU_AVX512_SKX = 256, //!< Skylake-X with AVX-512F/CD/BW/DQ/VL
  171. CPU_MAX_FEATURE = 512 // see CV_HARDWARE_MAX_FEATURE
  172. };
  173. #include "cv_cpu_dispatch.h"
  174. /* fundamental constants */
  175. #define CV_PI 3.1415926535897932384626433832795
  176. #define CV_2PI 6.283185307179586476925286766559
  177. #define CV_LOG2 0.69314718055994530941723212145818
  178. #if defined __ARM_FP16_FORMAT_IEEE \
  179. && !defined __CUDACC__
  180. # define CV_FP16_TYPE 1
  181. #else
  182. # define CV_FP16_TYPE 0
  183. #endif
  184. typedef union Cv16suf
  185. {
  186. short i;
  187. #if CV_FP16_TYPE
  188. __fp16 h;
  189. #endif
  190. struct _fp16Format
  191. {
  192. unsigned int significand : 10;
  193. unsigned int exponent : 5;
  194. unsigned int sign : 1;
  195. } fmt;
  196. }
  197. Cv16suf;
  198. typedef union Cv32suf
  199. {
  200. int i;
  201. unsigned u;
  202. float f;
  203. struct _fp32Format
  204. {
  205. unsigned int significand : 23;
  206. unsigned int exponent : 8;
  207. unsigned int sign : 1;
  208. } fmt;
  209. }
  210. Cv32suf;
  211. typedef union Cv64suf
  212. {
  213. int64 i;
  214. uint64 u;
  215. double f;
  216. }
  217. Cv64suf;
  218. #define OPENCV_ABI_COMPATIBILITY 300
  219. #ifdef __OPENCV_BUILD
  220. # define DISABLE_OPENCV_24_COMPATIBILITY
  221. #endif
  222. #ifdef CVAPI_EXPORTS
  223. # if (defined _WIN32 || defined WINCE || defined __CYGWIN__)
  224. # define CV_EXPORTS __declspec(dllexport)
  225. # elif defined __GNUC__ && __GNUC__ >= 4
  226. # define CV_EXPORTS __attribute__ ((visibility ("default")))
  227. # endif
  228. #endif
  229. #ifndef CV_EXPORTS
  230. # define CV_EXPORTS
  231. #endif
  232. #ifdef _MSC_VER
  233. # define CV_EXPORTS_TEMPLATE
  234. #else
  235. # define CV_EXPORTS_TEMPLATE CV_EXPORTS
  236. #endif
  237. #ifndef CV_DEPRECATED
  238. # if defined(__GNUC__)
  239. # define CV_DEPRECATED __attribute__ ((deprecated))
  240. # elif defined(_MSC_VER)
  241. # define CV_DEPRECATED __declspec(deprecated)
  242. # else
  243. # define CV_DEPRECATED
  244. # endif
  245. #endif
  246. #ifndef CV_EXTERN_C
  247. # ifdef __cplusplus
  248. # define CV_EXTERN_C extern "C"
  249. # else
  250. # define CV_EXTERN_C
  251. # endif
  252. #endif
  253. /* special informative macros for wrapper generators */
  254. #define CV_EXPORTS_W CV_EXPORTS
  255. #define CV_EXPORTS_W_SIMPLE CV_EXPORTS
  256. #define CV_EXPORTS_AS(synonym) CV_EXPORTS
  257. #define CV_EXPORTS_W_MAP CV_EXPORTS
  258. #define CV_IN_OUT
  259. #define CV_OUT
  260. #define CV_PROP
  261. #define CV_PROP_RW
  262. #define CV_WRAP
  263. #define CV_WRAP_AS(synonym)
  264. /****************************************************************************************\
  265. * Matrix type (Mat) *
  266. \****************************************************************************************/
  267. #define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
  268. #define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
  269. #define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
  270. #define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
  271. #define CV_MAT_CONT_FLAG_SHIFT 14
  272. #define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
  273. #define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
  274. #define CV_IS_CONT_MAT CV_IS_MAT_CONT
  275. #define CV_SUBMAT_FLAG_SHIFT 15
  276. #define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
  277. #define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
  278. /** Size of each channel item,
  279. 0x8442211 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
  280. #define CV_ELEM_SIZE1(type) \
  281. ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15)
  282. /** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
  283. #define CV_ELEM_SIZE(type) \
  284. (CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3))
  285. #ifndef MIN
  286. # define MIN(a,b) ((a) > (b) ? (b) : (a))
  287. #endif
  288. #ifndef MAX
  289. # define MAX(a,b) ((a) < (b) ? (b) : (a))
  290. #endif
  291. /****************************************************************************************\
  292. * static analysys *
  293. \****************************************************************************************/
  294. // In practice, some macro are not processed correctly (noreturn is not detected).
  295. // We need to use simplified definition for them.
  296. #ifndef CV_STATIC_ANALYSIS
  297. # if defined(__KLOCWORK__) || defined(__clang_analyzer__) || defined(__COVERITY__)
  298. # define CV_STATIC_ANALYSIS
  299. # endif
  300. #endif
  301. /****************************************************************************************\
  302. * Thread sanitizer *
  303. \****************************************************************************************/
  304. #ifndef CV_THREAD_SANITIZER
  305. # if defined(__has_feature)
  306. # if __has_feature(thread_sanitizer)
  307. # define CV_THREAD_SANITIZER
  308. # endif
  309. # endif
  310. #endif
  311. /****************************************************************************************\
  312. * exchange-add operation for atomic operations on reference counters *
  313. \****************************************************************************************/
  314. #ifdef CV_XADD
  315. // allow to use user-defined macro
  316. #elif defined __GNUC__ || defined __clang__
  317. # if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__)
  318. # ifdef __ATOMIC_ACQ_REL
  319. # define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
  320. # else
  321. # define CV_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
  322. # endif
  323. # else
  324. # if defined __ATOMIC_ACQ_REL && !defined __clang__
  325. // version for gcc >= 4.7
  326. # define CV_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
  327. # else
  328. # define CV_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
  329. # endif
  330. # endif
  331. #elif defined _MSC_VER && !defined RC_INVOKED
  332. # include <intrin.h>
  333. # define CV_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
  334. #else
  335. CV_INLINE CV_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; }
  336. #endif
  337. /****************************************************************************************\
  338. * CV_NORETURN attribute *
  339. \****************************************************************************************/
  340. #ifndef CV_NORETURN
  341. # if defined(__GNUC__)
  342. # define CV_NORETURN __attribute__((__noreturn__))
  343. # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
  344. # define CV_NORETURN __declspec(noreturn)
  345. # else
  346. # define CV_NORETURN /* nothing by default */
  347. # endif
  348. #endif
  349. /****************************************************************************************\
  350. * C++ 11 *
  351. \****************************************************************************************/
  352. #ifndef CV_CXX11
  353. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
  354. # define CV_CXX11 1
  355. # endif
  356. #else
  357. # if CV_CXX11 == 0
  358. # undef CV_CXX11
  359. # endif
  360. #endif
  361. /****************************************************************************************\
  362. * C++ Move semantics *
  363. \****************************************************************************************/
  364. #ifndef CV_CXX_MOVE_SEMANTICS
  365. # if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(_MSC_VER) && _MSC_VER >= 1600)
  366. # define CV_CXX_MOVE_SEMANTICS 1
  367. # elif defined(__clang)
  368. # if __has_feature(cxx_rvalue_references)
  369. # define CV_CXX_MOVE_SEMANTICS 1
  370. # endif
  371. # endif
  372. #else
  373. # if CV_CXX_MOVE_SEMANTICS == 0
  374. # undef CV_CXX_MOVE_SEMANTICS
  375. # endif
  376. #endif
  377. /****************************************************************************************\
  378. * C++11 std::array *
  379. \****************************************************************************************/
  380. #ifndef CV_CXX_STD_ARRAY
  381. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  382. # define CV_CXX_STD_ARRAY 1
  383. # include <array>
  384. # endif
  385. #else
  386. # if CV_CXX_STD_ARRAY == 0
  387. # undef CV_CXX_STD_ARRAY
  388. # endif
  389. #endif
  390. /****************************************************************************************\
  391. * C++11 override / final *
  392. \****************************************************************************************/
  393. #ifndef CV_OVERRIDE
  394. # ifdef CV_CXX11
  395. # define CV_OVERRIDE override
  396. # endif
  397. #endif
  398. #ifndef CV_OVERRIDE
  399. # define CV_OVERRIDE
  400. #endif
  401. #ifndef CV_FINAL
  402. # ifdef CV_CXX11
  403. # define CV_FINAL final
  404. # endif
  405. #endif
  406. #ifndef CV_FINAL
  407. # define CV_FINAL
  408. #endif
  409. // Integer types portatibility
  410. #ifdef OPENCV_STDINT_HEADER
  411. #include OPENCV_STDINT_HEADER
  412. #elif defined(__cplusplus)
  413. #if defined(_MSC_VER) && _MSC_VER < 1600 /* MSVS 2010 */
  414. namespace cv {
  415. typedef signed char int8_t;
  416. typedef unsigned char uint8_t;
  417. typedef signed short int16_t;
  418. typedef unsigned short uint16_t;
  419. typedef signed int int32_t;
  420. typedef unsigned int uint32_t;
  421. typedef signed __int64 int64_t;
  422. typedef unsigned __int64 uint64_t;
  423. }
  424. #elif defined(_MSC_VER) || __cplusplus >= 201103L
  425. #include <cstdint>
  426. namespace cv {
  427. using std::int8_t;
  428. using std::uint8_t;
  429. using std::int16_t;
  430. using std::uint16_t;
  431. using std::int32_t;
  432. using std::uint32_t;
  433. using std::int64_t;
  434. using std::uint64_t;
  435. }
  436. #else
  437. #include <stdint.h>
  438. namespace cv {
  439. typedef ::int8_t int8_t;
  440. typedef ::uint8_t uint8_t;
  441. typedef ::int16_t int16_t;
  442. typedef ::uint16_t uint16_t;
  443. typedef ::int32_t int32_t;
  444. typedef ::uint32_t uint32_t;
  445. typedef ::int64_t int64_t;
  446. typedef ::uint64_t uint64_t;
  447. }
  448. #endif
  449. #else // pure C
  450. #include <stdint.h>
  451. #endif
  452. //! @}
  453. #ifndef __cplusplus
  454. #include "opencv2/core/fast_math.hpp" // define cvRound(double)
  455. #endif
  456. #endif // OPENCV_CORE_CVDEF_H