cvdef.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. #ifndef CV_INLINE
  181. # if defined __cplusplus
  182. # define CV_INLINE static inline
  183. # elif defined _MSC_VER
  184. # define CV_INLINE __inline
  185. # else
  186. # define CV_INLINE static
  187. # endif
  188. #endif
  189. #ifndef CV_ALWAYS_INLINE
  190. #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
  191. #define CV_ALWAYS_INLINE inline __attribute__((always_inline))
  192. #elif defined(_MSC_VER)
  193. #define CV_ALWAYS_INLINE __forceinline
  194. #else
  195. #define CV_ALWAYS_INLINE inline
  196. #endif
  197. #endif
  198. #if defined CV_DISABLE_OPTIMIZATION || (defined CV_ICC && !defined CV_ENABLE_UNROLLED)
  199. # define CV_ENABLE_UNROLLED 0
  200. #else
  201. # define CV_ENABLE_UNROLLED 1
  202. #endif
  203. #ifdef __GNUC__
  204. # define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
  205. #elif defined _MSC_VER
  206. # define CV_DECL_ALIGNED(x) __declspec(align(x))
  207. #else
  208. # define CV_DECL_ALIGNED(x)
  209. #endif
  210. /* CPU features and intrinsics support */
  211. #define CV_CPU_NONE 0
  212. #define CV_CPU_MMX 1
  213. #define CV_CPU_SSE 2
  214. #define CV_CPU_SSE2 3
  215. #define CV_CPU_SSE3 4
  216. #define CV_CPU_SSSE3 5
  217. #define CV_CPU_SSE4_1 6
  218. #define CV_CPU_SSE4_2 7
  219. #define CV_CPU_POPCNT 8
  220. #define CV_CPU_FP16 9
  221. #define CV_CPU_AVX 10
  222. #define CV_CPU_AVX2 11
  223. #define CV_CPU_FMA3 12
  224. #define CV_CPU_AVX_512F 13
  225. #define CV_CPU_AVX_512BW 14
  226. #define CV_CPU_AVX_512CD 15
  227. #define CV_CPU_AVX_512DQ 16
  228. #define CV_CPU_AVX_512ER 17
  229. #define CV_CPU_AVX_512IFMA512 18 // deprecated
  230. #define CV_CPU_AVX_512IFMA 18
  231. #define CV_CPU_AVX_512PF 19
  232. #define CV_CPU_AVX_512VBMI 20
  233. #define CV_CPU_AVX_512VL 21
  234. #define CV_CPU_AVX_512VBMI2 22
  235. #define CV_CPU_AVX_512VNNI 23
  236. #define CV_CPU_AVX_512BITALG 24
  237. #define CV_CPU_AVX_512VPOPCNTDQ 25
  238. #define CV_CPU_AVX_5124VNNIW 26
  239. #define CV_CPU_AVX_5124FMAPS 27
  240. #define CV_CPU_NEON 100
  241. #define CV_CPU_NEON_DOTPROD 101
  242. #define CV_CPU_MSA 150
  243. #define CV_CPU_RISCVV 170
  244. #define CV_CPU_VSX 200
  245. #define CV_CPU_VSX3 201
  246. #define CV_CPU_RVV 210
  247. #define CV_CPU_LASX 230
  248. // CPU features groups
  249. #define CV_CPU_AVX512_SKX 256
  250. #define CV_CPU_AVX512_COMMON 257
  251. #define CV_CPU_AVX512_KNL 258
  252. #define CV_CPU_AVX512_KNM 259
  253. #define CV_CPU_AVX512_CNL 260
  254. #define CV_CPU_AVX512_CLX 261
  255. #define CV_CPU_AVX512_ICL 262
  256. // when adding to this list remember to update the following enum
  257. #define CV_HARDWARE_MAX_FEATURE 512
  258. /** @brief Available CPU features.
  259. */
  260. enum CpuFeatures {
  261. CPU_MMX = 1,
  262. CPU_SSE = 2,
  263. CPU_SSE2 = 3,
  264. CPU_SSE3 = 4,
  265. CPU_SSSE3 = 5,
  266. CPU_SSE4_1 = 6,
  267. CPU_SSE4_2 = 7,
  268. CPU_POPCNT = 8,
  269. CPU_FP16 = 9,
  270. CPU_AVX = 10,
  271. CPU_AVX2 = 11,
  272. CPU_FMA3 = 12,
  273. CPU_AVX_512F = 13,
  274. CPU_AVX_512BW = 14,
  275. CPU_AVX_512CD = 15,
  276. CPU_AVX_512DQ = 16,
  277. CPU_AVX_512ER = 17,
  278. CPU_AVX_512IFMA512 = 18, // deprecated
  279. CPU_AVX_512IFMA = 18,
  280. CPU_AVX_512PF = 19,
  281. CPU_AVX_512VBMI = 20,
  282. CPU_AVX_512VL = 21,
  283. CPU_AVX_512VBMI2 = 22,
  284. CPU_AVX_512VNNI = 23,
  285. CPU_AVX_512BITALG = 24,
  286. CPU_AVX_512VPOPCNTDQ= 25,
  287. CPU_AVX_5124VNNIW = 26,
  288. CPU_AVX_5124FMAPS = 27,
  289. CPU_NEON = 100,
  290. CPU_NEON_DOTPROD = 101,
  291. CPU_MSA = 150,
  292. CPU_RISCVV = 170,
  293. CPU_VSX = 200,
  294. CPU_VSX3 = 201,
  295. CPU_RVV = 210,
  296. CPU_LASX = 230,
  297. CPU_AVX512_SKX = 256, //!< Skylake-X with AVX-512F/CD/BW/DQ/VL
  298. CPU_AVX512_COMMON = 257, //!< Common instructions AVX-512F/CD for all CPUs that support AVX-512
  299. CPU_AVX512_KNL = 258, //!< Knights Landing with AVX-512F/CD/ER/PF
  300. CPU_AVX512_KNM = 259, //!< Knights Mill with AVX-512F/CD/ER/PF/4FMAPS/4VNNIW/VPOPCNTDQ
  301. CPU_AVX512_CNL = 260, //!< Cannon Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI
  302. CPU_AVX512_CLX = 261, //!< Cascade Lake with AVX-512F/CD/BW/DQ/VL/VNNI
  303. CPU_AVX512_ICL = 262, //!< Ice Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI/VNNI/VBMI2/BITALG/VPOPCNTDQ
  304. CPU_MAX_FEATURE = 512 // see CV_HARDWARE_MAX_FEATURE
  305. };
  306. #include "cv_cpu_dispatch.h"
  307. #if !defined(CV_STRONG_ALIGNMENT) && defined(__arm__) && !(defined(__aarch64__) || defined(_M_ARM64))
  308. // int*, int64* should be propertly aligned pointers on ARMv7
  309. #define CV_STRONG_ALIGNMENT 1
  310. #endif
  311. #if !defined(CV_STRONG_ALIGNMENT)
  312. #define CV_STRONG_ALIGNMENT 0
  313. #endif
  314. /* fundamental constants */
  315. #define CV_PI 3.1415926535897932384626433832795
  316. #define CV_2PI 6.283185307179586476925286766559
  317. #define CV_LOG2 0.69314718055994530941723212145818
  318. #if defined __ARM_FP16_FORMAT_IEEE \
  319. && !defined __CUDACC__
  320. # define CV_FP16_TYPE 1
  321. #else
  322. # define CV_FP16_TYPE 0
  323. #endif
  324. typedef union Cv16suf
  325. {
  326. short i;
  327. ushort u;
  328. #if CV_FP16_TYPE
  329. __fp16 h;
  330. #endif
  331. }
  332. Cv16suf;
  333. typedef union Cv32suf
  334. {
  335. int i;
  336. unsigned u;
  337. float f;
  338. }
  339. Cv32suf;
  340. typedef union Cv64suf
  341. {
  342. int64 i;
  343. uint64 u;
  344. double f;
  345. }
  346. Cv64suf;
  347. #ifndef OPENCV_ABI_COMPATIBILITY
  348. #define OPENCV_ABI_COMPATIBILITY 400
  349. #endif
  350. #ifdef __OPENCV_BUILD
  351. # define DISABLE_OPENCV_3_COMPATIBILITY
  352. # define OPENCV_DISABLE_DEPRECATED_COMPATIBILITY
  353. #endif
  354. #ifndef CV_EXPORTS
  355. # if (defined _WIN32 || defined WINCE || defined __CYGWIN__) && defined(CVAPI_EXPORTS)
  356. # define CV_EXPORTS __declspec(dllexport)
  357. # elif defined __GNUC__ && __GNUC__ >= 4 && (defined(CVAPI_EXPORTS) || defined(__APPLE__))
  358. # define CV_EXPORTS __attribute__ ((visibility ("default")))
  359. # endif
  360. #endif
  361. #ifndef CV_EXPORTS
  362. # define CV_EXPORTS
  363. #endif
  364. #ifdef _MSC_VER
  365. # define CV_EXPORTS_TEMPLATE
  366. #else
  367. # define CV_EXPORTS_TEMPLATE CV_EXPORTS
  368. #endif
  369. #ifndef CV_DEPRECATED
  370. # if defined(__GNUC__)
  371. # define CV_DEPRECATED __attribute__ ((deprecated))
  372. # elif defined(_MSC_VER)
  373. # define CV_DEPRECATED __declspec(deprecated)
  374. # else
  375. # define CV_DEPRECATED
  376. # endif
  377. #endif
  378. #ifndef CV_DEPRECATED_EXTERNAL
  379. # if defined(__OPENCV_BUILD)
  380. # define CV_DEPRECATED_EXTERNAL /* nothing */
  381. # else
  382. # define CV_DEPRECATED_EXTERNAL CV_DEPRECATED
  383. # endif
  384. #endif
  385. #ifndef CV_EXTERN_C
  386. # ifdef __cplusplus
  387. # define CV_EXTERN_C extern "C"
  388. # else
  389. # define CV_EXTERN_C
  390. # endif
  391. #endif
  392. /* special informative macros for wrapper generators */
  393. #define CV_EXPORTS_W CV_EXPORTS
  394. #define CV_EXPORTS_W_SIMPLE CV_EXPORTS
  395. #define CV_EXPORTS_AS(synonym) CV_EXPORTS
  396. #define CV_EXPORTS_W_MAP CV_EXPORTS
  397. #define CV_EXPORTS_W_PARAMS CV_EXPORTS
  398. #define CV_IN_OUT
  399. #define CV_OUT
  400. #define CV_PROP
  401. #define CV_PROP_RW
  402. #define CV_WRAP
  403. #define CV_WRAP_AS(synonym)
  404. #define CV_WRAP_MAPPABLE(mappable)
  405. #define CV_WRAP_PHANTOM(phantom_header)
  406. #define CV_WRAP_DEFAULT(val)
  407. /****************************************************************************************\
  408. * Matrix type (Mat) *
  409. \****************************************************************************************/
  410. #define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
  411. #define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
  412. #define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
  413. #define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
  414. #define CV_MAT_CONT_FLAG_SHIFT 14
  415. #define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
  416. #define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
  417. #define CV_IS_CONT_MAT CV_IS_MAT_CONT
  418. #define CV_SUBMAT_FLAG_SHIFT 15
  419. #define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
  420. #define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
  421. /** Size of each channel item,
  422. 0x28442211 = 0010 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
  423. #define CV_ELEM_SIZE1(type) ((0x28442211 >> CV_MAT_DEPTH(type)*4) & 15)
  424. #define CV_ELEM_SIZE(type) (CV_MAT_CN(type)*CV_ELEM_SIZE1(type))
  425. #ifndef MIN
  426. # define MIN(a,b) ((a) > (b) ? (b) : (a))
  427. #endif
  428. #ifndef MAX
  429. # define MAX(a,b) ((a) < (b) ? (b) : (a))
  430. #endif
  431. ///////////////////////////////////////// Enum operators ///////////////////////////////////////
  432. /**
  433. Provides compatibility operators for both classical and C++11 enum classes,
  434. as well as exposing the C++11 enum class members for backwards compatibility
  435. @code
  436. // Provides operators required for flag enums
  437. CV_ENUM_FLAGS(AccessFlag)
  438. // Exposes the listed members of the enum class AccessFlag to the current namespace
  439. CV_ENUM_CLASS_EXPOSE(AccessFlag, ACCESS_READ [, ACCESS_WRITE [, ...] ]);
  440. @endcode
  441. */
  442. #define __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST) \
  443. static const EnumType MEMBER_CONST = EnumType::MEMBER_CONST; \
  444. #define __CV_ENUM_CLASS_EXPOSE_2(EnumType, MEMBER_CONST, ...) \
  445. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  446. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_1(EnumType, __VA_ARGS__)); \
  447. #define __CV_ENUM_CLASS_EXPOSE_3(EnumType, MEMBER_CONST, ...) \
  448. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  449. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_2(EnumType, __VA_ARGS__)); \
  450. #define __CV_ENUM_CLASS_EXPOSE_4(EnumType, MEMBER_CONST, ...) \
  451. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  452. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_3(EnumType, __VA_ARGS__)); \
  453. #define __CV_ENUM_CLASS_EXPOSE_5(EnumType, MEMBER_CONST, ...) \
  454. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  455. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_4(EnumType, __VA_ARGS__)); \
  456. #define __CV_ENUM_CLASS_EXPOSE_6(EnumType, MEMBER_CONST, ...) \
  457. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  458. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_5(EnumType, __VA_ARGS__)); \
  459. #define __CV_ENUM_CLASS_EXPOSE_7(EnumType, MEMBER_CONST, ...) \
  460. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  461. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_6(EnumType, __VA_ARGS__)); \
  462. #define __CV_ENUM_CLASS_EXPOSE_8(EnumType, MEMBER_CONST, ...) \
  463. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  464. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_7(EnumType, __VA_ARGS__)); \
  465. #define __CV_ENUM_CLASS_EXPOSE_9(EnumType, MEMBER_CONST, ...) \
  466. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  467. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_8(EnumType, __VA_ARGS__)); \
  468. #define __CV_ENUM_FLAGS_LOGICAL_NOT(EnumType) \
  469. static inline bool operator!(const EnumType& val) \
  470. { \
  471. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  472. return !static_cast<UnderlyingType>(val); \
  473. } \
  474. #define __CV_ENUM_FLAGS_LOGICAL_NOT_EQ(Arg1Type, Arg2Type) \
  475. static inline bool operator!=(const Arg1Type& a, const Arg2Type& b) \
  476. { \
  477. return static_cast<int>(a) != static_cast<int>(b); \
  478. } \
  479. #define __CV_ENUM_FLAGS_LOGICAL_EQ(Arg1Type, Arg2Type) \
  480. static inline bool operator==(const Arg1Type& a, const Arg2Type& b) \
  481. { \
  482. return static_cast<int>(a) == static_cast<int>(b); \
  483. } \
  484. #define __CV_ENUM_FLAGS_BITWISE_NOT(EnumType) \
  485. static inline EnumType operator~(const EnumType& val) \
  486. { \
  487. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  488. return static_cast<EnumType>(~static_cast<UnderlyingType>(val)); \
  489. } \
  490. #define __CV_ENUM_FLAGS_BITWISE_OR(EnumType, Arg1Type, Arg2Type) \
  491. static inline EnumType operator|(const Arg1Type& a, const Arg2Type& b) \
  492. { \
  493. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  494. return static_cast<EnumType>(static_cast<UnderlyingType>(a) | static_cast<UnderlyingType>(b)); \
  495. } \
  496. #define __CV_ENUM_FLAGS_BITWISE_AND(EnumType, Arg1Type, Arg2Type) \
  497. static inline EnumType operator&(const Arg1Type& a, const Arg2Type& b) \
  498. { \
  499. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  500. return static_cast<EnumType>(static_cast<UnderlyingType>(a) & static_cast<UnderlyingType>(b)); \
  501. } \
  502. #define __CV_ENUM_FLAGS_BITWISE_XOR(EnumType, Arg1Type, Arg2Type) \
  503. static inline EnumType operator^(const Arg1Type& a, const Arg2Type& b) \
  504. { \
  505. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  506. return static_cast<EnumType>(static_cast<UnderlyingType>(a) ^ static_cast<UnderlyingType>(b)); \
  507. } \
  508. #define __CV_ENUM_FLAGS_BITWISE_OR_EQ(EnumType, Arg1Type) \
  509. static inline EnumType& operator|=(EnumType& _this, const Arg1Type& val) \
  510. { \
  511. _this = static_cast<EnumType>(static_cast<int>(_this) | static_cast<int>(val)); \
  512. return _this; \
  513. } \
  514. #define __CV_ENUM_FLAGS_BITWISE_AND_EQ(EnumType, Arg1Type) \
  515. static inline EnumType& operator&=(EnumType& _this, const Arg1Type& val) \
  516. { \
  517. _this = static_cast<EnumType>(static_cast<int>(_this) & static_cast<int>(val)); \
  518. return _this; \
  519. } \
  520. #define __CV_ENUM_FLAGS_BITWISE_XOR_EQ(EnumType, Arg1Type) \
  521. static inline EnumType& operator^=(EnumType& _this, const Arg1Type& val) \
  522. { \
  523. _this = static_cast<EnumType>(static_cast<int>(_this) ^ static_cast<int>(val)); \
  524. return _this; \
  525. } \
  526. #define CV_ENUM_CLASS_EXPOSE(EnumType, ...) \
  527. __CV_EXPAND(__CV_CAT(__CV_ENUM_CLASS_EXPOSE_, __CV_VA_NUM_ARGS(__VA_ARGS__))(EnumType, __VA_ARGS__)); \
  528. #define CV_ENUM_FLAGS(EnumType) \
  529. __CV_ENUM_FLAGS_LOGICAL_NOT (EnumType) \
  530. __CV_ENUM_FLAGS_LOGICAL_EQ (EnumType, int) \
  531. __CV_ENUM_FLAGS_LOGICAL_NOT_EQ (EnumType, int) \
  532. \
  533. __CV_ENUM_FLAGS_BITWISE_NOT (EnumType) \
  534. __CV_ENUM_FLAGS_BITWISE_OR (EnumType, EnumType, EnumType) \
  535. __CV_ENUM_FLAGS_BITWISE_AND (EnumType, EnumType, EnumType) \
  536. __CV_ENUM_FLAGS_BITWISE_XOR (EnumType, EnumType, EnumType) \
  537. \
  538. __CV_ENUM_FLAGS_BITWISE_OR_EQ (EnumType, EnumType) \
  539. __CV_ENUM_FLAGS_BITWISE_AND_EQ (EnumType, EnumType) \
  540. __CV_ENUM_FLAGS_BITWISE_XOR_EQ (EnumType, EnumType) \
  541. /****************************************************************************************\
  542. * static analysys *
  543. \****************************************************************************************/
  544. // In practice, some macro are not processed correctly (noreturn is not detected).
  545. // We need to use simplified definition for them.
  546. #ifndef CV_STATIC_ANALYSIS
  547. # if defined(__KLOCWORK__) || defined(__clang_analyzer__) || defined(__COVERITY__)
  548. # define CV_STATIC_ANALYSIS 1
  549. # endif
  550. #else
  551. # if defined(CV_STATIC_ANALYSIS) && !(__CV_CAT(1, CV_STATIC_ANALYSIS) == 1) // defined and not empty
  552. # if 0 == CV_STATIC_ANALYSIS
  553. # undef CV_STATIC_ANALYSIS
  554. # endif
  555. # endif
  556. #endif
  557. /****************************************************************************************\
  558. * Thread sanitizer *
  559. \****************************************************************************************/
  560. #ifndef CV_THREAD_SANITIZER
  561. # if defined(__has_feature)
  562. # if __has_feature(thread_sanitizer)
  563. # define CV_THREAD_SANITIZER
  564. # endif
  565. # endif
  566. #endif
  567. /****************************************************************************************\
  568. * exchange-add operation for atomic operations on reference counters *
  569. \****************************************************************************************/
  570. #ifdef CV_XADD
  571. // allow to use user-defined macro
  572. #elif defined __GNUC__ || defined __clang__
  573. # if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__) && !defined __INTEL_COMPILER
  574. # ifdef __ATOMIC_ACQ_REL
  575. # define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
  576. # else
  577. # define CV_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
  578. # endif
  579. # else
  580. # if defined __ATOMIC_ACQ_REL && !defined __clang__
  581. // version for gcc >= 4.7
  582. # define CV_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
  583. # else
  584. # define CV_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
  585. # endif
  586. # endif
  587. #elif defined _MSC_VER && !defined RC_INVOKED
  588. # include <intrin.h>
  589. # define CV_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
  590. #else
  591. #ifdef OPENCV_FORCE_UNSAFE_XADD
  592. CV_INLINE int CV_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; }
  593. #else
  594. #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)"
  595. #endif
  596. #endif
  597. /****************************************************************************************\
  598. * CV_NORETURN attribute *
  599. \****************************************************************************************/
  600. #ifndef CV_NORETURN
  601. # if defined(__GNUC__)
  602. # define CV_NORETURN __attribute__((__noreturn__))
  603. # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
  604. # define CV_NORETURN __declspec(noreturn)
  605. # else
  606. # define CV_NORETURN /* nothing by default */
  607. # endif
  608. #endif
  609. /****************************************************************************************\
  610. * CV_NODISCARD_STD attribute (C++17) *
  611. * encourages the compiler to issue a warning if the return value is discarded *
  612. \****************************************************************************************/
  613. #ifndef CV_NODISCARD_STD
  614. # ifndef __has_cpp_attribute
  615. // workaround preprocessor non-compliance https://reviews.llvm.org/D57851
  616. # define __has_cpp_attribute(__x) 0
  617. # endif
  618. # if __has_cpp_attribute(nodiscard)
  619. # define CV_NODISCARD_STD [[nodiscard]]
  620. # elif __cplusplus >= 201703L
  621. // available when compiler is C++17 compliant
  622. # define CV_NODISCARD_STD [[nodiscard]]
  623. # elif defined(__INTEL_COMPILER)
  624. // see above, available when C++17 is enabled
  625. # elif defined(_MSC_VER) && _MSC_VER >= 1911 && _MSVC_LANG >= 201703L
  626. // available with VS2017 v15.3+ with /std:c++17 or higher; works on functions and classes
  627. # define CV_NODISCARD_STD [[nodiscard]]
  628. # elif defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 700) && (__cplusplus >= 201103L)
  629. // available with GCC 7.0+; works on functions, works or silently fails on classes
  630. # define CV_NODISCARD_STD [[nodiscard]]
  631. # elif defined(__GNUC__) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 408) && (__cplusplus >= 201103L)
  632. // available with GCC 4.8+ but it usually does nothing and can fail noisily -- therefore not used
  633. // define CV_NODISCARD_STD [[gnu::warn_unused_result]]
  634. # endif
  635. #endif
  636. #ifndef CV_NODISCARD_STD
  637. # define CV_NODISCARD_STD /* nothing by default */
  638. #endif
  639. /****************************************************************************************\
  640. * CV_NODISCARD attribute (deprecated, GCC only) *
  641. * DONT USE: use instead the standard CV_NODISCARD_STD macro above *
  642. * this legacy method silently fails to issue warning until some version *
  643. * after gcc 6.3.0. Yet with gcc 7+ you can use the above standard method *
  644. * which makes this method useless. Don't use it. *
  645. * @deprecated use instead CV_NODISCARD_STD *
  646. \****************************************************************************************/
  647. #ifndef CV_NODISCARD
  648. # if defined(__GNUC__)
  649. # define CV_NODISCARD __attribute__((__warn_unused_result__))
  650. # elif defined(__clang__) && defined(__has_attribute)
  651. # if __has_attribute(__warn_unused_result__)
  652. # define CV_NODISCARD __attribute__((__warn_unused_result__))
  653. # endif
  654. # endif
  655. #endif
  656. #ifndef CV_NODISCARD
  657. # define CV_NODISCARD /* nothing by default */
  658. #endif
  659. /****************************************************************************************\
  660. * C++ 11 *
  661. \****************************************************************************************/
  662. #ifndef CV_CXX11
  663. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
  664. # define CV_CXX11 1
  665. # endif
  666. #else
  667. # if CV_CXX11 == 0
  668. # undef CV_CXX11
  669. # endif
  670. #endif
  671. #ifndef CV_CXX11
  672. # error "OpenCV 4.x+ requires enabled C++11 support"
  673. #endif
  674. #define CV_CXX_MOVE_SEMANTICS 1
  675. #define CV_CXX_MOVE(x) std::move(x)
  676. #define CV_CXX_STD_ARRAY 1
  677. #include <array>
  678. #ifndef CV_OVERRIDE
  679. # define CV_OVERRIDE override
  680. #endif
  681. #ifndef CV_FINAL
  682. # define CV_FINAL final
  683. #endif
  684. #ifndef CV_NOEXCEPT
  685. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  686. # define CV_NOEXCEPT noexcept
  687. # endif
  688. #endif
  689. #ifndef CV_NOEXCEPT
  690. # define CV_NOEXCEPT
  691. #endif
  692. #ifndef CV_CONSTEXPR
  693. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  694. # define CV_CONSTEXPR constexpr
  695. # endif
  696. #endif
  697. #ifndef CV_CONSTEXPR
  698. # define CV_CONSTEXPR
  699. #endif
  700. // Integer types portability
  701. #ifdef OPENCV_STDINT_HEADER
  702. #include OPENCV_STDINT_HEADER
  703. #elif defined(__cplusplus)
  704. #if defined(_MSC_VER) && _MSC_VER < 1600 /* MSVS 2010 */
  705. namespace cv {
  706. typedef signed char int8_t;
  707. typedef unsigned char uint8_t;
  708. typedef signed short int16_t;
  709. typedef unsigned short uint16_t;
  710. typedef signed int int32_t;
  711. typedef unsigned int uint32_t;
  712. typedef signed __int64 int64_t;
  713. typedef unsigned __int64 uint64_t;
  714. }
  715. #elif defined(_MSC_VER) || __cplusplus >= 201103L
  716. #include <cstdint>
  717. namespace cv {
  718. using std::int8_t;
  719. using std::uint8_t;
  720. using std::int16_t;
  721. using std::uint16_t;
  722. using std::int32_t;
  723. using std::uint32_t;
  724. using std::int64_t;
  725. using std::uint64_t;
  726. }
  727. #else
  728. #include <stdint.h>
  729. namespace cv {
  730. typedef ::int8_t int8_t;
  731. typedef ::uint8_t uint8_t;
  732. typedef ::int16_t int16_t;
  733. typedef ::uint16_t uint16_t;
  734. typedef ::int32_t int32_t;
  735. typedef ::uint32_t uint32_t;
  736. typedef ::int64_t int64_t;
  737. typedef ::uint64_t uint64_t;
  738. }
  739. #endif
  740. #else // pure C
  741. #include <stdint.h>
  742. #endif
  743. #ifdef __cplusplus
  744. namespace cv
  745. {
  746. class float16_t
  747. {
  748. public:
  749. #if CV_FP16_TYPE
  750. float16_t() : h(0) {}
  751. explicit float16_t(float x) { h = (__fp16)x; }
  752. operator float() const { return (float)h; }
  753. static float16_t fromBits(ushort w)
  754. {
  755. Cv16suf u;
  756. u.u = w;
  757. float16_t result;
  758. result.h = u.h;
  759. return result;
  760. }
  761. static float16_t zero()
  762. {
  763. float16_t result;
  764. result.h = (__fp16)0;
  765. return result;
  766. }
  767. ushort bits() const
  768. {
  769. Cv16suf u;
  770. u.h = h;
  771. return u.u;
  772. }
  773. protected:
  774. __fp16 h;
  775. #else
  776. float16_t() : w(0) {}
  777. explicit float16_t(float x)
  778. {
  779. #if CV_FP16
  780. __m128 v = _mm_load_ss(&x);
  781. w = (ushort)_mm_cvtsi128_si32(_mm_cvtps_ph(v, 0));
  782. #else
  783. Cv32suf in;
  784. in.f = x;
  785. unsigned sign = in.u & 0x80000000;
  786. in.u ^= sign;
  787. if( in.u >= 0x47800000 )
  788. w = (ushort)(in.u > 0x7f800000 ? 0x7e00 : 0x7c00);
  789. else
  790. {
  791. if (in.u < 0x38800000)
  792. {
  793. in.f += 0.5f;
  794. w = (ushort)(in.u - 0x3f000000);
  795. }
  796. else
  797. {
  798. unsigned t = in.u + 0xc8000fff;
  799. w = (ushort)((t + ((in.u >> 13) & 1)) >> 13);
  800. }
  801. }
  802. w = (ushort)(w | (sign >> 16));
  803. #endif
  804. }
  805. operator float() const
  806. {
  807. #if CV_FP16
  808. float f;
  809. _mm_store_ss(&f, _mm_cvtph_ps(_mm_cvtsi32_si128(w)));
  810. return f;
  811. #else
  812. Cv32suf out;
  813. unsigned t = ((w & 0x7fff) << 13) + 0x38000000;
  814. unsigned sign = (w & 0x8000) << 16;
  815. unsigned e = w & 0x7c00;
  816. out.u = t + (1 << 23);
  817. out.u = (e >= 0x7c00 ? t + 0x38000000 :
  818. e == 0 ? (static_cast<void>(out.f -= 6.103515625e-05f), out.u) : t) | sign;
  819. return out.f;
  820. #endif
  821. }
  822. static float16_t fromBits(ushort b)
  823. {
  824. float16_t result;
  825. result.w = b;
  826. return result;
  827. }
  828. static float16_t zero()
  829. {
  830. float16_t result;
  831. result.w = (ushort)0;
  832. return result;
  833. }
  834. ushort bits() const { return w; }
  835. protected:
  836. ushort w;
  837. #endif
  838. };
  839. }
  840. #endif
  841. //! @}
  842. #ifndef __cplusplus
  843. #include "opencv2/core/fast_math.hpp" // define cvRound(double)
  844. #endif
  845. #endif // OPENCV_CORE_CVDEF_H