cvdef.h 33 KB

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