Merge remote-tracking branch 'upstream/3.4' into merge-3.4
[platform/upstream/opencv.git] / modules / core / include / opencv2 / core / cvdef.h
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
45 #ifndef OPENCV_CORE_CVDEF_H
46 #define OPENCV_CORE_CVDEF_H
47
48 //! @addtogroup core_utils
49 //! @{
50
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; }
59 #endif
60 #endif
61
62 #ifndef CV__DEBUG_NS_BEGIN
63 #define CV__DEBUG_NS_BEGIN
64 #define CV__DEBUG_NS_END
65 #endif
66
67
68 #ifdef __OPENCV_BUILD
69 #include "cvconfig.h"
70 #endif
71
72 #ifndef __CV_EXPAND
73 #define __CV_EXPAND(x) x
74 #endif
75
76 #ifndef __CV_CAT
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)
80 #endif
81
82
83 // undef problematic defines sometimes defined by system headers (windows.h in particular)
84 #undef small
85 #undef min
86 #undef max
87 #undef abs
88 #undef Complex
89
90 #include <limits.h>
91 #include "opencv2/core/hal/interface.h"
92
93 #if defined __ICL
94 #  define CV_ICC   __ICL
95 #elif defined __ICC
96 #  define CV_ICC   __ICC
97 #elif defined __ECL
98 #  define CV_ICC   __ECL
99 #elif defined __ECC
100 #  define CV_ICC   __ECC
101 #elif defined __INTEL_COMPILER
102 #  define CV_ICC   __INTEL_COMPILER
103 #endif
104
105 #ifndef CV_INLINE
106 #  if defined __cplusplus
107 #    define CV_INLINE static inline
108 #  elif defined _MSC_VER
109 #    define CV_INLINE __inline
110 #  else
111 #    define CV_INLINE static
112 #  endif
113 #endif
114
115 #if defined CV_DISABLE_OPTIMIZATION || (defined CV_ICC && !defined CV_ENABLE_UNROLLED)
116 #  define CV_ENABLE_UNROLLED 0
117 #else
118 #  define CV_ENABLE_UNROLLED 1
119 #endif
120
121 #ifdef __GNUC__
122 #  define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
123 #elif defined _MSC_VER
124 #  define CV_DECL_ALIGNED(x) __declspec(align(x))
125 #else
126 #  define CV_DECL_ALIGNED(x)
127 #endif
128
129 /* CPU features and intrinsics support */
130 #define CV_CPU_NONE             0
131 #define CV_CPU_MMX              1
132 #define CV_CPU_SSE              2
133 #define CV_CPU_SSE2             3
134 #define CV_CPU_SSE3             4
135 #define CV_CPU_SSSE3            5
136 #define CV_CPU_SSE4_1           6
137 #define CV_CPU_SSE4_2           7
138 #define CV_CPU_POPCNT           8
139 #define CV_CPU_FP16             9
140 #define CV_CPU_AVX              10
141 #define CV_CPU_AVX2             11
142 #define CV_CPU_FMA3             12
143
144 #define CV_CPU_AVX_512F         13
145 #define CV_CPU_AVX_512BW        14
146 #define CV_CPU_AVX_512CD        15
147 #define CV_CPU_AVX_512DQ        16
148 #define CV_CPU_AVX_512ER        17
149 #define CV_CPU_AVX_512IFMA512   18 // deprecated
150 #define CV_CPU_AVX_512IFMA      18
151 #define CV_CPU_AVX_512PF        19
152 #define CV_CPU_AVX_512VBMI      20
153 #define CV_CPU_AVX_512VL        21
154
155 #define CV_CPU_NEON   100
156
157 #define CV_CPU_VSX 200
158
159 // CPU features groups
160 #define CV_CPU_AVX512_SKX       256
161
162 // when adding to this list remember to update the following enum
163 #define CV_HARDWARE_MAX_FEATURE 512
164
165 /** @brief Available CPU features.
166 */
167 enum CpuFeatures {
168     CPU_MMX             = 1,
169     CPU_SSE             = 2,
170     CPU_SSE2            = 3,
171     CPU_SSE3            = 4,
172     CPU_SSSE3           = 5,
173     CPU_SSE4_1          = 6,
174     CPU_SSE4_2          = 7,
175     CPU_POPCNT          = 8,
176     CPU_FP16            = 9,
177     CPU_AVX             = 10,
178     CPU_AVX2            = 11,
179     CPU_FMA3            = 12,
180
181     CPU_AVX_512F        = 13,
182     CPU_AVX_512BW       = 14,
183     CPU_AVX_512CD       = 15,
184     CPU_AVX_512DQ       = 16,
185     CPU_AVX_512ER       = 17,
186     CPU_AVX_512IFMA512  = 18, // deprecated
187     CPU_AVX_512IFMA     = 18,
188     CPU_AVX_512PF       = 19,
189     CPU_AVX_512VBMI     = 20,
190     CPU_AVX_512VL       = 21,
191
192     CPU_NEON            = 100,
193
194     CPU_VSX             = 200,
195
196     CPU_AVX512_SKX      = 256, //!< Skylake-X with AVX-512F/CD/BW/DQ/VL
197
198     CPU_MAX_FEATURE     = 512  // see CV_HARDWARE_MAX_FEATURE
199 };
200
201
202 #include "cv_cpu_dispatch.h"
203
204
205 /* fundamental constants */
206 #define CV_PI   3.1415926535897932384626433832795
207 #define CV_2PI  6.283185307179586476925286766559
208 #define CV_LOG2 0.69314718055994530941723212145818
209
210 #if defined __ARM_FP16_FORMAT_IEEE \
211     && !defined __CUDACC__
212 #  define CV_FP16_TYPE 1
213 #else
214 #  define CV_FP16_TYPE 0
215 #endif
216
217 typedef union Cv16suf
218 {
219     short i;
220 #if CV_FP16_TYPE
221     __fp16 h;
222 #endif
223     struct _fp16Format
224     {
225         unsigned int significand : 10;
226         unsigned int exponent    : 5;
227         unsigned int sign        : 1;
228     } fmt;
229 }
230 Cv16suf;
231
232 typedef union Cv32suf
233 {
234     int i;
235     unsigned u;
236     float f;
237     struct _fp32Format
238     {
239         unsigned int significand : 23;
240         unsigned int exponent    : 8;
241         unsigned int sign        : 1;
242     } fmt;
243 }
244 Cv32suf;
245
246 typedef union Cv64suf
247 {
248     int64 i;
249     uint64 u;
250     double f;
251 }
252 Cv64suf;
253
254 #define OPENCV_ABI_COMPATIBILITY 400
255
256 #ifdef __OPENCV_BUILD
257 #  define DISABLE_OPENCV_3_COMPATIBILITY
258 #  define OPENCV_DISABLE_DEPRECATED_COMPATIBILITY
259 #endif
260
261 #ifdef CVAPI_EXPORTS
262 # if (defined _WIN32 || defined WINCE || defined __CYGWIN__)
263 #   define CV_EXPORTS __declspec(dllexport)
264 # elif defined __GNUC__ && __GNUC__ >= 4
265 #   define CV_EXPORTS __attribute__ ((visibility ("default")))
266 # endif
267 #endif
268
269 #ifndef CV_EXPORTS
270 # define CV_EXPORTS
271 #endif
272
273 #ifdef _MSC_VER
274 #   define CV_EXPORTS_TEMPLATE
275 #else
276 #   define CV_EXPORTS_TEMPLATE CV_EXPORTS
277 #endif
278
279 #ifndef CV_DEPRECATED
280 #  if defined(__GNUC__)
281 #    define CV_DEPRECATED __attribute__ ((deprecated))
282 #  elif defined(_MSC_VER)
283 #    define CV_DEPRECATED __declspec(deprecated)
284 #  else
285 #    define CV_DEPRECATED
286 #  endif
287 #endif
288
289 #ifndef CV_EXTERN_C
290 #  ifdef __cplusplus
291 #    define CV_EXTERN_C extern "C"
292 #  else
293 #    define CV_EXTERN_C
294 #  endif
295 #endif
296
297 /* special informative macros for wrapper generators */
298 #define CV_EXPORTS_W CV_EXPORTS
299 #define CV_EXPORTS_W_SIMPLE CV_EXPORTS
300 #define CV_EXPORTS_AS(synonym) CV_EXPORTS
301 #define CV_EXPORTS_W_MAP CV_EXPORTS
302 #define CV_IN_OUT
303 #define CV_OUT
304 #define CV_PROP
305 #define CV_PROP_RW
306 #define CV_WRAP
307 #define CV_WRAP_AS(synonym)
308
309 /****************************************************************************************\
310 *                                  Matrix type (Mat)                                     *
311 \****************************************************************************************/
312
313 #define CV_MAT_CN_MASK          ((CV_CN_MAX - 1) << CV_CN_SHIFT)
314 #define CV_MAT_CN(flags)        ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
315 #define CV_MAT_TYPE_MASK        (CV_DEPTH_MAX*CV_CN_MAX - 1)
316 #define CV_MAT_TYPE(flags)      ((flags) & CV_MAT_TYPE_MASK)
317 #define CV_MAT_CONT_FLAG_SHIFT  14
318 #define CV_MAT_CONT_FLAG        (1 << CV_MAT_CONT_FLAG_SHIFT)
319 #define CV_IS_MAT_CONT(flags)   ((flags) & CV_MAT_CONT_FLAG)
320 #define CV_IS_CONT_MAT          CV_IS_MAT_CONT
321 #define CV_SUBMAT_FLAG_SHIFT    15
322 #define CV_SUBMAT_FLAG          (1 << CV_SUBMAT_FLAG_SHIFT)
323 #define CV_IS_SUBMAT(flags)     ((flags) & CV_MAT_SUBMAT_FLAG)
324
325 /** Size of each channel item,
326    0x8442211 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
327 #define CV_ELEM_SIZE1(type) \
328     ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15)
329
330 /** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
331 #define CV_ELEM_SIZE(type) \
332     (CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3))
333
334 #ifndef MIN
335 #  define MIN(a,b)  ((a) > (b) ? (b) : (a))
336 #endif
337
338 #ifndef MAX
339 #  define MAX(a,b)  ((a) < (b) ? (b) : (a))
340 #endif
341
342 /****************************************************************************************\
343 *                                    static analysys                                     *
344 \****************************************************************************************/
345
346 // In practice, some macro are not processed correctly (noreturn is not detected).
347 // We need to use simplified definition for them.
348 #ifndef CV_STATIC_ANALYSIS
349 # if defined(__KLOCWORK__) || defined(__clang_analyzer__) || defined(__COVERITY__)
350 #   define CV_STATIC_ANALYSIS
351 # endif
352 #endif
353
354 /****************************************************************************************\
355 *                                    Thread sanitizer                                    *
356 \****************************************************************************************/
357 #ifndef CV_THREAD_SANITIZER
358 # if defined(__has_feature)
359 #   if __has_feature(thread_sanitizer)
360 #     define CV_THREAD_SANITIZER
361 #   endif
362 # endif
363 #endif
364
365 /****************************************************************************************\
366 *          exchange-add operation for atomic operations on reference counters            *
367 \****************************************************************************************/
368
369 #ifdef CV_XADD
370   // allow to use user-defined macro
371 #elif defined __GNUC__ || defined __clang__
372 #  if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__)
373 #    ifdef __ATOMIC_ACQ_REL
374 #      define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL)
375 #    else
376 #      define CV_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4)
377 #    endif
378 #  else
379 #    if defined __ATOMIC_ACQ_REL && !defined __clang__
380        // version for gcc >= 4.7
381 #      define CV_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL)
382 #    else
383 #      define CV_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta))
384 #    endif
385 #  endif
386 #elif defined _MSC_VER && !defined RC_INVOKED
387 #  include <intrin.h>
388 #  define CV_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta)
389 #else
390    CV_INLINE CV_XADD(int* addr, int delta) { int tmp = *addr; *addr += delta; return tmp; }
391 #endif
392
393
394 /****************************************************************************************\
395 *                                  CV_NORETURN attribute                                 *
396 \****************************************************************************************/
397
398 #ifndef CV_NORETURN
399 #  if defined(__GNUC__)
400 #    define CV_NORETURN __attribute__((__noreturn__))
401 #  elif defined(_MSC_VER) && (_MSC_VER >= 1300)
402 #    define CV_NORETURN __declspec(noreturn)
403 #  else
404 #    define CV_NORETURN /* nothing by default */
405 #  endif
406 #endif
407
408
409 /****************************************************************************************\
410 *                                  CV_NODISCARD attribute                                *
411 * encourages the compiler to issue a warning if the return value is discarded (C++17)    *
412 \****************************************************************************************/
413 #ifndef CV_NODISCARD
414 #  if defined(__GNUC__)
415 #    define CV_NODISCARD __attribute__((__warn_unused_result__)) // at least available with GCC 3.4
416 #  elif defined(__clang__) && defined(__has_attribute)
417 #    if __has_attribute(__warn_unused_result__)
418 #      define CV_NODISCARD __attribute__((__warn_unused_result__))
419 #    endif
420 #  endif
421 #endif
422 #ifndef CV_NODISCARD
423 #  define CV_NODISCARD /* nothing by default */
424 #endif
425
426
427 /****************************************************************************************\
428 *                                    C++ 11                                              *
429 \****************************************************************************************/
430 #ifndef CV_CXX11
431 #  if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
432 #    define CV_CXX11 1
433 #  endif
434 #else
435 #  if CV_CXX11 == 0
436 #    undef CV_CXX11
437 #  endif
438 #endif
439 #ifndef CV_CXX11
440 #  error "OpenCV 4.x+ requires enabled C++11 support"
441 #endif
442
443 #define CV_CXX_MOVE_SEMANTICS 1
444 #define CV_CXX_STD_ARRAY 1
445 #include <array>
446 #ifndef CV_OVERRIDE
447 #  define CV_OVERRIDE override
448 #endif
449 #ifndef CV_FINAL
450 #  define CV_FINAL final
451 #endif
452
453
454 // Integer types portatibility
455 #ifdef OPENCV_STDINT_HEADER
456 #include OPENCV_STDINT_HEADER
457 #elif defined(__cplusplus)
458 #if defined(_MSC_VER) && _MSC_VER < 1600 /* MSVS 2010 */
459 namespace cv {
460 typedef signed char int8_t;
461 typedef unsigned char uint8_t;
462 typedef signed short int16_t;
463 typedef unsigned short uint16_t;
464 typedef signed int int32_t;
465 typedef unsigned int uint32_t;
466 typedef signed __int64 int64_t;
467 typedef unsigned __int64 uint64_t;
468 }
469 #elif defined(_MSC_VER) || __cplusplus >= 201103L
470 #include <cstdint>
471 namespace cv {
472 using std::int8_t;
473 using std::uint8_t;
474 using std::int16_t;
475 using std::uint16_t;
476 using std::int32_t;
477 using std::uint32_t;
478 using std::int64_t;
479 using std::uint64_t;
480 }
481 #else
482 #include <stdint.h>
483 namespace cv {
484 typedef ::int8_t int8_t;
485 typedef ::uint8_t uint8_t;
486 typedef ::int16_t int16_t;
487 typedef ::uint16_t uint16_t;
488 typedef ::int32_t int32_t;
489 typedef ::uint32_t uint32_t;
490 typedef ::int64_t int64_t;
491 typedef ::uint64_t uint64_t;
492 }
493 #endif
494 #else // pure C
495 #include <stdint.h>
496 #endif
497
498
499 //! @}
500
501 #ifndef __cplusplus
502 #include "opencv2/core/fast_math.hpp" // define cvRound(double)
503 #endif
504
505 #endif // OPENCV_CORE_CVDEF_H