1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
19 // Redistribution and use in source and binary forms, with or without modification,
20 // are permitted provided that the following conditions are met:
22 // * Redistribution's of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
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.
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.
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.
45 #ifndef OPENCV_CORE_CVDEF_H
46 #define OPENCV_CORE_CVDEF_H
48 //! @addtogroup core_utils
51 #if !defined CV_DOXYGEN && !defined CV_IGNORE_DEBUG_BUILD_GUARD
52 #if (defined(_MSC_VER) && (defined(DEBUG) || defined(_DEBUG))) || \
53 (defined(_GLIBCXX_DEBUG) || defined(_GLIBCXX_DEBUG_PEDANTIC))
54 // Guard to prevent using of binary incompatible binaries / runtimes
55 // https://github.com/opencv/opencv/pull/9161
56 #define CV__DEBUG_NS_BEGIN namespace debug_build_guard {
57 #define CV__DEBUG_NS_END }
58 namespace cv { namespace debug_build_guard { } using namespace debug_build_guard; }
62 #ifndef CV__DEBUG_NS_BEGIN
63 #define CV__DEBUG_NS_BEGIN
64 #define CV__DEBUG_NS_END
73 #define __CV_EXPAND(x) x
77 #define __CV_CAT__(x, y) x ## y
78 #define __CV_CAT_(x, y) __CV_CAT__(x, y)
79 #define __CV_CAT(x, y) __CV_CAT_(x, y)
82 #define __CV_VA_NUM_ARGS_HELPER(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
83 #define __CV_VA_NUM_ARGS(...) __CV_VA_NUM_ARGS_HELPER(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
86 #define CV_Func __func__
87 #elif defined _MSC_VER
88 #define CV_Func __FUNCTION__
95 //////////////// static assert /////////////////
96 #define CVAUX_CONCAT_EXP(a, b) a##b
97 #define CVAUX_CONCAT(a, b) CVAUX_CONCAT_EXP(a,b)
99 #if defined(__clang__)
100 # ifndef __has_extension
101 # define __has_extension __has_feature /* compatibility, for older versions of clang */
103 # if __has_extension(cxx_static_assert)
104 # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
105 # elif __has_extension(c_static_assert)
106 # define CV_StaticAssert(condition, reason) _Static_assert((condition), reason " " #condition)
108 #elif defined(__GNUC__)
109 # if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L)
110 # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
112 #elif defined(_MSC_VER)
113 # if _MSC_VER >= 1600 /* MSVC 10 */
114 # define CV_StaticAssert(condition, reason) static_assert((condition), reason " " #condition)
117 #ifndef CV_StaticAssert
118 # if !defined(__clang__) && defined(__GNUC__) && (__GNUC__*100 + __GNUC_MINOR__ > 302)
119 # define CV_StaticAssert(condition, reason) ({ extern int __attribute__((error("CV_StaticAssert: " reason " " #condition))) CV_StaticAssert(); ((condition) ? 0 : CV_StaticAssert()); })
122 template <bool x> struct CV_StaticAssert_failed;
123 template <> struct CV_StaticAssert_failed<true> { enum { val = 1 }; };
124 template<int x> struct CV_StaticAssert_test {};
126 # define CV_StaticAssert(condition, reason)\
127 typedef cv::CV_StaticAssert_test< sizeof(cv::CV_StaticAssert_failed< static_cast<bool>(condition) >) > CVAUX_CONCAT(CV_StaticAssert_failed_at_, __LINE__)
131 // Suppress warning "-Wdeprecated-declarations" / C4996
132 #if defined(_MSC_VER)
133 #define CV_DO_PRAGMA(x) __pragma(x)
134 #elif defined(__GNUC__)
135 #define CV_DO_PRAGMA(x) _Pragma (#x)
137 #define CV_DO_PRAGMA(x)
141 #define CV_SUPPRESS_DEPRECATED_START \
142 CV_DO_PRAGMA(warning(push)) \
143 CV_DO_PRAGMA(warning(disable: 4996))
144 #define CV_SUPPRESS_DEPRECATED_END CV_DO_PRAGMA(warning(pop))
145 #elif defined (__clang__) || ((__GNUC__) && (__GNUC__*100 + __GNUC_MINOR__ > 405))
146 #define CV_SUPPRESS_DEPRECATED_START \
147 CV_DO_PRAGMA(GCC diagnostic push) \
148 CV_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
149 #define CV_SUPPRESS_DEPRECATED_END CV_DO_PRAGMA(GCC diagnostic pop)
151 #define CV_SUPPRESS_DEPRECATED_START
152 #define CV_SUPPRESS_DEPRECATED_END
155 #define CV_UNUSED(name) (void)name
157 #if defined __GNUC__ && !defined __EXCEPTIONS
159 #define CV_CATCH(A, B) for (A B; false; )
160 #define CV_CATCH_ALL if (false)
161 #define CV_THROW(A) abort()
162 #define CV_RETHROW() abort()
165 #define CV_CATCH(A, B) catch(const A & B)
166 #define CV_CATCH_ALL catch(...)
167 #define CV_THROW(A) throw A
168 #define CV_RETHROW() throw
173 // undef problematic defines sometimes defined by system headers (windows.h in particular)
181 #include "opencv2/core/hal/interface.h"
184 # define CV_ICC __ICL
186 # define CV_ICC __ICC
188 # define CV_ICC __ECL
190 # define CV_ICC __ECC
191 #elif defined __INTEL_COMPILER
192 # define CV_ICC __INTEL_COMPILER
196 # if defined __cplusplus
197 # define CV_INLINE static inline
198 # elif defined _MSC_VER
199 # define CV_INLINE __inline
201 # define CV_INLINE static
205 #ifndef CV_ALWAYS_INLINE
206 #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
207 #define CV_ALWAYS_INLINE inline __attribute__((always_inline))
208 #elif defined(_MSC_VER)
209 #define CV_ALWAYS_INLINE __forceinline
211 #define CV_ALWAYS_INLINE inline
215 #if defined CV_DISABLE_OPTIMIZATION || (defined CV_ICC && !defined CV_ENABLE_UNROLLED)
216 # define CV_ENABLE_UNROLLED 0
218 # define CV_ENABLE_UNROLLED 1
222 # define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
223 #elif defined _MSC_VER
224 # define CV_DECL_ALIGNED(x) __declspec(align(x))
226 # define CV_DECL_ALIGNED(x)
229 /* CPU features and intrinsics support */
230 #define CV_CPU_NONE 0
233 #define CV_CPU_SSE2 3
234 #define CV_CPU_SSE3 4
235 #define CV_CPU_SSSE3 5
236 #define CV_CPU_SSE4_1 6
237 #define CV_CPU_SSE4_2 7
238 #define CV_CPU_POPCNT 8
239 #define CV_CPU_FP16 9
240 #define CV_CPU_AVX 10
241 #define CV_CPU_AVX2 11
242 #define CV_CPU_FMA3 12
244 #define CV_CPU_AVX_512F 13
245 #define CV_CPU_AVX_512BW 14
246 #define CV_CPU_AVX_512CD 15
247 #define CV_CPU_AVX_512DQ 16
248 #define CV_CPU_AVX_512ER 17
249 #define CV_CPU_AVX_512IFMA512 18 // deprecated
250 #define CV_CPU_AVX_512IFMA 18
251 #define CV_CPU_AVX_512PF 19
252 #define CV_CPU_AVX_512VBMI 20
253 #define CV_CPU_AVX_512VL 21
254 #define CV_CPU_AVX_512VBMI2 22
255 #define CV_CPU_AVX_512VNNI 23
256 #define CV_CPU_AVX_512BITALG 24
257 #define CV_CPU_AVX_512VPOPCNTDQ 25
258 #define CV_CPU_AVX_5124VNNIW 26
259 #define CV_CPU_AVX_5124FMAPS 27
261 #define CV_CPU_NEON 100
263 #define CV_CPU_MSA 150
265 #define CV_CPU_VSX 200
266 #define CV_CPU_VSX3 201
268 // CPU features groups
269 #define CV_CPU_AVX512_SKX 256
270 #define CV_CPU_AVX512_COMMON 257
271 #define CV_CPU_AVX512_KNL 258
272 #define CV_CPU_AVX512_KNM 259
273 #define CV_CPU_AVX512_CNL 260
274 #define CV_CPU_AVX512_CLX 261
275 #define CV_CPU_AVX512_ICL 262
277 // when adding to this list remember to update the following enum
278 #define CV_HARDWARE_MAX_FEATURE 512
280 /** @brief Available CPU features.
301 CPU_AVX_512IFMA512 = 18, // deprecated
302 CPU_AVX_512IFMA = 18,
304 CPU_AVX_512VBMI = 20,
306 CPU_AVX_512VBMI2 = 22,
307 CPU_AVX_512VNNI = 23,
308 CPU_AVX_512BITALG = 24,
309 CPU_AVX_512VPOPCNTDQ= 25,
310 CPU_AVX_5124VNNIW = 26,
311 CPU_AVX_5124FMAPS = 27,
320 CPU_AVX512_SKX = 256, //!< Skylake-X with AVX-512F/CD/BW/DQ/VL
321 CPU_AVX512_COMMON = 257, //!< Common instructions AVX-512F/CD for all CPUs that support AVX-512
322 CPU_AVX512_KNL = 258, //!< Knights Landing with AVX-512F/CD/ER/PF
323 CPU_AVX512_KNM = 259, //!< Knights Mill with AVX-512F/CD/ER/PF/4FMAPS/4VNNIW/VPOPCNTDQ
324 CPU_AVX512_CNL = 260, //!< Cannon Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI
325 CPU_AVX512_CLX = 261, //!< Cascade Lake with AVX-512F/CD/BW/DQ/VL/VNNI
326 CPU_AVX512_ICL = 262, //!< Ice Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI/VNNI/VBMI2/BITALG/VPOPCNTDQ
328 CPU_MAX_FEATURE = 512 // see CV_HARDWARE_MAX_FEATURE
332 #include "cv_cpu_dispatch.h"
335 /* fundamental constants */
336 #define CV_PI 3.1415926535897932384626433832795
337 #define CV_2PI 6.283185307179586476925286766559
338 #define CV_LOG2 0.69314718055994530941723212145818
340 #if defined __ARM_FP16_FORMAT_IEEE \
341 && !defined __CUDACC__
342 # define CV_FP16_TYPE 1
344 # define CV_FP16_TYPE 0
347 typedef union Cv16suf
357 typedef union Cv32suf
365 typedef union Cv64suf
373 #define OPENCV_ABI_COMPATIBILITY 300
375 #ifdef __OPENCV_BUILD
376 # define DISABLE_OPENCV_24_COMPATIBILITY
377 # define OPENCV_DISABLE_DEPRECATED_COMPATIBILITY
381 # if (defined _WIN32 || defined WINCE || defined __CYGWIN__) && defined(CVAPI_EXPORTS)
382 # define CV_EXPORTS __declspec(dllexport)
383 # elif defined __GNUC__ && __GNUC__ >= 4 && (defined(CVAPI_EXPORTS) || defined(__APPLE__))
384 # define CV_EXPORTS __attribute__ ((visibility ("default")))
393 # define CV_EXPORTS_TEMPLATE
395 # define CV_EXPORTS_TEMPLATE CV_EXPORTS
398 #ifndef CV_DEPRECATED
399 # if defined(__GNUC__)
400 # define CV_DEPRECATED __attribute__ ((deprecated))
401 # elif defined(_MSC_VER)
402 # define CV_DEPRECATED __declspec(deprecated)
404 # define CV_DEPRECATED
408 #ifndef CV_DEPRECATED_EXTERNAL
409 # if defined(__OPENCV_BUILD)
410 # define CV_DEPRECATED_EXTERNAL /* nothing */
412 # define CV_DEPRECATED_EXTERNAL CV_DEPRECATED
419 # define CV_EXTERN_C extern "C"
425 /* special informative macros for wrapper generators */
426 #define CV_EXPORTS_W CV_EXPORTS
427 #define CV_EXPORTS_W_SIMPLE CV_EXPORTS
428 #define CV_EXPORTS_AS(synonym) CV_EXPORTS
429 #define CV_EXPORTS_W_MAP CV_EXPORTS
435 #define CV_WRAP_AS(synonym)
437 /****************************************************************************************\
438 * Matrix type (Mat) *
439 \****************************************************************************************/
441 #define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
442 #define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
443 #define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
444 #define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
445 #define CV_MAT_CONT_FLAG_SHIFT 14
446 #define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
447 #define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
448 #define CV_IS_CONT_MAT CV_IS_MAT_CONT
449 #define CV_SUBMAT_FLAG_SHIFT 15
450 #define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
451 #define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
453 /** Size of each channel item,
454 0x8442211 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
455 #define CV_ELEM_SIZE1(type) \
456 ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15)
458 /** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
459 #define CV_ELEM_SIZE(type) \
460 (CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3))
463 # define MIN(a,b) ((a) > (b) ? (b) : (a))
467 # define MAX(a,b) ((a) < (b) ? (b) : (a))
470 /****************************************************************************************\
472 \****************************************************************************************/
474 // In practice, some macro are not processed correctly (noreturn is not detected).
475 // We need to use simplified definition for them.
476 #ifndef CV_STATIC_ANALYSIS
477 # if defined(__KLOCWORK__) || defined(__clang_analyzer__) || defined(__COVERITY__)
478 # define CV_STATIC_ANALYSIS 1
481 # if defined(CV_STATIC_ANALYSIS) && !(__CV_CAT(1, CV_STATIC_ANALYSIS) == 1) // defined and not empty
482 # if 0 == CV_STATIC_ANALYSIS
483 # undef CV_STATIC_ANALYSIS
488 /****************************************************************************************\
490 \****************************************************************************************/
491 #ifndef CV_THREAD_SANITIZER
492 # if defined(__has_feature)
493 # if __has_feature(thread_sanitizer)
494 # define CV_THREAD_SANITIZER
499 /****************************************************************************************\
500 * exchange-add operation for atomic operations on reference counters *
501 \****************************************************************************************/
504 // allow to use user-defined macro
505 #elif defined __GNUC__ || defined __clang__
506 # if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__) && !defined __INTEL_COMPILER
507 # ifdef __ATOMIC_ACQ_REL
508 # define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
510 # define CV_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
513 # if defined __ATOMIC_ACQ_REL && !defined __clang__
514 // version for gcc >= 4.7
515 # define CV_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
517 # define CV_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
520 #elif defined _MSC_VER && !defined RC_INVOKED
522 # define CV_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
524 CV_INLINE CV_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; }
528 /****************************************************************************************\
529 * CV_NORETURN attribute *
530 \****************************************************************************************/
533 # if defined(__GNUC__)
534 # define CV_NORETURN __attribute__((__noreturn__))
535 # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
536 # define CV_NORETURN __declspec(noreturn)
538 # define CV_NORETURN /* nothing by default */
543 /****************************************************************************************\
544 * CV_NODISCARD attribute *
545 * encourages the compiler to issue a warning if the return value is discarded (C++17) *
546 \****************************************************************************************/
548 # if defined(__GNUC__)
549 # define CV_NODISCARD __attribute__((__warn_unused_result__)) // at least available with GCC 3.4
550 # elif defined(__clang__) && defined(__has_attribute)
551 # if __has_attribute(__warn_unused_result__)
552 # define CV_NODISCARD __attribute__((__warn_unused_result__))
557 # define CV_NODISCARD /* nothing by default */
561 /****************************************************************************************\
563 \****************************************************************************************/
565 # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
575 /****************************************************************************************\
576 * C++ Move semantics *
577 \****************************************************************************************/
579 #ifndef CV_CXX_MOVE_SEMANTICS
580 # if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(_MSC_VER) && _MSC_VER >= 1600)
581 # define CV_CXX_MOVE_SEMANTICS 1
582 # elif defined(__clang)
583 # if __has_feature(cxx_rvalue_references)
584 # define CV_CXX_MOVE_SEMANTICS 1
588 # if CV_CXX_MOVE_SEMANTICS == 0
589 # undef CV_CXX_MOVE_SEMANTICS
593 #ifdef CV_CXX_MOVE_SEMANTICS
594 #define CV_CXX_MOVE(x) std::move(x)
596 #define CV_CXX_MOVE(x) (x)
600 /****************************************************************************************\
602 \****************************************************************************************/
604 #ifndef CV_CXX_STD_ARRAY
605 # if __cplusplus >= 201103L || (defined(__cplusplus) && defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
606 # define CV_CXX_STD_ARRAY 1
610 # if CV_CXX_STD_ARRAY == 0
611 # undef CV_CXX_STD_ARRAY
616 /****************************************************************************************\
617 * C++11 override / final *
618 \****************************************************************************************/
622 # define CV_OVERRIDE override
631 # define CV_FINAL final
638 /****************************************************************************************\
640 \****************************************************************************************/
643 # if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
644 # define CV_NOEXCEPT noexcept
653 // Integer types portatibility
654 #ifdef OPENCV_STDINT_HEADER
655 #include OPENCV_STDINT_HEADER
656 #elif defined(__cplusplus)
657 #if defined(_MSC_VER) && _MSC_VER < 1600 /* MSVS 2010 */
659 typedef signed char int8_t;
660 typedef unsigned char uint8_t;
661 typedef signed short int16_t;
662 typedef unsigned short uint16_t;
663 typedef signed int int32_t;
664 typedef unsigned int uint32_t;
665 typedef signed __int64 int64_t;
666 typedef unsigned __int64 uint64_t;
668 #elif defined(_MSC_VER) || __cplusplus >= 201103L
683 typedef ::int8_t int8_t;
684 typedef ::uint8_t uint8_t;
685 typedef ::int16_t int16_t;
686 typedef ::uint16_t uint16_t;
687 typedef ::int32_t int32_t;
688 typedef ::uint32_t uint32_t;
689 typedef ::int64_t int64_t;
690 typedef ::uint64_t uint64_t;
707 explicit float16_t(float x) { h = (__fp16)x; }
708 operator float() const { return (float)h; }
709 static float16_t fromBits(ushort w)
717 static float16_t zero()
720 result.h = (__fp16)0;
734 explicit float16_t(float x)
737 __m128 v = _mm_load_ss(&x);
738 w = (ushort)_mm_cvtsi128_si32(_mm_cvtps_ph(v, 0));
742 unsigned sign = in.u & 0x80000000;
745 if( in.u >= 0x47800000 )
746 w = (ushort)(in.u > 0x7f800000 ? 0x7e00 : 0x7c00);
749 if (in.u < 0x38800000)
752 w = (ushort)(in.u - 0x3f000000);
756 unsigned t = in.u + 0xc8000fff;
757 w = (ushort)((t + ((in.u >> 13) & 1)) >> 13);
761 w = (ushort)(w | (sign >> 16));
765 operator float() const
769 _mm_store_ss(&f, _mm_cvtph_ps(_mm_cvtsi32_si128(w)));
774 unsigned t = ((w & 0x7fff) << 13) + 0x38000000;
775 unsigned sign = (w & 0x8000) << 16;
776 unsigned e = w & 0x7c00;
778 out.u = t + (1 << 23);
779 out.u = (e >= 0x7c00 ? t + 0x38000000 :
780 e == 0 ? (out.f -= 6.103515625e-05f, out.u) : t) | sign;
785 static float16_t fromBits(ushort b)
791 static float16_t zero()
794 result.w = (ushort)0;
797 ushort bits() const { return w; }
810 #include "opencv2/core/fast_math.hpp" // define cvRound(double)
813 #endif // OPENCV_CORE_CVDEF_H