cvdef.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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_NEON 100
  213. #define CV_CPU_VSX 200
  214. #define CV_CPU_VSX3 201
  215. // CPU features groups
  216. #define CV_CPU_AVX512_SKX 256
  217. // when adding to this list remember to update the following enum
  218. #define CV_HARDWARE_MAX_FEATURE 512
  219. /** @brief Available CPU features.
  220. */
  221. enum CpuFeatures {
  222. CPU_MMX = 1,
  223. CPU_SSE = 2,
  224. CPU_SSE2 = 3,
  225. CPU_SSE3 = 4,
  226. CPU_SSSE3 = 5,
  227. CPU_SSE4_1 = 6,
  228. CPU_SSE4_2 = 7,
  229. CPU_POPCNT = 8,
  230. CPU_FP16 = 9,
  231. CPU_AVX = 10,
  232. CPU_AVX2 = 11,
  233. CPU_FMA3 = 12,
  234. CPU_AVX_512F = 13,
  235. CPU_AVX_512BW = 14,
  236. CPU_AVX_512CD = 15,
  237. CPU_AVX_512DQ = 16,
  238. CPU_AVX_512ER = 17,
  239. CPU_AVX_512IFMA512 = 18, // deprecated
  240. CPU_AVX_512IFMA = 18,
  241. CPU_AVX_512PF = 19,
  242. CPU_AVX_512VBMI = 20,
  243. CPU_AVX_512VL = 21,
  244. CPU_NEON = 100,
  245. CPU_VSX = 200,
  246. CPU_VSX3 = 201,
  247. CPU_AVX512_SKX = 256, //!< Skylake-X with AVX-512F/CD/BW/DQ/VL
  248. CPU_MAX_FEATURE = 512 // see CV_HARDWARE_MAX_FEATURE
  249. };
  250. #include "cv_cpu_dispatch.h"
  251. /* fundamental constants */
  252. #define CV_PI 3.1415926535897932384626433832795
  253. #define CV_2PI 6.283185307179586476925286766559
  254. #define CV_LOG2 0.69314718055994530941723212145818
  255. #if defined __ARM_FP16_FORMAT_IEEE \
  256. && !defined __CUDACC__
  257. # define CV_FP16_TYPE 1
  258. #else
  259. # define CV_FP16_TYPE 0
  260. #endif
  261. typedef union Cv16suf
  262. {
  263. short i;
  264. ushort u;
  265. #if CV_FP16_TYPE
  266. __fp16 h;
  267. #endif
  268. }
  269. Cv16suf;
  270. typedef union Cv32suf
  271. {
  272. int i;
  273. unsigned u;
  274. float f;
  275. }
  276. Cv32suf;
  277. typedef union Cv64suf
  278. {
  279. int64 i;
  280. uint64 u;
  281. double f;
  282. }
  283. Cv64suf;
  284. #define OPENCV_ABI_COMPATIBILITY 400
  285. #ifdef __OPENCV_BUILD
  286. # define DISABLE_OPENCV_3_COMPATIBILITY
  287. # define OPENCV_DISABLE_DEPRECATED_COMPATIBILITY
  288. #endif
  289. #ifdef CVAPI_EXPORTS
  290. # if (defined _WIN32 || defined WINCE || defined __CYGWIN__)
  291. # define CV_EXPORTS __declspec(dllexport)
  292. # elif defined __GNUC__ && __GNUC__ >= 4
  293. # define CV_EXPORTS __attribute__ ((visibility ("default")))
  294. # endif
  295. #endif
  296. #ifndef CV_EXPORTS
  297. # define CV_EXPORTS
  298. #endif
  299. #ifdef _MSC_VER
  300. # define CV_EXPORTS_TEMPLATE
  301. #else
  302. # define CV_EXPORTS_TEMPLATE CV_EXPORTS
  303. #endif
  304. #ifndef CV_DEPRECATED
  305. # if defined(__GNUC__)
  306. # define CV_DEPRECATED __attribute__ ((deprecated))
  307. # elif defined(_MSC_VER)
  308. # define CV_DEPRECATED __declspec(deprecated)
  309. # else
  310. # define CV_DEPRECATED
  311. # endif
  312. #endif
  313. #ifndef CV_DEPRECATED_EXTERNAL
  314. # if defined(__OPENCV_BUILD)
  315. # define CV_DEPRECATED_EXTERNAL /* nothing */
  316. # else
  317. # define CV_DEPRECATED_EXTERNAL CV_DEPRECATED
  318. # endif
  319. #endif
  320. #ifndef CV_EXTERN_C
  321. # ifdef __cplusplus
  322. # define CV_EXTERN_C extern "C"
  323. # else
  324. # define CV_EXTERN_C
  325. # endif
  326. #endif
  327. /* special informative macros for wrapper generators */
  328. #define CV_EXPORTS_W CV_EXPORTS
  329. #define CV_EXPORTS_W_SIMPLE CV_EXPORTS
  330. #define CV_EXPORTS_AS(synonym) CV_EXPORTS
  331. #define CV_EXPORTS_W_MAP CV_EXPORTS
  332. #define CV_IN_OUT
  333. #define CV_OUT
  334. #define CV_PROP
  335. #define CV_PROP_RW
  336. #define CV_WRAP
  337. #define CV_WRAP_AS(synonym)
  338. #define CV_WRAP_MAPPABLE(mappable)
  339. #define CV_WRAP_PHANTOM(phantom_header)
  340. #define CV_WRAP_DEFAULT(val)
  341. /****************************************************************************************\
  342. * Matrix type (Mat) *
  343. \****************************************************************************************/
  344. #define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
  345. #define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
  346. #define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
  347. #define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
  348. #define CV_MAT_CONT_FLAG_SHIFT 14
  349. #define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
  350. #define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
  351. #define CV_IS_CONT_MAT CV_IS_MAT_CONT
  352. #define CV_SUBMAT_FLAG_SHIFT 15
  353. #define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
  354. #define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
  355. /** Size of each channel item,
  356. 0x28442211 = 0010 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
  357. #define CV_ELEM_SIZE1(type) ((0x28442211 >> CV_MAT_DEPTH(type)*4) & 15)
  358. #define CV_ELEM_SIZE(type) (CV_MAT_CN(type)*CV_ELEM_SIZE1(type))
  359. #ifndef MIN
  360. # define MIN(a,b) ((a) > (b) ? (b) : (a))
  361. #endif
  362. #ifndef MAX
  363. # define MAX(a,b) ((a) < (b) ? (b) : (a))
  364. #endif
  365. ///////////////////////////////////////// Enum operators ///////////////////////////////////////
  366. /**
  367. Provides compatibility operators for both classical and C++11 enum classes,
  368. as well as exposing the C++11 enum class members for backwards compatibility
  369. @code
  370. // Provides operators required for flag enums
  371. CV_ENUM_FLAGS(AccessFlag)
  372. // Exposes the listed members of the enum class AccessFlag to the current namespace
  373. CV_ENUM_CLASS_EXPOSE(AccessFlag, ACCESS_READ [, ACCESS_WRITE [, ...] ]);
  374. @endcode
  375. */
  376. #define __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST) \
  377. static const EnumType MEMBER_CONST = EnumType::MEMBER_CONST; \
  378. #define __CV_ENUM_CLASS_EXPOSE_2(EnumType, MEMBER_CONST, ...) \
  379. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  380. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_1(EnumType, __VA_ARGS__)); \
  381. #define __CV_ENUM_CLASS_EXPOSE_3(EnumType, MEMBER_CONST, ...) \
  382. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  383. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_2(EnumType, __VA_ARGS__)); \
  384. #define __CV_ENUM_CLASS_EXPOSE_4(EnumType, MEMBER_CONST, ...) \
  385. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  386. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_3(EnumType, __VA_ARGS__)); \
  387. #define __CV_ENUM_CLASS_EXPOSE_5(EnumType, MEMBER_CONST, ...) \
  388. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  389. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_4(EnumType, __VA_ARGS__)); \
  390. #define __CV_ENUM_CLASS_EXPOSE_6(EnumType, MEMBER_CONST, ...) \
  391. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  392. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_5(EnumType, __VA_ARGS__)); \
  393. #define __CV_ENUM_CLASS_EXPOSE_7(EnumType, MEMBER_CONST, ...) \
  394. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  395. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_6(EnumType, __VA_ARGS__)); \
  396. #define __CV_ENUM_CLASS_EXPOSE_8(EnumType, MEMBER_CONST, ...) \
  397. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  398. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_7(EnumType, __VA_ARGS__)); \
  399. #define __CV_ENUM_CLASS_EXPOSE_9(EnumType, MEMBER_CONST, ...) \
  400. __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
  401. __CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_8(EnumType, __VA_ARGS__)); \
  402. #define __CV_ENUM_FLAGS_LOGICAL_NOT(EnumType) \
  403. static inline bool operator!(const EnumType& val) \
  404. { \
  405. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  406. return !static_cast<UnderlyingType>(val); \
  407. } \
  408. #define __CV_ENUM_FLAGS_LOGICAL_NOT_EQ(Arg1Type, Arg2Type) \
  409. static inline bool operator!=(const Arg1Type& a, const Arg2Type& b) \
  410. { \
  411. return static_cast<int>(a) != static_cast<int>(b); \
  412. } \
  413. #define __CV_ENUM_FLAGS_LOGICAL_EQ(Arg1Type, Arg2Type) \
  414. static inline bool operator==(const Arg1Type& a, const Arg2Type& b) \
  415. { \
  416. return static_cast<int>(a) == static_cast<int>(b); \
  417. } \
  418. #define __CV_ENUM_FLAGS_BITWISE_NOT(EnumType) \
  419. static inline EnumType operator~(const EnumType& val) \
  420. { \
  421. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  422. return static_cast<EnumType>(~static_cast<UnderlyingType>(val)); \
  423. } \
  424. #define __CV_ENUM_FLAGS_BITWISE_OR(EnumType, Arg1Type, Arg2Type) \
  425. static inline EnumType operator|(const Arg1Type& a, const Arg2Type& b) \
  426. { \
  427. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  428. return static_cast<EnumType>(static_cast<UnderlyingType>(a) | static_cast<UnderlyingType>(b)); \
  429. } \
  430. #define __CV_ENUM_FLAGS_BITWISE_AND(EnumType, Arg1Type, Arg2Type) \
  431. static inline EnumType operator&(const Arg1Type& a, const Arg2Type& b) \
  432. { \
  433. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  434. return static_cast<EnumType>(static_cast<UnderlyingType>(a) & static_cast<UnderlyingType>(b)); \
  435. } \
  436. #define __CV_ENUM_FLAGS_BITWISE_XOR(EnumType, Arg1Type, Arg2Type) \
  437. static inline EnumType operator^(const Arg1Type& a, const Arg2Type& b) \
  438. { \
  439. typedef std::underlying_type<EnumType>::type UnderlyingType; \
  440. return static_cast<EnumType>(static_cast<UnderlyingType>(a) ^ static_cast<UnderlyingType>(b)); \
  441. } \
  442. #define __CV_ENUM_FLAGS_BITWISE_OR_EQ(EnumType, Arg1Type) \
  443. static inline EnumType& operator|=(EnumType& _this, const Arg1Type& val) \
  444. { \
  445. _this = static_cast<EnumType>(static_cast<int>(_this) | static_cast<int>(val)); \
  446. return _this; \
  447. } \
  448. #define __CV_ENUM_FLAGS_BITWISE_AND_EQ(EnumType, Arg1Type) \
  449. static inline EnumType& operator&=(EnumType& _this, const Arg1Type& val) \
  450. { \
  451. _this = static_cast<EnumType>(static_cast<int>(_this) & static_cast<int>(val)); \
  452. return _this; \
  453. } \
  454. #define __CV_ENUM_FLAGS_BITWISE_XOR_EQ(EnumType, Arg1Type) \
  455. static inline EnumType& operator^=(EnumType& _this, const Arg1Type& val) \
  456. { \
  457. _this = static_cast<EnumType>(static_cast<int>(_this) ^ static_cast<int>(val)); \
  458. return _this; \
  459. } \
  460. #define CV_ENUM_CLASS_EXPOSE(EnumType, ...) \
  461. __CV_EXPAND(__CV_CAT(__CV_ENUM_CLASS_EXPOSE_, __CV_VA_NUM_ARGS(__VA_ARGS__))(EnumType, __VA_ARGS__)); \
  462. #define CV_ENUM_FLAGS(EnumType) \
  463. __CV_ENUM_FLAGS_LOGICAL_NOT (EnumType) \
  464. __CV_ENUM_FLAGS_LOGICAL_EQ (EnumType, int) \
  465. __CV_ENUM_FLAGS_LOGICAL_NOT_EQ (EnumType, int) \
  466. \
  467. __CV_ENUM_FLAGS_BITWISE_NOT (EnumType) \
  468. __CV_ENUM_FLAGS_BITWISE_OR (EnumType, EnumType, EnumType) \
  469. __CV_ENUM_FLAGS_BITWISE_AND (EnumType, EnumType, EnumType) \
  470. __CV_ENUM_FLAGS_BITWISE_XOR (EnumType, EnumType, EnumType) \
  471. \
  472. __CV_ENUM_FLAGS_BITWISE_OR_EQ (EnumType, EnumType) \
  473. __CV_ENUM_FLAGS_BITWISE_AND_EQ (EnumType, EnumType) \
  474. __CV_ENUM_FLAGS_BITWISE_XOR_EQ (EnumType, EnumType) \
  475. /****************************************************************************************\
  476. * static analysys *
  477. \****************************************************************************************/
  478. // In practice, some macro are not processed correctly (noreturn is not detected).
  479. // We need to use simplified definition for them.
  480. #ifndef CV_STATIC_ANALYSIS
  481. # if defined(__KLOCWORK__) || defined(__clang_analyzer__) || defined(__COVERITY__)
  482. # define CV_STATIC_ANALYSIS 1
  483. # endif
  484. #else
  485. # if defined(CV_STATIC_ANALYSIS) && !(__CV_CAT(1, CV_STATIC_ANALYSIS) == 1) // defined and not empty
  486. # if 0 == CV_STATIC_ANALYSIS
  487. # undef CV_STATIC_ANALYSIS
  488. # endif
  489. # endif
  490. #endif
  491. /****************************************************************************************\
  492. * Thread sanitizer *
  493. \****************************************************************************************/
  494. #ifndef CV_THREAD_SANITIZER
  495. # if defined(__has_feature)
  496. # if __has_feature(thread_sanitizer)
  497. # define CV_THREAD_SANITIZER
  498. # endif
  499. # endif
  500. #endif
  501. /****************************************************************************************\
  502. * exchange-add operation for atomic operations on reference counters *
  503. \****************************************************************************************/
  504. #ifdef CV_XADD
  505. // allow to use user-defined macro
  506. #elif defined __GNUC__ || defined __clang__
  507. # if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__) && !defined __INTEL_COMPILER
  508. # ifdef __ATOMIC_ACQ_REL
  509. # define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
  510. # else
  511. # define CV_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
  512. # endif
  513. # else
  514. # if defined __ATOMIC_ACQ_REL && !defined __clang__
  515. // version for gcc >= 4.7
  516. # define CV_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
  517. # else
  518. # define CV_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
  519. # endif
  520. # endif
  521. #elif defined _MSC_VER && !defined RC_INVOKED
  522. # include <intrin.h>
  523. # define CV_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
  524. #else
  525. CV_INLINE CV_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; }
  526. #endif
  527. /****************************************************************************************\
  528. * CV_NORETURN attribute *
  529. \****************************************************************************************/
  530. #ifndef CV_NORETURN
  531. # if defined(__GNUC__)
  532. # define CV_NORETURN __attribute__((__noreturn__))
  533. # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
  534. # define CV_NORETURN __declspec(noreturn)
  535. # else
  536. # define CV_NORETURN /* nothing by default */
  537. # endif
  538. #endif
  539. /****************************************************************************************\
  540. * CV_NODISCARD attribute *
  541. * encourages the compiler to issue a warning if the return value is discarded (C++17) *
  542. \****************************************************************************************/
  543. #ifndef CV_NODISCARD
  544. # if defined(__GNUC__)
  545. # define CV_NODISCARD __attribute__((__warn_unused_result__)) // at least available with GCC 3.4
  546. # elif defined(__clang__) && defined(__has_attribute)
  547. # if __has_attribute(__warn_unused_result__)
  548. # define CV_NODISCARD __attribute__((__warn_unused_result__))
  549. # endif
  550. # endif
  551. #endif
  552. #ifndef CV_NODISCARD
  553. # define CV_NODISCARD /* nothing by default */
  554. #endif
  555. /****************************************************************************************\
  556. * C++ 11 *
  557. \****************************************************************************************/
  558. #ifndef CV_CXX11
  559. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
  560. # define CV_CXX11 1
  561. # endif
  562. #else
  563. # if CV_CXX11 == 0
  564. # undef CV_CXX11
  565. # endif
  566. #endif
  567. #ifndef CV_CXX11
  568. # error "OpenCV 4.x+ requires enabled C++11 support"
  569. #endif
  570. #define CV_CXX_MOVE_SEMANTICS 1
  571. #define CV_CXX_STD_ARRAY 1
  572. #include <array>
  573. #ifndef CV_OVERRIDE
  574. # define CV_OVERRIDE override
  575. #endif
  576. #ifndef CV_FINAL
  577. # define CV_FINAL final
  578. #endif
  579. #ifndef CV_NOEXCEPT
  580. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  581. # define CV_NOEXCEPT noexcept
  582. # endif
  583. #endif
  584. #ifndef CV_NOEXCEPT
  585. # define CV_NOEXCEPT
  586. #endif
  587. #ifndef CV_CONSTEXPR
  588. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  589. # define CV_CONSTEXPR constexpr
  590. # endif
  591. #endif
  592. #ifndef CV_CONSTEXPR
  593. # define CV_CONSTEXPR
  594. #endif
  595. // Integer types portatibility
  596. #ifdef OPENCV_STDINT_HEADER
  597. #include OPENCV_STDINT_HEADER
  598. #elif defined(__cplusplus)
  599. #if defined(_MSC_VER) && _MSC_VER < 1600 /* MSVS 2010 */
  600. namespace cv {
  601. typedef signed char int8_t;
  602. typedef unsigned char uint8_t;
  603. typedef signed short int16_t;
  604. typedef unsigned short uint16_t;
  605. typedef signed int int32_t;
  606. typedef unsigned int uint32_t;
  607. typedef signed __int64 int64_t;
  608. typedef unsigned __int64 uint64_t;
  609. }
  610. #elif defined(_MSC_VER) || __cplusplus >= 201103L
  611. #include <cstdint>
  612. namespace cv {
  613. using std::int8_t;
  614. using std::uint8_t;
  615. using std::int16_t;
  616. using std::uint16_t;
  617. using std::int32_t;
  618. using std::uint32_t;
  619. using std::int64_t;
  620. using std::uint64_t;
  621. }
  622. #else
  623. #include <stdint.h>
  624. namespace cv {
  625. typedef ::int8_t int8_t;
  626. typedef ::uint8_t uint8_t;
  627. typedef ::int16_t int16_t;
  628. typedef ::uint16_t uint16_t;
  629. typedef ::int32_t int32_t;
  630. typedef ::uint32_t uint32_t;
  631. typedef ::int64_t int64_t;
  632. typedef ::uint64_t uint64_t;
  633. }
  634. #endif
  635. #else // pure C
  636. #include <stdint.h>
  637. #endif
  638. #ifdef __cplusplus
  639. namespace cv
  640. {
  641. class float16_t
  642. {
  643. public:
  644. #if CV_FP16_TYPE
  645. float16_t() : h(0) {}
  646. explicit float16_t(float x) { h = (__fp16)x; }
  647. operator float() const { return (float)h; }
  648. static float16_t fromBits(ushort w)
  649. {
  650. Cv16suf u;
  651. u.u = w;
  652. float16_t result;
  653. result.h = u.h;
  654. return result;
  655. }
  656. static float16_t zero()
  657. {
  658. float16_t result;
  659. result.h = (__fp16)0;
  660. return result;
  661. }
  662. ushort bits() const
  663. {
  664. Cv16suf u;
  665. u.h = h;
  666. return u.u;
  667. }
  668. protected:
  669. __fp16 h;
  670. #else
  671. float16_t() : w(0) {}
  672. explicit float16_t(float x)
  673. {
  674. #if CV_AVX2
  675. __m128 v = _mm_load_ss(&x);
  676. w = (ushort)_mm_cvtsi128_si32(_mm_cvtps_ph(v, 0));
  677. #else
  678. Cv32suf in;
  679. in.f = x;
  680. unsigned sign = in.u & 0x80000000;
  681. in.u ^= sign;
  682. if( in.u >= 0x47800000 )
  683. w = (ushort)(in.u > 0x7f800000 ? 0x7e00 : 0x7c00);
  684. else
  685. {
  686. if (in.u < 0x38800000)
  687. {
  688. in.f += 0.5f;
  689. w = (ushort)(in.u - 0x3f000000);
  690. }
  691. else
  692. {
  693. unsigned t = in.u + 0xc8000fff;
  694. w = (ushort)((t + ((in.u >> 13) & 1)) >> 13);
  695. }
  696. }
  697. w = (ushort)(w | (sign >> 16));
  698. #endif
  699. }
  700. operator float() const
  701. {
  702. #if CV_AVX2
  703. float f;
  704. _mm_store_ss(&f, _mm_cvtph_ps(_mm_cvtsi32_si128(w)));
  705. return f;
  706. #else
  707. Cv32suf out;
  708. unsigned t = ((w & 0x7fff) << 13) + 0x38000000;
  709. unsigned sign = (w & 0x8000) << 16;
  710. unsigned e = w & 0x7c00;
  711. out.u = t + (1 << 23);
  712. out.u = (e >= 0x7c00 ? t + 0x38000000 :
  713. e == 0 ? (static_cast<void>(out.f -= 6.103515625e-05f), out.u) : t) | sign;
  714. return out.f;
  715. #endif
  716. }
  717. static float16_t fromBits(ushort b)
  718. {
  719. float16_t result;
  720. result.w = b;
  721. return result;
  722. }
  723. static float16_t zero()
  724. {
  725. float16_t result;
  726. result.w = (ushort)0;
  727. return result;
  728. }
  729. ushort bits() const { return w; }
  730. protected:
  731. ushort w;
  732. #endif
  733. };
  734. }
  735. #endif
  736. //! @}
  737. #ifndef __cplusplus
  738. #include "opencv2/core/fast_math.hpp" // define cvRound(double)
  739. #endif
  740. #endif // OPENCV_CORE_CVDEF_H