next: drop HAVE_TEGRA_OPTIMIZATION/TADP
[platform/upstream/opencv.git] / modules / core / src / precomp.hpp
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 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #ifndef __OPENCV_PRECOMP_H__
44 #define __OPENCV_PRECOMP_H__
45
46 #include "opencv2/opencv_modules.hpp"
47 #include "cvconfig.h"
48
49 #include "opencv2/core/utility.hpp"
50 #include "opencv2/core/core_c.h"
51 #include "opencv2/core/cuda.hpp"
52 #include "opencv2/core/opengl.hpp"
53 #include "opencv2/core/va_intel.hpp"
54
55 #include "opencv2/core/private.hpp"
56 #include "opencv2/core/private.cuda.hpp"
57 #ifdef HAVE_OPENCL
58 #include "opencv2/core/ocl.hpp"
59 #endif
60
61 #include <assert.h>
62 #include <ctype.h>
63 #include <float.h>
64 #include <limits.h>
65 #include <math.h>
66 #include <stdarg.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70
71 #include <algorithm>
72 #include <cmath>
73 #include <cstdlib>
74 #include <limits>
75 #include <float.h>
76 #include <cstring>
77 #include <cassert>
78
79 #define USE_SSE2  (cv::checkHardwareSupport(CV_CPU_SSE2))
80 #define USE_SSE4_2  (cv::checkHardwareSupport(CV_CPU_SSE4_2))
81 #define USE_AVX  (cv::checkHardwareSupport(CV_CPU_AVX))
82 #define USE_AVX2  (cv::checkHardwareSupport(CV_CPU_AVX2))
83
84 #include "opencv2/core/hal/hal.hpp"
85 #include "opencv2/core/hal/intrin.hpp"
86 #include "opencv2/core/sse_utils.hpp"
87 #include "opencv2/core/neon_utils.hpp"
88 #include "opencv2/core/vsx_utils.hpp"
89 #include "arithm_core.hpp"
90 #include "hal_replacement.hpp"
91
92 #define GET_OPTIMIZED(func) (func)
93
94 namespace cv
95 {
96
97 // -128.f ... 255.f
98 extern const float g_8x32fTab[];
99 #define CV_8TO32F(x)  cv::g_8x32fTab[(x)+128]
100
101 extern const ushort g_8x16uSqrTab[];
102 #define CV_SQR_8U(x)  cv::g_8x16uSqrTab[(x)+255]
103
104 extern const uchar g_Saturate8u[];
105 #define CV_FAST_CAST_8U(t)   (assert(-256 <= (t) && (t) <= 512), cv::g_Saturate8u[(t)+256])
106 #define CV_MIN_8U(a,b)       ((a) - CV_FAST_CAST_8U((a) - (b)))
107 #define CV_MAX_8U(a,b)       ((a) + CV_FAST_CAST_8U((b) - (a)))
108
109 template<> inline uchar OpAdd<uchar>::operator ()(uchar a, uchar b) const
110 { return CV_FAST_CAST_8U(a + b); }
111
112 template<> inline uchar OpSub<uchar>::operator ()(uchar a, uchar b) const
113 { return CV_FAST_CAST_8U(a - b); }
114
115 template<> inline short OpAbsDiff<short>::operator ()(short a, short b) const
116 { return saturate_cast<short>(std::abs(a - b)); }
117
118 template<> inline schar OpAbsDiff<schar>::operator ()(schar a, schar b) const
119 { return saturate_cast<schar>(std::abs(a - b)); }
120
121 template<> inline uchar OpMin<uchar>::operator ()(uchar a, uchar b) const { return CV_MIN_8U(a, b); }
122
123 template<> inline uchar OpMax<uchar>::operator ()(uchar a, uchar b) const { return CV_MAX_8U(a, b); }
124
125 typedef void (*UnaryFunc)(const uchar* src1, size_t step1,
126                        uchar* dst, size_t step, Size sz,
127                        void*);
128
129 typedef void (*BinaryFunc)(const uchar* src1, size_t step1,
130                        const uchar* src2, size_t step2,
131                        uchar* dst, size_t step, Size sz,
132                        void*);
133
134 typedef void (*BinaryFuncC)(const uchar* src1, size_t step1,
135                        const uchar* src2, size_t step2,
136                        uchar* dst, size_t step, int width, int height,
137                        void*);
138
139 BinaryFunc getConvertFunc(int sdepth, int ddepth);
140 BinaryFunc getConvertScaleFunc(int sdepth, int ddepth);
141 BinaryFunc getCopyMaskFunc(size_t esz);
142
143 /* default memory block for sparse array elements */
144 #define  CV_SPARSE_MAT_BLOCK     (1<<12)
145
146 /* initial hash table size */
147 #define  CV_SPARSE_HASH_SIZE0    (1<<10)
148
149 /* maximal average node_count/hash_size ratio beyond which hash table is resized */
150 #define  CV_SPARSE_HASH_RATIO    3
151
152 inline Size getContinuousSize_( int flags, int cols, int rows, int widthScale )
153 {
154     int64 sz = (int64)cols * rows * widthScale;
155     return (flags & Mat::CONTINUOUS_FLAG) != 0 &&
156         (int)sz == sz ? Size((int)sz, 1) : Size(cols * widthScale, rows);
157 }
158
159 inline Size getContinuousSize( const Mat& m1, int widthScale=1 )
160 {
161     return getContinuousSize_(m1.flags,
162                               m1.cols, m1.rows, widthScale);
163 }
164
165 inline Size getContinuousSize( const Mat& m1, const Mat& m2, int widthScale=1 )
166 {
167     return getContinuousSize_(m1.flags & m2.flags,
168                               m1.cols, m1.rows, widthScale);
169 }
170
171 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
172                                const Mat& m3, int widthScale=1 )
173 {
174     return getContinuousSize_(m1.flags & m2.flags & m3.flags,
175                               m1.cols, m1.rows, widthScale);
176 }
177
178 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
179                                const Mat& m3, const Mat& m4,
180                                int widthScale=1 )
181 {
182     return getContinuousSize_(m1.flags & m2.flags & m3.flags & m4.flags,
183                               m1.cols, m1.rows, widthScale);
184 }
185
186 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
187                                const Mat& m3, const Mat& m4,
188                                const Mat& m5, int widthScale=1 )
189 {
190     return getContinuousSize_(m1.flags & m2.flags & m3.flags & m4.flags & m5.flags,
191                               m1.cols, m1.rows, widthScale);
192 }
193
194 void setSize( Mat& m, int _dims, const int* _sz, const size_t* _steps, bool autoSteps=false );
195 void finalizeHdr(Mat& m);
196
197 struct NoVec
198 {
199     size_t operator()(const void*, const void*, void*, size_t) const { return 0; }
200 };
201
202 #define CV_SPLIT_MERGE_MAX_BLOCK_SIZE(cn) ((INT_MAX/4)/cn) // HAL implementation accepts 'int' len, so INT_MAX doesn't work here
203
204 enum { BLOCK_SIZE = 1024 };
205
206 #if defined HAVE_IPP && (IPP_VERSION_X100 >= 700)
207 #define ARITHM_USE_IPP 1
208 #else
209 #define ARITHM_USE_IPP 0
210 #endif
211
212 inline bool checkScalar(const Mat& sc, int atype, int sckind, int akind)
213 {
214     if( sc.dims > 2 || !sc.isContinuous() )
215         return false;
216     Size sz = sc.size();
217     if(sz.width != 1 && sz.height != 1)
218         return false;
219     int cn = CV_MAT_CN(atype);
220     if( akind == _InputArray::MATX && sckind != _InputArray::MATX )
221         return false;
222     return sz == Size(1, 1) || sz == Size(1, cn) || sz == Size(cn, 1) ||
223            (sz == Size(1, 4) && sc.type() == CV_64F && cn <= 4);
224 }
225
226 inline bool checkScalar(InputArray sc, int atype, int sckind, int akind)
227 {
228     if( sc.dims() > 2 || !sc.isContinuous() )
229         return false;
230     Size sz = sc.size();
231     if(sz.width != 1 && sz.height != 1)
232         return false;
233     int cn = CV_MAT_CN(atype);
234     if( akind == _InputArray::MATX && sckind != _InputArray::MATX )
235         return false;
236     return sz == Size(1, 1) || sz == Size(1, cn) || sz == Size(cn, 1) ||
237            (sz == Size(1, 4) && sc.type() == CV_64F && cn <= 4);
238 }
239
240 void convertAndUnrollScalar( const Mat& sc, int buftype, uchar* scbuf, size_t blocksize );
241
242 #ifdef CV_COLLECT_IMPL_DATA
243 struct ImplCollector
244 {
245     ImplCollector()
246     {
247         useCollection   = false;
248         implFlags       = 0;
249     }
250     bool useCollection; // enable/disable impl data collection
251
252     int implFlags;
253     std::vector<int>    implCode;
254     std::vector<String> implFun;
255
256     cv::Mutex mutex;
257 };
258 #endif
259
260 struct CoreTLSData
261 {
262     CoreTLSData() :
263 //#ifdef HAVE_OPENCL
264         device(0), useOpenCL(-1),
265 //#endif
266         useIPP(-1),
267         useIPP_NE(-1)
268 #ifdef HAVE_OPENVX
269         ,useOpenVX(-1)
270 #endif
271     {}
272
273     RNG rng;
274 //#ifdef HAVE_OPENCL
275     int device; // device index of an array of devices in a context, see also Device::getDefault
276     ocl::Queue oclQueue; // the queue used for running a kernel, see also getQueue, Kernel::run
277     int useOpenCL; // 1 - use, 0 - do not use, -1 - auto/not initialized
278 //#endif
279     int useIPP;    // 1 - use, 0 - do not use, -1 - auto/not initialized
280     int useIPP_NE; // 1 - use, 0 - do not use, -1 - auto/not initialized
281 #ifdef HAVE_OPENVX
282     int useOpenVX; // 1 - use, 0 - do not use, -1 - auto/not initialized
283 #endif
284 };
285
286 TLSData<CoreTLSData>& getCoreTlsData();
287
288 #if defined(BUILD_SHARED_LIBS)
289 #if defined _WIN32 || defined WINCE
290 #define CL_RUNTIME_EXPORT __declspec(dllexport)
291 #elif defined __GNUC__ && __GNUC__ >= 4
292 #define CL_RUNTIME_EXPORT __attribute__ ((visibility ("default")))
293 #else
294 #define CL_RUNTIME_EXPORT
295 #endif
296 #else
297 #define CL_RUNTIME_EXPORT
298 #endif
299
300 extern bool __termination; // skip some cleanups, because process is terminating
301                            // (for example, if ExitProcess() was already called)
302
303 cv::Mutex& getInitializationMutex();
304
305 // TODO Memory barriers?
306 #define CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, RET_VALUE) \
307     static TYPE* volatile instance = NULL; \
308     if (instance == NULL) \
309     { \
310         cv::AutoLock lock(cv::getInitializationMutex()); \
311         if (instance == NULL) \
312             instance = INITIALIZER; \
313     } \
314     return RET_VALUE;
315
316 #define CV_SINGLETON_LAZY_INIT(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, instance)
317 #define CV_SINGLETON_LAZY_INIT_REF(TYPE, INITIALIZER) CV_SINGLETON_LAZY_INIT_(TYPE, INITIALIZER, *instance)
318
319 int cv_snprintf(char* buf, int len, const char* fmt, ...);
320 int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args);
321 }
322
323 #endif /*_CXCORE_INTERNAL_H_*/