cvdef.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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_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. #if defined __GNUC__ && !defined __EXCEPTIONS
  157. #define CV_TRY
  158. #define CV_CATCH(A, B) for (A B; false; )
  159. #define CV_CATCH_ALL if (false)
  160. #define CV_THROW(A) abort()
  161. #define CV_RETHROW() abort()
  162. #else
  163. #define CV_TRY try
  164. #define CV_CATCH(A, B) catch(const A & B)
  165. #define CV_CATCH_ALL catch(...)
  166. #define CV_THROW(A) throw A
  167. #define CV_RETHROW() throw
  168. #endif
  169. //! @endcond
  170. // undef problematic defines sometimes defined by system headers (windows.h in particular)
  171. #undef small
  172. #undef min
  173. #undef max
  174. #undef abs
  175. #undef Complex
  176. #if defined __cplusplus
  177. #include <limits>
  178. #else
  179. #include <limits.h>
  180. #endif
  181. #include "opencv2/core/hal/interface.h"
  182. #if defined __ICL
  183. # define CV_ICC __ICL
  184. #elif defined __ICC
  185. # define CV_ICC __ICC
  186. #elif defined __ECL
  187. # define CV_ICC __ECL
  188. #elif defined __ECC
  189. # define CV_ICC __ECC
  190. #elif defined __INTEL_COMPILER
  191. # define CV_ICC __INTEL_COMPILER
  192. #endif
  193. #ifndef CV_INLINE
  194. # if defined __cplusplus
  195. # define CV_INLINE static inline
  196. # elif defined _MSC_VER
  197. # define CV_INLINE __inline
  198. # else
  199. # define CV_INLINE static
  200. # endif
  201. #endif
  202. #ifndef CV_ALWAYS_INLINE
  203. #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
  204. #define CV_ALWAYS_INLINE inline __attribute__((always_inline))
  205. #elif defined(_MSC_VER)
  206. #define CV_ALWAYS_INLINE __forceinline
  207. #else
  208. #define CV_ALWAYS_INLINE inline
  209. #endif
  210. #endif
  211. #if defined CV_DISABLE_OPTIMIZATION || (defined CV_ICC && !defined CV_ENABLE_UNROLLED)
  212. # define CV_ENABLE_UNROLLED 0
  213. #else
  214. # define CV_ENABLE_UNROLLED 1
  215. #endif
  216. #ifdef __GNUC__
  217. # define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
  218. #elif defined _MSC_VER
  219. # define CV_DECL_ALIGNED(x) __declspec(align(x))
  220. #else
  221. # define CV_DECL_ALIGNED(x)
  222. #endif
  223. /* CPU features and intrinsics support */
  224. #define CV_CPU_NONE 0
  225. #define CV_CPU_MMX 1
  226. #define CV_CPU_SSE 2
  227. #define CV_CPU_SSE2 3
  228. #define CV_CPU_SSE3 4
  229. #define CV_CPU_SSSE3 5
  230. #define CV_CPU_SSE4_1 6
  231. #define CV_CPU_SSE4_2 7
  232. #define CV_CPU_POPCNT 8
  233. #define CV_CPU_FP16 9
  234. #define CV_CPU_AVX 10
  235. #define CV_CPU_AVX2 11
  236. #define CV_CPU_FMA3 12
  237. #define CV_CPU_AVX_512F 13
  238. #define CV_CPU_AVX_512BW 14
  239. #define CV_CPU_AVX_512CD 15
  240. #define CV_CPU_AVX_512DQ 16
  241. #define CV_CPU_AVX_512ER 17
  242. #define CV_CPU_AVX_512IFMA512 18 // deprecated
  243. #define CV_CPU_AVX_512IFMA 18
  244. #define CV_CPU_AVX_512PF 19
  245. #define CV_CPU_AVX_512VBMI 20
  246. #define CV_CPU_AVX_512VL 21
  247. #define CV_CPU_AVX_512VBMI2 22
  248. #define CV_CPU_AVX_512VNNI 23
  249. #define CV_CPU_AVX_512BITALG 24
  250. #define CV_CPU_AVX_512VPOPCNTDQ 25
  251. #define CV_CPU_AVX_5124VNNIW 26
  252. #define CV_CPU_AVX_5124FMAPS 27
  253. #define CV_CPU_NEON 100
  254. #define CV_CPU_MSA 150
  255. #define CV_CPU_VSX 200
  256. #define CV_CPU_VSX3 201
  257. // CPU features groups
  258. #define CV_CPU_AVX512_SKX 256
  259. #define CV_CPU_AVX512_COMMON 257
  260. #define CV_CPU_AVX512_KNL 258
  261. #define CV_CPU_AVX512_KNM 259
  262. #define CV_CPU_AVX512_CNL 260
  263. #define CV_CPU_AVX512_CLX 261
  264. #define CV_CPU_AVX512_ICL 262
  265. // when adding to this list remember to update the following enum
  266. #define CV_HARDWARE_MAX_FEATURE 512
  267. /** @brief Available CPU features.
  268. */
  269. enum CpuFeatures {
  270. CPU_MMX = 1,
  271. CPU_SSE = 2,
  272. CPU_SSE2 = 3,
  273. CPU_SSE3 = 4,
  274. CPU_SSSE3 = 5,
  275. CPU_SSE4_1 = 6,
  276. CPU_SSE4_2 = 7,
  277. CPU_POPCNT = 8,
  278. CPU_FP16 = 9,
  279. CPU_AVX = 10,
  280. CPU_AVX2 = 11,
  281. CPU_FMA3 = 12,
  282. CPU_AVX_512F = 13,
  283. CPU_AVX_512BW = 14,
  284. CPU_AVX_512CD = 15,
  285. CPU_AVX_512DQ = 16,
  286. CPU_AVX_512ER = 17,
  287. CPU_AVX_512IFMA512 = 18, // deprecated
  288. CPU_AVX_512IFMA = 18,
  289. CPU_AVX_512PF = 19,
  290. CPU_AVX_512VBMI = 20,
  291. CPU_AVX_512VL = 21,
  292. CPU_AVX_512VBMI2 = 22,
  293. CPU_AVX_512VNNI = 23,
  294. CPU_AVX_512BITALG = 24,
  295. CPU_AVX_512VPOPCNTDQ= 25,
  296. CPU_AVX_5124VNNIW = 26,
  297. CPU_AVX_5124FMAPS = 27,
  298. CPU_NEON = 100,
  299. CPU_MSA = 150,
  300. CPU_VSX = 200,
  301. CPU_VSX3 = 201,
  302. CPU_AVX512_SKX = 256, //!< Skylake-X with AVX-512F/CD/BW/DQ/VL
  303. CPU_AVX512_COMMON = 257, //!< Common instructions AVX-512F/CD for all CPUs that support AVX-512
  304. CPU_AVX512_KNL = 258, //!< Knights Landing with AVX-512F/CD/ER/PF
  305. CPU_AVX512_KNM = 259, //!< Knights Mill with AVX-512F/CD/ER/PF/4FMAPS/4VNNIW/VPOPCNTDQ
  306. CPU_AVX512_CNL = 260, //!< Cannon Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI
  307. CPU_AVX512_CLX = 261, //!< Cascade Lake with AVX-512F/CD/BW/DQ/VL/VNNI
  308. CPU_AVX512_ICL = 262, //!< Ice Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI/VNNI/VBMI2/BITALG/VPOPCNTDQ
  309. CPU_MAX_FEATURE = 512 // see CV_HARDWARE_MAX_FEATURE
  310. };
  311. #include "cv_cpu_dispatch.h"
  312. #if !defined(CV_STRONG_ALIGNMENT) && defined(__arm__) && !(defined(__aarch64__) || defined(_M_ARM64))
  313. // int*, int64* should be propertly aligned pointers on ARMv7
  314. #define CV_STRONG_ALIGNMENT 1
  315. #endif
  316. #if !defined(CV_STRONG_ALIGNMENT)
  317. #define CV_STRONG_ALIGNMENT 0
  318. #endif
  319. /* fundamental constants */
  320. #define CV_PI 3.1415926535897932384626433832795
  321. #define CV_2PI 6.283185307179586476925286766559
  322. #define CV_LOG2 0.69314718055994530941723212145818
  323. #if defined __ARM_FP16_FORMAT_IEEE \
  324. && !defined __CUDACC__
  325. # define CV_FP16_TYPE 1
  326. #else
  327. # define CV_FP16_TYPE 0
  328. #endif
  329. typedef union Cv16suf
  330. {
  331. short i;
  332. ushort u;
  333. #if CV_FP16_TYPE
  334. __fp16 h;
  335. #endif
  336. }
  337. Cv16suf;
  338. typedef union Cv32suf
  339. {
  340. int i;
  341. unsigned u;
  342. float f;
  343. }
  344. Cv32suf;
  345. typedef union Cv64suf
  346. {
  347. int64 i;
  348. uint64 u;
  349. double f;
  350. }
  351. Cv64suf;
  352. #ifndef OPENCV_ABI_COMPATIBILITY
  353. #define OPENCV_ABI_COMPATIBILITY 300
  354. #endif
  355. #ifdef __OPENCV_BUILD
  356. # define DISABLE_OPENCV_24_COMPATIBILITY
  357. # define OPENCV_DISABLE_DEPRECATED_COMPATIBILITY
  358. #endif
  359. #ifndef CV_EXPORTS
  360. # if (defined _WIN32 || defined WINCE || defined __CYGWIN__) && defined(CVAPI_EXPORTS)
  361. # define CV_EXPORTS __declspec(dllexport)
  362. # elif defined __GNUC__ && __GNUC__ >= 4 && (defined(CVAPI_EXPORTS) || defined(__APPLE__))
  363. # define CV_EXPORTS __attribute__ ((visibility ("default")))
  364. # endif
  365. #endif
  366. #ifndef CV_EXPORTS
  367. # define CV_EXPORTS
  368. #endif
  369. #ifdef _MSC_VER
  370. # define CV_EXPORTS_TEMPLATE
  371. #else
  372. # define CV_EXPORTS_TEMPLATE CV_EXPORTS
  373. #endif
  374. #ifndef CV_DEPRECATED
  375. # if defined(__GNUC__)
  376. # define CV_DEPRECATED __attribute__ ((deprecated))
  377. # elif defined(_MSC_VER)
  378. # define CV_DEPRECATED __declspec(deprecated)
  379. # else
  380. # define CV_DEPRECATED
  381. # endif
  382. #endif
  383. #ifndef CV_DEPRECATED_EXTERNAL
  384. # if defined(__OPENCV_BUILD)
  385. # define CV_DEPRECATED_EXTERNAL /* nothing */
  386. # else
  387. # define CV_DEPRECATED_EXTERNAL CV_DEPRECATED
  388. # endif
  389. #endif
  390. #ifndef CV_EXTERN_C
  391. # ifdef __cplusplus
  392. # define CV_EXTERN_C extern "C"
  393. # else
  394. # define CV_EXTERN_C
  395. # endif
  396. #endif
  397. /* special informative macros for wrapper generators */
  398. #define CV_EXPORTS_W CV_EXPORTS
  399. #define CV_EXPORTS_W_SIMPLE CV_EXPORTS
  400. #define CV_EXPORTS_AS(synonym) CV_EXPORTS
  401. #define CV_EXPORTS_W_MAP CV_EXPORTS
  402. #define CV_IN_OUT
  403. #define CV_OUT
  404. #define CV_PROP
  405. #define CV_PROP_RW
  406. #define CV_WRAP
  407. #define CV_WRAP_AS(synonym)
  408. /****************************************************************************************\
  409. * Matrix type (Mat) *
  410. \****************************************************************************************/
  411. #define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
  412. #define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
  413. #define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
  414. #define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
  415. #define CV_MAT_CONT_FLAG_SHIFT 14
  416. #define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
  417. #define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
  418. #define CV_IS_CONT_MAT CV_IS_MAT_CONT
  419. #define CV_SUBMAT_FLAG_SHIFT 15
  420. #define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
  421. #define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
  422. /** Size of each channel item,
  423. 0x8442211 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
  424. #define CV_ELEM_SIZE1(type) \
  425. ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15)
  426. /** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
  427. #define CV_ELEM_SIZE(type) \
  428. (CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3))
  429. #ifndef MIN
  430. # define MIN(a,b) ((a) > (b) ? (b) : (a))
  431. #endif
  432. #ifndef MAX
  433. # define MAX(a,b) ((a) < (b) ? (b) : (a))
  434. #endif
  435. /****************************************************************************************\
  436. * static analysys *
  437. \****************************************************************************************/
  438. // In practice, some macro are not processed correctly (noreturn is not detected).
  439. // We need to use simplified definition for them.
  440. #ifndef CV_STATIC_ANALYSIS
  441. # if defined(__KLOCWORK__) || defined(__clang_analyzer__) || defined(__COVERITY__)
  442. # define CV_STATIC_ANALYSIS 1
  443. # endif
  444. #else
  445. # if defined(CV_STATIC_ANALYSIS) && !(__CV_CAT(1, CV_STATIC_ANALYSIS) == 1) // defined and not empty
  446. # if 0 == CV_STATIC_ANALYSIS
  447. # undef CV_STATIC_ANALYSIS
  448. # endif
  449. # endif
  450. #endif
  451. /****************************************************************************************\
  452. * Thread sanitizer *
  453. \****************************************************************************************/
  454. #ifndef CV_THREAD_SANITIZER
  455. # if defined(__has_feature)
  456. # if __has_feature(thread_sanitizer)
  457. # define CV_THREAD_SANITIZER
  458. # endif
  459. # endif
  460. #endif
  461. /****************************************************************************************\
  462. * exchange-add operation for atomic operations on reference counters *
  463. \****************************************************************************************/
  464. #ifdef CV_XADD
  465. // allow to use user-defined macro
  466. #elif defined __GNUC__ || defined __clang__
  467. # if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__) && !defined __INTEL_COMPILER
  468. # ifdef __ATOMIC_ACQ_REL
  469. # define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
  470. # else
  471. # define CV_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
  472. # endif
  473. # else
  474. # if defined __ATOMIC_ACQ_REL && !defined __clang__
  475. // version for gcc >= 4.7
  476. # define CV_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
  477. # else
  478. # define CV_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
  479. # endif
  480. # endif
  481. #elif defined _MSC_VER && !defined RC_INVOKED
  482. # include <intrin.h>
  483. # define CV_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
  484. #else
  485. #ifdef OPENCV_FORCE_UNSAFE_XADD
  486. CV_INLINE CV_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; }
  487. #else
  488. #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)"
  489. #endif
  490. #endif
  491. /****************************************************************************************\
  492. * CV_NORETURN attribute *
  493. \****************************************************************************************/
  494. #ifndef CV_NORETURN
  495. # if defined(__GNUC__)
  496. # define CV_NORETURN __attribute__((__noreturn__))
  497. # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
  498. # define CV_NORETURN __declspec(noreturn)
  499. # else
  500. # define CV_NORETURN /* nothing by default */
  501. # endif
  502. #endif
  503. /****************************************************************************************\
  504. * CV_NODISCARD attribute *
  505. * encourages the compiler to issue a warning if the return value is discarded (C++17) *
  506. \****************************************************************************************/
  507. #ifndef CV_NODISCARD
  508. # if defined(__GNUC__)
  509. # define CV_NODISCARD __attribute__((__warn_unused_result__)) // at least available with GCC 3.4
  510. # elif defined(__clang__) && defined(__has_attribute)
  511. # if __has_attribute(__warn_unused_result__)
  512. # define CV_NODISCARD __attribute__((__warn_unused_result__))
  513. # endif
  514. # endif
  515. #endif
  516. #ifndef CV_NODISCARD
  517. # define CV_NODISCARD /* nothing by default */
  518. #endif
  519. /****************************************************************************************\
  520. * C++ 11 *
  521. \****************************************************************************************/
  522. #ifndef CV_CXX11
  523. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
  524. # define CV_CXX11 1
  525. # endif
  526. #else
  527. # if CV_CXX11 == 0
  528. # undef CV_CXX11
  529. # endif
  530. #endif
  531. /****************************************************************************************\
  532. * C++ Move semantics *
  533. \****************************************************************************************/
  534. #ifndef CV_CXX_MOVE_SEMANTICS
  535. # if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(_MSC_VER) && _MSC_VER >= 1600)
  536. # define CV_CXX_MOVE_SEMANTICS 1
  537. # elif defined(__clang)
  538. # if __has_feature(cxx_rvalue_references)
  539. # define CV_CXX_MOVE_SEMANTICS 1
  540. # endif
  541. # endif
  542. #else
  543. # if CV_CXX_MOVE_SEMANTICS == 0
  544. # undef CV_CXX_MOVE_SEMANTICS
  545. # endif
  546. #endif
  547. #ifdef CV_CXX_MOVE_SEMANTICS
  548. #define CV_CXX_MOVE(x) std::move(x)
  549. #else
  550. #define CV_CXX_MOVE(x) (x)
  551. #endif
  552. /****************************************************************************************\
  553. * C++11 std::array *
  554. \****************************************************************************************/
  555. #ifndef CV_CXX_STD_ARRAY
  556. # if __cplusplus >= 201103L || (defined(__cplusplus) && defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  557. # define CV_CXX_STD_ARRAY 1
  558. # include <array>
  559. # endif
  560. #else
  561. # if CV_CXX_STD_ARRAY == 0
  562. # undef CV_CXX_STD_ARRAY
  563. # endif
  564. #endif
  565. /****************************************************************************************\
  566. * C++11 override / final *
  567. \****************************************************************************************/
  568. #ifndef CV_OVERRIDE
  569. # ifdef CV_CXX11
  570. # define CV_OVERRIDE override
  571. # endif
  572. #endif
  573. #ifndef CV_OVERRIDE
  574. # define CV_OVERRIDE
  575. #endif
  576. #ifndef CV_FINAL
  577. # ifdef CV_CXX11
  578. # define CV_FINAL final
  579. # endif
  580. #endif
  581. #ifndef CV_FINAL
  582. # define CV_FINAL
  583. #endif
  584. /****************************************************************************************\
  585. * C++11 noexcept *
  586. \****************************************************************************************/
  587. #ifndef CV_NOEXCEPT
  588. # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  589. # define CV_NOEXCEPT noexcept
  590. # endif
  591. #endif
  592. #ifndef CV_NOEXCEPT
  593. # define CV_NOEXCEPT
  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() {}
  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() {}
  672. explicit float16_t(float x)
  673. {
  674. #if CV_FP16
  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_FP16
  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 ? (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