cvdef.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. #include "opencv2/core/version.hpp"
  47. //! @addtogroup core_utils
  48. //! @{
  49. #ifdef OPENCV_INCLUDE_PORT_FILE // User-provided header file with custom platform configuration
  50. #include OPENCV_INCLUDE_PORT_FILE
  51. #endif
  52. #if !defined CV_DOXYGEN && !defined CV_IGNORE_DEBUG_BUILD_GUARD
  53. #if (defined(_MSC_VER) && (defined(DEBUG) || defined(_DEBUG))) || \
  54. (defined(_GLIBCXX_DEBUG) || defined(_GLIBCXX_DEBUG_PEDANTIC))
  55. // Guard to prevent using of binary incompatible binaries / runtimes
  56. // https://github.com/opencv/opencv/pull/9161
  57. #define CV__DEBUG_NS_BEGIN namespace debug_build_guard {
  58. #define CV__DEBUG_NS_END }
  59. namespace cv { namespace debug_build_guard { } using namespace debug_build_guard; }
  60. #endif
  61. #endif
  62. #ifndef CV__DEBUG_NS_BEGIN
  63. #define CV__DEBUG_NS_BEGIN
  64. #define CV__DEBUG_NS_END
  65. #endif
  66. #ifdef __OPENCV_BUILD
  67. #include "cvconfig.h"
  68. #endif
  69. #ifndef __CV_EXPAND
  70. #define __CV_EXPAND(x) x
  71. #endif
  72. #ifndef __CV_CAT
  73. #define __CV_CAT__(x, y) x ## y
  74. #define __CV_CAT_(x, y) __CV_CAT__(x, y)
  75. #define __CV_CAT(x, y) __CV_CAT_(x, y)
  76. #endif
  77. #define __CV_VA_NUM_ARGS_HELPER(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
  78. #define __CV_VA_NUM_ARGS(...) __CV_EXPAND(__CV_VA_NUM_ARGS_HELPER(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
  79. #ifdef CV_Func
  80. // keep current value (through OpenCV port file)
  81. #elif defined __GNUC__ || (defined (__cpluscplus) && (__cpluscplus >= 201103))
  82. #define CV_Func __func__
  83. #elif defined __clang__ && (__clang_minor__ * 100 + __clang_major__ >= 305)
  84. #define CV_Func __func__
  85. #elif defined(__STDC_VERSION__) && (__STDC_VERSION >= 199901)
  86. #define CV_Func __func__
  87. #elif defined _MSC_VER
  88. #define CV_Func __FUNCTION__
  89. #elif defined(__INTEL_COMPILER) && (_INTEL_COMPILER >= 600)
  90. #define CV_Func __FUNCTION__
  91. #elif defined __IBMCPP__ && __IBMCPP__ >=500
  92. #define CV_Func __FUNCTION__
  93. #elif defined __BORLAND__ && (__BORLANDC__ >= 0x550)
  94. #define CV_Func __FUNC__
  95. #else
  96. #define CV_Func "<unknown>"
  97. #endif
  98. //! @cond IGNORED
  99. //////////////// static assert /////////////////
  100. #define CVAUX_CONCAT_EXP(a, b) a##b
  101. #define CVAUX_CONCAT(a, b) CVAUX_CONCAT_EXP(a,b)
  102. #if defined(__clang__)
  103. # ifndef __has_extension
  104. # define __has_extension __has_feature /* compatibility, for older versions of clang */
  105. # endif
  106. # if __has_extension(cxx_static_assert)
  107. # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
  108. # elif __has_extension(c_static_assert)
  109. # define CV_StaticAssert(condition, reason) _Static_assert((condition), reason " " #condition)
  110. # endif
  111. #elif defined(__GNUC__)
  112. # if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L)
  113. # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
  114. # endif
  115. #elif defined(_MSC_VER)
  116. # if _MSC_VER >= 1600 /* MSVC 10 */
  117. # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
  118. # endif
  119. #endif
  120. #ifndef CV_StaticAssert
  121. # if !defined(__clang__) && defined(__GNUC__) && (__GNUC__*100 + __GNUC_MINOR__ > 302)
  122. # define CV_StaticAssert(condition, reason) ({ extern int __attribute__((error("CV_StaticAssert: " reason " " #condition))) CV_StaticAssert(); ((condition) ? 0 : CV_StaticAssert()); })
  123. # else
  124. namespace cv {
  125. template <bool x> struct CV_StaticAssert_failed;
  126. template <> struct CV_StaticAssert_failed<true> { enum { val = 1 }; };
  127. template<int x> struct CV_StaticAssert_test {};
  128. }
  129. # define CV_StaticAssert(condition, reason)\
  130. typedef cv::CV_StaticAssert_test< sizeof(cv::CV_StaticAssert_failed< static_cast<bool>(condition) >) > CVAUX_CONCAT(CV_StaticAssert_failed_at_, __LINE__)
  131. # endif
  132. #endif
  133. // Suppress warning "-Wdeprecated-declarations" / C4996
  134. #if defined(_MSC_VER)
  135. #define CV_DO_PRAGMA(x) __pragma(x)
  136. #elif defined(__GNUC__)
  137. #define CV_DO_PRAGMA(x) _Pragma (#x)
  138. #else
  139. #define CV_DO_PRAGMA(x)
  140. #endif
  141. #ifdef _MSC_VER
  142. #define CV_SUPPRESS_DEPRECATED_START \
  143. CV_DO_PRAGMA(warning(push)) \
  144. CV_DO_PRAGMA(warning(disable: 4996))
  145. #define CV_SUPPRESS_DEPRECATED_END CV_DO_PRAGMA(warning(pop))
  146. #elif defined (__clang__) || ((__GNUC__) && (__GNUC__*100 + __GNUC_MINOR__ > 405))
  147. #define CV_SUPPRESS_DEPRECATED_START \
  148. CV_DO_PRAGMA(GCC diagnostic push) \
  149. CV_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
  150. #define CV_SUPPRESS_DEPRECATED_END CV_DO_PRAGMA(GCC diagnostic pop)
  151. #else
  152. #define CV_SUPPRESS_DEPRECATED_START
  153. #define CV_SUPPRESS_DEPRECATED_END
  154. #endif
  155. #define CV_UNUSED(name) (void)name
  156. //! @endcond
  157. // undef problematic defines sometimes defined by system headers (windows.h in particular)
  158. #undef small
  159. #undef min
  160. #undef max
  161. #undef abs
  162. #undef Complex
  163. #if defined __cplusplus
  164. #include <limits>
  165. #else
  166. #include <limits.h>
  167. #endif
  168. #include "opencv2/core/hal/interface.h"
  169. #if defined __ICL
  170. # define CV_ICC __ICL
  171. #elif defined __ICC
  172. # define CV_ICC __ICC
  173. #elif defined __ECL
  174. # define CV_ICC __ECL
  175. #elif defined __ECC
  176. # define CV_ICC __ECC
  177. #elif defined __INTEL_COMPILER
  178. # define CV_ICC __INTEL_COMPILER
  179. #endif
  180. #if defined _WIN32
  181. # define CV_CDECL __cdecl
  182. # define CV_STDCALL __stdcall
  183. #else
  184. # define CV_CDECL
  185. # define CV_STDCALL
  186. #endif
  187. #ifndef CV_INLINE
  188. # if defined __cplusplus
  189. # define CV_INLINE static inline
  190. # elif defined _MSC_VER
  191. # define CV_INLINE __inline
  192. # else
  193. # define CV_INLINE static
  194. # endif
  195. #endif
  196. #ifndef CV_ALWAYS_INLINE
  197. #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
  198. #define CV_ALWAYS_INLINE inline __attribute__((always_inline))
  199. #elif defined(_MSC_VER)
  200. #define CV_ALWAYS_INLINE __forceinline
  201. #else
  202. #define CV_ALWAYS_INLINE inline
  203. #endif
  204. #endif
  205. #if defined CV_DISABLE_OPTIMIZATION || (defined CV_ICC && !defined CV_ENABLE_UNROLLED)
  206. # define CV_ENABLE_UNROLLED 0
  207. #else
  208. # define CV_ENABLE_UNROLLED 1
  209. #endif
  210. #ifdef __GNUC__
  211. # define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
  212. #elif defined _MSC_VER
  213. # define CV_DECL_ALIGNED(x) __declspec(align(x))
  214. #else
  215. # define CV_DECL_ALIGNED(x)
  216. #endif
  217. /* CPU features and intrinsics support */
  218. #define CV_CPU_NONE 0
  219. #define CV_CPU_MMX 1
  220. #define CV_CPU_SSE 2
  221. #define CV_CPU_SSE2 3
  222. #define CV_CPU_SSE3 4
  223. #define CV_CPU_SSSE3 5
  224. #define CV_CPU_SSE4_1 6
  225. #define CV_CPU_SSE4_2 7
  226. #define CV_CPU_POPCNT 8
  227. #define CV_CPU_FP16 9
  228. #define CV_CPU_AVX 10
  229. #define CV_CPU_AVX2 11
  230. #define CV_CPU_FMA3 12
  231. #define CV_CPU_AVX_512F 13
  232. #define CV_CPU_AVX_512BW 14
  233. #define CV_CPU_AVX_512CD 15
  234. #define CV_CPU_AVX_512DQ 16
  235. #define CV_CPU_AVX_512ER 17
  236. #define CV_CPU_AVX_512IFMA512 18 // deprecated
  237. #define CV_CPU_AVX_512IFMA 18
  238. #define CV_CPU_AVX_512PF 19
  239. #define CV_CPU_AVX_512VBMI 20
  240. #define CV_CPU_AVX_512VL 21
  241. #define CV_CPU_AVX_512VBMI2 22
  242. #define CV_CPU_AVX_512VNNI 23
  243. #define CV_CPU_AVX_512BITALG 24
  244. #define CV_CPU_AVX_512VPOPCNTDQ 25
  245. #define CV_CPU_AVX_5124VNNIW 26
  246. #define CV_CPU_AVX_5124FMAPS 27
  247. #define CV_CPU_NEON 100
  248. #define CV_CPU_NEON_DOTPROD 101
  249. #define CV_CPU_NEON_FP16 102
  250. #define CV_CPU_NEON_BF16 103
  251. #define CV_CPU_MSA 150
  252. #define CV_CPU_RISCVV 170
  253. #define CV_CPU_VSX 200
  254. #define CV_CPU_VSX3 201
  255. #define CV_CPU_RVV 210
  256. #define CV_CPU_LSX 230
  257. #define CV_CPU_LASX 231
  258. // CPU features groups
  259. #define CV_CPU_AVX512_SKX 256
  260. #define CV_CPU_AVX512_COMMON 257
  261. #define CV_CPU_AVX512_KNL 258
  262. #define CV_CPU_AVX512_KNM 259
  263. #define CV_CPU_AVX512_CNL 260
  264. #define CV_CPU_AVX512_CLX 261
  265. #define CV_CPU_AVX512_ICL 262
  266. // when adding to this list remember to update the following enum
  267. #define CV_HARDWARE_MAX_FEATURE 512
  268. /** @brief Available CPU features.
  269. */
  270. enum CpuFeatures {
  271. CPU_MMX = 1,
  272. CPU_SSE = 2,
  273. CPU_SSE2 = 3,
  274. CPU_SSE3 = 4,
  275. CPU_SSSE3 = 5,
  276. CPU_SSE4_1 = 6,
  277. CPU_SSE4_2 = 7,
  278. CPU_POPCNT = 8,
  279. CPU_FP16 = 9,
  280. CPU_AVX = 10,
  281. CPU_AVX2 = 11,
  282. CPU_FMA3 = 12,
  283. CPU_AVX_512F = 13,
  284. CPU_AVX_512BW = 14,
  285. CPU_AVX_512CD = 15,
  286. CPU_AVX_512DQ = 16,
  287. CPU_AVX_512ER = 17,
  288. CPU_AVX_512IFMA512 = 18, // deprecated
  289. CPU_AVX_512IFMA = 18,
  290. CPU_AVX_512PF = 19,
  291. CPU_AVX_512VBMI = 20,
  292. CPU_AVX_512VL = 21,
  293. CPU_AVX_512VBMI2 = 22,
  294. CPU_AVX_512VNNI = 23,
  295. CPU_AVX_512BITALG = 24,
  296. CPU_AVX_512VPOPCNTDQ= 25,
  297. CPU_AVX_5124VNNIW = 26,
  298. CPU_AVX_5124FMAPS = 27,
  299. CPU_NEON = 100,
  300. CPU_NEON_DOTPROD = 101,
  301. CPU_NEON_FP16 = 102,
  302. CPU_NEON_BF16 = 103,
  303. CPU_MSA = 150,
  304. CPU_RISCVV = 170,
  305. CPU_VSX = 200,
  306. CPU_VSX3 = 201,
  307. CPU_RVV = 210,
  308. CPU_LSX = 230,
  309. CPU_LASX = 231,
  310. CPU_AVX512_SKX = 256, //!< Skylake-X with AVX-512F/CD/BW/DQ/VL
  311. CPU_AVX512_COMMON = 257, //!< Common instructions AVX-512F/CD for all CPUs that support AVX-512
  312. CPU_AVX512_KNL = 258, //!< Knights Landing with AVX-512F/CD/ER/PF
  313. CPU_AVX512_KNM = 259, //!< Knights Mill with AVX-512F/CD/ER/PF/4FMAPS/4VNNIW/VPOPCNTDQ
  314. CPU_AVX512_CNL = 260, //!< Cannon Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI
  315. CPU_AVX512_CLX = 261, //!< Cascade Lake with AVX-512F/CD/BW/DQ/VL/VNNI
  316. CPU_AVX512_ICL = 262, //!< Ice Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI/VNNI/VBMI2/BITALG/VPOPCNTDQ
  317. CPU_MAX_FEATURE = 512 // see CV_HARDWARE_MAX_FEATURE
  318. };
  319. #include "cv_cpu_dispatch.h"
  320. #if !defined(CV_STRONG_ALIGNMENT) && defined(__arm__) && !(defined(__aarch64__) || defined(_M_ARM64))
  321. // int*, int64* should be propertly aligned pointers on ARMv7
  322. #define CV_STRONG_ALIGNMENT 1
  323. #endif
  324. #if !defined(CV_STRONG_ALIGNMENT)
  325. #define CV_STRONG_ALIGNMENT 0
  326. #endif
  327. /* fundamental constants */
  328. #define CV_PI 3.1415926535897932384626433832795
  329. #define CV_2PI 6.283185307179586476925286766559
  330. #define CV_LOG2 0.69314718055994530941723212145818
  331. #if defined __ARM_FP16_FORMAT_IEEE \
  332. && !defined __CUDACC__
  333. # define CV_FP16_TYPE 1
  334. #else
  335. # define CV_FP16_TYPE 0
  336. #endif
  337. typedef union Cv16suf
  338. {
  339. short i;
  340. ushort u;
  341. #if CV_FP16_TYPE
  342. __fp16 h;
  343. #endif
  344. }
  345. Cv16suf;
  346. typedef union Cv32suf
  347. {
  348. int i;
  349. unsigned u;
  350. float f;
  351. }
  352. Cv32suf;
  353. typedef union Cv64suf
  354. {
  355. int64 i;
  356. uint64 u;
  357. double f;
  358. }
  359. Cv64suf;
  360. #ifndef OPENCV_ABI_COMPATIBILITY
  361. #define OPENCV_ABI_COMPATIBILITY 400
  362. #endif
  363. #ifdef __OPENCV_BUILD
  364. # define DISABLE_OPENCV_3_COMPATIBILITY
  365. # define OPENCV_DISABLE_DEPRECATED_COMPATIBILITY
  366. #endif
  367. #ifndef CV_EXPORTS
  368. # if (defined _WIN32 || defined WINCE || defined __CYGWIN__) && defined(CVAPI_EXPORTS)
  369. # define CV_EXPORTS __declspec(dllexport)
  370. # elif defined __GNUC__ && __GNUC__ >= 4 && (defined(CVAPI_EXPORTS) || defined(__APPLE__))
  371. # define CV_EXPORTS __attribute__ ((visibility ("default")))
  372. # endif
  373. #endif
  374. #ifndef CV_EXPORTS
  375. # define CV_EXPORTS
  376. #endif
  377. #ifdef _MSC_VER
  378. # define CV_EXPORTS_TEMPLATE
  379. #else
  380. # define CV_EXPORTS_TEMPLATE CV_EXPORTS
  381. #endif
  382. #ifndef CV_DEPRECATED
  383. # if defined(__GNUC__)
  384. # define CV_DEPRECATED __attribute__ ((deprecated))
  385. # elif defined(_MSC_VER)
  386. # define CV_DEPRECATED __declspec(deprecated)
  387. # else
  388. # define CV_DEPRECATED
  389. # endif
  390. #endif
  391. #ifndef CV_DEPRECATED_EXTERNAL
  392. # if defined(__OPENCV_BUILD)
  393. # define CV_DEPRECATED_EXTERNAL /* nothing */
  394. # else
  395. # define CV_DEPRECATED_EXTERNAL CV_DEPRECATED
  396. # endif
  397. #endif
  398. #ifndef CV_EXTERN_C
  399. # ifdef __cplusplus
  400. # define CV_EXTERN_C extern "C"
  401. # else
  402. # define CV_EXTERN_C
  403. # endif
  404. #endif
  405. /* special informative macros for wrapper generators */
  406. #define CV_EXPORTS_W CV_EXPORTS
  407. #define CV_EXPORTS_W_SIMPLE CV_EXPORTS
  408. #define CV_EXPORTS_AS(synonym) CV_EXPORTS
  409. #define CV_EXPORTS_W_MAP CV_EXPORTS
  410. #define CV_EXPORTS_W_PARAMS CV_EXPORTS
  411. #define CV_IN_OUT
  412. #define CV_OUT
  413. #define CV_PROP
  414. #define CV_PROP_RW
  415. #define CV_WRAP
  416. #define CV_WRAP_AS(synonym)
  417. #define CV_WRAP_MAPPABLE(mappable)
  418. #define CV_WRAP_PHANTOM(phantom_header)
  419. #define CV_WRAP_DEFAULT(val)
  420. /* Indicates that the function parameter has filesystem path semantic */
  421. #define CV_WRAP_FILE_PATH
  422. /****************************************************************************************\
  423. * Matrix type (Mat) *
  424. \****************************************************************************************/
  425. #define CV_MAX_DIM 32
  426. #define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
  427. #define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
  428. #define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
  429. #define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
  430. #define CV_MAT_CONT_FLAG_SHIFT 14
  431. #define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
  432. #define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
  433. #define CV_IS_CONT_MAT CV_IS_MAT_CONT
  434. #define CV_SUBMAT_FLAG_SHIFT 15
  435. #define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
  436. #define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
  437. /** Size of each channel item,
  438. 0x28442211 = 0010 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
  439. #define CV_ELEM_SIZE1(type) ((0x28442211 >> CV_MAT_DEPTH(type)*4) & 15)
  440. #define CV_ELEM_SIZE(type) (CV_MAT_CN(type)*CV_ELEM_SIZE1(type))
  441. #ifndef MIN
  442. # define MIN(a,b) ((a) > (b) ? (b) : (a))
  443. #endif
  444. #ifndef MAX
  445. # define MAX(a,b) ((a) < (b) ? (b) : (a))
  446. #endif
  447. /** min & max without jumps */
  448. #define CV_IMIN(a, b) ((a) ^ (((a)^(b)) & (((a) < (b)) - 1)))
  449. #define CV_IMAX(a, b) ((a) ^ (((a)^(b)) & (((a) > (b)) - 1)))
  450. #define CV_SWAP(a,b,t) ((t) = (a), (a) = (b), (b) = (t))
  451. #define CV_CMP(a,b) (((a) > (b)) - ((a) < (b)))
  452. #define CV_SIGN(a) CV_CMP((a),0)
  453. ///////////////////////////////////////// Enum operators ///////////////////////////////////////
  454. /**
  455. Provides compatibility operators for both classical and C++11 enum classes,
  456. as well as exposing the C++11 enum class members for backwards compatibility
  457. @code
  458. // Provides operators required for flag enums
  459. CV_ENUM_FLAGS(AccessFlag)
  460. // Exposes the listed members of the enum class AccessFlag to the current namespace
  461. CV_ENUM_CLASS_EXPOSE(AccessFlag, ACCESS_READ [, ACCESS_WRITE [, ...] ]);
  462. @endcode
  463. */
  464. #define __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST) \
  465. static const EnumType MEMBER_CONST = EnumType::MEMBER_CONST; \
  466. #define __CV_ENUM_CLASS_EXPOSE_2(EnumType, MEMBER_CONST, ...) \
  467. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  468. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_1(EnumType, __VA_ARGS__)); \
  469. #define __CV_ENUM_CLASS_EXPOSE_3(EnumType, MEMBER_CONST, ...) \
  470. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  471. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_2(EnumType, __VA_ARGS__)); \
  472. #define __CV_ENUM_CLASS_EXPOSE_4(EnumType, MEMBER_CONST, ...) \
  473. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  474. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_3(EnumType, __VA_ARGS__)); \
  475. #define __CV_ENUM_CLASS_EXPOSE_5(EnumType, MEMBER_CONST, ...) \
  476. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  477. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_4(EnumType, __VA_ARGS__)); \
  478. #define __CV_ENUM_CLASS_EXPOSE_6(EnumType, MEMBER_CONST, ...) \
  479. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  480. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_5(EnumType, __VA_ARGS__)); \
  481. #define __CV_ENUM_CLASS_EXPOSE_7(EnumType, MEMBER_CONST, ...) \
  482. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  483. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_6(EnumType, __VA_ARGS__)); \
  484. #define __CV_ENUM_CLASS_EXPOSE_8(EnumType, MEMBER_CONST, ...) \
  485. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  486. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_7(EnumType, __VA_ARGS__)); \
  487. #define __CV_ENUM_CLASS_EXPOSE_9(EnumType, MEMBER_CONST, ...) \
  488. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  489. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_8(EnumType, __VA_ARGS__)); \
  490. #define __CV_ENUM_FLAGS_LOGICAL_NOT(EnumType) \
  491. static inline bool operator!(const EnumType& val) \
  492. { \
  493. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  494. return !static_cast<UnderlyingType>(val); \
  495. } \
  496. #define __CV_ENUM_FLAGS_LOGICAL_NOT_EQ(Arg1Type, Arg2Type) \
  497. static inline bool operator!=(const Arg1Type& a, const Arg2Type& b) \
  498. { \
  499. return static_cast<int>(a) != static_cast<int>(b); \
  500. } \
  501. #define __CV_ENUM_FLAGS_LOGICAL_EQ(Arg1Type, Arg2Type) \
  502. static inline bool operator==(const Arg1Type& a, const Arg2Type& b) \
  503. { \
  504. return static_cast<int>(a) == static_cast<int>(b); \
  505. } \
  506. #define __CV_ENUM_FLAGS_BITWISE_NOT(EnumType) \
  507. static inline EnumType operator~(const EnumType& val) \
  508. { \
  509. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  510. return static_cast<EnumType>(~static_cast<UnderlyingType>(val)); \
  511. } \
  512. #define __CV_ENUM_FLAGS_BITWISE_OR(EnumType, Arg1Type, Arg2Type) \
  513. static inline EnumType operator|(const Arg1Type& a, const Arg2Type& b) \
  514. { \
  515. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  516. return static_cast<EnumType>(static_cast<UnderlyingType>(a) | static_cast<UnderlyingType>(b)); \
  517. } \
  518. #define __CV_ENUM_FLAGS_BITWISE_AND(EnumType, Arg1Type, Arg2Type) \
  519. static inline EnumType operator&(const Arg1Type& a, const Arg2Type& b) \
  520. { \
  521. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  522. return static_cast<EnumType>(static_cast<UnderlyingType>(a) & static_cast<UnderlyingType>(b)); \
  523. } \
  524. #define __CV_ENUM_FLAGS_BITWISE_XOR(EnumType, Arg1Type, Arg2Type) \
  525. static inline EnumType operator^(const Arg1Type& a, const Arg2Type& b) \
  526. { \
  527. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  528. return static_cast<EnumType>(static_cast<UnderlyingType>(a) ^ static_cast<UnderlyingType>(b)); \
  529. } \
  530. #define __CV_ENUM_FLAGS_BITWISE_OR_EQ(EnumType, Arg1Type) \
  531. static inline EnumType& operator|=(EnumType& _this, const Arg1Type& val) \
  532. { \
  533. _this = static_cast<EnumType>(static_cast<int>(_this) | static_cast<int>(val)); \
  534. return _this; \
  535. } \
  536. #define __CV_ENUM_FLAGS_BITWISE_AND_EQ(EnumType, Arg1Type) \
  537. static inline EnumType& operator&=(EnumType& _this, const Arg1Type& val) \
  538. { \
  539. _this = static_cast<EnumType>(static_cast<int>(_this) & static_cast<int>(val)); \
  540. return _this; \
  541. } \
  542. #define __CV_ENUM_FLAGS_BITWISE_XOR_EQ(EnumType, Arg1Type) \
  543. static inline EnumType& operator^=(EnumType& _this, const Arg1Type& val) \
  544. { \
  545. _this = static_cast<EnumType>(static_cast<int>(_this) ^ static_cast<int>(val)); \
  546. return _this; \
  547. } \
  548. #define CV_ENUM_CLASS_EXPOSE(EnumType, ...) \
  549. __CV_EXPAND(__CV_CAT(__CV_ENUM_CLASS_EXPOSE_, __CV_VA_NUM_ARGS(__VA_ARGS__))(EnumType, __VA_ARGS__)); \
  550. #define CV_ENUM_FLAGS(EnumType) \
  551. __CV_ENUM_FLAGS_LOGICAL_NOT (EnumType) \
  552. __CV_ENUM_FLAGS_LOGICAL_EQ (EnumType, int) \
  553. __CV_ENUM_FLAGS_LOGICAL_NOT_EQ (EnumType, int) \
  554. \
  555. __CV_ENUM_FLAGS_BITWISE_NOT (EnumType) \
  556. __CV_ENUM_FLAGS_BITWISE_OR (EnumType, EnumType, EnumType) \
  557. __CV_ENUM_FLAGS_BITWISE_AND (EnumType, EnumType, EnumType) \
  558. __CV_ENUM_FLAGS_BITWISE_XOR (EnumType, EnumType, EnumType) \
  559. \
  560. __CV_ENUM_FLAGS_BITWISE_OR_EQ (EnumType, EnumType) \
  561. __CV_ENUM_FLAGS_BITWISE_AND_EQ (EnumType, EnumType) \
  562. __CV_ENUM_FLAGS_BITWISE_XOR_EQ (EnumType, EnumType) \
  563. /****************************************************************************************\
  564. * static analysys *
  565. \****************************************************************************************/
  566. // In practice, some macro are not processed correctly (noreturn is not detected).
  567. // We need to use simplified definition for them.
  568. #ifndef CV_STATIC_ANALYSIS
  569. # if defined(__KLOCWORK__) || defined(__clang_analyzer__) || defined(__COVERITY__)
  570. # define CV_STATIC_ANALYSIS 1
  571. # endif
  572. #else
  573. # if defined(CV_STATIC_ANALYSIS) && !(__CV_CAT(1, CV_STATIC_ANALYSIS) == 1) // defined and not empty
  574. # if 0 == CV_STATIC_ANALYSIS
  575. # undef CV_STATIC_ANALYSIS
  576. # endif
  577. # endif
  578. #endif
  579. /****************************************************************************************\
  580. * Thread sanitizer *
  581. \****************************************************************************************/
  582. #ifndef CV_THREAD_SANITIZER
  583. # if defined(__has_feature)
  584. # if __has_feature(thread_sanitizer)
  585. # define CV_THREAD_SANITIZER
  586. # endif
  587. # endif
  588. #endif
  589. /****************************************************************************************\
  590. * exchange-add operation for atomic operations on reference counters *
  591. \****************************************************************************************/
  592. #ifdef CV_XADD
  593. // allow to use user-defined macro
  594. #elif defined __GNUC__ || defined __clang__
  595. # if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__) && !defined __INTEL_COMPILER
  596. # ifdef __ATOMIC_ACQ_REL
  597. # define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
  598. # else
  599. # define CV_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
  600. # endif
  601. # else
  602. # if defined __ATOMIC_ACQ_REL && !defined __clang__
  603. // version for gcc >= 4.7
  604. # define CV_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
  605. # else
  606. # define CV_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
  607. # endif
  608. # endif
  609. #elif defined _MSC_VER && !defined RC_INVOKED
  610. # include <intrin.h>
  611. # define CV_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
  612. #else
  613. #ifdef OPENCV_FORCE_UNSAFE_XADD
  614. CV_INLINE int CV_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; }
  615. #else
  616. #error "OpenCV: can't define safe CV_XADD macro for current platform (unsupported). Define CV_XADD macro through custom port header (see OPENCV_INCLUDE_PORT_FILE)"
  617. #endif
  618. #endif
  619. /****************************************************************************************\
  620. * CV_NORETURN attribute *
  621. \****************************************************************************************/
  622. #ifndef CV_NORETURN
  623. # if defined(__GNUC__)
  624. # define CV_NORETURN __attribute__((__noreturn__))
  625. # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
  626. # define CV_NORETURN __declspec(noreturn)
  627. # else
  628. # define CV_NORETURN /* nothing by default */
  629. # endif
  630. #endif
  631. /****************************************************************************************\
  632. * CV_NODISCARD_STD attribute (C++17) *
  633. * encourages the compiler to issue a warning if the return value is discarded *
  634. \****************************************************************************************/
  635. #ifndef CV_NODISCARD_STD
  636. # ifndef __has_cpp_attribute
  637. // workaround preprocessor non-compliance https://reviews.llvm.org/D57851
  638. # define __has_cpp_attribute(__x) 0
  639. # endif
  640. # if __has_cpp_attribute(nodiscard)
  641. # define CV_NODISCARD_STD [[nodiscard]]
  642. # elif __cplusplus >= 201703L
  643. // available when compiler is C++17 compliant
  644. # define CV_NODISCARD_STD [[nodiscard]]
  645. # elif defined(__INTEL_COMPILER)
  646. // see above, available when C++17 is enabled
  647. # elif defined(_MSC_VER) && _MSC_VER >= 1911 && _MSVC_LANG >= 201703L
  648. // available with VS2017 v15.3+ with /std:c++17 or higher; works on functions and classes
  649. # define CV_NODISCARD_STD [[nodiscard]]
  650. # elif defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 700) && (__cplusplus >= 201103L)
  651. // available with GCC 7.0+; works on functions, works or silently fails on classes
  652. # define CV_NODISCARD_STD [[nodiscard]]
  653. # elif defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 408) && (__cplusplus >= 201103L)
  654. // available with GCC 4.8+ but it usually does nothing and can fail noisily -- therefore not used
  655. // define CV_NODISCARD_STD [[gnu::warn_unused_result]]
  656. # endif
  657. #endif
  658. #ifndef CV_NODISCARD_STD
  659. # define CV_NODISCARD_STD /* nothing by default */
  660. #endif
  661. /****************************************************************************************\
  662. * C++ 11 *
  663. \****************************************************************************************/
  664. #ifdef __cplusplus
  665. // MSVC was stuck at __cplusplus == 199711L for a long time, even where it supports C++11,
  666. // so check _MSC_VER instead. See:
  667. // <https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus>
  668. # if defined(_MSC_VER)
  669. # if _MSC_VER < 1800
  670. # error "OpenCV 4.x+ requires enabled C++11 support"
  671. # endif
  672. # elif __cplusplus < 201103L
  673. # error "OpenCV 4.x+ requires enabled C++11 support"
  674. # endif
  675. #endif
  676. #ifndef CV_CXX11
  677. # define CV_CXX11 1
  678. #endif
  679. #ifndef CV_OVERRIDE
  680. # define CV_OVERRIDE override
  681. #endif
  682. #ifndef CV_FINAL
  683. # define CV_FINAL final
  684. #endif
  685. #ifndef CV_NOEXCEPT
  686. # define CV_NOEXCEPT noexcept
  687. #endif
  688. #ifndef CV_CONSTEXPR
  689. # define CV_CONSTEXPR constexpr
  690. #endif
  691. // Integer types portability
  692. #ifdef __cplusplus
  693. #include <cstdint>
  694. namespace cv {
  695. using std::int8_t;
  696. using std::uint8_t;
  697. using std::int16_t;
  698. using std::uint16_t;
  699. using std::int32_t;
  700. using std::uint32_t;
  701. using std::int64_t;
  702. using std::uint64_t;
  703. }
  704. #else // pure C
  705. #include <stdint.h>
  706. #endif
  707. #ifdef __cplusplus
  708. namespace cv
  709. {
  710. class hfloat
  711. {
  712. public:
  713. #if CV_FP16_TYPE
  714. hfloat() : h(0) {}
  715. explicit hfloat(float x) { h = (__fp16)x; }
  716. operator float() const { return (float)h; }
  717. protected:
  718. __fp16 h;
  719. #else
  720. hfloat() : w(0) {}
  721. explicit hfloat(float x)
  722. {
  723. #if CV_FP16 && CV_AVX2
  724. __m128 v = _mm_load_ss(&x);
  725. w = (ushort)_mm_cvtsi128_si32(_mm_cvtps_ph(v, 0));
  726. #else
  727. Cv32suf in;
  728. in.f = x;
  729. unsigned sign = in.u & 0x80000000;
  730. in.u ^= sign;
  731. if( in.u >= 0x47800000 )
  732. w = (ushort)(in.u > 0x7f800000 ? 0x7e00 : 0x7c00);
  733. else
  734. {
  735. if (in.u < 0x38800000)
  736. {
  737. in.f += 0.5f;
  738. w = (ushort)(in.u - 0x3f000000);
  739. }
  740. else
  741. {
  742. unsigned t = in.u + 0xc8000fff;
  743. w = (ushort)((t + ((in.u >> 13) & 1)) >> 13);
  744. }
  745. }
  746. w = (ushort)(w | (sign >> 16));
  747. #endif
  748. }
  749. operator float() const
  750. {
  751. #if CV_FP16 && CV_AVX2
  752. float f;
  753. _mm_store_ss(&f, _mm_cvtph_ps(_mm_cvtsi32_si128(w)));
  754. return f;
  755. #else
  756. Cv32suf out;
  757. unsigned t = ((w & 0x7fff) << 13) + 0x38000000;
  758. unsigned sign = (w & 0x8000) << 16;
  759. unsigned e = w & 0x7c00;
  760. out.u = t + (1 << 23);
  761. out.u = (e >= 0x7c00 ? t + 0x38000000 :
  762. e == 0 ? (static_cast<void>(out.f -= 6.103515625e-05f), out.u) : t) | sign;
  763. return out.f;
  764. #endif
  765. }
  766. protected:
  767. ushort w;
  768. #endif
  769. };
  770. inline hfloat hfloatFromBits(ushort w) {
  771. #if CV_FP16_TYPE
  772. Cv16suf u;
  773. u.u = w;
  774. hfloat res(float(u.h));
  775. return res;
  776. #else
  777. Cv32suf out;
  778. unsigned t = ((w & 0x7fff) << 13) + 0x38000000;
  779. unsigned sign = (w & 0x8000) << 16;
  780. unsigned e = w & 0x7c00;
  781. out.u = t + (1 << 23);
  782. out.u = (e >= 0x7c00 ? t + 0x38000000 :
  783. e == 0 ? (static_cast<void>(out.f -= 6.103515625e-05f), out.u) : t) | sign;
  784. hfloat res(out.f);
  785. return res;
  786. #endif
  787. }
  788. #if !defined(__OPENCV_BUILD) && !(defined __STDCPP_FLOAT16_T__) && !(defined __ARM_NEON)
  789. typedef hfloat float16_t;
  790. #endif
  791. }
  792. #endif
  793. //! @}
  794. #ifndef __cplusplus
  795. #include "opencv2/core/fast_math.hpp" // define cvRound(double)
  796. #endif
  797. #endif // OPENCV_CORE_CVDEF_H