cvdef.h 24 KB

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